home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / doc_net / starter.3 < prev    next >
Encoding:
Text File  |  1990-05-31  |  3.1 KB  |  200 lines

  1.  
  2. ---CUT HERE--- Save the following as UUDECODE.C, compile, then run
  3. /* uudecode.c */
  4.  
  5. #ifndef lint
  6. static char sccsid[] = "@(#)uudecode.c    5.1 (Berkeley) 7/2/83";
  7. #endif
  8.  
  9. /*
  10.  * uudecode [input]
  11.  *
  12.  * create the specified file, decoding as you go.
  13.  * used with uuencode.
  14.  */
  15. #include <stdio.h>
  16. #ifndef MSDOS
  17. #include <pwd.h>
  18. #endif
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21.  
  22. /* single character decode */
  23. #define DEC(c)    (((c) - ' ') & 077)
  24.  
  25. main(argc, argv)
  26. char **argv;
  27. {
  28.     FILE *in, *out;
  29.     struct stat sbuf;
  30.     int mode;
  31.     char dest[128];
  32.     char buf[80];
  33.  
  34.     /* optional input arg */
  35.     if (argc > 1) {
  36.         if ((in = fopen(argv[1], "r")) == NULL) {
  37.             perror(argv[1]);
  38.             exit(1);
  39.         }
  40.         argv++; argc--;
  41.     } else
  42.         in = stdin;
  43.  
  44.     if (argc != 1) {
  45.         printf("Usage: uudecode [infile]\n");
  46.         exit(2);
  47.     }
  48.  
  49.     /* search for header line */
  50.     for (;;) {
  51.         if (fgets(buf, sizeof buf, in) == NULL) {
  52.             fprintf(stderr, "No begin line\n");
  53.             exit(3);
  54.         }
  55.         if (strncmp(buf, "begin ", 6) == 0)
  56.             break;
  57.     }
  58.     sscanf(buf, "begin %o %s", &mode, dest);
  59.  
  60.     /* handle ~user/file format */
  61. #ifndef MSDOS
  62.     if (dest[0] == '~') {
  63.         char *sl;
  64.         struct passwd *getpwnam();
  65.         char *index();
  66.         struct passwd *user;
  67.         char dnbuf[100];
  68.  
  69.         sl = index(dest, '/');
  70.         if (sl == NULL) {
  71.             fprintf(stderr, "Illegal ~user\n");
  72.             exit(3);
  73.         }
  74.         *sl++ = 0;
  75.         user = getpwnam(dest+1);
  76.         if (user == NULL) {
  77.             fprintf(stderr, "No such user as %s\n", dest);
  78.             exit(4);
  79.         }
  80.         strcpy(dnbuf, user->pw_dir);
  81.         strcat(dnbuf, "/");
  82.         strcat(dnbuf, sl);
  83.         strcpy(dest, dnbuf);
  84.     }
  85. #endif
  86.  
  87.     /* create output file */
  88. #ifdef MSDOS
  89.     /* binary output file */
  90.     out = fopen(dest, "wb");
  91. #else
  92.     out = fopen(dest, "w");
  93. #endif
  94.     if (out == NULL) {
  95.         perror(dest);
  96.         exit(4);
  97.     }
  98.     chmod(dest, mode);
  99.  
  100.     decode(in, out);
  101.  
  102.     if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
  103.         fprintf(stderr, "No end line\n");
  104.         exit(5);
  105.     }
  106.     exit(0);
  107. }
  108.  
  109. /*
  110.  * copy from in to out, decoding as you go along.
  111.  */
  112. decode(in, out)
  113. FILE *in;
  114. FILE *out;
  115. {
  116.     char buf[80];
  117.     char *bp;
  118.     int n;
  119.  
  120.     for (;;) {
  121.         /* for each input line */
  122.         if (fgets(buf, sizeof buf, in) == NULL) {
  123.             printf("Short file\n");
  124.             exit(10);
  125.         }
  126.         n = DEC(buf[0]);
  127.         if (n <= 0)
  128.             break;
  129.  
  130.         bp = &buf[1];
  131.         while (n > 0) {
  132.             outdec(bp, out, n);
  133.             bp += 4;
  134.             n -= 3;
  135.         }
  136.     }
  137. }
  138.  
  139. /*
  140.  * output a group of 3 bytes (4 input characters).
  141.  * the input chars are pointed to by p, they are to
  142.  * be output to file f.  n is used to tell us not to
  143.  * output all of them at the end of the file.
  144.  */
  145. outdec(p, f, n)
  146. char *p;
  147. FILE *f;
  148. {
  149.     int c1, c2, c3;
  150.  
  151.     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  152.     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  153.     c3 = DEC(p[2]) << 6 | DEC(p[3]);
  154.     if (n >= 1)
  155.         putc(c1, f);
  156.     if (n >= 2)
  157.         putc(c2, f);
  158.     if (n >= 3)
  159.         putc(c3, f);
  160. }
  161.  
  162.  
  163. /* fr: like read but stdio */
  164. int
  165. fr(fd, buf, cnt)
  166. FILE *fd;
  167. char *buf;
  168. int cnt;
  169. {
  170.     int c, i;
  171.  
  172.     for (i=0; i<cnt; i++) {
  173.         c = getc(fd);
  174.         if (c == EOF)
  175.             return(i);
  176.         buf[i] = c;
  177.     }
  178.     return (cnt);
  179. }
  180.  
  181. /*
  182.  * Return the ptr in sp at which the character c appears;
  183.  * NULL if not found
  184.  */
  185.  
  186. #define    NULL    0
  187.  
  188. char *
  189. index(sp, c)
  190. register char *sp, c;
  191. {
  192.     do {
  193.         if (*sp == c)
  194.             return(sp);
  195.     } while (*sp++);
  196.     return(NULL);
  197. }
  198.  
  199.  
  200.