home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / uuencode-1.0-src.lha / src / amiga / uuencode-1.0 / uuencode.c < prev    next >
C/C++ Source or Header  |  1993-08-23  |  5KB  |  233 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.  
  96. #if __STDC__
  97. static void encode (void);
  98. static void usage (FILE *, int);
  99. #else
  100. static void encode ();
  101. static void usage ();
  102. #endif
  103.  
  104. extern char version[];
  105. static char *program_name;
  106.  
  107. int
  108. main (argc, argv)
  109.      int argc;
  110.      char **argv;
  111. {
  112.   int opt;
  113.   struct stat sb;
  114.   int mode;
  115.  
  116.   program_name = argv[0];
  117.  
  118.   while ((opt = getopt_long (argc, argv, "hv", longopts, (int *) NULL))
  119.      != EOF)
  120.     {
  121.       switch (opt)
  122.     {
  123.     case 'h':
  124.       printf ("Encode a file in a seven bit format\n");
  125.       usage (stdout, 0);
  126.  
  127.     case 'v':
  128.       printf ("%s\n", version);
  129.       exit (0);
  130.  
  131.     case 0:
  132.       break;
  133.  
  134.     default:
  135.       usage (stderr, 1);
  136.     }
  137.     }
  138.  
  139.   switch (argc - optind)
  140.     {
  141.     case 2:            /* optional first argument is input file */
  142.       if (! freopen (argv[optind], "r", stdin)
  143.       || fstat (fileno (stdin), &sb))
  144.     {
  145.       fprintf (stderr, "%s:", program_name);
  146.       perror (argv[optind]);
  147.       exit(1);
  148.     }
  149.       mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
  150.       ++optind;
  151.       break;
  152.     case 1:
  153.       mode = RW & ~umask (RW);
  154.       break;
  155.     case 0:
  156.     default:
  157.       usage (stderr, 1);
  158.     }
  159.  
  160. #if S_IRWXU != 0700
  161.  #error Must translate mode argument
  162. #endif
  163.  
  164.   printf ("begin %o %s\n", mode, argv[optind]);
  165.   encode ();
  166.   printf ("end\n");
  167.   if (ferror (stdout))
  168.     {
  169.       fprintf (stderr, "uuencode: write error\n");
  170.       exit (1);
  171.     }
  172.   exit (0);
  173. }
  174.  
  175. /* ENC is the basic 1 character encoding function to make a char printing */
  176. #define    ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  177.  
  178. /*
  179.  * copy from in to out, encoding as you go along.
  180.  */
  181. static void
  182. encode ()
  183. {
  184.   register int ch, n;
  185.   register char *p;
  186.   char buf[80];
  187.  
  188.   while ((n = fread (buf, 1, 45, stdin)) != 0)
  189.     {
  190.       ch = ENC (n);
  191.       if (putchar (ch) == EOF)
  192.     break;
  193.       for (p = buf; n > 0; n -= 3, p += 3)
  194.     {
  195.       ch = *p >> 2;
  196.       ch = ENC (ch);
  197.       if (putchar (ch) == EOF)
  198.         break;
  199.       ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
  200.       ch = ENC (ch);
  201.       if (putchar (ch) == EOF)
  202.         break;
  203.       ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
  204.       ch = ENC (ch);
  205.       if (putchar (ch) == EOF)
  206.         break;
  207.       ch = p[2] & 077;
  208.       ch = ENC (ch);
  209.       if (putchar (ch) == EOF)
  210.         break;
  211.     }
  212.       if (putchar ('\n') == EOF)
  213.     break;
  214.     }
  215.   if (ferror (stdin))
  216.     {
  217.       fprintf (stderr, "uuencode: read error\n");
  218.       exit(1);
  219.     }
  220.   ch = ENC ('\0');
  221.   putchar (ch);
  222.   putchar ('\n');
  223. }
  224.  
  225. static void
  226. usage (f, status)
  227.      FILE *f;
  228.      int status;
  229. {
  230.   fprintf (f, "Usage: %s [infile] remotefile\n", program_name);
  231.   exit (status);
  232. }
  233.