/* * * Uuencode -- encode a file so that it's printable ascii, short lines * * Slightly modified from a version posted to net.sources a while back, * and suitable for compilation on the IBM PC * * modified for Lattice C on the ST - 11.05.85 by MSD * modified for ALCYON on the ST -    10-24-86 by RDR * modified a little more for MWC...  02/09/87 by JPHD * (An optional first argument of the form: -nnumber (e.g. -500), will * produce a serie of files that long, linked by the include statement, * such files are automatically uudecoded by the companion program.) * (A lot more to do about I/O speed, avoiding completely the stdio.h...) * */#include <stdio.h>#include <ctype.h>#ifdef M6502char * rindex(str, chr)char * str;char chr;{  int n;  n = 0;  while (*str++)    n++;  while (--n >= 0)    if (*--str == chr)      return(str);  return((char * )0);}char tmp_path[80];char * fix_pathname(p)char * p;{  if (strchr(p, ':'))    return(p);  else    {      strcpy(tmp_path, "D:");      strcat(tmp_path, p);      return(tmp_path);    }}#define exit careful_exit#elseextern char * rindex();#define fix_pathname(foo) foo#endif#define USAGE "Usage: uuencode [-n] inputfile\n"/* ENC is the basic 1 character encoding function to make a char printing */#define ENC(c) (((c) & 077) + ' ')extern FILE  * freopen(), * fopen();FILE * fp;FILE * outfp;char ofname[80];int part = 'a';int split = 0; fileln = 32000;#ifdef M6502char argbuf[80];#endifmain(argc, argv)int argc; char *argv[];{#ifdef M6502  if (argc < 2)    argc = 1 + readargs("UUE>", argbuf, argv + 1);#endif  outfp = stdout;  if (argc < 2)     {      fprintf(stderr, USAGE);      exit(2);    }  if (argv[1][0] == '-')     {      fileln = -atoi(argv[1]);      if (fileln <= 0) {  fprintf(stderr, "Wrong file length arg.\n");  exit();}      split = 1;      argv++;    }  makename(argv[1]);  if ((fp=fopen(fix_pathname(argv[1]), "rb"))==NULL) /* binary input !!! */    {      fprintf(stderr,"Cannot open %s\n",argv[1]);      exit(1);    }/*  if(freopen(ofname, "w", stdout)!=stdout)  */  if (!(outfp = fopen(fix_pathname(ofname), "w")))    {      fprintf(stderr,"Cannot reassign stdout\n");      exit(1);    }  maketable();/*  printf("begin %o %s\n", 0644, argv[1]);  */  fprintf(outfp, "begin %o %s\n", 0644, argv[1]);  encode();/*  printf("end\n"); */  fprintf(outfp, "end\n");  exit(0);}/* create ASCII table so a mailer can screw it up and the decode * program can restore the error. */maketable(){  register int i, j;/*  fputs("table", outfp); */  fprintf(outfp, "table\n");  for(i = ' ', j = 0; i < '`' ; j++)     {      if (j == 32)/*putchar('\n'); */fputc('\n', outfp);      fputc(i++,outfp);    }/*  putchar('\n'); */  fputc('\n', outfp);}/* I include this in all of my programs to take the guess work out of * filenames. */makename(name)char name[];{  register char * ptr;  strcpy(ofname, name);#ifdef old  /* I think index is neat; just look for a character, and   * bomb it.  Voila, you have a substring, no mess.   */  if(ptr = rindex(ofname,'.'))     {      *ptr = '\0';      *--ptr = part;    }  else/* i.e. make the last character in   the first name = part */    ofname[strlen(ofname)-1] = part;  strcat(ofname,".uue");#else  if (!(ptr = rindex(ofname, '.')))    ptr = ofname + strlen(ofname);  *ptr++ = '.';  *ptr++ = 'u';  *ptr++ = ((part - 'a') / 26) + 'a';  *ptr++ = ((part - 'a') % 26) + 'a';  *ptr++ = '\0';#endif  return;}/* * copy from stdin to stdout, encoding as you go along. */char buf[80];char file[80];encode(){  register int i, n;  register int lines;  lines = 6;  strcpy(file,ofname);  for (;;)     {      n = fr(buf, 45);/*      putchar(ENC(n)); */      fputc(ENC(n), outfp);      for (i = 0; i < n; i += 3)outdec(&buf[i]);/*      putchar(part); */      fputc(part, outfp);/*      putchar('\n'); */      fputc('\n', outfp);      ++lines;      if ((split != 0) && (lines > fileln)) {  ++part;  makename(file);  fprintf(outfp, "include %s\n",ofname);/*  if(freopen(ofname, "w", stdout)!=stdout)  */  fclose(outfp);  if (!(outfp = fopen(fix_pathname(ofname), "w")))    {      fprintf(stderr,"Cannot reassign stdout\n");      exit(1);    }  maketable();/*  printf("begin part %c\n",part); */  fprintf(outfp, "begin part %c\n",part);  lines = 6;}      if (n <= 0)break;    }}/* * output one group of 3 bytes, pointed at by p, on file f. */outdec(p)register char *p;{  register int c1, c2, c3, c4;  c1 = *p >> 2;  c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;  c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;  c4 = p[2] & 077;/*  putchar(ENC(c1)); */  fputc(ENC(c1), outfp);/*  putchar(ENC(c2)); */  fputc(ENC(c2), outfp);/*  putchar(ENC(c3)); */  fputc(ENC(c3), outfp);/*  putchar(ENC(c4)); */  fputc(ENC(c4), outfp);}/* fr: like read but stdio */int fr(buf, cnt)register char * buf;register int cnt;{  register int c, i;  for (i = 0; i < cnt; i++)     {      c = fgetc(fp);      if /* (feof(fp)) */ (c == EOF)return(i);      buf[i] = c;    }  return (cnt);}#ifdef M6502#undef exitcareful_exit(x)int x;{  if (outfp)    fclose(outfp);  if (fp)    fclose(fp);  exit(x);}#endif