home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / HTTPD / HTTPD_SO.TAR / httpd_1.3 / support / auth / uudecode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-12  |  4.8 KB  |  185 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[] = "@(#)uudecode.c    5.10 (Berkeley) 6/1/90";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * uudecode [file ...]
  40.  *
  41.  * create the specified file, decoding as you go.
  42.  * used with uuencode.
  43.  */
  44. #include <sys/param.h>
  45. #include <sys/stat.h>
  46. #include <pwd.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. char *filename;
  51.  
  52. /* ARGSUSED */
  53. main(argc, argv)
  54.     int argc;
  55.     char **argv;
  56. {
  57.     extern int errno;
  58.     int rval;
  59. #if 0
  60.     if (*++argv) {
  61.         rval = 0;
  62.         do {
  63.             if (!freopen(filename = *argv, "r", stdin)) {
  64.                 (void)fprintf(stderr, "uudecode: %s: %s\n",
  65.                     *argv, strerror(errno));
  66.                 rval = 1;
  67.                 continue;
  68.             }
  69.             rval |= decode();
  70.         } while (*++argv);
  71.     } else {
  72.         filename = "stdin";
  73.         rval = decode();
  74.     }
  75. #else
  76.          filename = "stdin";
  77.          rval = decode();
  78. #endif
  79.     exit(rval);
  80. }
  81.  
  82. decode()
  83. {
  84.     extern int errno;
  85.     struct passwd *pw;
  86.     register int n;
  87.     register char ch, *p;
  88.     int mode, n1;
  89.     char buf[MAXPATHLEN];
  90.  
  91.     /* search for header line */
  92.     do {
  93.         if (!fgets(buf, sizeof(buf), stdin)) {
  94.             (void)fprintf(stderr,
  95.                 "uudecode: %s: no \"begin\" line\n", filename);
  96.             return(1);
  97.         }
  98.     } while (strncmp(buf, "begin ", 6));
  99.     (void)sscanf(buf, "begin %o %s", &mode, buf);
  100.  
  101.     /* handle ~user/file format */
  102.     if (buf[0] == '~') {
  103.         if (!(p = index(buf, '/'))) {
  104.             (void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
  105.                 filename);
  106.             return(1);
  107.         }
  108.         *p++ = NULL;
  109.         if (!(pw = getpwnam(buf + 1))) {
  110.             (void)fprintf(stderr, "uudecode: %s: no user %s.\n",
  111.                 filename, buf);
  112.             return(1);
  113.         }
  114.         n = strlen(pw->pw_dir);
  115.         n1 = strlen(p);
  116.         if (n + n1 + 2 > MAXPATHLEN) {
  117.             (void)fprintf(stderr, "uudecode: %s: path too long.\n",
  118.                 filename);
  119.             return(1);
  120.         }
  121.         bcopy(p, buf + n + 1, n1 + 1);
  122.         bcopy(pw->pw_dir, buf, n);
  123.         buf[n] = '/';
  124.     }
  125. #if 0
  126.     /* create output file, set mode */
  127.     if (!freopen(buf, "w", stdout) ||
  128.         fchmod(fileno(stdout), mode&0666)) {
  129.         (void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
  130.             filename, strerror(errno));
  131.         return(1);
  132.     }
  133. #endif
  134.     /* for each input line */
  135.     for (;;) {
  136.         if (!fgets(p = buf, sizeof(buf), stdin)) {
  137.             (void)fprintf(stderr, "uudecode: %s: short file.\n",
  138.                 filename);
  139.             return(1);
  140.         }
  141. #define    DEC(c)    (((c) - ' ') & 077)        /* single character decode */
  142.         /*
  143.          * `n' is used to avoid writing out all the characters
  144.          * at the end of the file.
  145.          */
  146.         if ((n = DEC(*p)) <= 0)
  147.             break;
  148.         for (++p; n > 0; p += 4, n -= 3)
  149.             if (n >= 3) {
  150.                 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
  151.                 putchar(ch);
  152.                 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  153.                 putchar(ch);
  154.                 ch = DEC(p[2]) << 6 | DEC(p[3]);
  155.                 putchar(ch);
  156.             }
  157.             else {
  158.                 if (n >= 1) {
  159.                     ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
  160.                     putchar(ch);
  161.                 }
  162.                 if (n >= 2) {
  163.                     ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  164.                     putchar(ch);
  165.                 }
  166.                 if (n >= 3) {
  167.                     ch = DEC(p[2]) << 6 | DEC(p[3]);
  168.                     putchar(ch);
  169.                 }
  170.             }
  171.     }
  172.     if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
  173.         (void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
  174.             filename);
  175.         return(1);
  176.     }
  177.     return(0);
  178. }
  179.  
  180. usage()
  181. {
  182.     (void)fprintf(stderr, "usage: uudecode [file ...]\n");
  183.     exit(1);
  184. }
  185.