]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fileutils: differentiate xmkstemp and xfmkstemp
authorSami Kerola <kerolasa@iki.fi>
Sat, 10 Mar 2012 11:29:35 +0000 (12:29 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 18 Mar 2012 13:29:38 +0000 (14:29 +0100)
Let developer to choose, case by case, what sort of return value is
best in her code.  The xmkstemp() is for users who want file
descriptor as return value of the function, xfmkstemp() will return
FILE pointer.

Proposed-By: Karel Zak <kzak@redhat.com>
CC: Davidlohr Bueso <dave@gnu.org>
Reference: http://marc.info/?l=util-linux-ng&m=133129570124003&w=2
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/fileutils.h
lib/fileutils.c
login-utils/setpwnam.c
login-utils/vipw.c
term-utils/wall.c

index 27b566190a0c9e1641c85654da7198b67066e07f..691429ba09b2c45353b6527bc3f7ff974ef2b641 100644 (file)
@@ -1,6 +1,20 @@
 #ifndef UTIL_LINUX_FILEUTILS
 #define UTIL_LINUX_FILEUTILS
 
-extern FILE * xmkstemp(char **tmpname);
+extern int xmkstemp(char **tmpname);
 
+static inline FILE *xfmkstemp(char **tmpname)
+{
+       int fd;
+       FILE *ret;
+       fd = xmkstemp(tmpname);
+       if (fd == -1) {
+               return NULL;
+       }
+       if (!(ret = fdopen(fd, "w+"))) {
+               close(fd);
+               return NULL;
+       }
+       return ret;
+}
 #endif
index 81929893584e5dd5ea692d1356563780f32d47a6..4849061bb755ddb342369ccb1ac0da6bbe9bc683 100644 (file)
@@ -8,38 +8,35 @@
 #include <unistd.h>
 
 #include "c.h"
+#include "fileutils.h"
 #include "pathnames.h"
 #include "xalloc.h"
 
 /* Create open temporary file in safe way.  Please notice that the
  * file permissions are -rw------- by default. */
-FILE *xmkstemp(char **tmpname)
+int xmkstemp(char **tmpname)
 {
        char *localtmp;
        char *tmpenv;
        mode_t old_mode;
        int fd;
-       FILE *ret;
 
        tmpenv = getenv("TMPDIR");
        if (tmpenv)
                xasprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
-                        program_invocation_short_name);
+                         program_invocation_short_name);
        else
                xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
-                        program_invocation_short_name);
+                         program_invocation_short_name);
        old_mode = umask(077);
        fd = mkstemp(localtmp);
        umask(old_mode);
-       if (fd == -1)
-               return NULL;
-       if (!(ret = fdopen(fd, "w+")))
-               goto err;
+       if (fd == -1) {
+               free(localtmp);
+               localtmp = NULL;
+       }
        *tmpname = localtmp;
-       return ret;
- err:
-       close(fd);
-       return NULL;
+       return fd;
 }
 
 #ifdef TEST_PROGRAM
@@ -47,7 +44,7 @@ int main(void)
 {
        FILE *f;
        char *tmpname;
-       f = xmkstemp(&tmpname);
+       f = xfmkstemp(&tmpname);
        unlink(tmpname);
        free(tmpname);
        fclose(f);
index 6f3f4920a7f674f9b097262171dea6ae12b6501d..7459fe29852e06aedb2a14686f10351ba5c37fbd 100644 (file)
@@ -83,7 +83,7 @@ int setpwnam(struct passwd *pwd)
 
        pw_init();
 
-       if ((fp = xmkstemp(&tmpname)) == NULL)
+       if ((fp = xfmkstemp(&tmpname)) == NULL)
                return -1;
 
        /* ptmp should be owned by root.root or root.wheel */
@@ -140,7 +140,7 @@ int setpwnam(struct passwd *pwd)
                fputs(linebuf, fp);
        }
 
-       /* xmkstemp is too restrictive by default for passwd file */
+       /* xfmkstemp is too restrictive by default for passwd file */
        if (fchmod(fileno(fp), 0644) < 0)
                goto fail;
        rc = fclose(fp);
index 765d11120602c916a5bc7a18bae41de5f919df50..29e485cc46516807f1369b4f8a046039be956527 100644 (file)
@@ -143,7 +143,7 @@ static FILE * pw_tmpfile(int lockfd)
        FILE *fd;
        char *tmpname = NULL;
 
-       if ((fd = xmkstemp(&tmpname)) == NULL) {
+       if ((fd = xfmkstemp(&tmpname)) == NULL) {
                ulckpwdf();
                err(EXIT_FAILURE, _("can't open temporary file"));
        }
index b9f53212ae952df0247934db6ac2f4befbb14052..fb3db21197b0b8eccb0b15a8c05d9a0c54e8aa90 100644 (file)
@@ -196,7 +196,7 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
        line_max = sysconf(_SC_LINE_MAX);
        lbuf = xmalloc(line_max);
 
-       if ((fp = xmkstemp(&tmpname)) == NULL)
+       if ((fp = xfmkstemp(&tmpname)) == NULL)
                err(EXIT_FAILURE, _("can't open temporary file"));
        unlink(tmpname);
        free(tmpname);