cleanups, add manual page
[unix-history] / usr / src / usr.bin / uuencode / uuencode.c
index 5152b1f..e1d1b69 100644 (file)
@@ -1,6 +1,23 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, 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'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)uuencode.c 5.2 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)uuencode.c 5.6 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * uuencode [input] output
 
 /*
  * uuencode [input] output
@@ -12,7 +29,7 @@ static char sccsid[] = "@(#)uuencode.c        5.2 (Berkeley) %G%";
 #include <sys/stat.h>
 
 /* ENC is the basic 1 character encoding function to make a char printing */
 #include <sys/stat.h>
 
 /* ENC is the basic 1 character encoding function to make a char printing */
-#define ENC(c) (((c) & 077) + ' ')
+#define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
 
 main(argc, argv)
 char **argv;
 
 main(argc, argv)
 char **argv;
@@ -32,15 +49,15 @@ char **argv;
                in = stdin;
 
        if (argc != 2) {
                in = stdin;
 
        if (argc != 2) {
-               printf("Usage: uuencode [infile] remotefile\n");
+               fprintf(stderr,"Usage: uuencode [infile] remotefile\n");
                exit(2);
        }
 
        /* figure out the input file mode */
                exit(2);
        }
 
        /* figure out the input file mode */
-       fstat(fileno(in), &sbuf);
-       mode = sbuf.st_mode & 0777;
-       if (in == stdin && mode == 0)
-               mode = 0600;    /* use a reasonable mode in case of pipes */
+       if (fstat(fileno(in), &sbuf) < 0 || !isatty(fileno(in)))
+               mode = 0666 & ~umask(0666);
+       else
+               mode = sbuf.st_mode & 0777;
        printf("begin %o %s\n", mode, argv[1]);
 
        encode(in, stdout);
        printf("begin %o %s\n", mode, argv[1]);
 
        encode(in, stdout);
@@ -53,15 +70,15 @@ char **argv;
  * copy from in to out, encoding as you go along.
  */
 encode(in, out)
  * copy from in to out, encoding as you go along.
  */
 encode(in, out)
-FILE *in;
-FILE *out;
+register FILE *in;
+register FILE *out;
 {
        char buf[80];
 {
        char buf[80];
-       int i, n;
+       register int i, n;
 
        for (;;) {
                /* 1 (up to) 45 character line */
 
        for (;;) {
                /* 1 (up to) 45 character line */
-               n = fr(in, buf, 45);
+               n = fread(buf, 1, 45, in);
                putc(ENC(n), out);
 
                for (i=0; i<n; i += 3)
                putc(ENC(n), out);
 
                for (i=0; i<n; i += 3)
@@ -77,10 +94,10 @@ FILE *out;
  * output one group of 3 bytes, pointed at by p, on file f.
  */
 outdec(p, f)
  * output one group of 3 bytes, pointed at by p, on file f.
  */
 outdec(p, f)
-char *p;
-FILE *f;
+register char *p;
+register FILE *f;
 {
 {
-       int c1, c2, c3, c4;
+       register int c1, c2, c3, c4;
 
        c1 = *p >> 2;
        c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
 
        c1 = *p >> 2;
        c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
@@ -91,21 +108,3 @@ FILE *f;
        putc(ENC(c3), f);
        putc(ENC(c4), f);
 }
        putc(ENC(c3), f);
        putc(ENC(c4), f);
 }
-
-/* fr: like read but stdio */
-int
-fr(fd, buf, cnt)
-FILE *fd;
-char *buf;
-int cnt;
-{
-       int c, i;
-
-       for (i=0; i<cnt; i++) {
-               c = getc(fd);
-               if (c == EOF)
-                       return(i);
-               buf[i] = c;
-       }
-       return (cnt);
-}