Download code

Jump to: navigation, search

Back to UUencode_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

uudecode.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/UUencode_(C)?oldid=16898
14 */
15 
16 #include<stdio.h>
17 #include<stdlib.h>
18 #include<string.h>
19 #include<ctype.h>
20 
21 int main(int argc, char *argv[])
22 {
23         char bytes[3];
24         char *p;
25 
26         char line[128];
27         size_t len;
28         FILE *fp=stdout;
29         int i;
30         size_t wlen;
31 
32         int mode;
33         char *token;
34 
35 
36         if(!fgets(line, 128, stdin)) {
37             fprintf(stderr, "ERROR: Reading input stream\n");
38             return EXIT_FAILURE;
39         }
40         
41 	    if(!(token=strtok(line, " \t\r\n")) || strcmp(token, "begin")) {
42 	        fprintf(stderr, "ERROR: Bad format - missing \"begin\"\n");
43 	        return EXIT_FAILURE;
44 	    }
45 
46 	    if(!(token=strtok(NULL, " \t\r\n")) || !isdigit(token[0])) {
47 	        fprintf(stderr, "ERROR: Bad format - missing mode specifier\n");
48 	        return EXIT_FAILURE;
49 	    }
50 	    mode=strtol(token, NULL, 8);
51 
52 	    if((token=strtok(NULL, " \t\r\n"))) {
53 	        if(!(fp=fopen(token, "wb"))) {
54 	            fprintf(stderr, "ERROR: Cannot open file %s\n", token);
55 	            return EXIT_FAILURE;
56 	        }
57 	    }
58 
59         while((fgets(line, 128, stdin))) {
60             if(!strncmp(line, "end", 3)) break;
61                 len=(line[0]-' ')&0x3f;
62 
63 	        p=line+1;
64 	        for(i=0; i<len; i+=3) {
65 	                bytes[0]=((p[0]-' ')<<2) + (((p[1]-' ')&0x30)>>4);
66 		        bytes[1]=(((p[1]-' ')&0x0f)<<4) + (((p[2]-' ')&0x3c)>>2);
67 		        bytes[2]=(((p[2]-' ')&0x03)<<6) + ((p[3]-' ')&0x3f);
68 	            if(len-i<3) wlen=len-i;
69 	            else wlen=3;
70 	            if(fwrite(bytes, sizeof(char), wlen, fp)!=wlen) {
71 	                fprintf(stderr, "ERROR: Write error\n");
72 	                fclose(fp);
73 	                return EXIT_FAILURE;
74 	            }
75 	            p+=4;
76 	        }		
77 
78         }
79 	
80     if(fp!=stdout) fclose(fp);
81 
82     return EXIT_SUCCESS;
83 }


hijacker
hijacker
hijacker
hijacker

build.log

1 /tmp/litprog4676419/uudecode.c: In function 'main':
2 /tmp/litprog4676419/uudecode.c:32:13: warning: variable 'mode' set but not used [-Wunused-but-set-variable]


Makefile

# The authors of this work have released all rights to it and placed it
# in the public domain under the Creative Commons CC0 1.0 waiver
# (http://creativecommons.org/publicdomain/zero/1.0/).
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
# Retrieved from: http://en.literateprograms.org/UUencode_(C)?oldid=16898

all: uuencode uudecode

uuencode: uuencode.o
	cc -o uuencode -Wall -pedantic -ansi uuencode.o

uuencode.o: uuencode.c
	cc -o uuencode.o -Wall -pedantic -ansi -c uuencode.c

uudecode: uudecode.o
	cc -o uudecode -Wall -pedantic -ansi uudecode.o

uudecode.o: uudecode.c
	cc -o uudecode.o -Wall -pedantic -ansi -c uudecode.c


hijacker
hijacker
hijacker
hijacker

uuencode.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/UUencode_(C)?oldid=16898
14 */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <ctype.h>
19 
20 #define EXIT_SUCCESS 0
21 
22 #define MAX_INPUT_BYTES_PER_LINE   45
23 #define MAX_OUTPUT_CHARS_PER_LINE  (MAX_INPUT_BYTES_PER_LINE*4/3)
24 
25 int main(int argc, char *argv[])
26 {
27     unsigned char b[3];
28     char *d;
29     int i;
30 
31     size_t num_read;
32     size_t num_bytes_in_line;
33     char outbuf[1 + MAX_OUTPUT_CHARS_PER_LINE + 1];
34     int outbufpos;
35     int mode = 0;
36     char *name;
37 
38 
39     if((++argv)[0]) {
40         if(isdigit(argv[0][0]))
41             mode=strtol((argv++)[0], NULL, 8);
42     } else mode=0644;
43     name=argv[0]?argv[0]:"uufile";
44 
45     printf("begin %o %s\n", mode, name);
46     /* Use maximum length character for all lines except the last line */
47     outbufpos = 1;
48     num_bytes_in_line = 0;
49     while((num_read = fread(b, sizeof(unsigned char), 3, stdin)) > 0) {
50         num_bytes_in_line += num_read;
51         for(i=num_read; i<3; i++)
52             b[i] = '\0';
53 
54         d = &outbuf[outbufpos];
55         d[0] = (b[0] >> 2) & 0x3f;
56 	d[1] = ((b[0] << 4) | ((b[1] >> 4) & 0x0f)) & 0x3f;
57 	d[2] = ((b[1] << 2) | ((b[2] >> 6) & 0x03)) & 0x3f;
58 	d[3] = b[2] & 0x3f;
59 
60 	for (i=0; i<4; i++) {
61 	    d[i] += ' ';
62 	    if (d[i] == ' ')
63 	        d[i] = '`';
64 	}
65 
66         outbufpos += 4;
67         outbuf[outbufpos] = '\0';
68 
69         if(outbufpos == 1 + MAX_OUTPUT_CHARS_PER_LINE) {
70             /* Set length character for a full line and write it out. */
71             outbuf[0] = ((char)num_bytes_in_line) + ' ';    /* == MAX_INPUT_BYTES_PER_LINE + ' ' == 45 + 32 == 77 == 'M' */
72             puts(outbuf);
73             /* Start the next output line. */
74             outbufpos = 1;
75             num_bytes_in_line = 0;
76         }
77     }
78     /* Set length character for final line and write it out. */
79     outbuf[0] = ((char)num_bytes_in_line) + ' ';
80     puts(outbuf);
81     puts("`");
82     puts("end");
83 	
84     return EXIT_SUCCESS;
85 }


hijacker
hijacker
hijacker
hijacker