home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / netx / bm / uudecode.c < prev    next >
C/C++ Source or Header  |  1987-06-11  |  2KB  |  119 lines

  1. /* uudecode.c */
  2.         /* decode a printable ascii file created by uuencode into */
  3.     /* its original binary file mode. Input is expected from */
  4.     /* stdin as uudecode is used via a pipe with mail. */
  5. #include <stdio.h>
  6.  
  7. FILE *outfile;
  8. int count;
  9.  
  10. main(argc,argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.     int c,i,j,k,l,loop;
  15.     char ch1,ch2;
  16.     char remotedest[65];
  17.     char *begin,*end;
  18.  
  19.     begin = "begin ";
  20.     end = "end";
  21.     count = 0;
  22.     if((argc < 2) || (argc > 2))
  23.         errexit("Usage: uudecode file (mail | uudecode file)",NULL);
  24.            if((outfile = fopen(argv[1],"w")) == NULL)
  25.                errexit("Cannot open",argv[1]);
  26.  
  27.     if(findbeg()){
  28.         i = 0;
  29.         while((c = get_n_c()) != 0x0a) remotedest[i++] = c;
  30.         remotedest[i] = 0x00;
  31.         printf(" Remote destination = %s\n",remotedest);
  32. /*        fndeol();
  33. */
  34.         loop = 1;
  35.         while(loop == 1)  {
  36.             i = get_n_c() - 0x20;
  37.             if( i == 0) 
  38.                 loop = 0;
  39.             else {
  40.                 j = 0;
  41.                 while(j < i) {
  42.                     switch( j % 3) {
  43.                     case    0: ch1 = (get_n_c() - 32) << 2;
  44.                            ch2 = get_n_c() - 32;
  45.                            ch1 = ch1 | (ch2 >> 4);
  46.                            putc(ch1,outfile);
  47.                            ch1 = (ch2 << 4);
  48.                             break ;
  49.                     case    1: ch2 = (get_n_c() - 32);
  50.                            ch1 = ch1 | (ch2 >>2);
  51.                            putc(ch1,outfile);
  52.                            ch1 = (ch2 << 6);
  53.                             break ;
  54.                     case    2: ch2 = (get_n_c() - 32);
  55.                            ch1 = ch1 | ch2;
  56.                            putc(ch1,outfile); 
  57.                             break ;
  58.                     }
  59.                     j++;
  60.                 }
  61.                 count+=1;
  62.             }
  63.             fndeol();
  64.         }
  65.     } 
  66.     if(get_n_c() == 'e')
  67.         printf("Processing complete, total lines = %d.\n",count);
  68.     fndeol();
  69.     fclose(outfile);
  70.  
  71.     exit(0);
  72. }
  73.  
  74. findbeg()
  75. {
  76.     char c;
  77.     int i,j,loop;
  78.  
  79.     loop = 1;
  80.     while(loop) {
  81.         c = get_n_c();
  82.         if( c != 'b') {
  83.         c = fndeol();
  84.         }
  85.         if(c == 'b') {
  86.             if(get_n_c() == 'e')
  87.             if(get_n_c() == 'g')
  88.             if(get_n_c() == 'i')
  89.             if(get_n_c() == 'n')
  90.             if(get_n_c() == ' '){
  91.                 return 1;
  92.             }
  93.         }
  94.     }
  95. }
  96. fndeol()
  97. {
  98.     char c;
  99.     while(((c = get_n_c()) != 0x0a)) ;
  100.     return c;
  101. }
  102. get_n_c()
  103. {
  104.     int c;
  105.     c = getchar();
  106.     if( c == EOF ) 
  107.         if( count == 0)
  108.             errexit(" begin not found",NULL);
  109.         else
  110.             errexit(" Unexpected EOF found",NULL);
  111.     return c;
  112. }
  113. errexit(s1,s2)        /* print error message and exit */
  114. char *s1,*s2;        /* the error strings          */
  115. {
  116.     printf(s2 == NULL ? "%s\n" : "%s %s\n",s1,s2);
  117.     exit(-1);
  118. }
  119.