[Pinfo-devel] r296 - pinfo/trunk/src
Bas Zoetekouw
bas at costa.debian.org
Thu Mar 16 14:07:24 UTC 2006
Author: bas
Date: 2006-03-16 14:07:22 +0000 (Thu, 16 Mar 2006)
New Revision: 296
Modified:
pinfo/trunk/src/filehandling_functions.c
pinfo/trunk/src/manual.c
pinfo/trunk/src/utils.c
pinfo/trunk/src/utils.h
Log:
Replace calls to tempnam() by a custom function based on tmpfile().
Modified: pinfo/trunk/src/filehandling_functions.c
===================================================================
--- pinfo/trunk/src/filehandling_functions.c 2006-03-15 21:52:38 UTC (rev 295)
+++ pinfo/trunk/src/filehandling_functions.c 2006-03-16 14:07:22 UTC (rev 296)
@@ -588,12 +588,13 @@
if (number == 0) /* initialize tmp filename for file 1 */
{
+ /* close and delete old tmp file */
if (tmpfilename1)
{
unlink(tmpfilename1); /* erase old tmpfile */
free(tmpfilename1);
}
- tmpfilename1 = tempnam("/tmp", NULL);
+ tmpfilename1 = make_tempfile();
tmpfilename = tmpfilename1; /* later we will refere only to tmp1 */
}
for (i = 0; i < infopathcount; i++) /* go through all paths */
@@ -754,7 +755,7 @@
unlink(tmpfilename1); /* erase old tmpfile */
free(tmpfilename1);
}
- tmpfilename1 = tempnam("/tmp", NULL);
+ tmpfilename1 = make_tempfile();
tmpfilename = tmpfilename1; /* later we will refere only to tmp1 */
}
else /* initialize tmp filename for file 2 */
@@ -764,7 +765,7 @@
unlink(tmpfilename2); /* erase old tmpfile */
free(tmpfilename2);
}
- tmpfilename2 = tempnam("/tmp", NULL);
+ tmpfilename2 = make_tempfile();
tmpfilename = tmpfilename2; /* later we will refere only to tmp2 */
}
Modified: pinfo/trunk/src/manual.c
===================================================================
--- pinfo/trunk/src/manual.c 2006-03-15 21:52:38 UTC (rev 295)
+++ pinfo/trunk/src/manual.c 2006-03-16 14:07:22 UTC (rev 296)
@@ -260,7 +260,7 @@
unlink(tmpfilename1);
xfree(tmpfilename1);
}
- tmpfilename1 = tempnam("/tmp", NULL);
+ tmpfilename1 = make_tempfile();
#ifdef getmaxyx
init_curses();
@@ -379,7 +379,7 @@
}
else /* from cmd output */
source = fopen(location, "r");
- name = tempnam("/tmp", NULL);
+ name = make_tempfile();
raw_tempfilename = name;
id = fopen(name, "w");
@@ -459,7 +459,7 @@
if (use_apropos)
{
printf(_("Calling apropos \n"));
- apropos_tempfilename = tempnam("/tmp", NULL);
+ apropos_tempfilename = make_tempfile();
snprintf(cmd, 255, "apropos %s > %s", name, apropos_tempfilename);
if (system(cmd) != 0)
{
@@ -506,7 +506,7 @@
unlink(tmpfilename2);
xfree(tmpfilename2);
}
- tmpfilename2 = tempnam("/tmp", NULL);
+ tmpfilename2 = make_tempfile();
/*
* key_back is not pressed; and return_value is an offset to
* manuallinks
Modified: pinfo/trunk/src/utils.c
===================================================================
--- pinfo/trunk/src/utils.c 2006-03-15 21:52:38 UTC (rev 295)
+++ pinfo/trunk/src/utils.c 2006-03-16 14:07:22 UTC (rev 296)
@@ -697,3 +697,37 @@
return len;
}
+
+/*
+ * create a temporary file in a safe way, and return its name in a newly
+ * allocated string
+ */
+char *
+make_tempfile()
+{
+ char *filename;
+ size_t len;
+
+ /* TODO: fix hardcoded /tmp */
+ char tmpfile_template[] = "/tmp/pinfo.XXXXXX";
+
+ /* create a tmpfile */
+ int fd = mkstemp(tmpfile_template);
+ /* bug out if it failed */
+ if (fd == -1)
+ {
+ closeprogram();
+ printf(_("Couldn't open temporary file\n"));
+ exit(1);
+ }
+
+ /* allocate a new string and copy the filename there */
+ len = strlen(tmpfile_template)+1;
+ filename = xmalloc(len+1); /* guarenteerd to be set to \0's */
+ strncpy(filename, tmpfile_template, len);
+
+ /* close the file */
+ close(fd);
+
+ return filename;
+}
Modified: pinfo/trunk/src/utils.h
===================================================================
--- pinfo/trunk/src/utils.h 2006-03-15 21:52:38 UTC (rev 295)
+++ pinfo/trunk/src/utils.h 2006-03-16 14:07:22 UTC (rev 296)
@@ -103,4 +103,12 @@
int
calculate_len(char *start, char *end);
+/*
+ * * create a temporary file in a safe way, and return its name in a newly
+ * * allocated string
+ * */
+char *
+make_tempfile();
+
+
#endif
More information about the Pinfo-devel
mailing list