aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2013-03-27 00:52:17 -0500
committerRob Landley <rob@landley.net>2013-03-27 00:52:17 -0500
commitf597042c66a3021768ee471f7b2a494ac32ee356 (patch)
treec4c0953c34e2aa2d5b214a1046ad29e332a26e66 /toys
parentb7e8385a24101d863482b6572d78efeea457a6ef (diff)
downloadtoybox-f597042c66a3021768ee471f7b2a494ac32ee356.tar.gz
Incremental cleanup of uudecode.
Diffstat (limited to 'toys')
-rw-r--r--toys/pending/uudecode.c54
1 files changed, 30 insertions, 24 deletions
diff --git a/toys/pending/uudecode.c b/toys/pending/uudecode.c
index ffdd63e1..abd8414f 100644
--- a/toys/pending/uudecode.c
+++ b/toys/pending/uudecode.c
@@ -4,7 +4,7 @@
*
* See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uudecode.html
-USE_UUDECODE(NEWTOY(uudecode, ">2o:", TOYFLAG_USR|TOYFLAG_BIN))
+USE_UUDECODE(NEWTOY(uudecode, ">1o:", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))
config UUDECODE
bool "uudecode"
@@ -142,28 +142,34 @@ static void uudecode_uu(int ifd, int ofd)
void uudecode_main(void)
{
- int ifd = 0, ofd = 1;
- char *out_filename = NULL, *line, *p,*p2;
+ int ifd = 0, ofd, idx = 0;
+ char *line;
void (*decoder)(int ifd, int ofd) = NULL;
- long mode = 0744;
-
- if (toys.optc == 1) ifd = xopen(toys.optargs[0],O_RDONLY);
-
- do {
- if ((line = get_line(ifd)) == NULL) perror_exit("empty file");
- } while (strlen(line) == 0); /* skip over empty lines */
- if (!strncmp(line, "begin ", 6)) decoder = uudecode_uu;
- else if (!strncmp(line, "begin-base64 ", 13)) decoder = uudecode_b64;
- else perror_exit("not a valid uu- or base64-encoded file");
- for (p = line; !isspace(*p); p++) /* skip first part */;
- for (; isspace(*p); p++) /* skip spaces */;
- mode = strtoul(p,&p2,8);
- p = p2 + 1; /* skip space */
- if (toys.optflags & FLAG_o) out_filename = TT.o;
- else out_filename = p;
-
- ofd = xcreate(out_filename,O_WRONLY|O_CREAT|O_TRUNC,mode);
- free(line);
- decoder(ifd,ofd);
- if (CFG_TOYBOX_FREE) close(ofd);
+
+ if (toys.optc) ifd = xopen(*toys.optargs, O_RDONLY);
+
+ for (;;) {
+ char mode[16];
+
+ if (!(line = get_line(ifd))) error_exit("no header");
+ sscanf(line, "begin%*[ ]%15s%*[ ]%n", mode, &idx);
+ if (idx) decoder = uudecode_uu;
+ else {
+ sscanf(line, "begin-base64%*[ ]%15s%*[ ]%n", mode, &idx);
+ if (idx) decoder = uudecode_b64;
+ }
+
+ if (!idx) continue;
+
+ ofd = xcreate(TT.o ? TT.o : line+idx, O_WRONLY|O_CREAT|O_TRUNC,
+ string_to_mode(mode, 0777^toys.old_umask));
+ free(line);
+ decoder(ifd,ofd);
+ break;
+ }
+
+ if (CFG_TOYBOX_FREE) {
+ if (ifd) close(ifd);
+ close(ofd);
+ }
}