[PATCH:xman 1/2] Provide a fallback mkstemp so we don't need to maintain 2 API versions

Alan Coopersmith alan.coopersmith at oracle.com
Sat Apr 20 10:08:20 PDT 2013


Unifies the file handling API's to always return an open fd or FILE *,
instead of delaying the open when mkstemp was not available.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 configure.ac |    3 +-
 man.h        |    8 +++
 misc.c       |  179 +++++++++++++++++-----------------------------------------
 search.c     |   10 ----
 vendor.h     |   38 +++----------
 5 files changed, 68 insertions(+), 170 deletions(-)

diff --git a/configure.ac b/configure.ac
index 62562f0..74a6fc8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -37,8 +37,7 @@ AC_CONFIG_HEADERS([config.h])
 
 AC_CANONICAL_HOST
 
-AC_CHECK_FUNC([mkstemp], 
-   AC_DEFINE(HAS_MKSTEMP,1,[Define to 1 if you have the "mkstemp" function.]))
+AC_CHECK_FUNCS([mkstemp])
 
 AC_ARG_WITH(helpdir,
         AS_HELP_STRING([--with-helpdir=<path>],
diff --git a/man.h b/man.h
index ea77236..45e7bc1 100644
--- a/man.h
+++ b/man.h
@@ -28,6 +28,9 @@ from the X Consortium.
 
 */
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 /* X toolkit header files */
 
@@ -256,6 +259,11 @@ Bool ReadManConfig(char manpath[]);
 int Man(void);
 
 /* misc.c */
+#ifndef HAVE_MKSTEMP
+_X_HIDDEN int Xmkstemp (char *template);
+# define mkstemp Xmkstemp
+#endif
+
 FILE *FindManualFile(ManpageGlobals * man_globals, int section_num,
                      int entry_num);
 ManpageGlobals *GetGlobals(Widget w);
diff --git a/misc.c b/misc.c
index 42eea49..dae20cf 100644
--- a/misc.c
+++ b/misc.c
@@ -48,19 +48,13 @@ from the X Consortium.
 
 static FILE *Uncompress(ManpageGlobals * man_globals, const char *filename);
 
-#ifndef HAS_MKSTEMP
-static Boolean UncompressNamed(ManpageGlobals * man_globals,
-                               const char *filename, char *output);
-static Boolean UncompressUnformatted(ManpageGlobals * man_globals,
-                                     const char *entry, char *filename);
-#else
 static Boolean UncompressNamed(ManpageGlobals * man_globals,
                                const char *filename, char *output,
-                               FILE ** output_fd);
+                               FILE ** output_file);
 static Boolean UncompressUnformatted(ManpageGlobals * man_globals,
                                      const char *entry, char *filename,
                                      FILE ** file);
-#endif
+
 #ifdef HANDLE_ROFFSEQ
 static Boolean ConstructCommand(char *cmdbuf, const char *path,
                                 const char *filename, const char *tempfile);
@@ -315,6 +309,31 @@ FindManualFile(ManpageGlobals * man_globals, int section_num, int entry_num)
     return (Format(man_globals, entry));
 }
 
+#ifndef HAVE_MKSTEMP
+/* Emulate mkstemp to allow use of a common API in the many calls below */
+_X_HIDDEN int
+Xmkstemp (char *template)
+{
+    int fd = 0;
+    char tmp[PATH_MAX];
+
+    if (strlen(template) >= sizeof(tmp))
+        return -1;
+    /* save copy of unmodified template in case we have to try again */
+    strcpy(tmp, template);
+
+    do {
+        if (fd == -1)
+            strcpy(template, tmp);
+        if ((mktemp(template) == NULL) || (template[0] == '\0'))
+            return -1;
+        fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
+    } while ((fd == -1) && (errno == EEXIST || errno == EINTR));
+
+    return fd;
+}
+#endif
+
 /*	Function Namecompress
  *	Description: This function will attempt to find a compressed man
  *                   page and uncompress it.
@@ -329,21 +348,11 @@ Uncompress(ManpageGlobals * man_globals, const char *filename)
     char tmp_file[BUFSIZ];
     FILE *file;
 
-#ifndef HAS_MKSTEMP
-    if (!UncompressNamed(man_globals, filename, tmp_file))
-        return (NULL);
-
-    else if ((file = fopen(tmp_file, "r")) == NULL) {
-        PopupWarning(man_globals, "Something went wrong in retrieving the "
-                     "uncompressed manual page try cleaning up /tmp.");
-    }
-#else
     if (!UncompressNamed(man_globals, filename, tmp_file, &file)) {
         PopupWarning(man_globals, "Something went wrong in retrieving the "
                      "uncompressed manual page try cleaning up /tmp.");
         return (NULL);
     }
-#endif
 
     remove(tmp_file);           /* remove name in tree, it will remain
                                    until we close the fd, however. */
@@ -359,22 +368,13 @@ Uncompress(ManpageGlobals * man_globals, const char *filename)
  *	Returns:; TRUE if the file was found.
  */
 
-#ifndef HAS_MKSTEMP
 static Boolean
 UncompressNamed(ManpageGlobals * man_globals, const char *filename,
-                char *output)
-#else
-static Boolean
-UncompressNamed(ManpageGlobals * man_globals, const char *filename,
-                char *output, FILE ** output_fd)
-#endif
+                char *output, FILE ** output_file)
 {
     char tmp[BUFSIZ], cmdbuf[BUFSIZ], error_buf[BUFSIZ];
     struct stat junk;
-
-#ifdef HAS_MKSTEMP
     int fd;
-#endif
 
     if (stat(filename, &junk) != 0) {   /* Check for existence of the file. */
         if (errno != ENOENT) {
@@ -392,16 +392,16 @@ UncompressNamed(ManpageGlobals * man_globals, const char *filename,
  */
 
     strcpy(tmp, MANTEMP);       /* get a temp file. */
-#ifndef HAS_MKSTEMP
-    (void) mktemp(tmp);
-#else
     fd = mkstemp(tmp);
     if (fd < 0) {
         PopupWarning(man_globals, "Error creating a temp file");
         return FALSE;
     }
-    *output_fd = fdopen(fd, "r");
-#endif
+    *output_file = fdopen(fd, "r");
+    if (*output_file == NULL) {
+        PopupWarning(man_globals, "Error opening temp file");
+        return FALSE;
+    }
     strcpy(output, tmp);
 
 #ifdef GZIP_EXTENSION
@@ -442,21 +442,13 @@ UncompressNamed(ManpageGlobals * man_globals, const char *filename,
  *	Returns:; TRUE if the file was found.
  */
 
-#ifndef HAS_MKSTEMP
-static Boolean
-SgmlToRoffNamed(ManpageGlobals * man_globals, char *filename, char *output)
-#else
 static Boolean
 SgmlToRoffNamed(ManpageGlobals * man_globals, char *filename, char *output,
-                FILE ** output_fd)
-#endif
+                FILE ** output_file)
 {
     char tmp[BUFSIZ], cmdbuf[BUFSIZ], error_buf[BUFSIZ];
     struct stat junk;
-
-#ifdef HAS_MKSTEMP
     int fd;
-#endif
 
     if (stat(filename, &junk) != 0) {   /* Check for existence of the file. */
         if (errno != ENOENT) {
@@ -469,19 +461,19 @@ SgmlToRoffNamed(ManpageGlobals * man_globals, char *filename, char *output,
     }
 
     strcpy(tmp, MANTEMP);       /* get a temp file. */
-#ifndef HAS_MKSTEMP
-    (void) mktemp(tmp);
-#else
     fd = mkstemp(tmp);
     if (fd < 0) {
         PopupWarning(man_globals, "Error creating a temp file");
         return FALSE;
     }
-    *output_fd = fdopen(fd, "r");
-#endif
+    *output_file = fdopen(fd, "r");
+    if (*output_file == NULL) {
+        PopupWarning(man_globals, "Error opening temp file");
+        return FALSE;
+    }
     strcpy(output, tmp);
 
-    snprintf(cmdbuf, sizeof(cmdbuf), "%s %s > %s", SFORMAT, filename, output);
+    snprintf(cmdbuf, sizeof(cmdbuf), "%s %s >> %s", SFORMAT, filename, output);
     if (system(cmdbuf) == 0)    /* execute search. */
         return (TRUE);
 
@@ -507,10 +499,8 @@ FILE *
 Format(ManpageGlobals * man_globals, const char *entry)
 {
     FILE *file = NULL;
-
-#ifdef HAS_MKSTEMP
     int fd;
-#endif
+
     Widget manpage = man_globals->manpagewidgets.manpage;
     char cmdbuf[BUFSIZ], tmp[BUFSIZ], filename[BUFSIZ], error_buf[BUFSIZ];
     char path[BUFSIZ], sect[BUFSIZ];
@@ -518,11 +508,7 @@ Format(ManpageGlobals * man_globals, const char *entry)
     Position x, y;              /* location to pop up the
                                    "would you like to save" widget. */
 
-#ifndef HAS_MKSTEMP
-    if (!UncompressUnformatted(man_globals, entry, filename)) {
-#else
     if (!UncompressUnformatted(man_globals, entry, filename, &file)) {
-#endif
         /* We Really could not find it, this should never happen, yea right. */
         snprintf(error_buf, sizeof(error_buf),
                  "Could not open manual page, %s", entry);
@@ -531,11 +517,7 @@ Format(ManpageGlobals * man_globals, const char *entry)
         return (NULL);
     }
 
-#ifndef HAS_MKSTEMP
-    if ((file = fopen(filename, "r")) != NULL) {
-#else
     if (file != NULL) {
-#endif
         char line[BUFSIZ];
 
         if (fgets(line, sizeof(line), file) != NULL) {
@@ -571,24 +553,23 @@ Format(ManpageGlobals * man_globals, const char *entry)
     XFlush(XtDisplay(man_globals->standby));
 
     strcpy(tmp, MANTEMP);       /* Get a temp file. */
-#ifndef HAS_MKSTEMP
-    (void) mktemp(tmp);
-#else
     fd = mkstemp(tmp);
-    file = fdopen(fd, "r");
-#endif
+    if (fd >= 0)
+        file = fdopen(fd, "r");
+    else
+        file = NULL;
+    if (file == NULL) {
+        PopupWarning(man_globals, "Something went wrong in opening the "
+                     "temp file, try cleaning up /tmp");
+        return NULL;
+    }
     strcpy(man_globals->tempfile, tmp);
 
     ParseEntry(entry, path, sect, NULL);
 
 #ifndef HANDLE_ROFFSEQ
-#ifndef HAS_MKSTEMP
-    snprintf(cmdbuf, sizeof(cmdbuf), "cd %s ; %s %s %s > %s %s", path, TBL,
-             filename, FORMAT, man_globals->tempfile, "2> /dev/null");
-#else
     snprintf(cmdbuf, sizeof(cmdbuf), "cd %s ; %s %s %s >> %s %s", path, TBL,
              filename, FORMAT, man_globals->tempfile, "2> /dev/null");
-#endif
 #else
     /* Handle more flexible way of specifying the formatting pipeline */
     if (!ConstructCommand(cmdbuf, path, filename, man_globals->tempfile)) {
@@ -605,14 +586,7 @@ Format(ManpageGlobals * man_globals, const char *entry)
         file = NULL;
     }
     else {
-#ifndef HAS_MKSTEMP
-        if ((file = fopen(man_globals->tempfile, "r")) == NULL) {
-            PopupWarning(man_globals, "Something went wrong in retrieving the "
-                         "temp file, try cleaning up /tmp");
-        }
-        else {
-#endif
-
+        if (file != NULL) {
             XtPopdown(XtParent(man_globals->standby));
 
             if ((man_globals->save == NULL) ||
@@ -646,9 +620,7 @@ Format(ManpageGlobals * man_globals, const char *entry)
                 else
                     remove(man_globals->tempfile);
             }
-#ifndef HAS_MKSTEMP
         }
-#endif
     }
 
     /*
@@ -789,11 +761,7 @@ ConstructCommand(char *cmdbuf, const char *path,
     }
 
     /* Now add the fixed trailing part 'formatprog > tempfile 2> /dev/null' */
-#ifndef HAS_MKSTEMP
-    used = snprintf(c, left, " | %s > %s 2>/dev/null", FORMAT, tempfile);
-#else
     used = snprintf(c, left, " | %s >> %s 2>/dev/null", FORMAT, tempfile);
-#endif
     left -= used;
     if (left <= 1)
         return (FALSE);
@@ -811,13 +779,8 @@ ConstructCommand(char *cmdbuf, const char *path,
  */
 
 static Boolean
-#ifndef HAS_MKSTEMP
-UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
-                      char *filename)
-#else
 UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
                       char *filename, FILE ** file)
-#endif
 {
     char path[BUFSIZ], page[BUFSIZ], section[BUFSIZ], input[BUFSIZ];
     int len_cat = strlen(CAT), len_man = strlen(MAN);
@@ -849,11 +812,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
      * Then for compressed files in an uncompressed directory.
      */
     snprintf(input, sizeof(input), "%s.%s", filename, COMPRESSION_EXTENSION);
-#ifndef HAS_MKSTEMP
-    if (UncompressNamed(man_globals, input, filename)) {
-#else
     if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
         man_globals->compress = TRUE;
         man_globals->deletetempfile = TRUE;
         snprintf(man_globals->save_file, sizeof(man_globals->save_file),
@@ -864,11 +823,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 #ifdef GZIP_EXTENSION
     else {
         snprintf(input, sizeof(input), "%s.%s", filename, GZIP_EXTENSION);
-#ifndef HAS_MKSTEMP
-        if (UncompressNamed(man_globals, input, filename)) {
-#else
         if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
             man_globals->compress = TRUE;
             man_globals->gzip = TRUE;
             man_globals->deletetempfile = TRUE;
@@ -884,11 +839,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 #ifdef BZIP2_EXTENSION
     {
         snprintf(input, sizeof(input), "%s.%s", filename, BZIP2_EXTENSION);
-#ifndef HAS_MKSTEMP
-        if (UncompressNamed(man_globals, input, filename)) {
-#else
         if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
             man_globals->compress = TRUE;
             man_globals->gzip = FALSE;
             man_globals->bzip2 = TRUE;
@@ -903,11 +854,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 #ifdef LZMA_EXTENSION
     {
         snprintf(input, sizeof(input), "%s.%s", filename, LZMA_EXTENSION);
-#ifndef HAS_MKSTEMP
-        if (UncompressNamed(man_globals, input, filename)) {
-#else
         if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
             man_globals->compress = TRUE;
             man_globals->gzip = FALSE;
             man_globals->lzma = TRUE;
@@ -940,11 +887,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
      */
 
     snprintf(input, BUFSIZ, "%s/%s%s/%s", path, SMAN, section + len_sman, page);
-#ifndef HAS_MKSTEMP
-    if (SgmlToRoffNamed(man_globals, input, filename)) {
-#else
     if (SgmlToRoffNamed(man_globals, input, filename, file)) {
-#endif
         man_globals->compress = FALSE;
         man_globals->gzip = FALSE;
         man_globals->deletetempfile = TRUE;
@@ -959,11 +902,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
  */
 
     snprintf(input, sizeof(input), "%s.%s", filename, COMPRESSION_EXTENSION);
-#ifndef HAS_MKSTEMP
-    if (UncompressNamed(man_globals, input, filename)) {
-#else
     if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
         man_globals->compress = TRUE;
         man_globals->deletetempfile = TRUE;
         snprintf(man_globals->save_file, sizeof(man_globals->save_file),
@@ -974,11 +913,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 #ifdef GZIP_EXTENSION
     else {
         snprintf(input, sizeof(input), "%s.%s", filename, GZIP_EXTENSION);
-#ifndef HAS_MKSTEMP
-        if (UncompressNamed(man_globals, input, filename)) {
-#else
         if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
             man_globals->compress = TRUE;
             man_globals->gzip = TRUE;
             man_globals->deletetempfile = TRUE;
@@ -993,11 +928,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 #ifdef BZIP2_EXTENSION
     {
         snprintf(input, sizeof(input), "%s.%s", filename, BZIP2_EXTENSION);
-#ifndef HAS_MKSTEMP
-        if (UncompressNamed(man_globals, input, filename)) {
-#else
         if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
             man_globals->compress = TRUE;
             man_globals->gzip = TRUE;
             snprintf(man_globals->save_file, sizeof(man_globals->save_file),
@@ -1011,11 +942,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 #ifdef LZMA_EXTENSION
     {
         snprintf(input, sizeof(input), "%s.%s", filename, LZMA_EXTENSION);
-#ifndef HAS_MKSTEMP
-        if (UncompressNamed(man_globals, input, filename)) {
-#else
         if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
             man_globals->compress = TRUE;
             man_globals->lzma = TRUE;
             snprintf(man_globals->save_file, sizeof(man_globals->save_file),
@@ -1032,11 +959,7 @@ UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 
     snprintf(input, sizeof(input), "%s/%s%s.%s/%s", path,
              MAN, section + len_man, COMPRESSION_EXTENSION, page);
-#ifndef HAS_MKSTEMP
-    if (UncompressNamed(man_globals, input, filename)) {
-#else
     if (UncompressNamed(man_globals, input, filename, file)) {
-#endif
         man_globals->compress = TRUE;
         man_globals->deletetempfile = TRUE;
         snprintf(man_globals->save_file, sizeof(man_globals->save_file),
diff --git a/search.c b/search.c
index 1b244da..c28b997 100644
--- a/search.c
+++ b/search.c
@@ -153,9 +153,7 @@ DoSearch(ManpageGlobals * man_globals, int type)
     char string_buf[BUFSIZ], cmp_str[BUFSIZ], error_buf[BUFSIZ];
     char *search_string = SearchString(man_globals);
     FILE *file;
-#ifdef HAS_MKSTEMP
     int fd;
-#endif
     int count;
     Boolean flag;
 
@@ -182,15 +180,11 @@ DoSearch(ManpageGlobals * man_globals, int type)
         char label[BUFSIZ];
 
         strcpy(tmp, MANTEMP);   /* get a temp file. */
-#ifdef HAS_MKSTEMP
         fd = mkstemp(tmp);
         if (fd < 0) {
             PopupWarning(man_globals, "Cant create temp file");
             return NULL;
         }
-#else
-        (void) mktemp(tmp);
-#endif
         mantmp = tmp;
 
         manpath = getenv("MANPATH");
@@ -226,11 +220,7 @@ DoSearch(ManpageGlobals * man_globals, int type)
             PopupWarning(man_globals, error_buf);
         }
 
-#ifdef HAS_MKSTEMP
         if ((file = fdopen(fd, "r")) == NULL)
-#else
-        if ((file = fopen(mantmp, "r")) == NULL)
-#endif
             PrintError("lost temp file? out of temp space?");
 
 /*
diff --git a/vendor.h b/vendor.h
index cedc101..548ded6 100644
--- a/vendor.h
+++ b/vendor.h
@@ -105,29 +105,16 @@ from the X Consortium.
 #  define NO_COMPRESS
 #else
 #  define COMPRESSION_EXTENSION "Z"
-#  ifndef HAS_MKSTEMP
-#    define UNCOMPRESS_FORMAT     "zcat < %s > %s"
-#  else
-#    define UNCOMPRESS_FORMAT     "zcat < %s >> %s"
-#  endif
+#  define UNCOMPRESS_FORMAT     "zcat < %s >> %s"
 #  define COMPRESS              "compress"
 #  define GZIP_EXTENSION "gz"
-#  ifndef HAS_MKSTEMP
-#    define GUNZIP_FORMAT "gzip -c -d < %s > %s"
-#  else
-#    define GUNZIP_FORMAT "gzip -c -d < %s >> %s"
-#  endif
+#  define GUNZIP_FORMAT "gzip -c -d < %s >> %s"
 #  define GZIP_COMPRESS "gzip"
 #  define BZIP2_EXTENSION "bz2"
-#  define LZMA_EXTENSION "lzma"
-#  ifndef HAS_MKSTEMP
-#    define BUNZIP2_FORMAT "bunzip2 -c -d < %s > %s"
-#    define UNLZMA_FORMAT "unlzma -c -d < %s > %s"
-#  else
-#    define BUNZIP2_FORMAT "bunzip2 -c -d < %s >> %s"
-#    define UNLZMA_FORMAT "unlzma -c -d < %s >> %s"
-#  endif
+#  define BUNZIP2_FORMAT "bunzip2 -c -d < %s >> %s"
 #  define BZIP2_COMPRESS "bzip2"
+#  define LZMA_EXTENSION "lzma"
+#  define UNLZMA_FORMAT "unlzma -c -d < %s >> %s"
 #  define LZMA_COMPRESS "lzma"
 #endif
 
@@ -141,19 +128,10 @@ from the X Consortium.
 #  define NO_MANPATH_SUPPORT
 #endif
 
-#ifndef HAS_MKSTEMP
-#  ifdef NO_MANPATH_SUPPORT
-#    define APROPOS_FORMAT ("man -k %s | pr -h Apropos > %s")
-#  else
-#    define APROPOS_FORMAT ("man -M %s -k %s | pr -h Apropos > %s")
-#  endif
+#ifdef NO_MANPATH_SUPPORT
+#  define APROPOS_FORMAT ("man -k %s | pr -h Apropos >> %s")
 #else
-/* with mkstemp the temp output file is already created */
-#  ifdef NO_MANPATH_SUPPORT
-#    define APROPOS_FORMAT ("man -k %s | pr -h Apropos >> %s")
-#  else
-#    define APROPOS_FORMAT ("man -M %s -k %s | pr -h Apropos >> %s")
-#  endif
+#  define APROPOS_FORMAT ("man -M %s -k %s | pr -h Apropos >> %s")
 #endif
 
 #ifndef HANDLE_ROFFSEQ
-- 
1.7.9.2



More information about the xorg-devel mailing list