Fix xargs option parsing
[oweals/busybox.git] / uuencode.c
1 /* uuencode.c -- uuencode utility.
2  * Copyright (C) 1994, 1995 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, 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
19
20 /* Copyright (c) 1983 Regents of the University of California.
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *       This product includes software developed by the University of
34  *       California, Berkeley and its contributors.
35  * 4. Neither the name of the University nor the names of its contributors
36  *    may be used to endorse or promote products derived from this software
37  *    without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  */
51
52 /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93.  */
53 /* Hacked to work with BusyBox by Alfred M. Szmidt */
54
55 #include "internal.h"
56
57 #include <stdio.h>
58 #include <errno.h>
59 #include <getopt.h>
60 #include <pwd.h>
61
62 #define RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
63
64 static void encode __P ((void));
65
66 /* Pointer to the translation table we currently use.  */
67 const char *trans_ptr;
68
69 /* The two currently defined translation tables.  The first is the
70    standard uuencoding, the second is base64 encoding.  */
71 const char uu_std[64] = {
72   '`', '!', '"', '#', '$', '%', '&', '\'',
73   '(', ')', '*', '+', ',', '-', '.', '/',
74   '0', '1', '2', '3', '4', '5', '6', '7',
75   '8', '9', ':', ';', '<', '=', '>', '?',
76   '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
77   'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
78   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
79   'X', 'Y', 'Z', '[', '\\', ']', '^', '_'
80 };
81
82 const char uu_base64[64] = {
83   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
84   'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
85   'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
86   'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
87   'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
88   'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
89   'w', 'x', 'y', 'z', '0', '1', '2', '3',
90   '4', '5', '6', '7', '8', '9', '+', '/'
91 };
92
93 /* ENC is the basic 1 character encoding function to make a char printing.  */
94 #define ENC(Char) (trans_ptr[(Char) & 077])
95
96 /* Copy from IN to OUT, encoding as you go along. */
97 static void encode()
98 {
99   register int ch, n;
100   char *p = NULL;
101   char buf[80];
102
103   while (1) {
104     n = 0;
105     do {
106       register int m = fread (buf, 1, 45 - n, stdin);
107       if (m == 0)
108         break;
109       n += m;
110     }
111     while (n < 45);
112
113     if (n == 0)
114       break;
115
116     if (trans_ptr == uu_std)
117       if (putchar (ENC (n)) == EOF)
118         break;
119     for (p = buf; n > 2; n -= 3, p += 3) {
120       ch = *p >> 2;
121       ch = ENC (ch);
122       if (putchar (ch) == EOF)
123         break;
124       ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
125       ch = ENC (ch);
126       if (putchar (ch) == EOF)
127         break;
128       ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
129       ch = ENC (ch);
130       if (putchar (ch) == EOF)
131         break;
132       ch = p[2] & 077;
133       ch = ENC (ch);
134       if (putchar (ch) == EOF)
135         break;
136     }
137
138     if (n != 0)
139       break;
140
141     if (putchar ('\n') == EOF)
142       break;
143   }
144
145   while (n != 0) {
146     char c1 = *p;
147     char c2 = n == 1 ? 0 : p[1];
148
149     ch = c1 >> 2;
150     ch = ENC (ch);
151     if (putchar (ch) == EOF)
152       break;
153
154     ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017);
155     ch = ENC (ch);
156     if (putchar (ch) == EOF)
157       break;
158
159     if (n == 1)
160       ch = trans_ptr == uu_std ? ENC ('\0') : '=';
161     else {
162       ch = (c2 << 2) & 074;
163       ch = ENC (ch);
164     }
165     if (putchar (ch) == EOF)
166       break;
167     ch = trans_ptr == uu_std ? ENC ('\0') : '=';
168     if (putchar (ch) == EOF)
169       break;
170     putchar ('\n');
171     break;
172   }
173
174   if (ferror (stdin))
175     errorMsg("Read error\n");
176
177   if (trans_ptr == uu_std) {
178     putchar (ENC ('\0'));
179     putchar ('\n');
180   }
181 }
182
183 int uuencode_main (int argc,
184                    char **argv)
185 {
186   int opt;
187   struct stat sb;
188   int mode;
189
190   trans_ptr = uu_std;      /* Standard encoding is old uu format */
191
192   /* Parse any options */
193   while ((opt = getopt (argc, argv, "m")) != EOF) {
194     switch (opt) {
195      case 'm':
196       trans_ptr = uu_base64;
197       break;
198
199      case 0:
200       break;
201
202      default:
203       usage(uuencode_usage);
204     }
205   }
206
207   switch (argc - optind) {
208    case 2:
209     /* Optional first argument is input file.  */
210     if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) {
211       errorMsg("%s: %s\n", argv[optind], strerror(errno));
212       exit FALSE;
213     }
214     mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
215     optind++;
216     break;
217
218    case 1:
219     mode = RW & ~umask (RW);
220     break;
221
222    case 0:
223    default:
224     usage(uuencode_usage);
225   }
226
227   printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64",
228             mode, argv[optind]);
229   encode();
230   printf(trans_ptr == uu_std ? "end\n" : "====\n");
231   if (ferror (stdout)) {
232     errorMsg("Write error\n");
233     exit FALSE;
234   }
235   return( TRUE);
236 }