home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / sharutils-4.1-src.tgz / tar.out / fsf / sharutils / uuencode.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  201 lines

  1. /* uuencode utility.
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.  
  4.    This product is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This product is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this product; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Copyright (c) 1983 Regents of the University of California.
  19.    All rights reserved.
  20.    
  21.    Redistribution and use in source and binary forms, with or without
  22.    modification, are permitted provided that the following conditions
  23.    are met:
  24.    1. Redistributions of source code must retain the above copyright
  25.       notice, this list of conditions and the following disclaimer.
  26.    2. Redistributions in binary form must reproduce the above copyright
  27.       notice, this list of conditions and the following disclaimer in the
  28.       documentation and/or other materials provided with the distribution.
  29.    3. All advertising materials mentioning features or use of this software
  30.       must display the following acknowledgement:
  31.      This product includes software developed by the University of
  32.      California, Berkeley and its contributors.
  33.    4. Neither the name of the University nor the names of its contributors
  34.       may be used to endorse or promote products derived from this software
  35.       without specific prior written permission.
  36.    
  37.    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  38.    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39.    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40.    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  41.    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42.    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  43.    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45.    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46.    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47.    SUCH DAMAGE.  */
  48.  
  49. /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93.  */
  50.  
  51. #include "system.h"
  52.  
  53. /*=======================================================\
  54. | uuencode [INPUT] OUTPUT                 |
  55. |                              |
  56. | Encode a file so it can be mailed to a remote system.     |
  57. \=======================================================*/
  58.  
  59. #include "getopt.h"
  60.  
  61. #define    RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
  62.  
  63. static struct option longopts[] =
  64. {
  65.   { "version", 0, 0, 'v' },
  66.   { "help", 0, 0, 'h' },
  67.   { NULL, 0, 0, 0 }
  68. };
  69.  
  70. static void encode _((void));
  71. static void usage _((int));
  72.  
  73. /* The name this program was run with. */
  74. const char *program_name;
  75.  
  76. /* ENC is the basic 1 character encoding function to make a char printing.  */
  77. #define    ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  78.  
  79. /*------------------------------------------------.
  80. | Copy from IN to OUT, encoding as you go along.  |
  81. `------------------------------------------------*/
  82.  
  83. static void
  84. encode (void)
  85. {
  86.   register int ch, n;
  87.   register char *p;
  88.   char buf[80];
  89.  
  90.   while ((n = fread (buf, 1, 45, stdin)) != 0)
  91.     {
  92.       ch = ENC (n);
  93.       if (putchar (ch) == EOF)
  94.     break;
  95.       for (p = buf; n > 0; n -= 3, p += 3)
  96.     {
  97.       ch = *p >> 2;
  98.       ch = ENC (ch);
  99.       if (putchar (ch) == EOF)
  100.         break;
  101.       ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
  102.       ch = ENC (ch);
  103.       if (putchar (ch) == EOF)
  104.         break;
  105.       ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
  106.       ch = ENC (ch);
  107.       if (putchar (ch) == EOF)
  108.         break;
  109.       ch = p[2] & 077;
  110.       ch = ENC (ch);
  111.       if (putchar (ch) == EOF)
  112.         break;
  113.     }
  114.       if (putchar ('\n') == EOF)
  115.     break;
  116.     }
  117.   if (ferror (stdin))
  118.     error (EXIT_FAILURE, 0, "read error");
  119.   ch = ENC ('\0');
  120.   putchar (ch);
  121.   putchar ('\n');
  122. }
  123.  
  124. static void
  125. usage (int status)
  126. {
  127.   if (status != 0)
  128.     fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
  129.   else
  130.     {
  131.       printf ("\
  132. Usage: %s [INFILE] REMOTEFILE\n", program_name);
  133.       printf ("\n\
  134.   -h, --help      display this help and exit\n\
  135.   -v, --version   output version information and exit\n");
  136.     }
  137.   exit (status);
  138. }
  139.  
  140. int
  141. main (int argc, char *const *argv)
  142. {
  143.   int opt;
  144.   struct stat sb;
  145.   int mode;
  146.  
  147.   program_name = argv[0];
  148.  
  149.   while (opt = getopt_long (argc, argv, "hv", longopts, (int *) NULL),
  150.      opt != EOF)
  151.     {
  152.       switch (opt)
  153.     {
  154.     case 'h':
  155.       usage (EXIT_SUCCESS);
  156.  
  157.     case 'v':
  158.       printf ("GNU %s %s\n", PRODUCT, VERSION);
  159.       exit (EXIT_SUCCESS);
  160.  
  161.     case 0:
  162.       break;
  163.  
  164.     default:
  165.       usage (EXIT_FAILURE);
  166.     }
  167.     }
  168.  
  169.   switch (argc - optind)
  170.     {
  171.     case 2:
  172.  
  173.       /* Optional first argument is input file.  */
  174.  
  175.       if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb))
  176.     error (EXIT_FAILURE, errno, "%s", argv[optind]);
  177.       mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
  178.       optind++;
  179.       break;
  180.  
  181.     case 1:
  182.       mode = RW & ~umask (RW);
  183.       break;
  184.  
  185.     case 0:
  186.     default:
  187.       usage (EXIT_FAILURE);
  188.     }
  189.  
  190. #if S_IRWXU != 0700
  191. choke me - Must translate mode argument
  192. #endif
  193.  
  194.   printf ("begin %o %s\n", mode, argv[optind]);
  195.   encode ();
  196.   printf ("end\n");
  197.   if (ferror (stdout))
  198.     error (EXIT_FAILURE, 0, "write error");
  199.   exit (EXIT_SUCCESS);
  200. }
  201.