pax, tar, and cpio are one binary that changes behavior based on the last 
component of argv[0].  Lots of shared code, so sure, but I find the 
strcmp() tests in the main code overly complex and perhaps an affront to 
clear style.  (It's _not_ really a perf issue, as in per-file processing 
it only checks this for directories and not each file or block.)

So how about we have a enum for the mode that gets set when it decides how 
to parse the options and is then checked directly after that?  Is this 
clearer?

Philip Guenther


Index: extern.h
===================================================================
RCS file: /cvs/src/bin/pax/extern.h,v
retrieving revision 1.55
diff -u -p -r1.55 extern.h
--- extern.h    14 Aug 2016 04:47:52 -0000      1.55
+++ extern.h    23 Aug 2016 03:49:10 -0000
@@ -240,6 +240,7 @@ extern int exit_val;
 extern int docrc;
 extern char *dirptr;
 extern char *argv0;
+extern enum op_mode { OP_PAX, OP_TAR, OP_CPIO } op_mode;
 extern FILE *listf;
 extern int listfd;
 extern char *tempfile;
Index: pax.c
===================================================================
RCS file: /cvs/src/bin/pax/pax.c,v
retrieving revision 1.45
diff -u -p -r1.45 pax.c
--- pax.c       23 Jun 2016 06:37:36 -0000      1.45
+++ pax.c       23 Aug 2016 03:49:10 -0000
@@ -90,6 +90,7 @@ int   exit_val;               /* exit value */
 int    docrc;                  /* check/create file crc */
 char   *dirptr;                /* destination dir in a copy */
 char   *argv0;                 /* root of argv[0] */
+enum op_mode op_mode;          /* what program are we acting as? */
 sigset_t s_mask;               /* signal mask for cleanup critical sect */
 FILE   *listf = stderr;        /* file pointer to print file list to */
 int    listfd = STDERR_FILENO; /* fd matching listf, for sighandler output */
Index: options.c
===================================================================
RCS file: /cvs/src/bin/pax/options.c,v
retrieving revision 1.94
diff -u -p -r1.94 options.c
--- options.c   14 Aug 2016 04:47:52 -0000      1.94
+++ options.c   23 Aug 2016 03:49:10 -0000
@@ -186,11 +186,13 @@ options(int argc, char **argv)
        argv0 = __progname;
 
        if (strcmp(NM_TAR, argv0) == 0) {
+               op_mode = OP_TAR;
                tar_options(argc, argv);
                return;
        }
 #ifndef NOCPIO
        else if (strcmp(NM_CPIO, argv0) == 0) {
+               op_mode = OP_CPIO;
                cpio_options(argc, argv);
                return;
        }
@@ -199,6 +201,7 @@ options(int argc, char **argv)
         * assume pax as the default
         */
        argv0 = NM_PAX;
+       op_mode = OP_PAX;
        pax_options(argc, argv);
 }
 
Index: ar_io.c
===================================================================
RCS file: /cvs/src/bin/pax/ar_io.c,v
retrieving revision 1.57
diff -u -p -r1.57 ar_io.c
--- ar_io.c     14 Aug 2016 18:30:33 -0000      1.57
+++ ar_io.c     23 Aug 2016 03:49:10 -0000
@@ -390,12 +390,12 @@ ar_close(int in_sig)
                return;
        }
 
-       if (strcmp(NM_PAX, argv0) == 0)
+       if (op_mode == OP_PAX)
                (void)dprintf(listfd, "%s: %s vol %d, %lu files,"
                    " %llu bytes read, %llu bytes written.\n",
                    argv0, frmt->name, arvol-1, flcnt, rdcnt, wrcnt);
 #ifndef NOCPIO
-       else if (strcmp(NM_CPIO, argv0) == 0)
+       else if (op_mode == OP_CPIO)
                (void)dprintf(listfd, "%llu blocks\n",
                    (rdcnt ? rdcnt : wrcnt) / 5120);
 #endif /* !NOCPIO */
@@ -1112,7 +1112,7 @@ ar_next(void)
        if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0)
                syswarn(0, errno, "Unable to restore signal mask");
 
-       if (done || !wr_trail || force_one_volume || strcmp(NM_TAR, argv0) == 0)
+       if (done || !wr_trail || force_one_volume || op_mode == OP_TAR)
                return(-1);
 
        tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
Index: file_subs.c
===================================================================
RCS file: /cvs/src/bin/pax/file_subs.c,v
retrieving revision 1.50
diff -u -p -r1.50 file_subs.c
--- file_subs.c 23 Aug 2016 03:31:44 -0000      1.50
+++ file_subs.c 23 Aug 2016 03:49:10 -0000
@@ -371,7 +371,7 @@ node_creat(ARCHD *arcn)
                         * potential symlink chain before trying to create the
                         * directory.
                         */
-                       if (strcmp(NM_TAR, argv0) == 0 && Lflag) {
+                       if (op_mode == OP_TAR && Lflag) {
                                while (lstat(nm, &sb) == 0 &&
                                    S_ISLNK(sb.st_mode)) {
                                        len = readlink(nm, target,
@@ -484,7 +484,7 @@ badlink:
        if (pmode && !defer_pmode)
                set_pmode(nm, arcn->sb.st_mode);
 
-       if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) {
+       if (arcn->type == PAX_DIR && op_mode != OP_CPIO) {
                /*
                 * Dirs must be processed again at end of extract to set times
                 * and modes to agree with those stored in the archive. However
@@ -758,7 +758,7 @@ set_ids(char *fnm, uid_t uid, gid_t gid)
                 * ignore EPERM unless in verbose mode or being run by root.
                 * if running as pax, POSIX requires a warning.
                 */
-               if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
+               if (op_mode == OP_PAX || errno != EPERM || vflag ||
                    geteuid() == 0)
                        syswarn(1, errno, "Unable to set file uid/gid of %s",
                            fnm);
@@ -775,7 +775,7 @@ fset_ids(char *fnm, int fd, uid_t uid, g
                 * ignore EPERM unless in verbose mode or being run by root.
                 * if running as pax, POSIX requires a warning.
                 */
-               if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
+               if (op_mode == OP_PAX || errno != EPERM || vflag ||
                    geteuid() == 0)
                        syswarn(1, errno, "Unable to set file uid/gid of %s",
                            fnm);

Reply via email to