home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d038 / uudecode.lha / UUdecode / uudecode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-20  |  3.0 KB  |  198 lines

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