uudecode.c (103195) | uudecode.c (103197) |
---|---|
1/*- 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. 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 --- 30 unchanged lines hidden (view full) --- 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94"; 43#endif /* not lint */ 44#endif 45 46#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. 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 --- 30 unchanged lines hidden (view full) --- 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94"; 43#endif /* not lint */ 44#endif 45 46#include <sys/cdefs.h> |
47__FBSDID("$FreeBSD: head/usr.bin/uudecode/uudecode.c 103195 2002-09-10 18:34:07Z fanf $"); | 47__FBSDID("$FreeBSD: head/usr.bin/uudecode/uudecode.c 103197 2002-09-10 18:52:03Z fanf $"); |
48 49/* 50 * uudecode [file ...] 51 * 52 * create the specified file, decoding as you go. 53 * used with uuencode. 54 */ 55#include <sys/param.h> --- 77 unchanged lines hidden (view full) --- 133 134int 135decode(void) 136{ 137 int flag; 138 139 /* decode only one file per input stream */ 140 if (!cflag) | 48 49/* 50 * uudecode [file ...] 51 * 52 * create the specified file, decoding as you go. 53 * used with uuencode. 54 */ 55#include <sys/param.h> --- 77 unchanged lines hidden (view full) --- 133 134int 135decode(void) 136{ 137 int flag; 138 139 /* decode only one file per input stream */ 140 if (!cflag) |
141 return(decode2(0)); | 141 return (decode2(0)); |
142 143 /* multiple uudecode'd files */ 144 for (flag = 0; ; flag++) 145 if (decode2(flag)) | 142 143 /* multiple uudecode'd files */ 144 for (flag = 0; ; flag++) 145 if (decode2(flag)) |
146 return(1); | 146 return (1); |
147 else if (feof(stdin)) 148 break; 149 | 147 else if (feof(stdin)) 148 break; 149 |
150 return(0); | 150 return (0); |
151} 152 153int 154decode2(int flag) 155{ 156 struct passwd *pw; 157 register int n; 158 register char ch, *p; --- 4 unchanged lines hidden (view full) --- 163 void *mode_handle; 164 struct stat st; 165 166 base64 = 0; 167 /* search for header line */ 168 do { 169 if (!fgets(buf, sizeof(buf), stdin)) { 170 if (flag) /* no error */ | 151} 152 153int 154decode2(int flag) 155{ 156 struct passwd *pw; 157 register int n; 158 register char ch, *p; --- 4 unchanged lines hidden (view full) --- 163 void *mode_handle; 164 struct stat st; 165 166 base64 = 0; 167 /* search for header line */ 168 do { 169 if (!fgets(buf, sizeof(buf), stdin)) { 170 if (flag) /* no error */ |
171 return(0); | 171 return (0); |
172 173 warnx("%s: no \"begin\" line", filename); | 172 173 warnx("%s: no \"begin\" line", filename); |
174 return(1); | 174 return (1); |
175 } 176 } while (strncmp(buf, "begin", 5) != 0); 177 178 if (strncmp(buf, "begin-base64", 12) == 0) 179 base64 = 1; 180 181 /* Parse the header: begin{,-base64} mode outfile. */ 182 s = strtok(buf, " "); --- 16 unchanged lines hidden (view full) --- 199 if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf)) 200 errx(1, "%s: filename too long", outfile); 201 if (!sflag && !pflag) { 202 strlcpy(buffn, buf, sizeof(buffn)); 203 if (strrchr(buffn, '/') != NULL) 204 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf)); 205 if (buf[0] == '\0') { 206 warnx("%s: illegal filename", buffn); | 175 } 176 } while (strncmp(buf, "begin", 5) != 0); 177 178 if (strncmp(buf, "begin-base64", 12) == 0) 179 base64 = 1; 180 181 /* Parse the header: begin{,-base64} mode outfile. */ 182 s = strtok(buf, " "); --- 16 unchanged lines hidden (view full) --- 199 if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf)) 200 errx(1, "%s: filename too long", outfile); 201 if (!sflag && !pflag) { 202 strlcpy(buffn, buf, sizeof(buffn)); 203 if (strrchr(buffn, '/') != NULL) 204 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf)); 205 if (buf[0] == '\0') { 206 warnx("%s: illegal filename", buffn); |
207 return(1); | 207 return (1); |
208 } 209 210 /* handle ~user/file format */ 211 if (buf[0] == '~') { 212 if (!(p = index(buf, '/'))) { 213 warnx("%s: illegal ~user", filename); | 208 } 209 210 /* handle ~user/file format */ 211 if (buf[0] == '~') { 212 if (!(p = index(buf, '/'))) { 213 warnx("%s: illegal ~user", filename); |
214 return(1); | 214 return (1); |
215 } 216 *p++ = '\0'; 217 if (!(pw = getpwnam(buf + 1))) { 218 warnx("%s: no user %s", filename, buf); | 215 } 216 *p++ = '\0'; 217 if (!(pw = getpwnam(buf + 1))) { 218 warnx("%s: no user %s", filename, buf); |
219 return(1); | 219 return (1); |
220 } 221 n = strlen(pw->pw_dir); 222 n1 = strlen(p); 223 if (n + n1 + 2 > MAXPATHLEN) { 224 warnx("%s: path too long", filename); | 220 } 221 n = strlen(pw->pw_dir); 222 n1 = strlen(p); 223 if (n + n1 + 2 > MAXPATHLEN) { 224 warnx("%s: path too long", filename); |
225 return(1); | 225 return (1); |
226 } 227 bcopy(p, buf + n + 1, n1 + 1); 228 bcopy(pw->pw_dir, buf, n); 229 buf[n] = '/'; 230 } 231 } 232 233 /* create output file, set mode */ 234 if (pflag) 235 ; /* print to stdout */ 236 237 else { 238 mode_handle = setmode(mode); 239 if (mode_handle == NULL) 240 err(1, "setmode()"); 241 if (iflag && !access(buf, F_OK)) { 242 warnx("not overwritten: %s", buf); | 226 } 227 bcopy(p, buf + n + 1, n1 + 1); 228 bcopy(pw->pw_dir, buf, n); 229 buf[n] = '/'; 230 } 231 } 232 233 /* create output file, set mode */ 234 if (pflag) 235 ; /* print to stdout */ 236 237 else { 238 mode_handle = setmode(mode); 239 if (mode_handle == NULL) 240 err(1, "setmode()"); 241 if (iflag && !access(buf, F_OK)) { 242 warnx("not overwritten: %s", buf); |
243 return(0); | 243 return (0); |
244 } else if (freopen(buf, "w", stdout) == NULL || 245 stat(buf, &st) < 0 || (S_ISREG(st.st_mode) && 246 fchmod(fileno(stdout), getmode(mode_handle, 0) & 0666))) { 247 warn("%s: %s", buf, filename); | 244 } else if (freopen(buf, "w", stdout) == NULL || 245 stat(buf, &st) < 0 || (S_ISREG(st.st_mode) && 246 fchmod(fileno(stdout), getmode(mode_handle, 0) & 0666))) { 247 warn("%s: %s", buf, filename); |
248 return(1); | 248 return (1); |
249 } 250 free(mode_handle); 251 free(mode); 252 } 253 strcpy(buffn, buf); /* store file name from header line */ 254 255 /* for each input line */ 256next: 257 for (;;) { 258 if (!fgets(p = buf, sizeof(buf), stdin)) { 259 warnx("%s: short file", filename); | 249 } 250 free(mode_handle); 251 free(mode); 252 } 253 strcpy(buffn, buf); /* store file name from header line */ 254 255 /* for each input line */ 256next: 257 for (;;) { 258 if (!fgets(p = buf, sizeof(buf), stdin)) { 259 warnx("%s: short file", filename); |
260 return(1); | 260 return (1); |
261 } 262 if (base64) { 263 if (strncmp(buf, "====", 4) == 0) 264 return (0); 265 base64_decode(buf); 266 goto next; 267 } 268#define DEC(c) (((c) - ' ') & 077) /* single character decode */ 269#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) 270/* #define IS_DEC(c) (1) */ 271 272#define OUT_OF_RANGE \ 273{ \ 274 warnx( \ 275"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \ 276 filename, buffn, 1 + ' ', 077 + ' ' + 1); \ | 261 } 262 if (base64) { 263 if (strncmp(buf, "====", 4) == 0) 264 return (0); 265 base64_decode(buf); 266 goto next; 267 } 268#define DEC(c) (((c) - ' ') & 077) /* single character decode */ 269#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) 270/* #define IS_DEC(c) (1) */ 271 272#define OUT_OF_RANGE \ 273{ \ 274 warnx( \ 275"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \ 276 filename, buffn, 1 + ' ', 077 + ' ' + 1); \ |
277 return(1); \ | 277 return (1); \ |
278} 279 280 /* 281 * `n' is used to avoid writing out all the characters 282 * at the end of the file. 283 */ 284 if ((n = DEC(*p)) <= 0) 285 break; --- 33 unchanged lines hidden (view full) --- 319 putchar(ch); 320 } 321 } 322 } 323 if (fgets(buf, sizeof(buf), stdin) == NULL || 324 (strcmp(buf, "end") && strcmp(buf, "end\n") && 325 strcmp(buf, "end\r\n"))) { 326 warnx("%s: no \"end\" line", filename); | 278} 279 280 /* 281 * `n' is used to avoid writing out all the characters 282 * at the end of the file. 283 */ 284 if ((n = DEC(*p)) <= 0) 285 break; --- 33 unchanged lines hidden (view full) --- 319 putchar(ch); 320 } 321 } 322 } 323 if (fgets(buf, sizeof(buf), stdin) == NULL || 324 (strcmp(buf, "end") && strcmp(buf, "end\n") && 325 strcmp(buf, "end\r\n"))) { 326 warnx("%s: no \"end\" line", filename); |
327 return(1); | 327 return (1); |
328 } | 328 } |
329 return(0); | 329 return (0); |
330} 331 332void 333base64_decode(const char *stream) 334{ 335 unsigned char out[MAXPATHLEN * 4]; 336 int rv; 337 --- 20 unchanged lines hidden --- | 330} 331 332void 333base64_decode(const char *stream) 334{ 335 unsigned char out[MAXPATHLEN * 4]; 336 int rv; 337 --- 20 unchanged lines hidden --- |