support.c revision 1.16 1 1.15 christos /* $NetBSD: support.c,v 1.16 2006/10/31 20:07:32 christos Exp $ */
2 1.1 tv
3 1.1 tv /*
4 1.1 tv * Copyright (c) 1980, 1993
5 1.1 tv * The Regents of the University of California. All rights reserved.
6 1.1 tv *
7 1.1 tv * Redistribution and use in source and binary forms, with or without
8 1.1 tv * modification, are permitted provided that the following conditions
9 1.1 tv * are met:
10 1.1 tv * 1. Redistributions of source code must retain the above copyright
11 1.1 tv * notice, this list of conditions and the following disclaimer.
12 1.1 tv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tv * notice, this list of conditions and the following disclaimer in the
14 1.1 tv * documentation and/or other materials provided with the distribution.
15 1.10 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 tv * may be used to endorse or promote products derived from this software
17 1.1 tv * without specific prior written permission.
18 1.1 tv *
19 1.1 tv * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 tv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 tv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 tv * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 tv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 tv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 tv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 tv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 tv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 tv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 tv * SUCH DAMAGE.
30 1.1 tv */
31 1.1 tv
32 1.1 tv #include <sys/cdefs.h>
33 1.1 tv #ifndef lint
34 1.1 tv #if 0
35 1.1 tv static char sccsid[] = "@(#)aux.c 8.1 (Berkeley) 6/6/93";
36 1.1 tv #else
37 1.15 christos __RCSID("$NetBSD: support.c,v 1.16 2006/10/31 20:07:32 christos Exp $");
38 1.1 tv #endif
39 1.1 tv #endif /* not lint */
40 1.1 tv
41 1.1 tv #include "rcv.h"
42 1.1 tv #include "extern.h"
43 1.15 christos #include "mime.h"
44 1.1 tv
45 1.1 tv /*
46 1.1 tv * Mail -- a mail program
47 1.1 tv *
48 1.1 tv * Auxiliary functions.
49 1.1 tv */
50 1.2 wiz static char *save2str(char *, char *);
51 1.15 christos static int gethfield(FILE *, char [], int, char **); /* don't call this outside hfield()
52 1.15 christos or decoding will not get done */
53 1.1 tv /*
54 1.1 tv * Return a pointer to a dynamic copy of the argument.
55 1.1 tv */
56 1.1 tv char *
57 1.2 wiz savestr(const char *str)
58 1.1 tv {
59 1.1 tv char *new;
60 1.14 christos size_t size = strlen(str) + 1;
61 1.1 tv
62 1.4 wiz if ((new = salloc(size)) != NULL)
63 1.14 christos (void)memmove(new, str, size);
64 1.1 tv return new;
65 1.1 tv }
66 1.1 tv
67 1.1 tv /*
68 1.1 tv * Make a copy of new argument incorporating old one.
69 1.1 tv */
70 1.1 tv static char *
71 1.2 wiz save2str(char *str, char *old)
72 1.1 tv {
73 1.1 tv char *new;
74 1.14 christos size_t newsize = strlen(str) + 1;
75 1.14 christos size_t oldsize = old ? strlen(old) + 1 : 0;
76 1.1 tv
77 1.4 wiz if ((new = salloc(newsize + oldsize)) != NULL) {
78 1.1 tv if (oldsize) {
79 1.14 christos (void)memmove(new, old, oldsize);
80 1.1 tv new[oldsize - 1] = ' ';
81 1.1 tv }
82 1.14 christos (void)memmove(new + oldsize, str, newsize);
83 1.1 tv }
84 1.1 tv return new;
85 1.1 tv }
86 1.1 tv
87 1.1 tv /*
88 1.1 tv * Touch the named message by setting its MTOUCH flag.
89 1.1 tv * Touched messages have the effect of not being sent
90 1.1 tv * back to the system mailbox on exit.
91 1.1 tv */
92 1.1 tv void
93 1.2 wiz touch(struct message *mp)
94 1.1 tv {
95 1.1 tv
96 1.1 tv mp->m_flag |= MTOUCH;
97 1.1 tv if ((mp->m_flag & MREAD) == 0)
98 1.1 tv mp->m_flag |= MREAD|MSTATUS;
99 1.1 tv }
100 1.1 tv
101 1.1 tv /*
102 1.1 tv * Test to see if the passed file name is a directory.
103 1.1 tv * Return true if it is.
104 1.1 tv */
105 1.1 tv int
106 1.13 christos isdir(const char name[])
107 1.1 tv {
108 1.1 tv struct stat sbuf;
109 1.1 tv
110 1.1 tv if (stat(name, &sbuf) < 0)
111 1.1 tv return(0);
112 1.1 tv return (S_ISDIR(sbuf.st_mode));
113 1.1 tv }
114 1.1 tv
115 1.1 tv /*
116 1.1 tv * Count the number of arguments in the given string raw list.
117 1.1 tv */
118 1.1 tv int
119 1.2 wiz argcount(char **argv)
120 1.1 tv {
121 1.1 tv char **ap;
122 1.1 tv
123 1.4 wiz for (ap = argv; *ap++ != NULL;)
124 1.1 tv ;
125 1.1 tv return ap - argv - 1;
126 1.1 tv }
127 1.1 tv
128 1.1 tv /*
129 1.1 tv * Return the desired header line from the passed message
130 1.4 wiz * pointer (or NULL if the desired header field is not available).
131 1.1 tv */
132 1.1 tv char *
133 1.11 ross hfield(const char field[], const struct message *mp)
134 1.1 tv {
135 1.1 tv FILE *ibuf;
136 1.1 tv char linebuf[LINESIZE];
137 1.1 tv int lc;
138 1.3 wiz char *headerfield;
139 1.4 wiz char *colon, *oldhfield = NULL;
140 1.15 christos #ifdef MIME_SUPPORT
141 1.15 christos int decode;
142 1.15 christos
143 1.15 christos decode = value(ENAME_MIME_DECODE_MSG) &&
144 1.15 christos value(ENAME_MIME_DECODE_HDR);
145 1.15 christos #endif
146 1.1 tv
147 1.1 tv ibuf = setinput(mp);
148 1.1 tv if ((lc = mp->m_lines - 1) < 0)
149 1.4 wiz return NULL;
150 1.15 christos if (mail_readline(ibuf, linebuf, LINESIZE) < 0)
151 1.4 wiz return NULL;
152 1.1 tv while (lc > 0) {
153 1.1 tv if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
154 1.1 tv return oldhfield;
155 1.15 christos #ifdef MIME_SUPPORT
156 1.15 christos if ((headerfield = ishfield(linebuf, colon, field)) != NULL) {
157 1.15 christos char linebuf2[LINESIZE];
158 1.15 christos if (decode && colon)
159 1.15 christos headerfield = mime_decode_hfield(linebuf2, sizeof(linebuf2), headerfield);
160 1.15 christos oldhfield = save2str(headerfield, oldhfield);
161 1.15 christos }
162 1.15 christos #else
163 1.3 wiz if ((headerfield = ishfield(linebuf, colon, field)) != NULL)
164 1.3 wiz oldhfield = save2str(headerfield, oldhfield);
165 1.15 christos #endif
166 1.1 tv }
167 1.1 tv return oldhfield;
168 1.1 tv }
169 1.1 tv
170 1.1 tv /*
171 1.1 tv * Return the next header field found in the given message.
172 1.1 tv * Return >= 0 if something found, < 0 elsewise.
173 1.1 tv * "colon" is set to point to the colon in the header.
174 1.1 tv * Must deal with \ continuations & other such fraud.
175 1.1 tv */
176 1.15 christos static int
177 1.2 wiz gethfield(FILE *f, char linebuf[], int rem, char **colon)
178 1.1 tv {
179 1.1 tv char line2[LINESIZE];
180 1.1 tv char *cp, *cp2;
181 1.1 tv int c;
182 1.1 tv
183 1.1 tv for (;;) {
184 1.1 tv if (--rem < 0)
185 1.1 tv return -1;
186 1.15 christos if ((c = mail_readline(f, linebuf, LINESIZE)) <= 0)
187 1.1 tv return -1;
188 1.1 tv for (cp = linebuf; isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
189 1.1 tv cp++)
190 1.1 tv ;
191 1.1 tv if (*cp != ':' || cp == linebuf)
192 1.1 tv continue;
193 1.1 tv /*
194 1.1 tv * I guess we got a headline.
195 1.1 tv * Handle wraparounding
196 1.1 tv */
197 1.1 tv *colon = cp;
198 1.1 tv cp = linebuf + c;
199 1.1 tv for (;;) {
200 1.1 tv while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
201 1.1 tv ;
202 1.1 tv cp++;
203 1.1 tv if (rem <= 0)
204 1.1 tv break;
205 1.14 christos (void)ungetc(c = getc(f), f);
206 1.1 tv if (c != ' ' && c != '\t')
207 1.1 tv break;
208 1.15 christos if ((c = mail_readline(f, line2, LINESIZE)) < 0)
209 1.1 tv break;
210 1.1 tv rem--;
211 1.1 tv for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
212 1.1 tv ;
213 1.1 tv c -= cp2 - line2;
214 1.1 tv if (cp + c >= linebuf + LINESIZE - 2)
215 1.1 tv break;
216 1.1 tv *cp++ = ' ';
217 1.14 christos (void)memmove(cp, cp2, (size_t)c);
218 1.1 tv cp += c;
219 1.1 tv }
220 1.1 tv *cp = 0;
221 1.1 tv return rem;
222 1.1 tv }
223 1.1 tv /* NOTREACHED */
224 1.1 tv }
225 1.1 tv
226 1.1 tv /*
227 1.1 tv * Check whether the passed line is a header line of
228 1.1 tv * the desired breed. Return the field body, or 0.
229 1.1 tv */
230 1.1 tv
231 1.1 tv char*
232 1.11 ross ishfield(const char linebuf[], char *colon, const char field[])
233 1.1 tv {
234 1.1 tv char *cp = colon;
235 1.1 tv
236 1.1 tv *cp = 0;
237 1.1 tv if (strcasecmp(linebuf, field) != 0) {
238 1.1 tv *cp = ':';
239 1.1 tv return 0;
240 1.1 tv }
241 1.1 tv *cp = ':';
242 1.1 tv for (cp++; *cp == ' ' || *cp == '\t'; cp++)
243 1.1 tv ;
244 1.1 tv return cp;
245 1.1 tv }
246 1.1 tv
247 1.1 tv /*
248 1.1 tv * Copy a string, lowercasing it as we go.
249 1.1 tv */
250 1.1 tv void
251 1.13 christos istrcpy(char *dest, const char *src)
252 1.1 tv {
253 1.1 tv
254 1.1 tv do {
255 1.12 dsl *dest++ = tolower((unsigned char)*src);
256 1.1 tv } while (*src++ != 0);
257 1.1 tv }
258 1.1 tv
259 1.1 tv /*
260 1.1 tv * The following code deals with input stacking to do source
261 1.1 tv * commands. All but the current file pointer are saved on
262 1.1 tv * the stack.
263 1.1 tv */
264 1.1 tv
265 1.1 tv static int ssp; /* Top of file stack */
266 1.1 tv struct sstack {
267 1.1 tv FILE *s_file; /* File we were in. */
268 1.1 tv int s_cond; /* Saved state of conditionals */
269 1.1 tv int s_loading; /* Loading .mailrc, etc. */
270 1.1 tv } sstack[NOFILE];
271 1.1 tv
272 1.1 tv /*
273 1.1 tv * Pushdown current input file and switch to a new one.
274 1.1 tv * Set the global flag "sourcing" so that others will realize
275 1.1 tv * that they are no longer reading from a tty (in all probability).
276 1.1 tv */
277 1.1 tv int
278 1.2 wiz source(void *v)
279 1.1 tv {
280 1.1 tv char **arglist = v;
281 1.1 tv FILE *fi;
282 1.13 christos const char *cp;
283 1.1 tv
284 1.4 wiz if ((cp = expand(*arglist)) == NULL)
285 1.1 tv return(1);
286 1.1 tv if ((fi = Fopen(cp, "r")) == NULL) {
287 1.9 wiz warn("%s", cp);
288 1.1 tv return(1);
289 1.1 tv }
290 1.1 tv if (ssp >= NOFILE - 1) {
291 1.14 christos (void)printf("Too much \"sourcing\" going on.\n");
292 1.14 christos (void)Fclose(fi);
293 1.1 tv return(1);
294 1.1 tv }
295 1.1 tv sstack[ssp].s_file = input;
296 1.1 tv sstack[ssp].s_cond = cond;
297 1.1 tv sstack[ssp].s_loading = loading;
298 1.1 tv ssp++;
299 1.1 tv loading = 0;
300 1.1 tv cond = CANY;
301 1.1 tv input = fi;
302 1.1 tv sourcing++;
303 1.1 tv return(0);
304 1.1 tv }
305 1.1 tv
306 1.1 tv /*
307 1.1 tv * Pop the current input back to the previous level.
308 1.1 tv * Update the "sourcing" flag as appropriate.
309 1.1 tv */
310 1.1 tv int
311 1.2 wiz unstack(void)
312 1.1 tv {
313 1.1 tv if (ssp <= 0) {
314 1.14 christos (void)printf("\"Source\" stack over-pop.\n");
315 1.1 tv sourcing = 0;
316 1.1 tv return(1);
317 1.1 tv }
318 1.14 christos (void)Fclose(input);
319 1.1 tv if (cond != CANY)
320 1.14 christos (void)printf("Unmatched \"if\"\n");
321 1.1 tv ssp--;
322 1.1 tv cond = sstack[ssp].s_cond;
323 1.1 tv loading = sstack[ssp].s_loading;
324 1.1 tv input = sstack[ssp].s_file;
325 1.1 tv if (ssp == 0)
326 1.1 tv sourcing = loading;
327 1.1 tv return(0);
328 1.1 tv }
329 1.1 tv
330 1.1 tv /*
331 1.1 tv * Touch the indicated file.
332 1.1 tv * This is nifty for the shell.
333 1.1 tv */
334 1.1 tv void
335 1.2 wiz alter(char *name)
336 1.1 tv {
337 1.1 tv struct stat sb;
338 1.1 tv struct timeval tv[2];
339 1.1 tv
340 1.1 tv if (stat(name, &sb))
341 1.1 tv return;
342 1.8 wiz (void)gettimeofday(&tv[0], NULL);
343 1.1 tv tv[0].tv_sec++;
344 1.1 tv TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
345 1.8 wiz (void)utimes(name, tv);
346 1.1 tv }
347 1.1 tv
348 1.1 tv /*
349 1.1 tv * Examine the passed line buffer and
350 1.1 tv * return true if it is all blanks and tabs.
351 1.1 tv */
352 1.1 tv int
353 1.2 wiz blankline(char linebuf[])
354 1.1 tv {
355 1.1 tv char *cp;
356 1.1 tv
357 1.1 tv for (cp = linebuf; *cp; cp++)
358 1.1 tv if (*cp != ' ' && *cp != '\t')
359 1.1 tv return(0);
360 1.1 tv return(1);
361 1.1 tv }
362 1.1 tv
363 1.1 tv /*
364 1.1 tv * Get sender's name from this message. If the message has
365 1.1 tv * a bunch of arpanet stuff in it, we may have to skin the name
366 1.1 tv * before returning it.
367 1.1 tv */
368 1.1 tv char *
369 1.2 wiz nameof(struct message *mp, int reptype)
370 1.1 tv {
371 1.1 tv char *cp, *cp2;
372 1.1 tv
373 1.1 tv cp = skin(name1(mp, reptype));
374 1.1 tv if (reptype != 0 || charcount(cp, '!') < 2)
375 1.1 tv return(cp);
376 1.1 tv cp2 = strrchr(cp, '!');
377 1.1 tv cp2--;
378 1.1 tv while (cp2 > cp && *cp2 != '!')
379 1.1 tv cp2--;
380 1.1 tv if (*cp2 == '!')
381 1.1 tv return(cp2 + 1);
382 1.1 tv return(cp);
383 1.1 tv }
384 1.1 tv
385 1.1 tv /*
386 1.1 tv * Start of a "comment".
387 1.1 tv * Ignore it.
388 1.1 tv */
389 1.1 tv char *
390 1.2 wiz skip_comment(char *cp)
391 1.1 tv {
392 1.1 tv int nesting = 1;
393 1.1 tv
394 1.1 tv for (; nesting > 0 && *cp; cp++) {
395 1.1 tv switch (*cp) {
396 1.1 tv case '\\':
397 1.1 tv if (cp[1])
398 1.1 tv cp++;
399 1.1 tv break;
400 1.1 tv case '(':
401 1.1 tv nesting++;
402 1.1 tv break;
403 1.1 tv case ')':
404 1.1 tv nesting--;
405 1.1 tv break;
406 1.1 tv }
407 1.1 tv }
408 1.1 tv return cp;
409 1.1 tv }
410 1.1 tv
411 1.1 tv /*
412 1.1 tv * Skin an arpa net address according to the RFC 822 interpretation
413 1.1 tv * of "host-phrase."
414 1.1 tv */
415 1.1 tv char *
416 1.2 wiz skin(char *name)
417 1.1 tv {
418 1.1 tv int c;
419 1.1 tv char *cp, *cp2;
420 1.1 tv char *bufend;
421 1.1 tv int gotlt, lastsp;
422 1.1 tv char nbuf[BUFSIZ];
423 1.1 tv
424 1.4 wiz if (name == NULL)
425 1.4 wiz return(NULL);
426 1.4 wiz if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
427 1.4 wiz && strchr(name, ' ') == NULL)
428 1.1 tv return(name);
429 1.1 tv gotlt = 0;
430 1.1 tv lastsp = 0;
431 1.1 tv bufend = nbuf;
432 1.15 christos for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; /* EMPTY */) {
433 1.1 tv switch (c) {
434 1.1 tv case '(':
435 1.1 tv cp = skip_comment(cp);
436 1.1 tv lastsp = 0;
437 1.1 tv break;
438 1.1 tv
439 1.1 tv case '"':
440 1.1 tv /*
441 1.1 tv * Start of a "quoted-string".
442 1.1 tv * Copy it in its entirety.
443 1.1 tv */
444 1.1 tv while ((c = *cp) != '\0') {
445 1.1 tv cp++;
446 1.1 tv if (c == '"')
447 1.1 tv break;
448 1.1 tv if (c != '\\')
449 1.1 tv *cp2++ = c;
450 1.1 tv else if ((c = *cp) != '\0') {
451 1.1 tv *cp2++ = c;
452 1.1 tv cp++;
453 1.1 tv }
454 1.1 tv }
455 1.1 tv lastsp = 0;
456 1.1 tv break;
457 1.1 tv
458 1.1 tv case ' ':
459 1.1 tv if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
460 1.1 tv cp += 3, *cp2++ = '@';
461 1.1 tv else
462 1.1 tv if (cp[0] == '@' && cp[1] == ' ')
463 1.1 tv cp += 2, *cp2++ = '@';
464 1.1 tv else
465 1.1 tv lastsp = 1;
466 1.1 tv break;
467 1.1 tv
468 1.1 tv case '<':
469 1.1 tv cp2 = bufend;
470 1.1 tv gotlt++;
471 1.1 tv lastsp = 0;
472 1.1 tv break;
473 1.1 tv
474 1.1 tv case '>':
475 1.1 tv if (gotlt) {
476 1.1 tv gotlt = 0;
477 1.1 tv while ((c = *cp) && c != ',') {
478 1.1 tv cp++;
479 1.1 tv if (c == '(')
480 1.1 tv cp = skip_comment(cp);
481 1.1 tv else if (c == '"')
482 1.1 tv while ((c = *cp) != '\0') {
483 1.1 tv cp++;
484 1.1 tv if (c == '"')
485 1.1 tv break;
486 1.1 tv if (c == '\\' && *cp)
487 1.1 tv cp++;
488 1.1 tv }
489 1.1 tv }
490 1.1 tv lastsp = 0;
491 1.1 tv break;
492 1.1 tv }
493 1.14 christos /* FALLTHROUGH */
494 1.1 tv
495 1.1 tv default:
496 1.1 tv if (lastsp) {
497 1.1 tv lastsp = 0;
498 1.1 tv *cp2++ = ' ';
499 1.1 tv }
500 1.1 tv *cp2++ = c;
501 1.1 tv if (c == ',' && !gotlt) {
502 1.1 tv *cp2++ = ' ';
503 1.1 tv for (; *cp == ' '; cp++)
504 1.1 tv ;
505 1.1 tv lastsp = 0;
506 1.1 tv bufend = cp2;
507 1.1 tv }
508 1.1 tv }
509 1.1 tv }
510 1.1 tv *cp2 = 0;
511 1.1 tv
512 1.1 tv return(savestr(nbuf));
513 1.1 tv }
514 1.1 tv
515 1.1 tv /*
516 1.1 tv * Fetch the sender's name from the passed message.
517 1.1 tv * Reptype can be
518 1.1 tv * 0 -- get sender's name for display purposes
519 1.1 tv * 1 -- get sender's name for reply
520 1.1 tv * 2 -- get sender's name for Reply
521 1.1 tv */
522 1.1 tv char *
523 1.2 wiz name1(struct message *mp, int reptype)
524 1.1 tv {
525 1.1 tv char namebuf[LINESIZE];
526 1.1 tv char linebuf[LINESIZE];
527 1.1 tv char *cp, *cp2;
528 1.1 tv FILE *ibuf;
529 1.3 wiz int firstrun = 1;
530 1.1 tv
531 1.4 wiz if ((cp = hfield("from", mp)) != NULL)
532 1.1 tv return cp;
533 1.4 wiz if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
534 1.1 tv return cp;
535 1.1 tv ibuf = setinput(mp);
536 1.1 tv namebuf[0] = '\0';
537 1.15 christos if (mail_readline(ibuf, linebuf, LINESIZE) < 0)
538 1.1 tv return(savestr(namebuf));
539 1.1 tv newname:
540 1.1 tv for (cp = linebuf; *cp && *cp != ' '; cp++)
541 1.1 tv ;
542 1.1 tv for (; *cp == ' ' || *cp == '\t'; cp++)
543 1.1 tv ;
544 1.1 tv for (cp2 = &namebuf[strlen(namebuf)];
545 1.1 tv *cp && *cp != ' ' && *cp != '\t' && cp2 < namebuf + LINESIZE - 1;)
546 1.1 tv *cp2++ = *cp++;
547 1.1 tv *cp2 = '\0';
548 1.15 christos if (mail_readline(ibuf, linebuf, LINESIZE) < 0)
549 1.1 tv return(savestr(namebuf));
550 1.1 tv if ((cp = strchr(linebuf, 'F')) == NULL)
551 1.1 tv return(savestr(namebuf));
552 1.1 tv if (strncmp(cp, "From", 4) != 0)
553 1.1 tv return(savestr(namebuf));
554 1.1 tv while ((cp = strchr(cp, 'r')) != NULL) {
555 1.1 tv if (strncmp(cp, "remote", 6) == 0) {
556 1.1 tv if ((cp = strchr(cp, 'f')) == NULL)
557 1.1 tv break;
558 1.1 tv if (strncmp(cp, "from", 4) != 0)
559 1.1 tv break;
560 1.1 tv if ((cp = strchr(cp, ' ')) == NULL)
561 1.1 tv break;
562 1.1 tv cp++;
563 1.3 wiz if (firstrun) {
564 1.1 tv cp2 = namebuf;
565 1.3 wiz firstrun = 0;
566 1.1 tv } else
567 1.1 tv cp2 = strrchr(namebuf, '!') + 1;
568 1.1 tv while (*cp && cp2 < namebuf + LINESIZE - 1)
569 1.1 tv *cp2++ = *cp++;
570 1.1 tv if (cp2 < namebuf + LINESIZE - 1)
571 1.1 tv *cp2++ = '!';
572 1.1 tv *cp2 = '\0';
573 1.1 tv if (cp2 < namebuf + LINESIZE - 1)
574 1.1 tv goto newname;
575 1.1 tv else
576 1.1 tv break;
577 1.1 tv }
578 1.1 tv cp++;
579 1.1 tv }
580 1.1 tv return(savestr(namebuf));
581 1.1 tv }
582 1.1 tv
583 1.1 tv /*
584 1.15 christos * Count the occurrences of c in str
585 1.1 tv */
586 1.1 tv int
587 1.2 wiz charcount(char *str, int c)
588 1.1 tv {
589 1.1 tv char *cp;
590 1.1 tv int i;
591 1.1 tv
592 1.1 tv for (i = 0, cp = str; *cp; cp++)
593 1.1 tv if (*cp == c)
594 1.1 tv i++;
595 1.1 tv return(i);
596 1.1 tv }
597 1.1 tv
598 1.1 tv /*
599 1.1 tv * Convert c to upper case
600 1.1 tv */
601 1.1 tv int
602 1.2 wiz upcase(int c)
603 1.1 tv {
604 1.1 tv
605 1.1 tv if (islower(c))
606 1.1 tv return toupper(c);
607 1.1 tv return c;
608 1.1 tv }
609 1.1 tv
610 1.1 tv /*
611 1.1 tv * Copy s1 to s2, return pointer to null in s2.
612 1.1 tv */
613 1.1 tv char *
614 1.2 wiz copy(char *s1, char *s2)
615 1.1 tv {
616 1.1 tv
617 1.1 tv while ((*s2++ = *s1++) != '\0')
618 1.1 tv ;
619 1.1 tv return s2 - 1;
620 1.1 tv }
621 1.1 tv
622 1.1 tv /*
623 1.1 tv * See if the given header field is supposed to be ignored.
624 1.1 tv */
625 1.1 tv int
626 1.13 christos isign(const char *field, struct ignoretab ignoretabs[2])
627 1.1 tv {
628 1.1 tv char realfld[LINESIZE];
629 1.1 tv
630 1.3 wiz if (ignoretabs == ignoreall)
631 1.1 tv return 1;
632 1.1 tv /*
633 1.1 tv * Lower-case the string, so that "Status" and "status"
634 1.1 tv * will hash to the same place.
635 1.1 tv */
636 1.1 tv istrcpy(realfld, field);
637 1.3 wiz if (ignoretabs[1].i_count > 0)
638 1.3 wiz return (!member(realfld, ignoretabs + 1));
639 1.1 tv else
640 1.3 wiz return (member(realfld, ignoretabs));
641 1.1 tv }
642 1.1 tv
643 1.1 tv int
644 1.2 wiz member(char *realfield, struct ignoretab *table)
645 1.1 tv {
646 1.1 tv struct ignore *igp;
647 1.1 tv
648 1.1 tv for (igp = table->i_head[hash(realfield)]; igp != 0; igp = igp->i_link)
649 1.1 tv if (*igp->i_field == *realfield &&
650 1.1 tv equal(igp->i_field, realfield))
651 1.1 tv return (1);
652 1.1 tv return (0);
653 1.1 tv }
654