home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / HTTPD / HTTPD_SO.TAR / httpd_1.3 / support / auth / uuencode.c < prev   
Encoding:
C/C++ Source or Header  |  1994-01-12  |  3.9 KB  |  147 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. #ifndef lint
  35. static char sccsid[] = "@(#)uuencode.c    5.9 (Berkeley) 6/1/90";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * uuencode [input] output
  40.  *
  41.  * Encode a file so it can be mailed to a remote system.
  42.  */
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <stdio.h>
  46.  
  47. main(argc, argv)
  48.     int argc;
  49.     char **argv;
  50. {
  51.     extern int optind;
  52.     extern int errno;
  53.     struct stat sb;
  54.     int mode;
  55. #if 0
  56.     char *strerror();
  57. #else
  58.           #define strerror(s) "strerror disabled"
  59. #endif
  60.  
  61.     while (getopt(argc, argv, "") != EOF)
  62.         usage();
  63.     argv += optind;
  64.     argc -= optind;
  65.  
  66.     switch(argc) {
  67.     case 2:            /* optional first argument is input file */
  68.         if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) {
  69.             (void)fprintf(stderr, "uuencode: %s: %s.\n",
  70.                 *argv, strerror(errno));
  71.             exit(1);
  72.         }
  73. #define    RWX    (S_IRWXU|S_IRWXG|S_IRWXO)
  74.         mode = sb.st_mode & RWX;
  75.         ++argv;
  76.         break;
  77.     case 1:
  78. #define    RW    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
  79.         mode = RW & ~umask(RW);
  80.         break;
  81.     case 0:
  82.     default:
  83.         usage();
  84.     }
  85.  
  86.     (void)printf("begin %o %s\n", mode, *argv);
  87.     encode();
  88.     (void)printf("end\n");
  89.     if (ferror(stdout)) {
  90.         (void)fprintf(stderr, "uuencode: write error.\n");
  91.         exit(1);
  92.     }
  93.     exit(0);
  94. }
  95.  
  96. /* ENC is the basic 1 character encoding function to make a char printing */
  97. #define    ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  98.  
  99. /*
  100.  * copy from in to out, encoding as you go along.
  101.  */
  102. encode()
  103. {
  104.     register int ch, n;
  105.     register char *p;
  106.     char buf[80];
  107.  
  108.     while (n = fread(buf, 1, 45, stdin)) {
  109.         ch = ENC(n);
  110.         if (putchar(ch) == EOF)
  111.             break;
  112.         for (p = buf; n > 0; n -= 3, p += 3) {
  113.             ch = *p >> 2;
  114.             ch = ENC(ch);
  115.             if (putchar(ch) == EOF)
  116.                 break;
  117.             ch = (*p << 4) & 060 | (p[1] >> 4) & 017;
  118.             ch = ENC(ch);
  119.             if (putchar(ch) == EOF)
  120.                 break;
  121.             ch = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  122.             ch = ENC(ch);
  123.             if (putchar(ch) == EOF)
  124.                 break;
  125.             ch = p[2] & 077;
  126.             ch = ENC(ch);
  127.             if (putchar(ch) == EOF)
  128.                 break;
  129.         }
  130.         if (putchar('\n') == EOF)
  131.             break;
  132.     }
  133.     if (ferror(stdin)) {
  134.         (void)fprintf(stderr, "uuencode: read error.\n");
  135.         exit(1);
  136.     }
  137.     ch = ENC('\0');
  138.     (void)putchar(ch);
  139.     (void)putchar('\n');
  140. }
  141.  
  142. usage()
  143. {
  144.     (void)fprintf(stderr,"usage: uuencode [infile] remotefile\n");
  145.     exit(1);
  146. }
  147.