Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/chpass Don't leave temporary file on abnormal exit (...



details:   https://anonhg.NetBSD.org/src/rev/723c4c5b0d5e
branches:  trunk
changeset: 535010:723c4c5b0d5e
user:      enami <enami%NetBSD.org@localhost>
date:      Thu Aug 08 04:49:26 2002 +0000

description:
Don't leave temporary file on abnormal exit (like, interrupted by user).
Minor cosmetic chagnes while I'm here.

diffstat:

 usr.bin/chpass/chpass.c |  41 +++++++++++++++++++++++++++--------------
 usr.bin/chpass/edit.c   |   5 ++---
 2 files changed, 29 insertions(+), 17 deletions(-)

diffs (140 lines):

diff -r b3cdc786d16c -r 723c4c5b0d5e usr.bin/chpass/chpass.c
--- a/usr.bin/chpass/chpass.c   Thu Aug 08 02:55:36 2002 +0000
+++ b/usr.bin/chpass/chpass.c   Thu Aug 08 04:49:26 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chpass.c,v 1.24 2002/08/07 23:20:30 soren Exp $        */
+/*     $NetBSD: chpass.c,v 1.25 2002/08/08 04:49:26 enami Exp $        */
 
 /*-
  * Copyright (c) 1988, 1993, 1994
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)chpass.c   8.4 (Berkeley) 4/2/94";
 #else 
-__RCSID("$NetBSD: chpass.c,v 1.24 2002/08/07 23:20:30 soren Exp $");
+__RCSID("$NetBSD: chpass.c,v 1.25 2002/08/08 04:49:26 enami Exp $");
 #endif
 #endif /* not lint */
 
@@ -66,10 +66,9 @@
 #include "chpass.h"
 #include "pathnames.h"
 
-char *tempname;
+static char tempname[] = "/etc/pw.XXXXXX";
 uid_t uid;
 int use_yp;
-int yflag;
 
 void   (*Pw_error) __P((const char *, int, int));
 
@@ -78,6 +77,7 @@
 #endif
 
 void   baduser __P((void));
+void   cleanup __P((void));
 int    main __P((int, char **));
 void   usage __P((void));
 
@@ -88,8 +88,8 @@
 {
        enum { NEWSH, LOADENTRY, EDITENTRY } op;
        struct passwd *pw, lpw, old_pw;
-       int ch, pfd, tfd, dfd;
-       char *arg, *username = NULL, tempname[] = "/etc/pw.XXXXXX";
+       int ch, dfd, pfd, tfd, yflag;
+       char *arg, *username = NULL;
 
 #ifdef __GNUC__
        pw = NULL;              /* XXX gcc -Wuninitialized */
@@ -101,7 +101,7 @@
 
        op = EDITENTRY;
        while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
-               switch(ch) {
+               switch (ch) {
                case 'a':
                        op = LOADENTRY;
                        arg = optarg;
@@ -164,8 +164,9 @@
                        if (pw != NULL)
                                use_yp = 0;
                        else {
-                               errx(1, "master YP server not running yppasswd daemon.\n\t%s\n",
-                                   "Can't change password.");
+                               warnx("master YP server not running yppasswd"
+                                   " daemon.");
+                               errx(1, "Can't change password.");
                        }
                }
        }
@@ -180,7 +181,8 @@
 
 #ifdef YP
        if (op == LOADENTRY && use_yp)
-               errx(1, "cannot load entry using YP.\n\tUse the -l flag to load local.");
+               errx(1, "cannot load entry using YP.\n"
+                   "\tUse the -l flag to load local.");
 #endif
 
        if (op == EDITENTRY || op == NEWSH) {
@@ -222,9 +224,12 @@
                dfd = mkstemp(tempname);
                if (dfd < 0 || fcntl(dfd, F_SETFD, 1) < 0)
                        (*Pw_error)(tempname, 1, 1);
+               if (atexit(cleanup)) {
+                       cleanup();
+                       errx(1, "couldn't register cleanup");
+               }
                display(tempname, dfd, pw);
                edit(tempname, pw);
-               (void)unlink(tempname);
        }
 
 #ifdef YP
@@ -284,8 +289,16 @@
 usage()
 {
 
-       (void)fprintf(stderr, "usage: %s [-a list] [-s shell] [-l] [user]\n"
-                             "       %s [-a list] [-s shell] [-y] [user]\n",
-                       getprogname(), getprogname());
+       (void)fprintf(stderr,
+           "usage: %s [-a list] [-s shell] [-l] [user]\n"
+           "       %s [-a list] [-s shell] [-y] [user]\n",
+           getprogname(), getprogname());
        exit(1);
 }
+
+void
+cleanup()
+{
+
+       (void)unlink(tempname);
+}
diff -r b3cdc786d16c -r 723c4c5b0d5e usr.bin/chpass/edit.c
--- a/usr.bin/chpass/edit.c     Thu Aug 08 02:55:36 2002 +0000
+++ b/usr.bin/chpass/edit.c     Thu Aug 08 04:49:26 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: edit.c,v 1.12 1998/07/26 21:25:16 mycroft Exp $        */
+/*     $NetBSD: edit.c,v 1.13 2002/08/08 04:49:26 enami Exp $  */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)edit.c     8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: edit.c,v 1.12 1998/07/26 21:25:16 mycroft Exp $");
+__RCSID("$NetBSD: edit.c,v 1.13 2002/08/08 04:49:26 enami Exp $");
 #endif
 #endif /* not lint */
 
@@ -73,7 +73,6 @@
                        (*Pw_error)(tempname, 1, 1);
                if (begin.st_mtime == end.st_mtime) {
                        warnx("no changes made");
-                       unlink(tempname);
                        (*Pw_error)(NULL, 0, 0);
                }
                if (verify(tempname, pw))



Home | Main Index | Thread Index | Old Index