apply new hurd patch to my tree
[debian/pax] / gen_subs.c
index 6ff16f0a6eb7ded7a0ed827cdf321098644d6388..b541cc930d57e932e83ffec1fa9268b7d8edbd62 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: gen_subs.c,v 1.8 1997/09/01 18:29:51 deraadt Exp $    */
+/*     $OpenBSD: gen_subs.c,v 1.19 2007/04/04 21:55:10 millert Exp $   */
 /*     $NetBSD: gen_subs.c,v 1.5 1995/03/21 09:07:26 cgd Exp $ */
 
 /*-
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
 
 #ifndef lint
 #if 0
-static char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
+static const char sccsid[] = "@(#)gen_subs.c   8.1 (Berkeley) 5/31/93";
 #else
-static char rcsid[] = "$OpenBSD: gen_subs.c,v 1.8 1997/09/01 18:29:51 deraadt Exp $";
+static const char rcsid[] = "$OpenBSD: gen_subs.c,v 1.19 2007/04/04 21:55:10 millert Exp $";
 #endif
 #endif /* not lint */
 
 #include <sys/types.h>
+#include <sys/sysmacros.h>
 #include <sys/time.h>
+#include <time.h>
 #include <sys/stat.h>
 #include <sys/param.h>
 #include <stdio.h>
-#include <tzfile.h>
+#include "tzfile.h"
 #include <utmp.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include "vis.h"
 #include "pax.h"
 #include "extern.h"
 
+#include "strmode.h"
+
 /*
  * a collection of general purpose subroutines used by pax
  */
@@ -68,40 +69,39 @@ static char rcsid[] = "$OpenBSD: gen_subs.c,v 1.8 1997/09/01 18:29:51 deraadt Ex
  */
 #define MODELEN 20
 #define DATELEN 64
+#define DAYSPERNYEAR 365
+/* #define SECSPERDAY 86400 */
+/* #define VIS_CSTYLE 0 */
 #define SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)
 #define CURFRMT                "%b %e %H:%M"
 #define OLDFRMT                "%b %e  %Y"
-#ifndef UT_NAMESIZE
-#define UT_NAMESIZE    8
-#endif
-#define UT_GRPSIZE     6
+#define NAME_WIDTH     8
 
 /*
  * ls_list()
  *     list the members of an archive in ls format
  */
 
-#ifdef __STDC__
-void
-ls_list(register ARCHD *arcn, time_t now, FILE *fp)
-#else
 void
-ls_list(arcn, now, fp)
-       register ARCHD *arcn;
-       time_t now;
-       FILE *fp;
-#endif
+ls_list(ARCHD *arcn, time_t now, FILE *fp)
 {
-       register struct stat *sbp;
+       struct stat *sbp;
        char f_mode[MODELEN];
        char f_date[DATELEN];
-       char *timefrmt;
+       const char *timefrmt;
+       int term;
+
+       term = zeroflag ? '\0' : '\n';  /* path termination character */
 
        /*
         * if not verbose, just print the file name
         */
        if (!vflag) {
-               (void)fprintf(fp, "%s\n", arcn->name);
+               if (zeroflag)
+                       (void)fputs(arcn->name, fp);
+               else
+                       safe_print(arcn->name, fp);
+               (void)putc(term, fp);
                (void)fflush(fp);
                return;
        }
@@ -129,22 +129,22 @@ ls_list(arcn, now, fp)
         */
        if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
                f_date[0] = '\0';
-       (void)fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink,
-               UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE,
-               name_gid(sbp->st_gid, 1));
+       (void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink,
+               NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1),
+               NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1));
 
        /*
         * print device id's for devices, or sizes for other nodes
         */
        if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
-#              ifdef NET2_STAT
+#              ifdef LONG_OFF_T
                (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
 #              else
                (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
 #              endif
                    (unsigned long)MINOR(sbp->st_rdev));
        else {
-#              ifdef NET2_STAT
+#              ifdef LONG_OFF_T
                (void)fprintf(fp, "%9lu ", sbp->st_size);
 #              else
                (void)fprintf(fp, "%9qu ", sbp->st_size);
@@ -154,34 +154,32 @@ ls_list(arcn, now, fp)
        /*
         * print name and link info for hard and soft links
         */
-       (void)fprintf(fp, "%s %s", f_date, arcn->name);
-       if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
-               (void)fprintf(fp, " == %s\n", arcn->ln_name);
-       else if (arcn->type == PAX_SLK)
-               (void)fprintf(fp, " => %s\n", arcn->ln_name);
-       else
-               (void)putc('\n', fp);
+       (void)fputs(f_date, fp);
+       (void)putc(' ', fp);
+       safe_print(arcn->name, fp);
+       if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
+               fputs(" == ", fp);
+               safe_print(arcn->ln_name, fp);
+       } else if (arcn->type == PAX_SLK) {
+               fputs(" -> ", fp);
+               safe_print(arcn->ln_name, fp);
+       }
+       (void)putc(term, fp);
        (void)fflush(fp);
        return;
 }
 
 /*
  * tty_ls()
- *     print a short summary of file to tty.
+ *     print a short summary of file to tty.
  */
 
-#ifdef __STDC__
 void
-ls_tty(register ARCHD *arcn)
-#else
-void
-ls_tty(arcn)
-       register ARCHD *arcn;
-#endif
+ls_tty(ARCHD *arcn)
 {
        char f_date[DATELEN];
        char f_mode[MODELEN];
-       char *timefrmt;
+       const char *timefrmt;
 
        if (ltmfrmt == NULL) {
                /*
@@ -205,37 +203,23 @@ ls_tty(arcn)
        return;
 }
 
-/*
- * l_strncpy()
- *     copy src to dest up to len chars (stopping at first '\0').
- *     when src is shorter than len, pads to len with '\0'. 
- * Return:
- *     number of chars copied. (Note this is a real performance win over
- *     doing a strncpy(), a strlen(), and then a possible memset())
- */
-
-#ifdef __STDC__
-int
-l_strncpy(register char *dest, register char *src, int len)
-#else
-int
-l_strncpy(dest, src, len)
-       register char *dest;
-       register char *src;
-       int len;
-#endif
+void
+safe_print(const char *str, FILE *fp)
 {
-       register char *stop;
-       register char *start;
-
-       stop = dest + len;
-       start = dest;
-       while ((dest < stop) && (*src != '\0'))
-               *dest++ = *src++;
-       len = dest - start;
-       while (dest < stop)
-               *dest++ = '\0';
-       return(len);
+       char visbuf[5];
+       const char *cp;
+
+       /*
+        * if printing to a tty, use vis(3) to print special characters.
+        */
+       if (0 && isatty(fileno(fp))) {
+               for (cp = str; *cp; cp++) {
+                       (void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]);
+                       (void)fputs(visbuf, fp);
+               }
+       } else {
+               (void)fputs(str, fp);
+       }
 }
 
 /*
@@ -248,18 +232,10 @@ l_strncpy(dest, src, len)
  *     unsigned long value
  */
 
-#ifdef __STDC__
-u_long
-asc_ul(register char *str, int len, register int base)
-#else
 u_long
-asc_ul(str, len, base)
-       register char *str;
-       int len;
-       register int base;
-#endif
+asc_ul(char *str, int len, int base)
 {
-       register char *stop;
+       char *stop;
        u_long tval = 0;
 
        stop = str + len;
@@ -286,7 +262,7 @@ asc_ul(str, len, base)
                                break;
                }
        } else {
-               while ((str < stop) && (*str >= '0') && (*str <= '7'))
+               while ((str < stop) && (*str >= '0') && (*str <= '7'))
                        tval = (tval << 3) + (*str++ - '0');
        }
        return(tval);
@@ -299,19 +275,10 @@ asc_ul(str, len, base)
  *     NOTE: the string created is NOT TERMINATED.
  */
 
-#ifdef __STDC__
-int
-ul_asc(u_long val, register char *str, register int len, register int base)
-#else
 int
-ul_asc(val, str, len, base)
-       u_long val;
-       register char *str;
-       register int len;
-       register int base;
-#endif
+ul_asc(u_long val, char *str, int len, int base)
 {
-       register char *pt;
+       char *pt;
        u_long digit;
 
        /*
@@ -351,7 +318,7 @@ ul_asc(val, str, len, base)
        return(0);
 }
 
-#ifndef NET2_STAT
+#ifndef LONG_OFF_T
 /*
  * asc_uqd()
  *     convert hex/octal character string into a u_quad_t. We do not have to
@@ -362,18 +329,10 @@ ul_asc(val, str, len, base)
  *     u_quad_t value
  */
 
-#ifdef __STDC__
-u_quad_t
-asc_uqd(register char *str, int len, register int base)
-#else
 u_quad_t
-asc_uqd(str, len, base)
-       register char *str;
-       int len;
-       register int base;
-#endif
+asc_uqd(char *str, int len, int base)
 {
-       register char *stop;
+       char *stop;
        u_quad_t tval = 0;
 
        stop = str + len;
@@ -400,7 +359,7 @@ asc_uqd(str, len, base)
                                break;
                }
        } else {
-               while ((str < stop) && (*str >= '0') && (*str <= '7'))
+               while ((str < stop) && (*str >= '0') && (*str <= '7'))
                        tval = (tval << 3) + (*str++ - '0');
        }
        return(tval);
@@ -413,19 +372,10 @@ asc_uqd(str, len, base)
  *     NOTE: the string created is NOT TERMINATED.
  */
 
-#ifdef __STDC__
-int
-uqd_asc(u_quad_t val, register char *str, register int len, register int base)
-#else
 int
-uqd_asc(val, str, len, base)
-       u_quad_t val;
-       register char *str;
-       register int len;
-       register int base;
-#endif
+uqd_asc(u_quad_t val, char *str, int len, int base)
 {
-       register char *pt;
+       char *pt;
        u_quad_t digit;
 
        /*
@@ -465,3 +415,25 @@ uqd_asc(val, str, len, base)
        return(0);
 }
 #endif
+
+/*
+ * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
+ * at the first NUL char. NUL terminate buf if there is room left.
+ */
+size_t
+fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz)
+{
+       char *p = buf;
+       const char *q = field;
+       size_t i = 0;
+
+       if (fieldsz > bufsz)
+               fieldsz = bufsz;
+       while (i < fieldsz && *q != '\0') {
+               *p++ = *q++;
+               i++;
+       }
+       if (i < bufsz)
+               *p = '\0';
+       return(i);
+}