1 /*
2 Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
3 
4 Permission to use, copy, modify, and distribute this material
5 for any purpose and without fee is hereby granted, provided
6 that the above copyright notice and this permission notice
7 appear in all copies, and that the name of Bellcore not be
8 used in advertising or publicity pertaining to this
9 material without the specific, prior written permission
10 of an authorized representative of Bellcore.  BELLCORE
11 MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
12 OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
13 WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
14 */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <config.h>
18 #ifdef MSDOS
19 #include <fcntl.h>
20 #endif
21 
22 #define BASE64 1
23 #define QP 2 /* quoted-printable */
24 
main(argc,argv)25 main(argc, argv)
26 int argc;
27 char **argv;
28 {
29     int encode = 1, which = BASE64, i, portablenewlines = 0;
30     FILE *fp = stdin;
31     FILE *fpo = stdout;
32 
33     for (i=1; i<argc; ++i) {
34         if (argv[i][0] == '-') {
35 	    switch (argv[i][1]) {
36 		case 'o':
37 		    if (++i >= argc) {
38 			fprintf(stderr, "mimencode: -o requires a file name.\n");
39 			exit(-1);
40 		    }
41 		    fpo = fopen(argv[i], "w");
42 		    if (!fpo) {
43 			perror(argv[i]);
44 			exit(-1);
45 		    }
46 		    break;
47                 case 'u':
48                     encode = 0;
49                     break;
50                 case 'q':
51                     which = QP;
52                     break;
53                 case 'p':
54                     portablenewlines = 1;
55                     break;
56                 case 'b':
57                     which = BASE64;
58                     break;
59 		default:
60                     fprintf(stderr,
61                        "Usage: mmencode [-u] [-q] [-b] [-p] [-o outputfile] [file name]\n");
62                     exit(-1);
63             }
64         } else {
65 #ifdef MSDOS
66             if (encode)
67                 fp = fopen(argv[i], "rb");
68             else
69             {
70                 fp = fopen(argv[i], "rt");
71                 setmode(fileno(fpo), O_BINARY);
72             } /* else */
73 #else
74             fp = fopen(argv[i], "r");
75 #endif /* MSDOS */
76             if (!fp) {
77                 perror(argv[i]);
78                 exit(-1);
79             }
80         }
81     }
82 #ifdef MSDOS
83     if (fp == stdin) setmode(fileno(fp), O_BINARY);
84 #endif /* MSDOS */
85     if (which == BASE64) {
86         if (encode) {
87             to64(fp, fpo, portablenewlines);
88         } else {
89             from64(fp,fpo, (char **) NULL, (int *) 0, portablenewlines);
90         }
91     } else {
92         if (encode) toqp(fp, fpo); else fromqp(fp, fpo, NULL, 0);
93     }
94     return(0);
95 }
96 
97