]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw.c
Cast uid/git to uintmax_t when using printf-like functions so the size of
[pw-darwin.git] / pw / pw.c
diff --git a/pw/pw.c b/pw/pw.c
index b21f6c0826dbdad16c8f2ac8213f232358f586ec..88c83dbfa125a5cdda983d620d2a555ab774c6c0 100644 (file)
--- a/pw/pw.c
+++ b/pw/pw.c
@@ -113,7 +113,7 @@ main(int argc, char *argv[])
                        "R:V:C:qn:u:rY",
                        "R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY",
                        "R:V:C:qn:u:FPa7",
-                       "R:V:C:",
+                       "R:V:C:q",
                        "R:V:C:q",
                        "R:V:C:q"
                },
@@ -122,7 +122,7 @@ main(int argc, char *argv[])
                        "R:V:C:qn:g:Y",
                        "R:V:C:qn:d:g:l:h:H:FM:m:NPY",
                        "R:V:C:qn:g:FPa",
-                       "R:V:C:"
+                       "R:V:C:q"
                 }
        };
 
@@ -136,7 +136,10 @@ main(int argc, char *argv[])
        name = NULL;
        relocated = nis = false;
        memset(&conf, 0, sizeof(conf));
+       strlcpy(conf.rootdir, "/", sizeof(conf.rootdir));
        strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath));
+       conf.fd = -1;
+       conf.checkduplicate = true;
 
        LIST_INIT(&arglist);
 
@@ -196,7 +199,7 @@ main(int argc, char *argv[])
                        cmdhelp(mode, which);
                else if (which != -1 && mode != -1) {
                        if (strspn(argv[1], "0123456789") == strlen(argv[1])) {
-                               id = strtonum(argv[1], 0, LONG_MAX, &errstr);
+                               id = strtounum(argv[1], 0, UID_MAX, &errstr);
                                if (errstr != NULL)
                                        errx(EX_USAGE, "Bad id '%s': %s",
                                            argv[1], errstr);
@@ -214,6 +217,10 @@ main(int argc, char *argv[])
        if (mode == -1 || which == -1)
                cmdhelp(mode, which);
 
+       conf.rootfd = open(conf.rootdir, O_DIRECTORY|O_CLOEXEC);
+       if (conf.rootfd == -1)
+               errx(EXIT_FAILURE, "Unable to open '%s'", conf.rootdir);
+       conf.which = which;
        /*
         * We know which mode we're in and what we're about to do, so now
         * let's dispatch the remaining command line args in a genric way.
@@ -232,6 +239,9 @@ main(int argc, char *argv[])
                        conf.config = optarg;
                        config = conf.config;
                        break;
+               case 'F':
+                       conf.force = true;
+                       break;
                case 'N':
                        conf.dryrun = true;
                        break;
@@ -246,6 +256,12 @@ main(int argc, char *argv[])
                case 'Y':
                        nis = true;
                        break;
+               case 'a':
+                       conf.all = true;
+                       break;
+               case 'c':
+                       conf.gecos = pw_checkname(optarg, 1);
+                       break;
                case 'g':
                        if (which == 0) { /* for user* */
                                addarg(&arglist, 'g', optarg);
@@ -253,7 +269,7 @@ main(int argc, char *argv[])
                        }
                        if (strspn(optarg, "0123456789") != strlen(optarg))
                                errx(EX_USAGE, "-g expects a number");
-                       id = strtonum(optarg, 0, LONG_MAX, &errstr);
+                       id = strtounum(optarg, 0, GID_MAX, &errstr);
                        if (errstr != NULL)
                                errx(EX_USAGE, "Bad id '%s': %s", optarg,
                                    errstr);
@@ -265,23 +281,51 @@ main(int argc, char *argv[])
                                addarg(&arglist, 'u', optarg);
                                break;
                        }
-                       id = strtonum(optarg, 0, LONG_MAX, &errstr);
+                       id = strtounum(optarg, 0, UID_MAX, &errstr);
                        if (errstr != NULL)
                                errx(EX_USAGE, "Bad id '%s': %s", optarg,
                                    errstr);
                        break;
                case 'n':
-                       if (strspn(optarg, "0123456789") != strlen(optarg)) {
-                               name = optarg;
-                               break;
-                       }
-                       id = strtonum(optarg, 0, LONG_MAX, &errstr);
+                       name = optarg;
+                       break;
+               case 'H':
+                       if (conf.fd != -1)
+                               errx(EX_USAGE, "'-h' and '-H' are mutually "
+                                   "exclusive options");
+                       conf.precrypted = true;
+                       if (strspn(optarg, "0123456789") != strlen(optarg))
+                               errx(EX_USAGE, "'-H' expects a file descriptor");
+
+                       conf.fd = strtonum(optarg, 0, INT_MAX, &errstr);
                        if (errstr != NULL)
-                               errx(EX_USAGE, "Bad id '%s': %s", optarg,
-                                   errstr);
+                               errx(EX_USAGE, "Bad file descriptor '%s': %s",
+                                   optarg, errstr);
+                       break;
+               case 'h':
+                       if (conf.fd != -1)
+                               errx(EX_USAGE, "'-h' and '-H' are mutually "
+                                   "exclusive options");
+
+                       if (strcmp(optarg, "-") == 0)
+                               conf.fd = '-';
+                       else if (strspn(optarg, "0123456789") == strlen(optarg)) {
+                               conf.fd = strtonum(optarg, 0, INT_MAX, &errstr);
+                               if (errstr != NULL)
+                                       errx(EX_USAGE, "'-h' expects a "
+                                           "file descriptor or '-'");
+                       } else
+                               errx(EX_USAGE, "'-h' expects a file "
+                                   "descriptor or '-'");
                        break;
                case 'o':
-                       conf.checkduplicate = true;
+                       conf.checkduplicate = false;
+                       break;
+               case 'q':
+                       conf.quiet = true;
+                       break;
+               case 'r':
+                       conf.deletehome = true;
                        break;
                default:
                        addarg(&arglist, ch, optarg);
@@ -303,7 +347,7 @@ main(int argc, char *argv[])
         * We should immediately look for the -q 'quiet' switch so that we
         * don't bother with extraneous errors
         */
-       if (getarg(&arglist, 'q') != NULL)
+       if (conf.quiet)
                freopen(_PATH_DEVNULL, "w", stderr);
 
        /*
@@ -469,7 +513,8 @@ cmdhelp(int mode, int which)
                                "usage: pw usernext [switches]\n"
                                "\t-V etcdir      alternate /etc location\n"
                                "\t-R rootir      alternate root directory\n"
-                               "\t-C config      configuration file\n",
+                               "\t-C config      configuration file\n"
+                               "\t-q             quiet operation\n",
                                "usage pw: lock [switches]\n"
                                "\t-V etcdir      alternate /etc locations\n"
                                "\t-C config      configuration file\n"
@@ -523,6 +568,7 @@ cmdhelp(int mode, int which)
                                "\t-V etcdir      alternate /etc location\n"
                                "\t-R rootir      alternate root directory\n"
                                "\t-C config      configuration file\n"
+                               "\t-q             quiet operation\n"
                        }
                };
 
@@ -534,7 +580,12 @@ cmdhelp(int mode, int which)
 struct carg    *
 getarg(struct cargs * _args, int ch)
 {
-       struct carg    *c = LIST_FIRST(_args);
+       struct carg    *c;
+
+       if (_args == NULL)
+               return (NULL);
+       
+       c = LIST_FIRST(_args);
 
        while (c != NULL && c->ch != ch)
                c = LIST_NEXT(c, list);