fio.c revision 196c7f05d2deba7404e90ad67f3861185c78ca2d
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
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 * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2014 Joyent, Inc.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 1999 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* All Rights Reserved */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * mailx -- a modified version of a University of California at Berkeley
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * mail program
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Set up the input pointers while copying the mail file into
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 (struct message *)calloc(space, sizeof (struct message));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "calloc: insufficient memory for %d messages\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* NOTREACHED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Set default flags. When reading from
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a folder, assume the message has been
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * previously read.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((n = getln(linebuf, sizeof (linebuf), ibuf)) > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!hdr && cflg) { /* nonheader, Content-length seen */
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 /* shift line to the left, copy null as well */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* here, clen == 0 or clen >= n */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* leading empty line */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%s:\tYour mailfile was found to be corrupted.\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\t(Unexpected end-of-file).\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\tMessage #%d may be truncated.\n\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* All done, go to top for next message */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Look for a From line that starts a new message */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (blankline && linebuf[0] == 'F' && is_headline(linebuf)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Limit the speed at which the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * allocated space grows.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "realloc: insufficient memory for %d messages\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if didn't get a header line, we're no longer in the header */
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.
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 s += (long)n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Blank line. Skip StartNewMsg check below */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%s:\tYour mailfile was found to be corrupted\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\twith another message concatenated to it.\n\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Compute the content length of a message and set it into m_clen.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (c > 0L) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * First line is the From line, so no headers
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * there to worry about.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If line is blank, we've reached end of headers.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If this line is a continuation
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * of a previous header field, keep going.
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 register int c;
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 register char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cp++ = (char)c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * linecount - determine the number of lines in a printable file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int count;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Return a file buffer all ready to read up the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * passed message pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Delete a file, but only if the file is a plain file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0); /* it's already gone, no error */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
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 int noremove /* don't allow the file to be removed, trunc instead */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int gotcha, c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((id = hfield("article-id", mp, addone)) != NOSTR)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fstat(fileno(ibuf), &statb) >= 0 && statb.st_size > mailsize) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (mp = &message[0]; mp < &message[msgCount]; mp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (gotcha && !noremove && (value("keep") == NOSTR)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int sigdepth = 0; /* depth of holdsigs() */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int omask = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Hold signals SIGHUP - SIGQUIT.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sigdepth++ == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Release signals SIGHUP - SIGQUIT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((void (*)(int))-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Flush the standard output.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Determine the size of the file possessed by
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the passed buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int f;
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 * 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 register char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = strchr(name, '\0') - 1; /* pointer to last char of name */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* strip off trailing blanks */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(xname, sizeof (xname), "%s/%s", foldbuf, name + 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext("Syntax error in \"%s\"\n"), name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, gettext("\"%s\": Ambiguous\n"), name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (debug) fprintf(stderr, "%s\n", wrdexp_buf.we_wordv[0]);
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 * Determine the current folder directory name.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((folder = value("folder")) == NOSTR || *folder == '\0')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If name looks like a folder name, don't try
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to expand it, to prevent infinite recursion.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*folder != '+' && (folder = expand(folder)) == NOSTR ||
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(name, BUFSIZ, "%s/%s", homedir, folder);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * A nicer version of Fdopen, which allows us to fclose
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * without losing the open file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register int f;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (f < 0) {
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 ForteGetf(register char *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte register char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "dead.letter");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* no recursion allowed! */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (".");