17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*6c83d09fSrobbin 23*6c83d09fSrobbin /* 24*6c83d09fSrobbin * Copyright 1999 Sun Microsystems, Inc. All rights reserved. 25*6c83d09fSrobbin * Use is subject to license terms. 26*6c83d09fSrobbin */ 27*6c83d09fSrobbin 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 31*6c83d09fSrobbin #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include "rcv.h" 347c478bd9Sstevel@tonic-gate #include <locale.h> 357c478bd9Sstevel@tonic-gate #include <wordexp.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley 397c478bd9Sstevel@tonic-gate * mail program 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * File I/O. 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate static int getln(char *line, int max, FILE *f); 457c478bd9Sstevel@tonic-gate static int linecount(char *lp, long size); 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* 487c478bd9Sstevel@tonic-gate * Set up the input pointers while copying the mail file into 497c478bd9Sstevel@tonic-gate * /tmp. 507c478bd9Sstevel@tonic-gate */ 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate void 537c478bd9Sstevel@tonic-gate setptr(register FILE *ibuf) 547c478bd9Sstevel@tonic-gate { 557c478bd9Sstevel@tonic-gate int n, newline = 1, blankline = 1; 567c478bd9Sstevel@tonic-gate int StartNewMsg = TRUE; 577c478bd9Sstevel@tonic-gate int ToldUser = FALSE; 587c478bd9Sstevel@tonic-gate long clen = -1L; 597c478bd9Sstevel@tonic-gate int hdr = 0; 607c478bd9Sstevel@tonic-gate int cflg = 0; /* found Content-length in header */ 617c478bd9Sstevel@tonic-gate register char *cp; 627c478bd9Sstevel@tonic-gate register int l; 637c478bd9Sstevel@tonic-gate register long s; 647c478bd9Sstevel@tonic-gate off_t offset; 657c478bd9Sstevel@tonic-gate char linebuf[LINESIZE]; 667c478bd9Sstevel@tonic-gate int inhead, newmail, Odot; 677c478bd9Sstevel@tonic-gate short flag; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate if (!space) { 707c478bd9Sstevel@tonic-gate msgCount = 0; 717c478bd9Sstevel@tonic-gate offset = 0; 727c478bd9Sstevel@tonic-gate space = 32; 737c478bd9Sstevel@tonic-gate newmail = 0; 747c478bd9Sstevel@tonic-gate message = 757c478bd9Sstevel@tonic-gate (struct message *)calloc(space, sizeof (struct message)); 767c478bd9Sstevel@tonic-gate if (message == NULL) { 777c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 787c478bd9Sstevel@tonic-gate "calloc: insufficient memory for %d messages\n"), 797c478bd9Sstevel@tonic-gate space); 807c478bd9Sstevel@tonic-gate exit(1); 817c478bd9Sstevel@tonic-gate /* NOTREACHED */ 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate dot = message; 847c478bd9Sstevel@tonic-gate } else { 857c478bd9Sstevel@tonic-gate newmail = 1; 867c478bd9Sstevel@tonic-gate offset = fsize(otf); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate s = 0L; 897c478bd9Sstevel@tonic-gate l = 0; 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * Set default flags. When reading from 927c478bd9Sstevel@tonic-gate * a folder, assume the message has been 937c478bd9Sstevel@tonic-gate * previously read. 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate if (edit) 967c478bd9Sstevel@tonic-gate flag = MUSED|MREAD; 977c478bd9Sstevel@tonic-gate else 987c478bd9Sstevel@tonic-gate flag = MUSED|MNEW; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate inhead = 0; 1017c478bd9Sstevel@tonic-gate while ((n = getln(linebuf, sizeof (linebuf), ibuf)) > 0) { 1027c478bd9Sstevel@tonic-gate if (!newline) { 1037c478bd9Sstevel@tonic-gate goto putout; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate top: 1067c478bd9Sstevel@tonic-gate hdr = inhead && (headerp(linebuf) || 1077c478bd9Sstevel@tonic-gate (linebuf[0] == ' ' || linebuf[0] == '\t')); 1087c478bd9Sstevel@tonic-gate if (!hdr && cflg) { /* nonheader, Content-length seen */ 1097c478bd9Sstevel@tonic-gate if (clen > 0 && clen < n) { /* read too much */ 1107c478bd9Sstevel@tonic-gate /* 1117c478bd9Sstevel@tonic-gate * NB: this only can happen if there is a 1127c478bd9Sstevel@tonic-gate * small content that is NOT \n terminated 1137c478bd9Sstevel@tonic-gate * and has no leading blank line, i.e., never. 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate if (fwrite(linebuf, 1, (int)clen, otf) != 1167c478bd9Sstevel@tonic-gate clen) { 1177c478bd9Sstevel@tonic-gate fclose(ibuf); 1187c478bd9Sstevel@tonic-gate fflush(otf); 1197c478bd9Sstevel@tonic-gate } else { 1207c478bd9Sstevel@tonic-gate l += linecount(linebuf, clen); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate offset += clen; 1237c478bd9Sstevel@tonic-gate s += clen; 1247c478bd9Sstevel@tonic-gate n -= (int)clen; 1257c478bd9Sstevel@tonic-gate /* shift line to the left, copy null as well */ 1267c478bd9Sstevel@tonic-gate memcpy(linebuf, linebuf+clen, n+1); 1277c478bd9Sstevel@tonic-gate cflg = 0; 1287c478bd9Sstevel@tonic-gate message[msgCount-1].m_clen = clen + 1; 1297c478bd9Sstevel@tonic-gate blankline = 1; 1307c478bd9Sstevel@tonic-gate StartNewMsg = TRUE; 1317c478bd9Sstevel@tonic-gate goto top; 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate /* here, clen == 0 or clen >= n */ 1347c478bd9Sstevel@tonic-gate if (n == 1 && linebuf[0] == '\n') { 1357c478bd9Sstevel@tonic-gate /* leading empty line */ 1367c478bd9Sstevel@tonic-gate clen++; /* cheat */ 1377c478bd9Sstevel@tonic-gate inhead = 0; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate offset += clen; 1407c478bd9Sstevel@tonic-gate s += (long)clen; 1417c478bd9Sstevel@tonic-gate message[msgCount-1].m_clen = clen; 1427c478bd9Sstevel@tonic-gate for (;;) { 1437c478bd9Sstevel@tonic-gate if (fwrite(linebuf, 1, n, otf) != n) { 1447c478bd9Sstevel@tonic-gate fclose(ibuf); 1457c478bd9Sstevel@tonic-gate fflush(otf); 1467c478bd9Sstevel@tonic-gate } else { 1477c478bd9Sstevel@tonic-gate l += linecount(linebuf, n); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate clen -= n; 1507c478bd9Sstevel@tonic-gate if (clen <= 0) { 1517c478bd9Sstevel@tonic-gate break; 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate n = clen < sizeof (linebuf) ? 1547c478bd9Sstevel@tonic-gate (int)clen : (int)sizeof (linebuf); 1557c478bd9Sstevel@tonic-gate if ((n = fread(linebuf, 1, n, ibuf)) <= 0) { 1567c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 1577c478bd9Sstevel@tonic-gate "%s:\tYour mailfile was found to be corrupted.\n"), 1587c478bd9Sstevel@tonic-gate progname); 1597c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 1607c478bd9Sstevel@tonic-gate "\t(Unexpected end-of-file).\n")); 1617c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 1627c478bd9Sstevel@tonic-gate "\tMessage #%d may be truncated.\n\n"), 1637c478bd9Sstevel@tonic-gate msgCount); 1647c478bd9Sstevel@tonic-gate offset -= clen; 1657c478bd9Sstevel@tonic-gate s -= clen; 1667c478bd9Sstevel@tonic-gate clen = 0; /* stop the loop */ 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate /* All done, go to top for next message */ 1707c478bd9Sstevel@tonic-gate cflg = 0; 1717c478bd9Sstevel@tonic-gate blankline = 1; 1727c478bd9Sstevel@tonic-gate StartNewMsg = TRUE; 1737c478bd9Sstevel@tonic-gate continue; 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* Look for a From line that starts a new message */ 1777c478bd9Sstevel@tonic-gate if (blankline && linebuf[0] == 'F' && ishead(linebuf)) { 1787c478bd9Sstevel@tonic-gate if (msgCount > 0 && !newmail) { 1797c478bd9Sstevel@tonic-gate message[msgCount-1].m_size = s; 1807c478bd9Sstevel@tonic-gate message[msgCount-1].m_lines = l; 1817c478bd9Sstevel@tonic-gate message[msgCount-1].m_flag = flag; 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate if (msgCount >= space) { 1847c478bd9Sstevel@tonic-gate /* 1857c478bd9Sstevel@tonic-gate * Limit the speed at which the 1867c478bd9Sstevel@tonic-gate * allocated space grows. 1877c478bd9Sstevel@tonic-gate */ 1887c478bd9Sstevel@tonic-gate if (space < 512) 1897c478bd9Sstevel@tonic-gate space = space*2; 1907c478bd9Sstevel@tonic-gate else 1917c478bd9Sstevel@tonic-gate space += 512; 1927c478bd9Sstevel@tonic-gate errno = 0; 1937c478bd9Sstevel@tonic-gate Odot = dot - &(message[0]); 1947c478bd9Sstevel@tonic-gate message = (struct message *) 1957c478bd9Sstevel@tonic-gate realloc(message, 1967c478bd9Sstevel@tonic-gate space*(sizeof (struct message))); 1977c478bd9Sstevel@tonic-gate if (message == NULL) { 1987c478bd9Sstevel@tonic-gate perror("realloc failed"); 1997c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 2007c478bd9Sstevel@tonic-gate "realloc: insufficient memory for %d messages\n"), 2017c478bd9Sstevel@tonic-gate space); 2027c478bd9Sstevel@tonic-gate exit(1); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate dot = &message[Odot]; 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate message[msgCount].m_offset = offset; 2077c478bd9Sstevel@tonic-gate message[msgCount].m_text = TRUE; 2087c478bd9Sstevel@tonic-gate message[msgCount].m_clen = 0; 2097c478bd9Sstevel@tonic-gate newmail = 0; 2107c478bd9Sstevel@tonic-gate msgCount++; 2117c478bd9Sstevel@tonic-gate if (edit) 2127c478bd9Sstevel@tonic-gate flag = MUSED|MREAD; 2137c478bd9Sstevel@tonic-gate else 2147c478bd9Sstevel@tonic-gate flag = MUSED|MNEW; 2157c478bd9Sstevel@tonic-gate inhead = 1; 2167c478bd9Sstevel@tonic-gate s = 0L; 2177c478bd9Sstevel@tonic-gate l = 0; 2187c478bd9Sstevel@tonic-gate StartNewMsg = FALSE; 2197c478bd9Sstevel@tonic-gate ToldUser = FALSE; 2207c478bd9Sstevel@tonic-gate goto putout; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* if didn't get a header line, we're no longer in the header */ 2247c478bd9Sstevel@tonic-gate if (!hdr) 2257c478bd9Sstevel@tonic-gate inhead = 0; 2267c478bd9Sstevel@tonic-gate if (!inhead) 2277c478bd9Sstevel@tonic-gate goto putout; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * Look for Status: line. Do quick check for second character, 2317c478bd9Sstevel@tonic-gate * many headers start with "S" but few have "t" as second char. 2327c478bd9Sstevel@tonic-gate */ 2337c478bd9Sstevel@tonic-gate if ((linebuf[1] == 't' || linebuf[1] == 'T') && 2347c478bd9Sstevel@tonic-gate ishfield(linebuf, "status")) { 2357c478bd9Sstevel@tonic-gate cp = hcontents(linebuf); 2367c478bd9Sstevel@tonic-gate flag = MUSED|MNEW; 2377c478bd9Sstevel@tonic-gate if (strchr(cp, 'R')) 2387c478bd9Sstevel@tonic-gate flag |= MREAD; 2397c478bd9Sstevel@tonic-gate if (strchr(cp, 'O')) 2407c478bd9Sstevel@tonic-gate flag &= ~MNEW; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate /* 2437c478bd9Sstevel@tonic-gate * Look for Content-Length and Content-Type headers. Like 2447c478bd9Sstevel@tonic-gate * above, do a quick check for the "-", which is rare. 2457c478bd9Sstevel@tonic-gate */ 2467c478bd9Sstevel@tonic-gate if (linebuf[7] == '-') { 2477c478bd9Sstevel@tonic-gate if (ishfield(linebuf, "content-length")) { 2487c478bd9Sstevel@tonic-gate if (!cflg) { 2497c478bd9Sstevel@tonic-gate clen = atol(hcontents(linebuf)); 2507c478bd9Sstevel@tonic-gate cflg = clen >= 0; 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate } else if (ishfield(linebuf, "content-type")) { 2537c478bd9Sstevel@tonic-gate char word[LINESIZE]; 2547c478bd9Sstevel@tonic-gate char *cp2; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate cp = hcontents(linebuf); 2577c478bd9Sstevel@tonic-gate cp2 = word; 2587c478bd9Sstevel@tonic-gate while (!isspace(*cp)) 2597c478bd9Sstevel@tonic-gate *cp2++ = *cp++; 2607c478bd9Sstevel@tonic-gate *cp2 = '\0'; 2617c478bd9Sstevel@tonic-gate if (icequal(word, "binary")) 2627c478bd9Sstevel@tonic-gate message[msgCount-1].m_text = FALSE; 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate putout: 2667c478bd9Sstevel@tonic-gate offset += n; 2677c478bd9Sstevel@tonic-gate s += (long)n; 2687c478bd9Sstevel@tonic-gate if (fwrite(linebuf, 1, n, otf) != n) { 2697c478bd9Sstevel@tonic-gate fclose(ibuf); 2707c478bd9Sstevel@tonic-gate fflush(otf); 2717c478bd9Sstevel@tonic-gate } else { 2727c478bd9Sstevel@tonic-gate l++; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate if (ferror(otf)) { 2757c478bd9Sstevel@tonic-gate perror("/tmp"); 2767c478bd9Sstevel@tonic-gate exit(1); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate if (msgCount == 0) { 2797c478bd9Sstevel@tonic-gate fclose(ibuf); 2807c478bd9Sstevel@tonic-gate fflush(otf); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate if (linebuf[n-1] == '\n') { 2837c478bd9Sstevel@tonic-gate blankline = newline && n == 1; 2847c478bd9Sstevel@tonic-gate newline = 1; 2857c478bd9Sstevel@tonic-gate if (n == 1) { 2867c478bd9Sstevel@tonic-gate /* Blank line. Skip StartNewMsg check below */ 2877c478bd9Sstevel@tonic-gate continue; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate } else { 2907c478bd9Sstevel@tonic-gate newline = 0; 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate if (StartNewMsg && !ToldUser) { 2937c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 2947c478bd9Sstevel@tonic-gate "%s:\tYour mailfile was found to be corrupted\n"), 2957c478bd9Sstevel@tonic-gate progname); 2967c478bd9Sstevel@tonic-gate fprintf(stderr, 2977c478bd9Sstevel@tonic-gate gettext("\t(Content-length mismatch).\n")); 2987c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 2997c478bd9Sstevel@tonic-gate "\tMessage #%d may be truncated,\n"), msgCount); 3007c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3017c478bd9Sstevel@tonic-gate "\twith another message concatenated to it.\n\n")); 3027c478bd9Sstevel@tonic-gate ToldUser = TRUE; 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if (n == 0) { 3077c478bd9Sstevel@tonic-gate fflush(otf); 3087c478bd9Sstevel@tonic-gate if (fferror(otf)) { 3097c478bd9Sstevel@tonic-gate perror("/tmp"); 3107c478bd9Sstevel@tonic-gate exit(1); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate if (msgCount) { 3137c478bd9Sstevel@tonic-gate message[msgCount-1].m_size = s; 3147c478bd9Sstevel@tonic-gate message[msgCount-1].m_lines = l; 3157c478bd9Sstevel@tonic-gate message[msgCount-1].m_flag = flag; 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate fclose(ibuf); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * Compute the content length of a message and set it into m_clen. 3237c478bd9Sstevel@tonic-gate */ 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate void 3267c478bd9Sstevel@tonic-gate setclen(register struct message *mp) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate long c; 3297c478bd9Sstevel@tonic-gate FILE *ibuf; 3307c478bd9Sstevel@tonic-gate char line[LINESIZE]; 3317c478bd9Sstevel@tonic-gate int fline, nread; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate ibuf = setinput(mp); 3347c478bd9Sstevel@tonic-gate c = mp->m_size; 3357c478bd9Sstevel@tonic-gate fline = 1; 3367c478bd9Sstevel@tonic-gate while (c > 0L) { 3377c478bd9Sstevel@tonic-gate nread = getln(line, sizeof (line), ibuf); 3387c478bd9Sstevel@tonic-gate c -= nread; 3397c478bd9Sstevel@tonic-gate /* 3407c478bd9Sstevel@tonic-gate * First line is the From line, so no headers 3417c478bd9Sstevel@tonic-gate * there to worry about. 3427c478bd9Sstevel@tonic-gate */ 3437c478bd9Sstevel@tonic-gate if (fline) { 3447c478bd9Sstevel@tonic-gate fline = 0; 3457c478bd9Sstevel@tonic-gate continue; 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * If line is blank, we've reached end of headers. 3497c478bd9Sstevel@tonic-gate */ 3507c478bd9Sstevel@tonic-gate if (line[0] == '\n') 3517c478bd9Sstevel@tonic-gate break; 3527c478bd9Sstevel@tonic-gate /* 3537c478bd9Sstevel@tonic-gate * If this line is a continuation 3547c478bd9Sstevel@tonic-gate * of a previous header field, keep going. 3557c478bd9Sstevel@tonic-gate */ 3567c478bd9Sstevel@tonic-gate if (isspace(line[0])) 3577c478bd9Sstevel@tonic-gate continue; 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * If we are no longer looking at real 3607c478bd9Sstevel@tonic-gate * header lines, we're done. 3617c478bd9Sstevel@tonic-gate * This happens in uucp style mail where 3627c478bd9Sstevel@tonic-gate * there are no headers at all. 3637c478bd9Sstevel@tonic-gate */ 3647c478bd9Sstevel@tonic-gate if (!headerp(line)) { 3657c478bd9Sstevel@tonic-gate c += nread; 3667c478bd9Sstevel@tonic-gate break; 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate if (c == 0) 3707c478bd9Sstevel@tonic-gate c = 1; 3717c478bd9Sstevel@tonic-gate mp->m_clen = c; 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate static int 3757c478bd9Sstevel@tonic-gate getln(char *line, int max, FILE *f) 3767c478bd9Sstevel@tonic-gate { 3777c478bd9Sstevel@tonic-gate register int c; 3787c478bd9Sstevel@tonic-gate register char *cp, *ecp; 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate cp = line; 3817c478bd9Sstevel@tonic-gate ecp = cp + max - 1; 3827c478bd9Sstevel@tonic-gate while (cp < ecp && (c = getc(f)) != EOF) 3837c478bd9Sstevel@tonic-gate if ((*cp++ = (char)c) == '\n') 3847c478bd9Sstevel@tonic-gate break; 3857c478bd9Sstevel@tonic-gate *cp = '\0'; 3867c478bd9Sstevel@tonic-gate return (cp - line); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* 3907c478bd9Sstevel@tonic-gate * Read up a line from the specified input into the line 3917c478bd9Sstevel@tonic-gate * buffer. Return the number of characters read. Do not 3927c478bd9Sstevel@tonic-gate * include the newline at the end. 3937c478bd9Sstevel@tonic-gate */ 3947c478bd9Sstevel@tonic-gate 395*6c83d09fSrobbin int 3967c478bd9Sstevel@tonic-gate readline(FILE *ibuf, char *linebuf) 3977c478bd9Sstevel@tonic-gate { 3987c478bd9Sstevel@tonic-gate register char *cp; 3997c478bd9Sstevel@tonic-gate register int c; 4007c478bd9Sstevel@tonic-gate int seennulls = 0; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate clearerr(ibuf); 4037c478bd9Sstevel@tonic-gate c = getc(ibuf); 4047c478bd9Sstevel@tonic-gate for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) { 4057c478bd9Sstevel@tonic-gate if (c == 0) { 4067c478bd9Sstevel@tonic-gate if (!seennulls) { 4077c478bd9Sstevel@tonic-gate fprintf(stderr, 4087c478bd9Sstevel@tonic-gate gettext("mailx: NUL changed to @\n")); 4097c478bd9Sstevel@tonic-gate seennulls++; 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate c = '@'; 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate if (cp - linebuf < LINESIZE-2) 4147c478bd9Sstevel@tonic-gate *cp++ = (char)c; 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate *cp = 0; 4177c478bd9Sstevel@tonic-gate if (c == EOF && cp == linebuf) 4187c478bd9Sstevel@tonic-gate return (0); 4197c478bd9Sstevel@tonic-gate return (cp - linebuf + 1); 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate /* 4237c478bd9Sstevel@tonic-gate * linecount - determine the number of lines in a printable file. 4247c478bd9Sstevel@tonic-gate */ 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate static int 4277c478bd9Sstevel@tonic-gate linecount(char *lp, long size) 4287c478bd9Sstevel@tonic-gate { 4297c478bd9Sstevel@tonic-gate register char *cp, *ecp; 4307c478bd9Sstevel@tonic-gate register int count; 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate count = 0; 4337c478bd9Sstevel@tonic-gate cp = lp; 4347c478bd9Sstevel@tonic-gate ecp = cp + size; 4357c478bd9Sstevel@tonic-gate while (cp < ecp) 4367c478bd9Sstevel@tonic-gate if (*cp++ == '\n') 4377c478bd9Sstevel@tonic-gate count++; 4387c478bd9Sstevel@tonic-gate return (count); 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate /* 4427c478bd9Sstevel@tonic-gate * Return a file buffer all ready to read up the 4437c478bd9Sstevel@tonic-gate * passed message pointer. 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate FILE * 4477c478bd9Sstevel@tonic-gate setinput(register struct message *mp) 4487c478bd9Sstevel@tonic-gate { 4497c478bd9Sstevel@tonic-gate fflush(otf); 4507c478bd9Sstevel@tonic-gate if (fseek(itf, mp->m_offset, 0) < 0) { 4517c478bd9Sstevel@tonic-gate perror("fseek"); 4527c478bd9Sstevel@tonic-gate panic("temporary file seek"); 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate return (itf); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate /* 4597c478bd9Sstevel@tonic-gate * Delete a file, but only if the file is a plain file. 4607c478bd9Sstevel@tonic-gate */ 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate int 4637c478bd9Sstevel@tonic-gate removefile(char name[]) 4647c478bd9Sstevel@tonic-gate { 4657c478bd9Sstevel@tonic-gate struct stat statb; 4667c478bd9Sstevel@tonic-gate extern int errno; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate if (stat(name, &statb) < 0) 4697c478bd9Sstevel@tonic-gate if (errno == ENOENT) 4707c478bd9Sstevel@tonic-gate return (0); /* it's already gone, no error */ 4717c478bd9Sstevel@tonic-gate else 4727c478bd9Sstevel@tonic-gate return (-1); 4737c478bd9Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) != S_IFREG) { 4747c478bd9Sstevel@tonic-gate errno = EISDIR; 4757c478bd9Sstevel@tonic-gate return (-1); 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate return (unlink(name)); 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * Terminate an editing session by attempting to write out the user's 4827c478bd9Sstevel@tonic-gate * file from the temporary. Save any new stuff appended to the file. 4837c478bd9Sstevel@tonic-gate */ 4847c478bd9Sstevel@tonic-gate int 4857c478bd9Sstevel@tonic-gate edstop( 4867c478bd9Sstevel@tonic-gate int noremove /* don't allow the file to be removed, trunc instead */ 4877c478bd9Sstevel@tonic-gate ) 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate register int gotcha, c; 4907c478bd9Sstevel@tonic-gate register struct message *mp; 4917c478bd9Sstevel@tonic-gate FILE *obuf, *ibuf, *tbuf = 0, *readstat; 4927c478bd9Sstevel@tonic-gate struct stat statb; 4937c478bd9Sstevel@tonic-gate char tempname[STSIZ], *id; 4947c478bd9Sstevel@tonic-gate int tmpfd = -1; 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate if (readonly) 4977c478bd9Sstevel@tonic-gate return (0); 4987c478bd9Sstevel@tonic-gate holdsigs(); 4997c478bd9Sstevel@tonic-gate if (Tflag != NOSTR) { 5007c478bd9Sstevel@tonic-gate if ((readstat = fopen(Tflag, "w")) == NULL) 5017c478bd9Sstevel@tonic-gate Tflag = NOSTR; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) { 5047c478bd9Sstevel@tonic-gate if (mp->m_flag & MNEW) { 5057c478bd9Sstevel@tonic-gate mp->m_flag &= ~MNEW; 5067c478bd9Sstevel@tonic-gate mp->m_flag |= MSTATUS; 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate if (mp->m_flag & (MODIFY|MDELETED|MSTATUS)) 5097c478bd9Sstevel@tonic-gate gotcha++; 5107c478bd9Sstevel@tonic-gate if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) { 5117c478bd9Sstevel@tonic-gate if ((id = hfield("article-id", mp, addone)) != NOSTR) 5127c478bd9Sstevel@tonic-gate fprintf(readstat, "%s\n", id); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate if (Tflag != NOSTR) 5167c478bd9Sstevel@tonic-gate fclose(readstat); 5177c478bd9Sstevel@tonic-gate if (!gotcha || Tflag != NOSTR) 5187c478bd9Sstevel@tonic-gate goto done; 5197c478bd9Sstevel@tonic-gate if ((ibuf = fopen(editfile, "r+")) == NULL) { 5207c478bd9Sstevel@tonic-gate perror(editfile); 5217c478bd9Sstevel@tonic-gate relsesigs(); 5227c478bd9Sstevel@tonic-gate longjmp(srbuf, 1); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate lock(ibuf, "r+", 1); 5257c478bd9Sstevel@tonic-gate if (fstat(fileno(ibuf), &statb) >= 0 && statb.st_size > mailsize) { 5267c478bd9Sstevel@tonic-gate nstrcpy(tempname, STSIZ, "/tmp/mboxXXXXXX"); 5277c478bd9Sstevel@tonic-gate if ((tmpfd = mkstemp(tempname)) == -1) { 5287c478bd9Sstevel@tonic-gate perror(tempname); 5297c478bd9Sstevel@tonic-gate fclose(ibuf); 5307c478bd9Sstevel@tonic-gate relsesigs(); 5317c478bd9Sstevel@tonic-gate longjmp(srbuf, 1); 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate if ((obuf = fdopen(tmpfd, "w")) == NULL) { 5347c478bd9Sstevel@tonic-gate perror(tempname); 5357c478bd9Sstevel@tonic-gate fclose(ibuf); 5367c478bd9Sstevel@tonic-gate removefile(tempname); 5377c478bd9Sstevel@tonic-gate relsesigs(); 5387c478bd9Sstevel@tonic-gate (void) close(tmpfd); 5397c478bd9Sstevel@tonic-gate longjmp(srbuf, 1); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate fseek(ibuf, mailsize, 0); 5427c478bd9Sstevel@tonic-gate while ((c = getc(ibuf)) != EOF) 5437c478bd9Sstevel@tonic-gate putc(c, obuf); 5447c478bd9Sstevel@tonic-gate fclose(obuf); 5457c478bd9Sstevel@tonic-gate if ((tbuf = fopen(tempname, "r")) == NULL) { 5467c478bd9Sstevel@tonic-gate perror(tempname); 5477c478bd9Sstevel@tonic-gate fclose(ibuf); 5487c478bd9Sstevel@tonic-gate removefile(tempname); 5497c478bd9Sstevel@tonic-gate relsesigs(); 5507c478bd9Sstevel@tonic-gate longjmp(srbuf, 1); 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate removefile(tempname); 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate if ((obuf = fopen(editfile, "r+")) == NULL) { 5557c478bd9Sstevel@tonic-gate if ((obuf = fopen(editfile, "w")) == NULL) { 5567c478bd9Sstevel@tonic-gate perror(editfile); 5577c478bd9Sstevel@tonic-gate fclose(ibuf); 5587c478bd9Sstevel@tonic-gate if (tbuf) 5597c478bd9Sstevel@tonic-gate fclose(tbuf); 5607c478bd9Sstevel@tonic-gate relsesigs(); 5617c478bd9Sstevel@tonic-gate longjmp(srbuf, 1); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate printf("\"%s\" ", editfile); 5657c478bd9Sstevel@tonic-gate flush(); 5667c478bd9Sstevel@tonic-gate c = 0; 5677c478bd9Sstevel@tonic-gate for (mp = &message[0]; mp < &message[msgCount]; mp++) { 5687c478bd9Sstevel@tonic-gate if ((mp->m_flag & MDELETED) != 0) 5697c478bd9Sstevel@tonic-gate continue; 5707c478bd9Sstevel@tonic-gate c++; 5717c478bd9Sstevel@tonic-gate if (msend(mp, obuf, 0, fputs) < 0) { 5727c478bd9Sstevel@tonic-gate perror(editfile); 5737c478bd9Sstevel@tonic-gate fclose(ibuf); 5747c478bd9Sstevel@tonic-gate fclose(obuf); 5757c478bd9Sstevel@tonic-gate if (tbuf) 5767c478bd9Sstevel@tonic-gate fclose(tbuf); 5777c478bd9Sstevel@tonic-gate relsesigs(); 5787c478bd9Sstevel@tonic-gate longjmp(srbuf, 1); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate gotcha = (c == 0 && tbuf == NULL); 5827c478bd9Sstevel@tonic-gate if (tbuf != NULL) { 5837c478bd9Sstevel@tonic-gate while ((c = getc(tbuf)) != EOF) 5847c478bd9Sstevel@tonic-gate putc(c, obuf); 5857c478bd9Sstevel@tonic-gate fclose(tbuf); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate fflush(obuf); 5887c478bd9Sstevel@tonic-gate if (fferror(obuf)) { 5897c478bd9Sstevel@tonic-gate perror(editfile); 5907c478bd9Sstevel@tonic-gate fclose(ibuf); 5917c478bd9Sstevel@tonic-gate fclose(obuf); 5927c478bd9Sstevel@tonic-gate relsesigs(); 5937c478bd9Sstevel@tonic-gate longjmp(srbuf, 1); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate if (gotcha && !noremove && (value("keep") == NOSTR)) { 5967c478bd9Sstevel@tonic-gate removefile(editfile); 5977c478bd9Sstevel@tonic-gate printf(gettext("removed.\n")); 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate else 6007c478bd9Sstevel@tonic-gate printf(gettext("updated.\n")); 6017c478bd9Sstevel@tonic-gate fclose(ibuf); 6027c478bd9Sstevel@tonic-gate trunc(obuf); 6037c478bd9Sstevel@tonic-gate fclose(obuf); 6047c478bd9Sstevel@tonic-gate flush(); 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate done: 6077c478bd9Sstevel@tonic-gate relsesigs(); 6087c478bd9Sstevel@tonic-gate return (1); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate #ifndef OLD_BSD_SIGS 6127c478bd9Sstevel@tonic-gate static int sigdepth = 0; /* depth of holdsigs() */ 6137c478bd9Sstevel@tonic-gate #ifdef VMUNIX 6147c478bd9Sstevel@tonic-gate static int omask = 0; 6157c478bd9Sstevel@tonic-gate #else 6167c478bd9Sstevel@tonic-gate static sigset_t mask, omask; 6177c478bd9Sstevel@tonic-gate #endif 6187c478bd9Sstevel@tonic-gate #endif 6197c478bd9Sstevel@tonic-gate /* 6207c478bd9Sstevel@tonic-gate * Hold signals SIGHUP - SIGQUIT. 6217c478bd9Sstevel@tonic-gate */ 6227c478bd9Sstevel@tonic-gate void 6237c478bd9Sstevel@tonic-gate holdsigs(void) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate #ifndef OLD_BSD_SIGS 6267c478bd9Sstevel@tonic-gate if (sigdepth++ == 0) { 6277c478bd9Sstevel@tonic-gate #ifdef VMUNIX 6287c478bd9Sstevel@tonic-gate omask = sigblock(sigmask(SIGHUP) | 6297c478bd9Sstevel@tonic-gate sigmask(SIGINT)|sigmask(SIGQUIT)); 6307c478bd9Sstevel@tonic-gate #else 6317c478bd9Sstevel@tonic-gate sigemptyset(&mask); 6327c478bd9Sstevel@tonic-gate sigaddset(&mask, SIGHUP); 6337c478bd9Sstevel@tonic-gate sigaddset(&mask, SIGINT); 6347c478bd9Sstevel@tonic-gate sigaddset(&mask, SIGQUIT); 6357c478bd9Sstevel@tonic-gate sigprocmask(SIG_BLOCK, &mask, &omask); 6367c478bd9Sstevel@tonic-gate #endif 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate #else 6397c478bd9Sstevel@tonic-gate sighold(SIGHUP); 6407c478bd9Sstevel@tonic-gate sighold(SIGINT); 6417c478bd9Sstevel@tonic-gate sighold(SIGQUIT); 6427c478bd9Sstevel@tonic-gate #endif 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * Release signals SIGHUP - SIGQUIT 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate void 6497c478bd9Sstevel@tonic-gate relsesigs(void) 6507c478bd9Sstevel@tonic-gate { 6517c478bd9Sstevel@tonic-gate #ifndef OLD_BSD_SIGS 6527c478bd9Sstevel@tonic-gate if (--sigdepth == 0) 6537c478bd9Sstevel@tonic-gate #ifdef VMUNIX 6547c478bd9Sstevel@tonic-gate sigsetmask(omask); 6557c478bd9Sstevel@tonic-gate #else 6567c478bd9Sstevel@tonic-gate sigprocmask(SIG_SETMASK, &omask, NULL); 6577c478bd9Sstevel@tonic-gate #endif 6587c478bd9Sstevel@tonic-gate #else 6597c478bd9Sstevel@tonic-gate sigrelse(SIGHUP); 6607c478bd9Sstevel@tonic-gate sigrelse(SIGINT); 6617c478bd9Sstevel@tonic-gate sigrelse(SIGQUIT); 6627c478bd9Sstevel@tonic-gate #endif 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate 6657c478bd9Sstevel@tonic-gate #if !defined(OLD_BSD_SIGS) && !defined(VMUNIX) 6667c478bd9Sstevel@tonic-gate void 6677c478bd9Sstevel@tonic-gate (*sigset(int sig, void (*act)(int)))(int) 6687c478bd9Sstevel@tonic-gate { 6697c478bd9Sstevel@tonic-gate struct sigaction sa, osa; 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate sa.sa_handler = (void (*)())act; 6727c478bd9Sstevel@tonic-gate sigemptyset(&sa.sa_mask); 6737c478bd9Sstevel@tonic-gate sa.sa_flags = SA_RESTART; 6747c478bd9Sstevel@tonic-gate if (sigaction(sig, &sa, &osa) < 0) 6757c478bd9Sstevel@tonic-gate return ((void (*)(int))-1); 6767c478bd9Sstevel@tonic-gate return ((void (*)(int))osa.sa_handler); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate #endif 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* 6817c478bd9Sstevel@tonic-gate * Flush the standard output. 6827c478bd9Sstevel@tonic-gate */ 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate void 6857c478bd9Sstevel@tonic-gate flush(void) 6867c478bd9Sstevel@tonic-gate { 6877c478bd9Sstevel@tonic-gate fflush(stdout); 6887c478bd9Sstevel@tonic-gate fflush(stderr); 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate /* 6927c478bd9Sstevel@tonic-gate * Determine the size of the file possessed by 6937c478bd9Sstevel@tonic-gate * the passed buffer. 6947c478bd9Sstevel@tonic-gate */ 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate off_t 6977c478bd9Sstevel@tonic-gate fsize(FILE *iob) 6987c478bd9Sstevel@tonic-gate { 6997c478bd9Sstevel@tonic-gate register int f; 7007c478bd9Sstevel@tonic-gate struct stat sbuf; 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate f = fileno(iob); 7037c478bd9Sstevel@tonic-gate if (fstat(f, &sbuf) < 0) 7047c478bd9Sstevel@tonic-gate return (0); 7057c478bd9Sstevel@tonic-gate return (sbuf.st_size); 7067c478bd9Sstevel@tonic-gate } 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate /* 7097c478bd9Sstevel@tonic-gate * Check for either a stdio recognized error, or 7107c478bd9Sstevel@tonic-gate * a possibly delayed write error (in case it's 7117c478bd9Sstevel@tonic-gate * an NFS file, for instance). 7127c478bd9Sstevel@tonic-gate */ 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate int 7157c478bd9Sstevel@tonic-gate fferror(FILE *iob) 7167c478bd9Sstevel@tonic-gate { 7177c478bd9Sstevel@tonic-gate return (ferror(iob) || fsync(fileno(iob)) < 0); 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate /* 7217c478bd9Sstevel@tonic-gate * Take a file name, possibly with shell meta characters 7227c478bd9Sstevel@tonic-gate * in it and expand it by using wordexp(). 7237c478bd9Sstevel@tonic-gate * Return the file name as a dynamic string. 7247c478bd9Sstevel@tonic-gate * If the name cannot be expanded (for whatever reason) 7257c478bd9Sstevel@tonic-gate * return NULL. 7267c478bd9Sstevel@tonic-gate */ 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate char * 7297c478bd9Sstevel@tonic-gate expand(char *name) 7307c478bd9Sstevel@tonic-gate { 7317c478bd9Sstevel@tonic-gate char xname[BUFSIZ]; 7327c478bd9Sstevel@tonic-gate char foldbuf[BUFSIZ]; 7337c478bd9Sstevel@tonic-gate register char *cp; 7347c478bd9Sstevel@tonic-gate register int l; 7357c478bd9Sstevel@tonic-gate wordexp_t wrdexp_buf; 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "expand(%s)=", name); 7387c478bd9Sstevel@tonic-gate cp = strchr(name, '\0') - 1; /* pointer to last char of name */ 7397c478bd9Sstevel@tonic-gate if (isspace(*cp)) { 7407c478bd9Sstevel@tonic-gate /* strip off trailing blanks */ 7417c478bd9Sstevel@tonic-gate while (cp > name && isspace(*cp)) 7427c478bd9Sstevel@tonic-gate cp--; 7437c478bd9Sstevel@tonic-gate l = *++cp; /* save char */ 7447c478bd9Sstevel@tonic-gate *cp = '\0'; 7457c478bd9Sstevel@tonic-gate name = savestr(name); 7467c478bd9Sstevel@tonic-gate *cp = (char)l; /* restore char */ 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate if (name[0] == '+' && getfold(foldbuf) >= 0) { 7497c478bd9Sstevel@tonic-gate snprintf(xname, sizeof (xname), "%s/%s", foldbuf, name + 1); 7507c478bd9Sstevel@tonic-gate cp = safeexpand(savestr(xname)); 7517c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "%s\n", cp); 7527c478bd9Sstevel@tonic-gate return (cp); 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate if (!anyof(name, "~{[*?$`'\"\\")) { 7557c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "%s\n", name); 7567c478bd9Sstevel@tonic-gate return (name); 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate if (wordexp(name, &wrdexp_buf, 0) != 0) { 7597c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Syntax error in \"%s\"\n"), name); 7607c478bd9Sstevel@tonic-gate fflush(stderr); 7617c478bd9Sstevel@tonic-gate return (NOSTR); 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate if (wrdexp_buf.we_wordc > 1) { 7647c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("\"%s\": Ambiguous\n"), name); 7657c478bd9Sstevel@tonic-gate fflush(stderr); 7667c478bd9Sstevel@tonic-gate return (NOSTR); 7677c478bd9Sstevel@tonic-gate } 7687c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "%s\n", wrdexp_buf.we_wordv[0]); 7697c478bd9Sstevel@tonic-gate return (savestr(wrdexp_buf.we_wordv[0])); 7707c478bd9Sstevel@tonic-gate } 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate /* 7737c478bd9Sstevel@tonic-gate * Take a file name, possibly with shell meta characters 7747c478bd9Sstevel@tonic-gate * in it and expand it by using "sh -c echo filename" 7757c478bd9Sstevel@tonic-gate * Return the file name as a dynamic string. 7767c478bd9Sstevel@tonic-gate * If the name cannot be expanded (for whatever reason) 7777c478bd9Sstevel@tonic-gate * return the original file name. 7787c478bd9Sstevel@tonic-gate */ 7797c478bd9Sstevel@tonic-gate 7807c478bd9Sstevel@tonic-gate char * 7817c478bd9Sstevel@tonic-gate safeexpand(char name[]) 7827c478bd9Sstevel@tonic-gate { 7837c478bd9Sstevel@tonic-gate char *t = expand(name); 7847c478bd9Sstevel@tonic-gate return (t) ? t : savestr(name); 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate /* 7887c478bd9Sstevel@tonic-gate * Determine the current folder directory name. 7897c478bd9Sstevel@tonic-gate */ 790*6c83d09fSrobbin int 7917c478bd9Sstevel@tonic-gate getfold(char *name) 7927c478bd9Sstevel@tonic-gate { 7937c478bd9Sstevel@tonic-gate char *folder; 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate if ((folder = value("folder")) == NOSTR || *folder == '\0') 7967c478bd9Sstevel@tonic-gate return (-1); 7977c478bd9Sstevel@tonic-gate /* 7987c478bd9Sstevel@tonic-gate * If name looks like a folder name, don't try 7997c478bd9Sstevel@tonic-gate * to expand it, to prevent infinite recursion. 8007c478bd9Sstevel@tonic-gate */ 8017c478bd9Sstevel@tonic-gate if (*folder != '+' && (folder = expand(folder)) == NOSTR || 8027c478bd9Sstevel@tonic-gate *folder == '\0') 8037c478bd9Sstevel@tonic-gate return (-1); 8047c478bd9Sstevel@tonic-gate if (*folder == '/') { 8057c478bd9Sstevel@tonic-gate nstrcpy(name, BUFSIZ, folder); 8067c478bd9Sstevel@tonic-gate } else 8077c478bd9Sstevel@tonic-gate snprintf(name, BUFSIZ, "%s/%s", homedir, folder); 8087c478bd9Sstevel@tonic-gate return (0); 8097c478bd9Sstevel@tonic-gate } 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * A nicer version of Fdopen, which allows us to fclose 8137c478bd9Sstevel@tonic-gate * without losing the open file. 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate FILE * 8177c478bd9Sstevel@tonic-gate Fdopen(int fildes, char *mode) 8187c478bd9Sstevel@tonic-gate { 8197c478bd9Sstevel@tonic-gate register int f; 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate f = dup(fildes); 8227c478bd9Sstevel@tonic-gate if (f < 0) { 8237c478bd9Sstevel@tonic-gate perror("dup"); 8247c478bd9Sstevel@tonic-gate return (NULL); 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate return (fdopen(f, mode)); 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate /* 8307c478bd9Sstevel@tonic-gate * return the filename associated with "s". This function always 8317c478bd9Sstevel@tonic-gate * returns a non-null string (no error checking is done on the receiving end) 8327c478bd9Sstevel@tonic-gate */ 8337c478bd9Sstevel@tonic-gate char * 8347c478bd9Sstevel@tonic-gate Getf(register char *s) 8357c478bd9Sstevel@tonic-gate { 8367c478bd9Sstevel@tonic-gate register char *cp; 8377c478bd9Sstevel@tonic-gate static char defbuf[PATHSIZE]; 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate if (((cp = value(s)) != 0) && *cp) { 8407c478bd9Sstevel@tonic-gate return (safeexpand(cp)); 8417c478bd9Sstevel@tonic-gate } else if (strcmp(s, "MBOX") == 0) { 8427c478bd9Sstevel@tonic-gate snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"), 8437c478bd9Sstevel@tonic-gate "mbox"); 8447c478bd9Sstevel@tonic-gate return (defbuf); 8457c478bd9Sstevel@tonic-gate } else if (strcmp(s, "DEAD") == 0) { 8467c478bd9Sstevel@tonic-gate snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"), 8477c478bd9Sstevel@tonic-gate "dead.letter"); 8487c478bd9Sstevel@tonic-gate return (defbuf); 8497c478bd9Sstevel@tonic-gate } else if (strcmp(s, "MAILRC") == 0) { 8507c478bd9Sstevel@tonic-gate snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"), 8517c478bd9Sstevel@tonic-gate ".mailrc"); 8527c478bd9Sstevel@tonic-gate return (defbuf); 8537c478bd9Sstevel@tonic-gate } else if (strcmp(s, "HOME") == 0) { 8547c478bd9Sstevel@tonic-gate /* no recursion allowed! */ 8557c478bd9Sstevel@tonic-gate return ("."); 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate return ("DEAD"); /* "cannot happen" */ 8587c478bd9Sstevel@tonic-gate } 859