]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - login-utils/setpwnam.c
scriptreplay: cleanup usage()
[thirdparty/util-linux.git] / login-utils / setpwnam.c
index 23aef532dd13147019778854018a17d4993a34a9..3e3c1abde509e991ac9e84f73649d7abfcdfe424 100644 (file)
@@ -9,8 +9,8 @@
  *
  *  Edited 11/10/96 (DD/MM/YY ;-) by Nicolai Langfeldt (janl@math.uio.no)
  *  to read /etc/passwd directly so that passwd, chsh and chfn can work on
- *  machines that run NIS (nĂ© YP).  Changes will not be made to usernames
- *  starting with +.
+ *  machines that run NIS (previously YP).  Changes will not be made to
+ *  usernames starting with +.
  *
  *  This file is distributed with no warranty.
  *
@@ -71,21 +71,20 @@ static void pw_init(void);
  *     If the given username exists in the passwd file, the entry is
  *     replaced with the given entry.
  */
-int setpwnam(struct passwd *pwd)
+int setpwnam(struct passwd *pwd, const char *prefix)
 {
        FILE *fp = NULL, *pwf = NULL;
-       int save_errno;
-       int found;
-       int namelen;
-       int buflen = 256;
-       int contlen, rc;
+       int save_errno, rc;
+       uint8_t found = 0;
+       size_t namelen;
+       size_t contlen;
+       size_t buflen = 256;
        char *linebuf = NULL;
        char *tmpname = NULL;
-       char *atomic_dir = "/etc";
 
        pw_init();
 
-       if ((fp = xfmkstemp(&tmpname, atomic_dir)) == NULL)
+       if ((fp = xfmkstemp(&tmpname, "/etc", prefix)) == NULL)
                return -1;
 
        /* ptmp should be owned by root.root or root.wheel */
@@ -106,8 +105,6 @@ int setpwnam(struct passwd *pwd)
                goto fail;
 
        /* parse the passwd file */
-       found = false;
-
        /* Do you wonder why I don't use getpwent? Read comments at top of
         * file */
        while (fgets(linebuf, buflen, pwf) != NULL) {
@@ -135,7 +132,7 @@ int setpwnam(struct passwd *pwd)
                         * change it!  */
                        if (putpwent(pwd, fp) < 0)
                                goto fail;
-                       found = true;
+                       found = 1;
                        continue;
                }
                /* Nothing in particular happened, copy input to output */
@@ -167,6 +164,7 @@ int setpwnam(struct passwd *pwd)
                goto fail;
        /* finally:  success */
        ulckpwdf();
+       free(linebuf);
        return 0;
 
  fail: