annotate toys/posix/uuencode.c @ 1775:57f2a26fa92c draft toast

To ensure that toybox can be installed alongside busybox without confusing update-alternatives, the paths of the links installed by toybox should match those installed by busybox. This is accomplished by changing the flags of a few tools within toybox.
author Paul Barker <paul@paulbarker.me.uk>
date Sat, 04 Apr 2015 11:58:06 -0500
parents 1c152e2c5b27
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
831
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* uuencode.c - uuencode / base64 encode
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2013 Erich Plondke <toybox@erich.wreck.org>
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
6
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN))
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config UUENCODE
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 bool "uuencode"
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default y
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 usage: uuencode [-m] [file] encode-filename
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
14
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 Uuencode stdin (or file) to stdout, with encode-filename in the output.
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 -m base64-encode
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 */
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 #define FOR_uuencode
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 #include "toys.h"
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 void uuencode_main(void)
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 {
1609
1c152e2c5b27 Leftover variable from factoring out base64_init() caused an unused variable warning.
Rob Landley <rob@landley.net>
parents: 1605
diff changeset
25 char *name = toys.optargs[toys.optc-1], buf[(76/4)*3];
831
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 int i, m = toys.optflags & FLAG_m, fd = 0;
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 if (toys.optc > 1) fd = xopen(toys.optargs[0], O_RDONLY);
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
30
1605
c4c9267467f8 Add base64.
Rob Landley <rob@landley.net>
parents: 853
diff changeset
31 base64_init(toybuf);
831
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
32
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 xprintf("begin%s 744 %s\n", m ? "-base64" : "", name);
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 for (;;) {
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 char *in;
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break;
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
38
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 if (!m) xputc(i+32);
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 in = buf;
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
41
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 for (in = buf; in-buf < i; ) {
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 int j, x, bytes = i - (in-buf);
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
44
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 if (bytes > 3) bytes = 3;
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
46
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 for (j = x = 0; j<4; j++) {
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 int out;
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j));
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 out = (x>>((3-j)*6)) & 0x3f;
853
357b31ae2f04 uuencode tweak: output a non-whitespace character for 0 in traditional mode.
Rob Landley <rob@landley.net>
parents: 831
diff changeset
52 xputc(m ? (j > bytes ? '=' : toybuf[out]) : (out ? out + 0x20 : 0x60));
831
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 }
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 }
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 xputc('\n');
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 }
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 xputs(m ? "====" : "end");
4b9b7c9a194f Finish uuencode cleanup, default y, move it from pending to posix.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 }