BSD 4_4_Lite2 release
[unix-history] / usr / src / usr.bin / printf / printf.c
index ea1f68a..82ffbda 100644 (file)
@@ -1,35 +1,73 @@
 /*
 /*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 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
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
  */
 
-#if ! defined(BUILTIN) && ! defined(SHELL)
+#if !defined(BUILTIN) && !defined(SHELL)
 #ifndef lint
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1989, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 #endif
 
 #ifndef lint
 #endif /* not lint */
 #endif
 
 #ifndef lint
-static char sccsid[] = "@(#)printf.c   5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)printf.c   8.2 (Berkeley) 3/22/95";
 #endif /* not lint */
 
 #endif /* not lint */
 
-
 #include <sys/types.h>
 #include <sys/types.h>
+
+#include <err.h>
 #include <errno.h>
 #include <errno.h>
-#ifndef SHELL
+#include <limits.h>
+#ifdef SHELL
+#define        EOF     -1
+#else
 #include <stdio.h>
 #endif
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdio.h>
 #endif
 #include <stdlib.h>
 #include <string.h>
 
+/*
+ * XXX
+ * This *has* to go away.  TK.
+ */
 #ifdef SHELL
 #define main printfcmd
 #ifdef SHELL
 #define main printfcmd
-#define err error
-#include "/usr/src/devel/sh/bltin/bltin.h"
+#define warnx(a, b, c) {                                               \
+       char buf[64];                                                   \
+       (void)sprintf(buf, sizeof(buf), a, b, c);                       \
+       error(buf);                                                     \
+}
+#include "../../bin/sh/bltin/bltin.h"
 #endif
 
 #define PF(f, func) { \
 #endif
 
 #define PF(f, func) { \
@@ -45,16 +83,14 @@ static char sccsid[] = "@(#)printf.c        5.11 (Berkeley) %G%";
 }
 
 static int      asciicode __P((void));
 }
 
 static int      asciicode __P((void));
-#ifndef SHELL
-static void     err __P((const char *fmt, ...));
-#endif
 static void     escape __P((char *));
 static int      getchr __P((void));
 static double   getdouble __P((void));
 static void     escape __P((char *));
 static int      getchr __P((void));
 static double   getdouble __P((void));
-static int      getint __P((void));
-static long     getlong __P((void));
+static int      getint __P((int *));
+static int      getlong __P((long *));
 static char    *getstr __P((void));
 static char    *mklong __P((char *, int));
 static char    *getstr __P((void));
 static char    *mklong __P((char *, int));
+static void     usage __P((void));
 
 static char **gargv;
 
 
 static char **gargv;
 
@@ -65,15 +101,25 @@ progprintf(argc, argv)
 main(argc, argv)
 #endif
        int argc;
 main(argc, argv)
 #endif
        int argc;
-       char **argv;
+       char *argv[];
 {
 {
+       extern int optind;
        static char *skip1, *skip2;
        static char *skip1, *skip2;
-       register char *format, *fmt, *start;
-       register int end, fieldwidth, precision;
-       char convch, nextch;
+       int ch, end, fieldwidth, precision;
+       char convch, nextch, *format, *fmt, *start;
+
+       while ((ch = getopt(argc, argv, "")) != EOF)
+               switch (ch) {
+               case '?':
+               default:
+                       usage();
+                       return (1);
+               }
+       argc -= optind;
+       argv += optind;
 
 
-       if (argc < 2) {
-               (void)fprintf(stderr, "usage: printf format [arg ...]\n");
+       if (argc < 1) {
+               usage();
                return (1);
        }
 
                return (1);
        }
 
@@ -88,7 +134,7 @@ main(argc, argv)
        skip1 = "#-+ 0";
        skip2 = "*0123456789";
 
        skip1 = "#-+ 0";
        skip2 = "*0123456789";
 
-       escape(fmt = format = *++argv);         /* backslash interpretation */
+       escape(fmt = format = *argv);           /* backslash interpretation */
        gargv = ++argv;
        for (;;) {
                end = 0;
        gargv = ++argv;
        for (;;) {
                end = 0;
@@ -97,7 +143,8 @@ next:                for (start = fmt;; ++fmt) {
                        if (!*fmt) {
                                /* avoid infinite loop */
                                if (end == 1) {
                        if (!*fmt) {
                                /* avoid infinite loop */
                                if (end == 1) {
-                                       err("missing format character");
+                                       warnx("missing format character",
+                                           NULL, NULL);
                                        return (1);
                                }
                                end = 1;
                                        return (1);
                                }
                                end = 1;
@@ -119,19 +166,27 @@ next:             for (start = fmt;; ++fmt) {
                }
 
                /* skip to field width */
                }
 
                /* skip to field width */
-               for (; index(skip1, *fmt); ++fmt);
-               fieldwidth = *fmt == '*' ? getint() : 0;
+               for (; strchr(skip1, *fmt); ++fmt);
+               if (*fmt == '*') {
+                       if (getint(&fieldwidth))
+                               return (1);
+               } else
+                       fieldwidth = 0;
 
                /* skip to possible '.', get following precision */
 
                /* skip to possible '.', get following precision */
-               for (; index(skip2, *fmt); ++fmt);
+               for (; strchr(skip2, *fmt); ++fmt);
                if (*fmt == '.')
                        ++fmt;
                if (*fmt == '.')
                        ++fmt;
-               precision = *fmt == '*' ? getint() : 0;
+               if (*fmt == '*') {
+                       if (getint(&precision))
+                               return (1);
+               } else
+                       precision = 0;
 
                /* skip to conversion char */
 
                /* skip to conversion char */
-               for (; index(skip2, *fmt); ++fmt);
+               for (; strchr(skip2, *fmt); ++fmt);
                if (!*fmt) {
                if (!*fmt) {
-                       err("missing format character");
+                       warnx("missing format character", NULL, NULL);
                        return (1);
                }
 
                        return (1);
                }
 
@@ -159,7 +214,8 @@ next:               for (start = fmt;; ++fmt) {
                        
                        if ((f = mklong(start, convch)) == NULL)
                                return (1);
                        
                        if ((f = mklong(start, convch)) == NULL)
                                return (1);
-                       p = getlong();
+                       if (getlong(&p))
+                               return (1);
                        PF(f, p);
                        break;
                }
                        PF(f, p);
                        break;
                }
@@ -171,7 +227,7 @@ next:               for (start = fmt;; ++fmt) {
                        break;
                }
                default:
                        break;
                }
                default:
-                       err("illegal format character.\n");
+                       warnx("illegal format character", NULL, NULL);
                        return (1);
                }
                *fmt = nextch;
                        return (1);
                }
                *fmt = nextch;
@@ -188,11 +244,11 @@ mklong(str, ch)
        int len;
 
        len = strlen(str) + 2;
        int len;
 
        len = strlen(str) + 2;
-       bcopy(str, copy, len - 3);
+       memmove(copy, str, len - 3);
        copy[len - 3] = 'l';
        copy[len - 2] = ch;
        copy[len - 1] = '\0';
        copy[len - 3] = 'l';
        copy[len - 2] = ch;
        copy[len - 1] = '\0';
-       return(copy);
+       return (copy);
 }
 
 static void
 }
 
 static void
@@ -202,7 +258,7 @@ escape(fmt)
        register char *store;
        register int value, c;
 
        register char *store;
        register int value, c;
 
-       for (store = fmt; c = *fmt; ++fmt, ++store) {
+       for (store = fmt; (c = *fmt) != '\0'; ++fmt, ++store) {
                if (c != '\\') {
                        *store = c;
                        continue;
                if (c != '\\') {
                        *store = c;
                        continue;
@@ -260,47 +316,79 @@ static int
 getchr()
 {
        if (!*gargv)
 getchr()
 {
        if (!*gargv)
-               return('\0');
-       return((int)**gargv++);
+               return ('\0');
+       return ((int)**gargv++);
 }
 
 static char *
 getstr()
 {
        if (!*gargv)
 }
 
 static char *
 getstr()
 {
        if (!*gargv)
-               return("");
-       return(*gargv++);
+               return ("");
+       return (*gargv++);
 }
 
 static char *Number = "+-.0123456789";
 static int
 }
 
 static char *Number = "+-.0123456789";
 static int
-getint()
+getint(ip)
+       int *ip;
 {
 {
-       if (!*gargv)
-               return(0);
-       if (index(Number, **gargv))
-               return(atoi(*gargv++));
-       return(asciicode());
+       long val;
+
+       if (getlong(&val))
+               return (1);
+       if (val > INT_MAX) {
+               warnx("%s: %s", *gargv, strerror(ERANGE));
+               return (1);
+       }
+       *ip = val;
+       return (0);
 }
 
 }
 
-static long
-getlong()
+static int
+getlong(lp)
+       long *lp;
 {
 {
-       if (!*gargv)
-               return((long)0);
-       if (index(Number, **gargv))
-               return(strtol(*gargv++, (char **)NULL, 0));
-       return((long)asciicode());
+       long val;
+       char *ep;
+
+       if (!*gargv) {
+               *lp = 0;
+               return (0);
+       }
+       if (strchr(Number, **gargv)) {
+               errno = 0;
+               val = strtol(*gargv, &ep, 0);
+               if (*ep != '\0') {
+                       warnx("%s: illegal number", *gargv, NULL);
+                       return (1);
+               }
+               if (errno == ERANGE)
+                       if (val == LONG_MAX) {
+                               warnx("%s: %s", *gargv, strerror(ERANGE));
+                               return (1);
+                       }
+                       if (val == LONG_MIN) {
+                               warnx("%s: %s", *gargv, strerror(ERANGE));
+                               return (1);
+                       }
+                       
+               *lp = val;
+               ++gargv;
+               return (0);
+       }
+       *lp =  (long)asciicode();
+       return (0);
 }
 
 static double
 getdouble()
 {
        if (!*gargv)
 }
 
 static double
 getdouble()
 {
        if (!*gargv)
-               return((double)0);
-       if (index(Number, **gargv))
-               return(atof(*gargv++));
-       return((double)asciicode());
+               return ((double)0);
+       if (strchr(Number, **gargv))
+               return (atof(*gargv++));
+       return ((double)asciicode());
 }
 
 static int
 }
 
 static int
@@ -312,34 +400,11 @@ asciicode()
        if (ch == '\'' || ch == '"')
                ch = (*gargv)[1];
        ++gargv;
        if (ch == '\'' || ch == '"')
                ch = (*gargv)[1];
        ++gargv;
-       return(ch);
+       return (ch);
 }
 
 }
 
-#ifndef SHELL
-#if __STDC__
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
-
 static void
 static void
-#if __STDC__
-err(const char *fmt, ...)
-#else
-err(fmt, va_alist)
-       char *fmt;
-       va_dcl
-#endif
+usage()
 {
 {
-       va_list ap;
-#if __STDC__
-       va_start(ap, fmt);
-#else
-       va_start(ap);
-#endif
-       (void)fprintf(stderr, "printf: ");
-       (void)vfprintf(stderr, fmt, ap);
-       va_end(ap);
-       (void)fprintf(stderr, "\n");
+       (void)fprintf(stderr, "usage: printf format [arg ...]\n");
 }
 }
-#endif /* !SHELL */