home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / comms / encoders / uuenc / c / uuencode < prev    next >
Encoding:
Text File  |  1995-03-11  |  5.7 KB  |  230 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93.  */
  35.  
  36. /*
  37.  * uuencode [input] output
  38.  *
  39.  * Encode a file so it can be mailed to a remote system.
  40.  */
  41. /*#include <sys/types.h>
  42. #include <sys/stat.h>*/
  43. #include <stdio.h>
  44. #include "getopt.h"
  45.  
  46. /* Get definitions for the file permission bits.  */
  47.  
  48. #ifndef S_IRWXU
  49. #define S_IRWXU 0700
  50. #endif
  51. #ifndef S_IRUSR
  52. #define S_IRUSR 0400
  53. #endif
  54. #ifndef S_IWUSR
  55. #define S_IWUSR 0200
  56. #endif
  57. #ifndef S_IXUSR
  58. #define S_IXUSR 0100
  59. #endif
  60.  
  61. #ifndef S_IRWXG
  62. #define S_IRWXG 0070
  63. #endif
  64. #ifndef S_IRGRP
  65. #define S_IRGRP 0040
  66. #endif
  67. #ifndef S_IWGRP
  68. #define S_IWGRP 0020
  69. #endif
  70. #ifndef S_IXGRP
  71. #define S_IXGRP 0010
  72. #endif
  73.  
  74. #ifndef S_IRWXO
  75. #define S_IRWXO 0007
  76. #endif
  77. #ifndef S_IROTH
  78. #define S_IROTH 0004
  79. #endif
  80. #ifndef S_IWOTH
  81. #define S_IWOTH 0002
  82. #endif
  83. #ifndef S_IXOTH
  84. #define S_IXOTH 0001
  85. #endif
  86.  
  87. #define    RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
  88.  
  89. static struct option longopts[] =
  90. {
  91.   { "version", 0, 0, 'v' },
  92.   { "help", 0, 0, 'h' },
  93.   { NULL, 0, 0, 0 }
  94. };
  95. #if __STDC__
  96. static void encode (void);
  97. static void usage (FILE *, int);
  98. #else
  99. static void encode ();
  100. static void usage ();
  101. #endif
  102.  
  103. extern char version[];
  104. static char *program_name;
  105. FILE *inputfile;
  106. FILE *outputfile; /* TOW added this */
  107.  
  108. int
  109. main (argc, argv)
  110.      int argc;
  111.      char **argv;
  112. {
  113.   int opt;
  114.   /*struct stat sb;*/
  115.   int mode;     
  116.  
  117.   program_name = argv[0];
  118.  
  119.   while ((opt = getopt_long (argc, argv, "hv", longopts, (int *) NULL))
  120.      != EOF)
  121.     {
  122.       switch (opt)
  123.     {
  124.     case 'h':
  125.       printf ("Encode a file in a seven bit format\n");
  126.       usage (stdout, 0);
  127.  
  128.     case 'v':
  129.       printf ("%s\n", version);
  130.       exit (0);
  131.  
  132.     case 0:
  133.       break;
  134.  
  135.     default:
  136.       usage (stderr, 1);
  137.     }
  138.     }                                          
  139.  
  140.     /* Alterations by TOW for RISCOS */
  141.  
  142.     if (! (inputfile=fopen (argv[optind], "rb")))
  143.     { /* Error if can't open input file */
  144.       fprintf (stderr, "%s:", program_name);
  145.       perror (argv[optind]);
  146.       exit(1);
  147.     }
  148.     mode=0x1b4;
  149.     ++optind;                         
  150.     if (! (outputfile=fopen (argv[optind],"w")))
  151.     { /* Error if can't open output file */
  152.       fprintf (stderr, "%s:", argv[optind]);
  153.       perror (argv[optind]);
  154.       exit(1);
  155.     }
  156.     if ((argc - optind) == 2) /* Are there two more parameters */
  157.     fprintf (outputfile,"begin %o %s\n", mode, argv[optind+1]); /* The duplicate input filename */
  158.     else
  159.     fprintf (outputfile,"begin %o %s\n", mode, argv[optind-1]); /* The input filename */
  160.  
  161.  
  162.   encode ();
  163.   fprintf (outputfile,"end\n");
  164.   if (ferror (outputfile))
  165.     {
  166.       fprintf (stderr, "uuencode: write error\n");
  167.       exit (1);
  168.     }
  169.   exit (0);
  170. }
  171.  
  172. /* ENC is the basic 1 character encoding function to make a char printing */
  173. #define    ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  174.  
  175. /*
  176.  * copy from in to out, encoding as you go along.
  177.  */
  178. static void
  179. encode ()
  180. {
  181.   register int ch, n;
  182.   register char *p;
  183.   char buf[80];
  184.  
  185.   while ((n = fread (buf, 1, 45, inputfile)) != 0)
  186.     {
  187.       ch = ENC (n);
  188.       if (putc (ch,outputfile) == EOF)
  189.     break;
  190.       for (p = buf; n > 0; n -= 3, p += 3)
  191.     {
  192.       ch = *p >> 2;
  193.       ch = ENC (ch);
  194.       if (putc (ch,outputfile) == EOF)
  195.         break;
  196.       ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
  197.       ch = ENC (ch);
  198.       if (putc (ch,outputfile) == EOF)
  199.         break;
  200.       ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
  201.       ch = ENC (ch);
  202.       if (putc (ch,outputfile) == EOF)
  203.         break;
  204.       ch = p[2] & 077;
  205.       ch = ENC (ch);
  206.       if (putc (ch,outputfile) == EOF)
  207.         break;
  208.     }
  209.       if (putc ('\n',outputfile) == EOF)
  210.     break;
  211.     }
  212.   if (ferror (inputfile))
  213.     {
  214.       fprintf (stderr, "uuencode: read error\n");
  215.       exit(1);
  216.     }
  217.   ch = ENC ('\0');
  218.   putc (ch,outputfile);
  219.   putc ('\n',outputfile);
  220. }
  221.  
  222. static void
  223. usage (f, status)
  224.      FILE *f;
  225.      int status;
  226. {
  227.   fprintf (f, "Usage: %s infile outfile [new name]\n", program_name);
  228.   exit (status);
  229. }
  230.