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

  1. /* uudecode 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. | uudecode [FILE ...]                               |
  55. |                                        |
  56. | Create the specified FILE, decoding as you go.  Used with uuencode.  |
  57. \=====================================================================*/
  58.  
  59. #include <pwd.h>
  60. #include "getopt.h"
  61.  
  62. struct passwd *getpwnam ();
  63.  
  64. static struct option longopts[] =
  65. {
  66.   { "version", 0, 0, 'v' },
  67.   { "help", 0, 0, 'h' },
  68.   { NULL, 0, 0, 0 }
  69. };
  70.  
  71. static int decode _((const char *));
  72. static void usage _((int));
  73.  
  74. /* The name this program was run with. */
  75. const char *program_name;
  76.  
  77. /* Single character decode.  */
  78. #define    DEC(c)    (((c) - ' ') & 077)
  79.  
  80. static int
  81. decode (const char *filename)
  82. {
  83.   struct passwd *pw;
  84.   register int n;
  85.   register char ch, *p;
  86.   int mode, n1;
  87.   char buf[2 * BUFSIZ];
  88.   char *outname;
  89.  
  90.   /* Search for header line.  */
  91.  
  92.   do
  93.     {
  94.       if (fgets (buf, sizeof (buf), stdin) == NULL)
  95.     {
  96.       error (0, 0, "%s: no `begin' line", filename);
  97.       return 1;
  98.     }
  99.     }
  100.   while (strncmp (buf, "begin ", 6) != 0)
  101.     ;
  102.  
  103.   sscanf (buf, "begin %o %s", &mode, buf);
  104.  
  105.   /* Handle ~user/file format.  */
  106.  
  107.   if (buf[0] != '~')
  108.     outname = buf;
  109.   else
  110.     {
  111.       p = buf + 1;
  112.       while (*p != '/')
  113.     ++p;
  114.       if (*p == '\0')
  115.     {
  116.       error (0, 0, "%s: illegal ~user", filename);
  117.       return 1;
  118.     }
  119.       *p++ = '\0';
  120.       pw = getpwnam (buf + 1);
  121.       if (pw == NULL)
  122.     {
  123.       error (0, 0, "%s: no user `%s'", filename, buf + 1);
  124.       return 1;
  125.     }
  126.       n = strlen (pw->pw_dir);
  127.       n1 = strlen (p);
  128.       outname = (char *) alloca ((size_t) (n + n1 + 2));
  129.       memcpy (outname + n + 1, p, (size_t) (n1 + 1));
  130.       memcpy (outname, pw->pw_dir, (size_t) n);
  131.       outname[n] = '/';
  132.     }
  133.  
  134.   /* Create output file and set mode.  */
  135.  
  136.   if (freopen (outname, "w", stdout) == NULL
  137. #if HAVE_FCHMOD      
  138.       || fchmod (fileno (stdout), mode & (S_IRWXU | S_IRWXG | S_IRWXO))
  139. #else
  140.       || chmod (outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO))
  141. #endif
  142.       )
  143.     {
  144.       error (0, errno, "%s: %s", outname, filename);
  145.       return 1;
  146.     }
  147.  
  148.   /* For each input line:  */
  149.  
  150.   while (1)
  151.     {
  152.       if (fgets (buf, sizeof(buf), stdin) == NULL)
  153.     {
  154.       error (0, 0, "%s: short file", filename);
  155.       return 1;
  156.     }
  157.       p = buf;
  158.  
  159.       /* N is used to avoid writing out all the characters at the end of
  160.      the file.  */
  161.  
  162.       n = DEC (*p);
  163.       if (n <= 0)
  164.     break;
  165.       for (++p; n > 0; p += 4, n -= 3)
  166.     {
  167.       if (n >= 3)
  168.         {
  169.           ch = DEC (p[0]) << 2 | DEC (p[1]) >> 4;
  170.           putchar (ch);
  171.           ch = DEC (p[1]) << 4 | DEC (p[2]) >> 2;
  172.           putchar (ch);
  173.           ch = DEC (p[2]) << 6 | DEC (p[3]);
  174.           putchar (ch);
  175.         }
  176.       else
  177.         {
  178.           if (n >= 1)
  179.         {
  180.           ch = DEC (p[0]) << 2 | DEC (p[1]) >> 4;
  181.           putchar (ch);
  182.         }
  183.           if (n >= 2)
  184.         {
  185.           ch = DEC (p[1]) << 4 | DEC (p[2]) >> 2;
  186.           putchar (ch);
  187.         }
  188.         }
  189.     }
  190.     }
  191.  
  192.   if (fgets (buf, sizeof(buf), stdin) == NULL
  193.       || strcmp (buf, "end\n"))
  194.     {
  195.       error (0, 0, "%s: no `end' line", filename);
  196.       return 1;
  197.     }
  198.  
  199.   return 0;
  200. }
  201.  
  202. static void
  203. usage (int status)
  204. {
  205.   if (status != 0)
  206.     fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
  207.   else
  208.     {
  209.       printf ("\
  210. Usage: %s [FILE]...\n", program_name);
  211.       printf ("\n\
  212.   -h, --help      display this help and exit\n\
  213.   -v, --version   output version information and exit\n");
  214.     }
  215.   exit (status);
  216. }
  217.  
  218. int
  219. main (int argc, char *const *argv)
  220. {
  221.   int opt;
  222.   int exit_status;
  223.  
  224.   program_name = argv[0];
  225.  
  226.   while (opt = getopt_long (argc, argv, "hv", longopts, (int *) NULL),
  227.      opt != EOF)
  228.     {
  229.       switch (opt)
  230.     {
  231.     case 'h':
  232.       usage (EXIT_SUCCESS);
  233.  
  234.     case 'v':
  235.       printf ("GNU %s %s\n", PRODUCT, VERSION);
  236.       exit (EXIT_SUCCESS);
  237.  
  238.     case 0:
  239.       break;
  240.  
  241.     default:
  242.       usage (EXIT_FAILURE);
  243.     }
  244.     }
  245.  
  246.   if (optind == argc)
  247.     exit_status = decode ("stdin") == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  248.   else
  249.     {
  250.       exit_status = EXIT_SUCCESS;
  251.       do
  252.     {
  253.       if (freopen (argv[optind], "r", stdin) != NULL)
  254.         {
  255.           if (decode (argv[optind]) != 0)
  256.         exit_status = EXIT_FAILURE;
  257.         }
  258.       else
  259.         {
  260.           error (0, errno, "%s", argv[optind]);
  261.           exit_status = EXIT_FAILURE;
  262.         }
  263.       optind++;
  264.     }
  265.       while (optind < argc);
  266.     }
  267.  
  268.   exit (exit_status);
  269. }
  270.