home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / uudeco.zip / UUDECODE.C next >
Text File  |  1993-12-11  |  4KB  |  186 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. // Modified to compile under os/2 2.1 using Borland C++ for OS/2 version 1.0 12/10/93
  19. // only changes mader were:
  20. //  - remarked out code to handle ~... paths (under UNIX)
  21. //  - changed passed arguments to functions from old style to new style
  22. //        ie: decode(FILE *in, FILE *out) - is the new format
  23. //  - remarked out #include <pwd.h>
  24. //  - added #include <process.h>
  25.  
  26.  
  27. #ifndef lint
  28. static char sccsid[] = "@(#)uudecode.c    5.5 (Berkeley) 7/6/88";
  29. #endif /* not lint */
  30.  
  31. /*
  32.  * uudecode [input]
  33.  *
  34.  * create the specified file, decoding as you go.
  35.  * used with uuencode.
  36.  */
  37. #include <stdio.h>
  38. //include <pwd.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41.  
  42. #include <process.h>
  43.  
  44. /* single character decode */
  45. #define DEC(c)    (((c) - ' ') & 077)
  46.  
  47. main(int argc, char **argv)
  48. {
  49.     FILE *in, *out;
  50.     int mode;
  51.     char dest[128];
  52.     char buf[80];
  53.  
  54.     /* optional input arg */
  55.     if (argc > 1) {
  56.         if ((in = fopen(argv[1], "r")) == NULL) {
  57.             perror(argv[1]);
  58.             exit(1);
  59.         }
  60.         argv++; argc--;
  61.     } else
  62.         in = stdin;
  63.  
  64.     if (argc != 1) {
  65.         printf("Usage: uudecode [infile]\n");
  66.         exit(2);
  67.     }
  68.  
  69.     /* search for header line */
  70.     for (;;) {
  71.         if (fgets(buf, sizeof buf, in) == NULL) {
  72.             fprintf(stderr, "No begin line\n");
  73.             exit(3);
  74.         }
  75.         if (strncmp(buf, "begin ", 6) == 0)
  76.             break;
  77.     }
  78.     (void)sscanf(buf, "begin %o %s", &mode, dest);
  79.  
  80.     /* handle ~user/file format */
  81. /*    if (dest[0] == '~') {
  82.         char *sl;
  83.         struct passwd *getpwnam();
  84.         struct passwd *user;
  85.         char dnbuf[100], *index(), *strcat(), *strcpy();
  86.  
  87.         sl = index(dest, '/');
  88.         if (sl == NULL) {
  89.             fprintf(stderr, "Illegal ~user\n");
  90.             exit(3);
  91.         }
  92.         *sl++ = 0;
  93.         user = getpwnam(dest+1);
  94.         if (user == NULL) {
  95.             fprintf(stderr, "No such user as %s\n", dest);
  96.             exit(4);
  97.         }
  98.         strcpy(dnbuf, user->pw_dir);
  99.         strcat(dnbuf, "/");
  100.         strcat(dnbuf, sl);
  101.         strcpy(dest, dnbuf);
  102.     }
  103. */
  104.     /* create output file */
  105.     out = fopen(dest, "wb");
  106.     if (out == NULL) {
  107.         perror(dest);
  108.         exit(4);
  109.     }
  110.     chmod(dest, mode);
  111.  
  112.     decode(in, out);
  113.  
  114.     if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
  115.         fprintf(stderr, "No end line\n");
  116.         exit(5);
  117.     }
  118.     exit(0);
  119. }
  120.  
  121. /*
  122.  * copy from in to out, decoding as you go along.
  123.  */
  124. decode(FILE *in, FILE *out)
  125. {
  126.     char buf[80];
  127.     char *bp;
  128.     int n;
  129.  
  130.     for (;;) {
  131.         /* for each input line */
  132.         if (fgets(buf, sizeof buf, in) == NULL) {
  133.             printf("Short file\n");
  134.             exit(10);
  135.         }
  136.         n = DEC(buf[0]);
  137.         if (n <= 0)
  138.             break;
  139.  
  140.         bp = &buf[1];
  141.         while (n > 0) {
  142.             outdec(bp, out, n);
  143.             bp += 4;
  144.             n -= 3;
  145.         }
  146.     }
  147. }
  148.  
  149. /*
  150.  * output a group of 3 bytes (4 input characters).
  151.  * the input chars are pointed to by p, they are to
  152.  * be output to file f.  n is used to tell us not to
  153.  * output all of them at the end of the file.
  154.  */
  155. outdec(char *p, FILE *f, int n)
  156. {
  157.     int c1, c2, c3;
  158.  
  159.     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  160.     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  161.     c3 = DEC(p[2]) << 6 | DEC(p[3]);
  162.     if (n >= 1)
  163.         putc(c1, f);
  164.     if (n >= 2)
  165.         putc(c2, f);
  166.     if (n >= 3)
  167.         putc(c3, f);
  168. }
  169.  
  170. /*
  171.  * Return the ptr in sp at which the character c appears;
  172.  * NULL if not found
  173.  */
  174.  
  175. //define    NULL    0
  176.  
  177. char *
  178. index(char *sp, char c)
  179. {
  180.     do {
  181.         if (*sp == c)
  182.             return(sp);
  183.     } while (*sp++);
  184.     return(NULL);
  185. }
  186.