home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8710 / 2 < prev    next >
Encoding:
Text File  |  1990-07-13  |  8.6 KB  |  338 lines

  1. Path: uunet!husc6!necntc!ncoast!allbery
  2. From: drw@culdev1.UUCP (Dale Worley)
  3. Newsgroups: comp.sources.misc
  4. Subject: Upgrade to uudecode
  5. Message-ID: <4810@ncoast.UUCP>
  6. Date: 5 Oct 87 00:16:11 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Organization: Cullinet Software, Westwood, MA, USA
  9. Lines: 325
  10. Approved: allbery@ncoast.UUCP
  11. X-Archive: comp.sources.misc/8710/2
  12.  
  13. The following is a patch to the p.d. uudecode posted here recently.
  14. It adds an option "-f", which cures what I consider to be the most
  15. annoying feature of uudecode:  it insists on writing its output to the
  16. file name specified in the input.  When "-f" is given, output goes to
  17. stdout, so uudecode can be used as a filter.  (If there is more than
  18. one file in the encoded file, they are concatenated together.)
  19.  
  20. Dale
  21. ----------------------------------------
  22. *** uudecode.old    Tue Sep 29 12:51:21 1987
  23. --- uudecode.c    Tue Sep 29 13:43:11 1987
  24. ***************
  25. *** 13,21 ****
  26.      loop for multiple decodes from a single file, and to handle common
  27.      BITNET mangling.  Also kludged around a missing string function in Aztec
  28.      C */
  29.   
  30.   /*
  31. !  * uudecode [input]
  32.    *
  33.    * Decode a file encoded with uuencode.  WIll extract multiple encoded
  34.    * modules from a single file.    Can deal with most mangled files, including
  35. --- 13,26 ----
  36.      loop for multiple decodes from a single file, and to handle common
  37.      BITNET mangling.  Also kludged around a missing string function in Aztec
  38.      C */
  39. + /* Modified by drw (Dale Worley, drw@math.mit.edu) to add the -f switch, which
  40. +    forces the output to be on stdout.  This makes uudecode into a filter, and
  41. +    frees the user from perversions in the file name and mode given in the
  42. +    input file. These changes Copyright (C) 1987 Dale R. Worley, and are hereby
  43. +    put in the public domain. */
  44.   
  45.   /*
  46. !  * uudecode [-f] [input]
  47.    *
  48.    * Decode a file encoded with uuencode.  WIll extract multiple encoded
  49.    * modules from a single file.    Can deal with most mangled files, including
  50. ***************
  51. *** 37,42 ****
  52. --- 42,50 ----
  53.   #include <sys/stat.h>
  54.   #endif
  55.   
  56. + long    written_size;    /* number of bytes written to output, used instead of
  57. +                of ftell, because ftell doesn't work with -f */
  58.   #define SUMSIZE 64
  59.   #define DEC(c)    (((c) - ' ') & 077)    /* single character decode */
  60.   
  61. ***************
  62. *** 49,54 ****
  63. --- 57,63 ----
  64.   long    filesize;    /* theoretical file size (from header) */
  65.   char    dest[128];
  66.   char    buf[80];
  67. + int    f_option = 0;    /* set to 1 if -f option is present */
  68.   
  69.   #ifdef AMIGA_LATTICE
  70.   extern    int Enable_Abort;
  71. ***************
  72. *** 55,60 ****
  73. --- 64,76 ----
  74.       Enable_Abort=1;
  75.   #endif
  76.   
  77. +     /* First, check for the -f option */
  78. +     if (argc >= 2 && strcmp(argv[1], "-f") == 0)
  79. +         {
  80. +         f_option = 1;
  81. +         argc--; argv++;
  82. +         }
  83.       /* A filename can be specified to be uudecoded, or nothing can
  84.       be specified, and the input will come from STDIN */
  85.   
  86. ***************
  87. *** 68,80 ****
  88.       if ((in = fopen(argv[1], "r")) == NULL)
  89.           {
  90.           fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
  91. !         fprintf(stderr, "USAGE: uudecode [infile]\n");
  92.           exit(10);
  93.           }
  94.       break;
  95.   
  96.       default:
  97. !     fprintf(stderr, "USAGE: uudecode [infile]\n");
  98.       exit(11);
  99.       break;
  100.       }
  101. --- 84,96 ----
  102.       if ((in = fopen(argv[1], "r")) == NULL)
  103.           {
  104.           fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
  105. !         fprintf(stderr, "USAGE: uudecode [-f] [infile]\n");
  106.           exit(10);
  107.           }
  108.       break;
  109.   
  110.       default:
  111. !     fprintf(stderr, "USAGE: uudecode [-f] [infile]\n");
  112.       exit(11);
  113.       break;
  114.       }
  115. ***************
  116. *** 104,149 ****
  117.       }
  118.       sscanf(buf, "begin %o %s", &mode, dest);
  119.   
  120.   #ifdef unix
  121. !     /* handle ~user/file format */
  122. !     if (dest[0] == '~')
  123. !     {
  124. !     char *sl;
  125. !     struct passwd *getpwnam();
  126. !     char *index();
  127. !     struct passwd *user;
  128. !     char dnbuf[100];
  129. !     sl = index(dest, '/');
  130. !     if (sl == NULL)
  131.           {
  132. !         fprintf(stderr, "Illegal ~user\n");
  133. !         exit(13);
  134.           }
  135. -     *sl++ = 0;
  136. -     user = getpwnam(dest+1);
  137. -     if (user == NULL)
  138. -         {
  139. -         fprintf(stderr, "No such user as %s\n", dest);
  140. -         exit(14);
  141. -         }
  142. -     strcpy(dnbuf, user->pw_dir);
  143. -     strcat(dnbuf, "/");
  144. -     strcat(dnbuf, sl);
  145. -     strcpy(dest, dnbuf);
  146. -     }
  147.   #endif
  148.   
  149. !     /* create output file */
  150. !     if ((out = fopen(dest, "w")) == NULL)
  151. !     {
  152. !     fprintf(stderr, "ERROR: can't open output file %s\n", dest);
  153. !     exit(15);
  154. !     }
  155.   #ifdef unix
  156. !     chmod(dest, mode);
  157.   #endif
  158.   
  159.       decode(in, out, dest);
  160.   
  161.       if (fgets(buf, sizeof buf, in) == NULL || strncmp(buf,"end",3))
  162. --- 120,177 ----
  163.       }
  164.       sscanf(buf, "begin %o %s", &mode, dest);
  165.   
  166. +     /* set up the output file */
  167. +     if (f_option)
  168. +         {
  169. +         /* the -f option is used, so use stdout */
  170. +     out = stdout;
  171. +         }
  172. +     else
  173. +         {
  174. +         /* the -f option is not used, so use the filename (and mode) in the
  175. +          * begin line */
  176.   #ifdef unix
  177. !     /* handle ~user/file format */
  178. !     if (dest[0] == '~')
  179.           {
  180. !         char *sl;
  181. !         struct passwd *getpwnam();
  182. !         char *index();
  183. !         struct passwd *user;
  184. !         char dnbuf[100];
  185. !         sl = index(dest, '/');
  186. !         if (sl == NULL)
  187. !         {
  188. !         fprintf(stderr, "Illegal ~user\n");
  189. !             exit(13);
  190. !         }
  191. !         *sl++ = 0;
  192. !         user = getpwnam(dest+1);
  193. !         if (user == NULL)
  194. !         {
  195. !         fprintf(stderr, "No such user as %s\n", dest);
  196. !         exit(14);
  197. !         }
  198. !         strcpy(dnbuf, user->pw_dir);
  199. !         strcat(dnbuf, "/");
  200. !         strcat(dnbuf, sl);
  201. !         strcpy(dest, dnbuf);
  202.           }
  203.   #endif
  204.   
  205. !     /* create output file */
  206. !     if ((out = fopen(dest, "w")) == NULL)
  207. !         {
  208. !         fprintf(stderr, "ERROR: can't open output file %s\n", dest);
  209. !         exit(15);
  210. !         }
  211.   #ifdef unix
  212. !     chmod(dest, mode);
  213.   #endif
  214. +        }
  215.   
  216. +     /* actually decode the data */
  217.       decode(in, out, dest);
  218.   
  219.       if (fgets(buf, sizeof buf, in) == NULL || strncmp(buf,"end",3))
  220. ***************
  221. *** 155,166 ****
  222.       if (!(fgets(buf,sizeof buf,in) == NULL || strncmp(buf,"size ",3)))
  223.       {
  224.       sscanf(buf, "size %ld", &filesize);
  225. !     if (ftell(out) != filesize)
  226.           {
  227. !         fprintf(stderr, "ERROR: file should have been %ld bytes long but was %ld.\n", filesize, ftell(out));
  228.           exit(17);
  229.           }
  230.       }
  231.       through_loop = 1;
  232.   }   /* forever */
  233.   }   /* main */
  234. --- 183,203 ----
  235.       if (!(fgets(buf,sizeof buf,in) == NULL || strncmp(buf,"size ",3)))
  236.       {
  237.       sscanf(buf, "size %ld", &filesize);
  238. !     if (written_size != filesize)
  239.           {
  240. !         fprintf(stderr, "ERROR: file should have been %ld bytes long but was %ld.\n", filesize, written_size);
  241.           exit(17);
  242.           }
  243.       }
  244. +     /* close the output file */
  245. +     if (!f_option)
  246. +         if (fclose(out) != 0)
  247. +             {
  248. +         fprintf(stderr, "ERROR: error closing file %s.\n", dest);
  249. +         exit(20);
  250. +         }
  251.       through_loop = 1;
  252.   }   /* forever */
  253.   }   /* main */
  254. ***************
  255. *** 185,190 ****
  256. --- 222,230 ----
  257.   register int n;
  258.   int checksum, line;
  259.   
  260. +     /* zero the byte count */
  261. +     written_size = 0;
  262.       for (line = 1; ; line++)    /* for each input line */
  263.       {
  264.       if (fgets(buf, sizeof buf, in) == NULL)
  265. ***************
  266. *** 220,228 ****
  267.   
  268.       while (n >= 4)
  269.           {
  270. !         j = DEC(bp[0]) << 2 | DEC(bp[1]) >> 4; putc(j, out); checksum += j;
  271. !         j = DEC(bp[1]) << 4 | DEC(bp[2]) >> 2; putc(j, out); checksum += j;
  272. !         j = DEC(bp[2]) << 6 | DEC(bp[3]);       putc(j, out); checksum += j;
  273.           checksum = checksum % SUMSIZE;
  274.           bp += 4;
  275.           n -= 3;
  276. --- 260,280 ----
  277.   
  278.       while (n >= 4)
  279.           {
  280. !         j = DEC(bp[0]) << 2 | DEC(bp[1]) >> 4;
  281. !         putc(j, out);
  282. !         checksum += j;
  283. !         written_size++;
  284. !         j = DEC(bp[1]) << 4 | DEC(bp[2]) >> 2;
  285. !         putc(j, out);
  286. !         checksum += j;
  287. !         written_size++;
  288. !         j = DEC(bp[2]) << 6 | DEC(bp[3]);
  289. !         putc(j, out);
  290. !         checksum += j;
  291. !         written_size++;
  292.           checksum = checksum % SUMSIZE;
  293.           bp += 4;
  294.           n -= 3;
  295. ***************
  296. *** 231,245 ****
  297. --- 283,306 ----
  298.           j = DEC(bp[0]) << 2 | DEC(bp[1]) >> 4;
  299.           checksum += j;
  300.           if (n >= 1)
  301. +             {
  302.               putc(j, out);
  303. +             written_size++;
  304. +             }
  305.           j = DEC(bp[1]) << 4 | DEC(bp[2]) >> 2;
  306.           checksum += j;
  307.           if (n >= 2)
  308. +             {
  309.               putc(j, out);
  310. +             written_size++;
  311. +             }
  312.           j = DEC(bp[2]) << 6 | DEC(bp[3]);
  313.           checksum += j;
  314.           if (n >= 3)
  315. +             {
  316.               putc(j, out);
  317. +             written_size++;
  318. +             }
  319.           checksum = checksum % SUMSIZE;
  320.           bp += 4;
  321.           n -= 3;
  322. ----------------------------------------
  323. -- 
  324. Dale Worley    Cullinet Software      ARPA: culdev1!drw@eddie.mit.edu
  325. UUCP: ...!seismo!harvard!mit-eddie!culdev1!drw
  326. "Thank you for your cooperation.  Goodnight." -- Robocop
  327. "Don't f--k with the babysitter." -- Adventures in Babysitting
  328.