|
|
1.1 root 1: /* uudecode - decode a uuencoded file */
2:
3: /* call: uudecode [input_file] */
4:
5: /* hgm - June 9, 1987 - added code to handle short lines and tabs */
6:
7: #include <stdio.h>
8: #include <pwd.h>
9:
10: extern FILE *fopen();
11: char *fgets();
12: #define NULLF (FILE *) 0
13: #define NULLP (char *) 0
14:
15: /* single character decode */
16: #define DEC(c) (((c) - ' ') & 077)
17:
18: main(argc, argv)
19: char **argv;
20: {
21: FILE *in, *out;
22: int mode;
23: char dest[128];
24: char buf[80];
25:
26: /* optional input arg */
27: if (argc > 1) {
28: if ((in = fopen(argv[1], "r")) == NULLF) {
29: perror(argv[1]);
30: exit(1);
31: }
32: argv++; argc--;
33: } else
34: in = stdin;
35:
36: if (argc != 1) {
37: printf("Usage: uudecode [infile]\n");
38: exit(2);
39: }
40:
41: /* search for header line */
42: for (;;) {
43: if (fgets(buf, sizeof buf, in) == NULLP) {
44: fprintf(stderr, "No begin line\n");
45: exit(3);
46: }
47: if (strncmp(buf, "begin ", 6) == 0)
48: break;
49: }
50: sscanf(buf, "begin %o %s", &mode, dest);
51:
52: /* handle ~user/file format */
53: if (dest[0] == '~') {
54: char *sl;
55: struct passwd *getpwnam();
56: char *index();
57: struct passwd *user;
58: char dnbuf[100];
59:
60: sl = index(dest, '/');
61: if (sl == NULLP) {
62: fprintf(stderr, "Illegal ~user\n");
63: exit(3);
64: }
65: *sl++ = 0;
66: user = getpwnam(dest+1);
67: if (user == NULL) {
68: fprintf(stderr, "No such user as %s\n", dest);
69: exit(4);
70: }
71: strcpy(dnbuf, user->pw_dir);
72: strcat(dnbuf, "/");
73: strcat(dnbuf, sl);
74: strcpy(dest, dnbuf);
75: }
76:
77: /* create output file */
78: out = fopen(dest, "w");
79: if (out == NULLF) {
80: perror(dest);
81: exit(4);
82: }
83: chmod(dest, mode);
84:
85: decode(in, out);
86:
87: if (fgets(buf, sizeof buf, in) == NULLP || strcmp(buf, "end\n")) {
88: fprintf(stderr, "No end line\n");
89: exit(5);
90: }
91: exit(0);
92: }
93:
94: /*
95: * copy from in to out, decoding as you go along.
96: */
97: decode(in, out)
98: FILE *in;
99: FILE *out;
100: {
101: char tbuf[82];
102: char buf[82];
103: char *bp;
104: char *p;
105: int n;
106: int col;
107:
108: for (;;) {
109: /* for each input line */
110: if (fgets(tbuf, sizeof tbuf, in) == NULLP) {
111: printf("Short file\n");
112: exit(10);
113: }
114: n = tbuf[0] - ' ';
115: if (n <= 0)
116: break;
117:
118: /* expand tabs */
119: for (col = 0, p = tbuf; ((*p) && (*p != '\n') && (col < 78)); p++) {
120: if (*p == '\t') {
121: do {
122: buf[col++] = ' ';
123: } while (col % 8);
124: } else {
125: buf[col++] = *p;
126: }
127: }
128:
129: /* fill with trailing blanks */
130: for (; ((col < (4*n + 1)) && (col < 78)); col++)
131: buf[col] = ' ';
132: buf[col++] = '\n';
133: buf[col] = '\0';
134:
135: bp = &buf[1];
136: while (n > 0) {
137: outdec(bp, out, n);
138: bp += 4;
139: n -= 3;
140: }
141: }
142: }
143:
144: /*
145: * output a group of 3 bytes (4 input characters).
146: * the input chars are pointed to by p, they are to
147: * be output to file f. n is used to tell us not to
148: * output all of them at the end of the file.
149: */
150: outdec(p, f, n)
151: char *p;
152: FILE *f;
153: {
154: int c1, c2, c3;
155:
156: c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
157: c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
158: c3 = DEC(p[2]) << 6 | DEC(p[3]);
159: if (n >= 1)
160: putc(c1, f);
161: if (n >= 2)
162: putc(c2, f);
163: if (n >= 3)
164: putc(c3, f);
165: }
166:
167:
168: /* fr: like read but stdio */
169: int
170: fr(fd, buf, cnt)
171: FILE *fd;
172: char *buf;
173: int cnt;
174: {
175: int c, i;
176:
177: for (i=0; i<cnt; i++) {
178: c = getc(fd);
179: if (c == EOF)
180: return(i);
181: buf[i] = c;
182: }
183: return (cnt);
184: }
185:
186: /*
187: * Return the ptr in sp at which the character c appears;
188: * NULL if not found
189: */
190:
191:
192: char *
193: index(sp, c)
194: register char *sp, c;
195: {
196: do {
197: if (*sp == c)
198: return(sp);
199: } while (*sp++);
200: return(NULL);
201: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.