]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/progs/comp.c
Replaced use of snprintf() with memcpy()/memmove().
[nmh] / docs / historical / mh-jun-1982 / progs / comp.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9 #include <stdio.h>
10 #include <strings.h>
11 #include <signal.h>
12
13 /* #define NEWS 1 */
14 /* #define TEST 1 */
15
16 struct swit anyul[] = {
17 "no", 0,
18 "yes", 0,
19 "use", 0,
20 "list", 0,
21 0
22 };
23
24 struct swit aleqs[] = {
25 "list", 0, /* 0 */
26 "edit [<editor>]", 0, /* 1 */
27 "quit [delete]", 0, /* 2 */
28 "send [switches]", 0, /* 3 */
29 0
30 };
31
32
33 struct swit switches[] = {
34 "editor editor", 0, /* 0 */
35 "form formfile", 0, /* 1 */
36 "use", 0, /* 2 */
37 "nouse", 0, /* 3 */
38 "help", 4, /* 4 */
39 0, 0
40 };
41
42 /*ARGSUSED*/
43 main(argc, argv)
44 char *argv[];
45 {
46 register char *cp;
47 register int in, out;
48 int use, cnt, status;
49 char buf[BUFSIZ], *ed, *file, *form;
50 static char path[128];
51 char **ap;
52 char *arguments[50], **argp;
53
54 invo_name = argv[0];
55 /*** setbuf(stdout, _sobuf); ***/
56 #ifdef NEWS
57 m_news();
58 #endif
59 form = 0; use = 0; file = 0; ed = 0;
60 cp = r1bindex(argv[0], '/');
61 if((cp = m_find(cp)) != NULL) {
62 ap = brkstring(cp = getcpy(cp), " ", "\n");
63 ap = copyip(ap, arguments);
64 } else
65 ap = arguments;
66 VOID copyip(argv+1, ap);
67 argp = arguments;
68 while(cp = *argp++) {
69 if(*cp == '-') {
70 switch(smatch(++cp, switches)) {
71 case -2:ambigsw(cp, switches); /* ambiguous */
72 goto leave;
73 /* unknown */
74 case -1:fprintf(stderr, "comp: -%s unknown\n", cp);
75 goto leave;
76 case 0: if(!(ed = *argp++)) { /* -editor */
77 missing: fprintf(stderr, "comp: Missing argument for %s switch\n", argp[-2]);
78 goto leave;
79 }
80 continue;
81 case 1: if(!(form = *argp++)) /* -form */
82 goto missing;
83 continue;
84 case 2: use = 1; continue; /* -use */
85 case 3: use = 0; continue; /* -nouse */
86 case 4: help("comp [file] [switches]",
87 switches);
88 goto leave;
89 }
90 }
91 file = cp;
92 }
93 if(form) {
94 if((in = open(m_maildir(form), 0)) < 0) {
95 fprintf(stderr, "comp: Can't open form file: %s\n", form);
96 goto leave;
97 }
98 } else if((in = open(m_maildir(components), 0)) < 0 &&
99 (in = open(stdcomps, 0)) < 0) {
100 fprintf(stderr, "comp: Can't open default components file!!\n");
101 goto leave;
102 }
103 if(!file)
104 file = draft;
105 VOID copy(m_maildir(file), path);
106 if((out = open(path, 0)) >= 0) {
107 cp = concat("\n\"", path, "\" exists; delete? ", 0);
108 if(use || fdcompare(in, out))
109 goto editit;
110 while((status = gans(cp, anyul)) == 3)
111 VOID showfile(path);
112 if(status == 2) {
113 use++;
114 goto editit;
115 }
116 if(status == 0)
117 goto leave;
118 VOID close(out);
119 } else if(use) {
120 fprintf(stderr, "comp: \"%s\" doesn't exist!\n", path);
121 goto leave;
122 }
123 if((out = creat(path, m_gmprot())) < 0) {
124 fprintf(stderr, "comp: Can't create \"%s\"\n", path);
125 goto leave;
126 }
127 do
128 if(cnt = read(in, buf, sizeof buf))
129 if(write(out, buf, cnt) != cnt) {
130 fprintf(stderr, "comp: error writing ");
131 perror(path);
132 goto leave;
133 }
134 while(cnt == sizeof buf);
135 VOID close(in);
136 editit:
137 VOID close(out);
138 if(m_edit(&ed, path, use, NULLCP) < 0)
139 goto leave;
140 #ifdef TEST
141 fprintf(stderr, "!! Test Version of SEND Being Run !!\n");
142 fprintf(stderr, " Send verbose !\n\n");
143 #endif
144
145 for(;;) {
146 if(!(argp = getans("\nWhat now? ", aleqs)))
147 goto leave;
148 switch(smatch(*argp, aleqs)) {
149 case 0: VOID showfile(path); /* list */
150 break;
151
152 case 1: if(*++argp) /* edit */
153 ed = *argp;
154 if(m_edit(&ed, path, use, NULLCP) == -1)
155 goto leave;
156 break;
157 /* quit */
158 case 2: if(*++argp && (*argp[0] == 'd' ||
159 (*argp[0]=='-' && *argp[1]=='d')))
160 if(unlink(path) == -1) {
161 fprintf(stderr, "Can't unlink %s ", path);
162 perror("");
163 }
164 goto leave;
165
166 case 3: VOID m_send(++argp, path); /* send */
167 goto leave;
168
169 default:fprintf(stderr, "comp: illegal option\n"); /*##*/
170 break;
171 }
172 }
173
174 leave:
175 m_update();
176 done(0);
177 }
178