Release 6
[unix-history] / usr / src / usr.bin / printenv / printenv.c
index f084aec..33fec35 100644 (file)
@@ -2,12 +2,7 @@
  * Copyright (c) 1987 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1987 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -17,36 +12,62 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)printenv.c 5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)printenv.c 5.5 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
+#include <sys/types.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+void   usage __P((void));
+
 /*
  * printenv
  *
  * Bill Joy, UCB
  * February, 1979
  */
 /*
  * printenv
  *
  * Bill Joy, UCB
  * February, 1979
  */
+int
 main(argc, argv)
        int argc;
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
        extern char **environ;
        register char *cp, **ep;
 {
        extern char **environ;
        register char *cp, **ep;
-       register int len;
+       register size_t len;
+       int ch;
+
+       while ((ch = getopt(argc, argv, "")) != EOF)
+               switch(ch) {
+               case '?':
+               default:
+                       usage();
+               }
+       argc -= optind;
+       argv += optind;
 
 
-       if (argc < 2) {
+       if (argc == 0) {
                for (ep = environ; *ep; ep++)
                for (ep = environ; *ep; ep++)
-                       puts(*ep);
+                       (void)printf("%s\n", *ep);
                exit(0);
        }
                exit(0);
        }
-       len = strlen(*++argv);
+       len = strlen(*argv);
        for (ep = environ; *ep; ep++)
        for (ep = environ; *ep; ep++)
-               if (!strncmp(*ep, *argv, len)) {
+               if (!memcmp(*ep, *argv, len)) {
                        cp = *ep + len;
                        if (!*cp || *cp == '=') {
                        cp = *ep + len;
                        if (!*cp || *cp == '=') {
-                               puts(*cp ? cp + 1 : cp);
+                               (void)printf("%s\n", *cp ? cp + 1 : cp);
                                exit(0);
                        }
                }
        exit(1);
 }
                                exit(0);
                        }
                }
        exit(1);
 }
+
+void
+usage()
+{
+       (void)fprintf(stderr, "usage: printenv [name]\n");
+       exit(1);
+}