home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / uucd_pls.zip / UUDECODE.C < prev    next >
C/C++ Source or Header  |  1996-01-08  |  3KB  |  150 lines

  1. /* uudecode [input]
  2.  *
  3.  * create the specified file, decoding as you go.
  4.  * used with uuencode.
  5.  */
  6. /*Last modified by boris@innonyc.com
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <dos.h>
  12. /* single-character decode */
  13. #define DEC(c)    (((c) - ' ') & 077)
  14. char * index(char * sp, char c);
  15. void decode( FILE * in, FILE * out);
  16. void outdec(char * p, FILE * f,int n);
  17.  
  18.  
  19. void main(int argc, char ** argv)
  20. {
  21. FILE *in, *out;    int cur_arg=1;    char dest[256];    char buf[80];
  22.  struct find_t ffblk; int ii, done=1;char file_name[256];
  23.  
  24. if((argc==2)&&((!strcmp(argv[1],"-?"))||(!strcmp(argv[1],"-h"))
  25. ||(!strcmp(argv[1],"/?"))||(!strcmp(argv[1],"/h")) )){
  26. printf("Program performs uudecode on files.\nTo use type %s <file names>\n\
  27. When activated without arguments uses standart input\nWild card are SUPPORTED\n"
  28. ,argv[0]);
  29. exit(0);
  30. }
  31.  
  32. do{
  33.     /* optional input arg */
  34.     if (argc > 1) {
  35.          if(done){/*first attempt to decode the argument*/
  36.          done =_dos_findfirst(argv[cur_arg],_A_NORMAL|_A_HIDDEN|
  37.          _A_RDONLY|_A_SYSTEM|_A_ARCH,&ffblk);          ii=strlen(argv[cur_arg]);
  38.           while((ii>=0)&&(argv[cur_arg][ii]!='\\'))ii--;
  39.           ii++;
  40.          }
  41.          strncpy(file_name,argv[cur_arg],ii);file_name[ii]=0;
  42.          strcat(file_name,ffblk.name);
  43.          printf("Working with %s\n",file_name);
  44.          if ((in = fopen(file_name, "r")) == NULL) {
  45.         perror(file_name);
  46.         exit(1);
  47.          }
  48.     } else{
  49.         in = stdin;  cur_arg=argc;
  50.      }
  51.     /* search for header line */
  52.     do{
  53.         if (fgets(buf, sizeof buf, in) == NULL) {
  54.             fprintf(stderr, "No begin line\n");
  55.             goto ending;
  56.         }
  57.     } while(strncmp(buf, "begin ", 6) != 0);
  58.  
  59.     sscanf(buf, "begin %*o %s",dest);
  60.  
  61.     /* create output file */
  62.     out = fopen(dest, "wb");
  63.     if (out == NULL) {
  64.         perror(dest);
  65.         goto ending;
  66.     }
  67.     decode(in, out);
  68.  
  69.     if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
  70.         fprintf(stderr, "No end line\n");
  71.         goto ending;
  72.     }
  73.     ending: fclose(out); fclose(in);
  74.     done = _dos_findnext(&ffblk);
  75.     if ((argc > 1)&&(done!=0)){cur_arg++;}
  76.  
  77.   }while(cur_arg<argc);
  78. }
  79.  
  80. /*
  81.  * copy from in to out, decoding as you go along.
  82.  */
  83. void decode( FILE * in, FILE * out)
  84. {
  85.     char buf[80];
  86.     char *bp;
  87.     int n, i, expected;
  88.  
  89.     for (;;) {
  90.         /* for each input line */
  91.         if (fgets(buf, sizeof buf, in) == NULL) {
  92.             printf("Short file\n");
  93.             return;
  94.         }
  95.         if (buf[0] == '\n')
  96.             continue;
  97.         n = DEC(buf[0]);
  98.         if (n <= 0)
  99.             break;
  100.  
  101.         /* Calculate expected # of chars and pad if necessary */
  102.         expected = ((n+2)/3)<<2;
  103.         for (i = strlen(buf)-1; i <= expected; i++) buf[i] = ' ';
  104.  
  105.         bp = &buf[1];
  106.         while (n > 0) {
  107.             outdec(bp, out, n);
  108.             bp += 4;
  109.             n -= 3;
  110.         }
  111.     }
  112. }
  113.  
  114. /*
  115.  * output a group of 3 bytes (4 input characters).
  116.  * the input chars are pointed to by p, they are to
  117.  * be output to file f.  n is used to tell us not to
  118.  * output all of them at the end of the file.
  119.  */
  120. void outdec(char * p, FILE * f,int n)
  121. {
  122.     int c1, c2, c3;
  123.  
  124.     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  125.     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  126.     c3 = DEC(p[2]) << 6 | DEC(p[3]);
  127.     if (n >= 1)
  128.         putc(c1, f);
  129.     if (n >= 2)
  130.         putc(c2, f);
  131.     if (n >= 3)
  132.         putc(c3, f);
  133. }
  134.  
  135. /*
  136.  * Return the ptr in sp at which the character c appears;
  137.  * NULL if not found
  138.  */
  139.  
  140. #define    NULL    0
  141.  
  142. char * index(char * sp, char c)
  143. {
  144.     while(*sp) {
  145.         if (*sp == c)return(sp);
  146.         sp++;
  147.     }
  148.     return(NULL);
  149. }
  150.