home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / RXSHIP24.ZIP / XXDECODE.C < prev    next >
C/C++ Source or Header  |  1992-09-30  |  7KB  |  341 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)xxdecode.c  5.3 (Berkeley) 4/10/85";
  3. #endif
  4.  
  5. /*
  6.  * xxdecode [input]
  7.  *
  8.  * create the specified file, decoding as you go.
  9.  * used with xxencode.
  10.  */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #ifndef MSDOS
  15. #ifndef VMCMS
  16. #include <pwd.h>
  17. #endif /* VMCMS */
  18. #else /* MSDOS */
  19. #include <fcntl.h>
  20. #include <io.h>
  21. #endif /* MSDOS */
  22.  
  23. #ifndef VMCMS
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #else
  27. #include <types.h>
  28. #include <stat.h>
  29. #include <dir.h>
  30. DIR * dir;
  31. #define perror(string) fprintf (stderr, "%s\n", string)
  32. #endif /* VMCMS */
  33.  
  34. /* single character decode */
  35. #define DEC(c)  ( table[ (c) & 0177 ] )
  36.  
  37. static char set[] = 
  38.     "+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  39. static char table[128];
  40. int replace;
  41.  
  42. main (argc, argv)
  43. int argc;
  44. char * argv [];
  45.  
  46. {
  47. FILE *in, *out;
  48. int mode;
  49. char source [128];
  50. char cdest [128];
  51. char * dest;
  52. char * temp;
  53. int i;
  54. char buf[80];
  55. int did = 0;
  56. int replflg = 0;
  57. int nameptr = 1;
  58. struct stat statbuf;
  59.  
  60. if (argc > 1)
  61.     {
  62.     if (strcmp (argv [1], "-r") == 0 || strcmp (argv [1], "-R") == 0)
  63.         {
  64.         replflg = 1;
  65.         nameptr = 2;
  66.         }
  67.     }
  68. /* input filename */
  69. if (nameptr <= argc - 1)
  70.     {
  71.     strcpy (source, argv [nameptr]);
  72. #ifdef VMCMS
  73.     for (i = nameptr + 1; i < argc; i++)
  74.         {
  75.         strcat (source, " ");
  76.         strcat (source, argv [i]);
  77.         }
  78. #endif /* VMCMS */
  79.     if ((in = fopen(source, "r")) == NULL)
  80.         {
  81. #ifndef VMCMS
  82.         perror(source);
  83. #else /* VMCMS */
  84.         fprintf (stderr, "Cannot open file <%s>\n", source);
  85. #endif /* VMCMS */
  86.         exit(1);
  87.         }
  88.     }
  89. else
  90. #ifdef VMCMS
  91.     {
  92.     fprintf (stderr, "Usage: xxdecode [-r] fn ft [fm] ");
  93.     fprintf (stderr, "[> fn ft [fm] [(options] (bin]\n");
  94.     exit (2);
  95.     }
  96. #else
  97.     in = stdin;
  98. if (isatty (fileno (in)) || argc > 2)
  99.     {
  100.     fprintf (stderr, "Usage: xxdecode [-r] [infile]\n");
  101.     exit(2);
  102.     }
  103. #endif /* VMCMS */
  104.  
  105. while (1)
  106.     {
  107.     dest = cdest;
  108.     /* search for header line */
  109.     for (;;)
  110.         {
  111.         if (fgets(buf, sizeof buf, in) == NULL)
  112.             {
  113.             if (! did)
  114.                 {
  115.                 fprintf (stderr, "No begin line\n");
  116.                 exit (3);
  117.                 }
  118.             else
  119.                 exit (0);
  120.             }
  121.         if (strncmp(buf, "begin ", 6) == 0)
  122.             {
  123.             did = 1;
  124.             break;
  125.             }
  126.         }
  127.     sscanf(buf, "begin %o %s", &mode, dest);
  128.  
  129.     /* handle ~user/file format */
  130.     if (dest[0] == '~')
  131.         {
  132. #ifndef VMCMS
  133. #ifndef MSDOS
  134.         char *sl;
  135.         struct passwd *getpwnam();
  136.         char *index();
  137.         struct passwd *user;
  138.         char dnbuf[100];
  139.  
  140.         sl = index(dest, '/');
  141.         if (sl == NULL)
  142.             {
  143.             fprintf(stderr, "Illegal ~user\n");
  144.             exit(3);
  145.             }
  146.         *sl++ = 0;
  147.         user = getpwnam(dest+1);
  148.         if (user == NULL)
  149.             {
  150.             fprintf(stderr, "No such user as %s\n", dest);
  151.             exit(4);
  152.             }
  153.         strcpy(dnbuf, user->pw_dir);
  154.         strcat(dnbuf, "/");
  155.         strcat(dnbuf, sl);
  156.         strcpy(dest, dnbuf);
  157. #else
  158.         dest++;
  159. #endif /* MSDOS */
  160. #endif /* VMCMS */
  161.         }
  162.     replace = replflg;
  163.     if (strcmp (dest, "/dev/stdout") != 0 && strcmp (dest, "-") != 0)
  164.         {
  165. #ifdef VMCMS
  166.         for (i = strlen (dest) - 1; i >= 0 && dest [i] != '/'; i--)
  167.             dest [i] = toupper (dest [i]);
  168.         dest = &dest [i + 1];
  169.         for (i = 0; dest [i] && dest [i] != '.'; i++)
  170.             dest [i] = toupper (dest [i]);
  171.         if (dest [i] == '.')
  172.             dest [i] = ' ';
  173.         for (; dest [i] && dest [i] != '.'; i++)
  174.             ;
  175.         if (dest [i] == '.')
  176.             dest [i] = '\0';
  177. #endif /* VMCMS */
  178.         if (stat (dest, &statbuf) == 0 && ! replflg)
  179.             {
  180.             fprintf (stderr, "File <%s> already exists.\n", dest);
  181.             fprintf (stderr, "Replace? (Y or N): ");
  182. #ifdef VMCMS
  183.             fprintf (stderr, "\n");
  184. #endif /* VMCMS */
  185.             do
  186.                 {
  187.                 fscanf (stdin, "%s", buf);
  188.                 buf [0] = toupper (buf [0]);
  189.                 }
  190.                 while (buf [0] != 'N' && buf [0] != 'Y');
  191.             replace = (buf [0] == 'Y');
  192.             }
  193.         else
  194.             replace = 1;
  195. #ifdef VMCMS
  196.         strcat (dest, " (bin");
  197. #endif /* VMCMS */
  198.         }
  199.     else
  200.         replace = 1;
  201.     /* create output file */
  202.     if (replace)
  203.         {
  204.         fprintf (stderr, "Opening file: %s\n", dest);
  205.         if (strcmp (dest, "/dev/stdout") == 0 || strcmp (dest, "-") == 0)
  206.             out = stdout;
  207.         else
  208.             out = fopen(dest, "w");
  209. #ifdef MSDOS
  210.         if (setmode (fileno (out), O_BINARY) == -1)
  211.             {
  212.             perror ("Cannot open stdout as binary\n");
  213.             exit (3);
  214.             }
  215. #endif /* MSDOS */
  216.         if (out == NULL)
  217.             {
  218. #ifndef VMCMS
  219.             perror(dest);
  220. #else /* VMCMS */
  221.             fprintf (stderr, "Cannot open file <%s>\n", dest);
  222. #endif /* VMCMS */
  223.             exit(4);
  224.             }
  225.         decode(in, out);
  226.         fclose (out);
  227. #ifndef VMCMS
  228.         chmod(dest, mode);
  229. #endif /* VMCMS */
  230.         }
  231.  
  232.     if (fgets(buf,sizeof (buf), in) == NULL || strcmp (buf, "end\n"))
  233.         {
  234.         fprintf(stderr, "No end line\n");
  235.         exit(5);
  236.         }
  237.     }
  238. }
  239.  
  240. /*
  241.  * copy from in to out, decoding as you go along.
  242.  */
  243. decode(in, out)
  244. FILE *in;
  245. FILE *out;
  246. {
  247.         char buf[80];
  248.         char *bp;
  249.         int n;
  250.  
  251.         bp=table;       /* clear table */
  252.         for( n=128 ; n ; --n ) {
  253.           *(bp++) = 0;
  254.         };
  255.  
  256.         bp=set;         /* fill table */
  257.         for( n=64 ; n ; --n ) {
  258.           table[ *(bp++) & 0177 ] = 64-n;
  259.         };
  260.  
  261.         for (;;) {
  262.                 /* for each input line */
  263.                 if (fgets(buf, sizeof buf, in) == NULL) {
  264.                         printf("Short file\n");
  265.                         exit(10);
  266.                 }
  267.                 n = DEC(buf[0]);
  268.                 if (n <= 0)
  269.                         break;
  270.  
  271.                 bp = &buf[1];
  272.                 while (n > 0) {
  273.                         if (replace)
  274.                             outdec(bp, out, n);
  275.                         bp += 4;
  276.                         n -= 3;
  277.                 }
  278.         }
  279. }
  280.  
  281. /*
  282.  * output a group of 3 bytes (4 input characters).
  283.  * the input chars are pointed to by p, they are to
  284.  * be output to file f.  n is used to tell us not to
  285.  * output all of them at the end of the file.
  286.  */
  287. outdec(p, f, n)
  288. char *p;
  289. FILE *f;
  290. int n;
  291. {
  292.         int c1, c2, c3;
  293.  
  294.         c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  295.         c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  296.         c3 = DEC(p[2]) << 6 | DEC(p[3]);
  297.         if (n >= 1)
  298.                 putc(c1, f);
  299.         if (n >= 2)
  300.                 putc(c2, f);
  301.         if (n >= 3)
  302.                 putc(c3, f);
  303. }
  304.  
  305.  
  306. /* fr: like read but stdio */
  307. int
  308. fr(fd, buf, cnt)
  309. FILE *fd;
  310. char *buf;
  311. int cnt;
  312. {
  313.         int c, i;
  314.  
  315.         for (i=0; i<cnt; i++) {
  316.                 c = getc(fd);
  317.                 if (c == EOF)
  318.                         return(i);
  319.                 buf[i] = c;
  320.         }
  321.         return (cnt);
  322. }
  323.  
  324. /*
  325.  * Return the ptr in sp at which the character c appears;
  326.  * NULL if not found
  327.  */
  328.  
  329. #define NULL    0
  330.  
  331. char *
  332. index(sp, c)
  333. register char *sp, c;
  334. {
  335.         do {
  336.                 if (*sp == c)
  337.                         return(sp);
  338.         } while (*sp++);
  339.         return(NULL);
  340. }
  341.