fio.c revision 196c7f05d2deba7404e90ad67f3861185c78ca2d
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License, Version 1.0 only
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * (the "License"). You may not use this file except in compliance
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2014 Joyent, Inc.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 1999 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* All Rights Reserved */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "rcv.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <locale.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <wordexp.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * mailx -- a modified version of a University of California at Berkeley
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * mail program
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * File I/O.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int getln(char *line, int max, FILE *f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int linecount(char *lp, long size);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Set up the input pointers while copying the mail file into
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * /tmp.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesetptr(register FILE *ibuf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int n, newline = 1, blankline = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int StartNewMsg = TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ToldUser = FALSE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte long clen = -1L;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int hdr = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int cflg = 0; /* found Content-length in header */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register long s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte off_t offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char linebuf[LINESIZE];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int inhead, newmail, Odot;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte short flag;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!space) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte msgCount = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte space = 32;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newmail = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message =
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (struct message *)calloc(space, sizeof (struct message));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (message == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "calloc: insufficient memory for %d messages\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte space);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* NOTREACHED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dot = message;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newmail = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset = fsize(otf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s = 0L;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Set default flags. When reading from
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a folder, assume the message has been
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * previously read.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (edit)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flag = MUSED|MREAD;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flag = MUSED|MNEW;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte inhead = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((n = getln(linebuf, sizeof (linebuf), ibuf)) > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!newline) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto putout;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte top:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hdr = inhead && (headerp(linebuf) ||
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (linebuf[0] == ' ' || linebuf[0] == '\t'));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!hdr && cflg) { /* nonheader, Content-length seen */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (clen > 0 && clen < n) { /* read too much */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * NB: this only can happen if there is a
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * small content that is NOT \n terminated
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and has no leading blank line, i.e., never.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fwrite(linebuf, 1, (int)clen, otf) !=
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(otf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l += linecount(linebuf, clen);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset += clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s += clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n -= (int)clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* shift line to the left, copy null as well */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(linebuf, linebuf+clen, n+1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cflg = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_clen = clen + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte blankline = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte StartNewMsg = TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto top;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* here, clen == 0 or clen >= n */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == 1 && linebuf[0] == '\n') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* leading empty line */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clen++; /* cheat */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte inhead = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset += clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s += (long)clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_clen = clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (;;) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fwrite(linebuf, 1, n, otf) != n) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(otf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l += linecount(linebuf, n);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clen -= n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (clen <= 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = clen < sizeof (linebuf) ?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (int)clen : (int)sizeof (linebuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((n = fread(linebuf, 1, n, ibuf)) <= 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%s:\tYour mailfile was found to be corrupted.\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte progname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\t(Unexpected end-of-file).\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\tMessage #%d may be truncated.\n\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte msgCount);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset -= clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s -= clen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clen = 0; /* stop the loop */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* All done, go to top for next message */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cflg = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte blankline = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte StartNewMsg = TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Look for a From line that starts a new message */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (blankline && linebuf[0] == 'F' && is_headline(linebuf)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msgCount > 0 && !newmail) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_size = s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_lines = l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_flag = flag;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msgCount >= space) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Limit the speed at which the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * allocated space grows.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (space < 512)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte space = space*2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte space += 512;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte errno = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Odot = dot - &(message[0]);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message = (struct message *)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte realloc(message,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte space*(sizeof (struct message)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (message == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("realloc failed");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "realloc: insufficient memory for %d messages\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte space);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dot = &message[Odot];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount].m_offset = offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount].m_text = TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount].m_clen = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newmail = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte msgCount++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (edit)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flag = MUSED|MREAD;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flag = MUSED|MNEW;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte inhead = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s = 0L;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte StartNewMsg = FALSE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ToldUser = FALSE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto putout;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if didn't get a header line, we're no longer in the header */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!hdr)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte inhead = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!inhead)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto putout;
6cefaae1e90a413ba01560575bb3998e1a3df40eJack Meng
6cefaae1e90a413ba01560575bb3998e1a3df40eJack Meng /*
6cefaae1e90a413ba01560575bb3998e1a3df40eJack Meng * Look for Status: line. Do quick check for second character,
6cefaae1e90a413ba01560575bb3998e1a3df40eJack Meng * many headers start with "S" but few have "t" as second char.
6cefaae1e90a413ba01560575bb3998e1a3df40eJack Meng */
6cefaae1e90a413ba01560575bb3998e1a3df40eJack Meng if ((linebuf[1] == 't' || linebuf[1] == 'T') &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ishfield(linebuf, "status")) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = hcontents(linebuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flag = MUSED|MNEW;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strchr(cp, 'R'))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flag |= MREAD;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strchr(cp, 'O'))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flag &= ~MNEW;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Look for Content-Length and Content-Type headers. Like
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * above, do a quick check for the "-", which is rare.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (linebuf[7] == '-') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ishfield(linebuf, "content-length")) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!cflg) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clen = atol(hcontents(linebuf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cflg = clen >= 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (ishfield(linebuf, "content-type")) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char word[LINESIZE];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *cp2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = hcontents(linebuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp2 = word;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (!isspace(*cp))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp2++ = *cp++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp2 = '\0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (icequal(word, "binary"))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_text = FALSE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteputout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset += n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s += (long)n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fwrite(linebuf, 1, n, otf) != n) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(otf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ferror(otf)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("/tmp");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msgCount == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(otf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (linebuf[n-1] == '\n') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte blankline = newline && n == 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newline = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Blank line. Skip StartNewMsg check below */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newline = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (StartNewMsg && !ToldUser) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%s:\tYour mailfile was found to be corrupted\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte progname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gettext("\t(Content-length mismatch).\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\tMessage #%d may be truncated,\n"), msgCount);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\twith another message concatenated to it.\n\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ToldUser = TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(otf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fferror(otf)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("/tmp");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msgCount) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_size = s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_lines = l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message[msgCount-1].m_flag = flag;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Compute the content length of a message and set it into m_clen.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesetclen(register struct message *mp)
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng{
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng long c;
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng FILE *ibuf;
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng char line[LINESIZE];
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng int fline, nread;
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng ibuf = setinput(mp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = mp->m_size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fline = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (c > 0L) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nread = getln(line, sizeof (line), ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c -= nread;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * First line is the From line, so no headers
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * there to worry about.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fline) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fline = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If line is blank, we've reached end of headers.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
d233de7e52b23edf30b2270f51e28cda6e08a98cJack Meng if (line[0] == '\n')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If this line is a continuation
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * of a previous header field, keep going.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (isspace(line[0]))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If we are no longer looking at real
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * header lines, we're done.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This happens in uucp style mail where
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * there are no headers at all.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!headerp(line)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c += nread;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mp->m_clen = c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortegetln(char *line, int max, FILE *f)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register char *cp, *ecp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = line;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ecp = cp + max - 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (cp < ecp && (c = getc(f)) != EOF)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((*cp++ = (char)c) == '\n')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp = '\0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (cp - line);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Read up a line from the specified input into the line
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * buffer. Return the number of characters read. Do not
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * include the newline at the end.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortereadline(FILE *ibuf, char *linebuf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int seennulls = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clearerr(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = getc(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!seennulls) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gettext("mailx: NUL changed to @\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte seennulls++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = '@';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (cp - linebuf < LINESIZE-2)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp++ = (char)c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c == EOF && cp == linebuf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (cp - linebuf + 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * linecount - determine the number of lines in a printable file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelinecount(char *lp, long size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register char *cp, *ecp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int count;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte count = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = lp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ecp = cp + size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (cp < ecp)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*cp++ == '\n')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte count++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (count);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Return a file buffer all ready to read up the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * passed message pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteFILE *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesetinput(register struct message *mp)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(otf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fseek(itf, mp->m_offset, 0) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("fseek");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte panic("temporary file seek");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (itf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Delete a file, but only if the file is a plain file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteremovefile(char name[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct stat statb;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte extern int errno;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (stat(name, &statb) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (errno == ENOENT)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0); /* it's already gone, no error */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((statb.st_mode & S_IFMT) != S_IFREG) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte errno = EISDIR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (unlink(name));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Terminate an editing session by attempting to write out the user's
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file from the temporary. Save any new stuff appended to the file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteedstop(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int noremove /* don't allow the file to be removed, trunc instead */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int gotcha, c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register struct message *mp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *obuf, *ibuf, *tbuf = 0, *readstat;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct stat statb;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char tempname[STSIZ], *id;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int tmpfd = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (readonly)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte holdsigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (Tflag != NOSTR) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((readstat = fopen(Tflag, "w")) == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Tflag = NOSTR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (mp->m_flag & MNEW) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mp->m_flag &= ~MNEW;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mp->m_flag |= MSTATUS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gotcha++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((id = hfield("article-id", mp, addone)) != NOSTR)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(readstat, "%s\n", id);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (Tflag != NOSTR)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(readstat);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!gotcha || Tflag != NOSTR)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto done;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((ibuf = fopen(editfile, "r+")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(editfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte longjmp(srbuf, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock(ibuf, "r+", 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fstat(fileno(ibuf), &statb) >= 0 && statb.st_size > mailsize) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nstrcpy(tempname, STSIZ, "/tmp/mboxXXXXXX");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tmpfd = mkstemp(tempname)) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(tempname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte longjmp(srbuf, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((obuf = fdopen(tmpfd, "w")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(tempname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte removefile(tempname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(tmpfd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte longjmp(srbuf, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fseek(ibuf, mailsize, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((c = getc(ibuf)) != EOF)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte putc(c, obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tbuf = fopen(tempname, "r")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(tempname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte removefile(tempname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte longjmp(srbuf, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte removefile(tempname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((obuf = fopen(editfile, "r+")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((obuf = fopen(editfile, "w")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(editfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tbuf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(tbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte longjmp(srbuf, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte printf("\"%s\" ", editfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flush();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (mp = &message[0]; mp < &message[msgCount]; mp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((mp->m_flag & MDELETED) != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msend(mp, obuf, 0, fputs) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(editfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tbuf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(tbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte longjmp(srbuf, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gotcha = (c == 0 && tbuf == NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tbuf != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((c = getc(tbuf)) != EOF)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte putc(c, obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(tbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fferror(obuf)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(editfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte longjmp(srbuf, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (gotcha && !noremove && (value("keep") == NOSTR)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte removefile(editfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte printf(gettext("removed.\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte printf(gettext("updated.\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(ibuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte trunc(obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(obuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte flush();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortedone:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte relsesigs();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef OLD_BSD_SIGS
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int sigdepth = 0; /* depth of holdsigs() */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef VMUNIX
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int omask = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic sigset_t mask, omask;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Hold signals SIGHUP - SIGQUIT.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteholdsigs(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef OLD_BSD_SIGS
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sigdepth++ == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef VMUNIX
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte omask = sigblock(sigmask(SIGHUP) |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigmask(SIGINT)|sigmask(SIGQUIT));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigemptyset(&mask);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigaddset(&mask, SIGHUP);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigaddset(&mask, SIGINT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigaddset(&mask, SIGQUIT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigprocmask(SIG_BLOCK, &mask, &omask);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sighold(SIGHUP);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sighold(SIGINT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sighold(SIGQUIT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Release signals SIGHUP - SIGQUIT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forterelsesigs(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef OLD_BSD_SIGS
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (--sigdepth == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef VMUNIX
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigsetmask(omask);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigprocmask(SIG_SETMASK, &omask, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigrelse(SIGHUP);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigrelse(SIGINT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigrelse(SIGQUIT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if !defined(OLD_BSD_SIGS) && !defined(VMUNIX)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte(*sigset(int sig, void (*act)(int)))(int)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct sigaction sa, osa;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sa.sa_handler = (void (*)())act;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sigemptyset(&sa.sa_mask);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sa.sa_flags = SA_RESTART;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sigaction(sig, &sa, &osa) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((void (*)(int))-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((void (*)(int))osa.sa_handler);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Flush the standard output.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteflush(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(stdout);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(stderr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Determine the size of the file possessed by
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the passed buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteoff_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefsize(FILE *iob)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int f;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct stat sbuf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte f = fileno(iob);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fstat(f, &sbuf) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (sbuf.st_size);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Check for either a stdio recognized error, or
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a possibly delayed write error (in case it's
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * an NFS file, for instance).
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefferror(FILE *iob)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ferror(iob) || fsync(fileno(iob)) < 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Take a file name, possibly with shell meta characters
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * in it and expand it by using wordexp().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Return the file name as a dynamic string.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If the name cannot be expanded (for whatever reason)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return NULL.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteexpand(char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char xname[BUFSIZ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char foldbuf[BUFSIZ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte wordexp_t wrdexp_buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (debug) fprintf(stderr, "expand(%s)=", name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = strchr(name, '\0') - 1; /* pointer to last char of name */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (isspace(*cp)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* strip off trailing blanks */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (cp > name && isspace(*cp))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp--;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l = *++cp; /* save char */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp = '\0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte name = savestr(name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp = (char)l; /* restore char */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (name[0] == '+' && getfold(foldbuf) >= 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(xname, sizeof (xname), "%s/%s", foldbuf, name + 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = safeexpand(savestr(xname));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (debug) fprintf(stderr, "%s\n", cp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (cp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!anyof(name, "~{[*?$`'\"\\")) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (debug) fprintf(stderr, "%s\n", name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (wordexp(name, &wrdexp_buf, 0) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext("Syntax error in \"%s\"\n"), name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(stderr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NOSTR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (wrdexp_buf.we_wordc > 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext("\"%s\": Ambiguous\n"), name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fflush(stderr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NOSTR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (debug) fprintf(stderr, "%s\n", wrdexp_buf.we_wordv[0]);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (savestr(wrdexp_buf.we_wordv[0]));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Take a file name, possibly with shell meta characters
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * in it and expand it by using "sh -c echo filename"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Return the file name as a dynamic string.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If the name cannot be expanded (for whatever reason)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return the original file name.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesafeexpand(char name[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *t = expand(name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (t) ? t : savestr(name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Determine the current folder directory name.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortegetfold(char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *folder;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((folder = value("folder")) == NOSTR || *folder == '\0')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If name looks like a folder name, don't try
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to expand it, to prevent infinite recursion.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*folder != '+' && (folder = expand(folder)) == NOSTR ||
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *folder == '\0')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*folder == '/') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nstrcpy(name, BUFSIZ, folder);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(name, BUFSIZ, "%s/%s", homedir, folder);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * A nicer version of Fdopen, which allows us to fclose
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * without losing the open file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteFILE *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteFdopen(int fildes, char *mode)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int f;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte f = dup(fildes);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (f < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("dup");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NULL);
6cefaae1e90a413ba01560575bb3998e1a3df40eJack Meng }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (fdopen(f, mode));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return the filename associated with "s". This function always
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * returns a non-null string (no error checking is done on the receiving end)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteGetf(register char *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static char defbuf[PATHSIZE];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (((cp = value(s)) != 0) && *cp) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (safeexpand(cp));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (strcmp(s, "MBOX") == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "mbox");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (defbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (strcmp(s, "DEAD") == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "dead.letter");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (defbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (strcmp(s, "MAILRC") == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ".mailrc");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (defbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (strcmp(s, "HOME") == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* no recursion allowed! */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (".");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ("DEAD"); /* "cannot happen" */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte