xref: /minix3/external/bsd/nvi/dist/ex/ex_write.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_write.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_write.c,v 10.38 2001/06/25 15:19:22 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:22 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_write.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/stat.h>
2584d9c625SLionel Sambuc 
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <ctype.h>
2884d9c625SLionel Sambuc #include <errno.h>
2984d9c625SLionel Sambuc #include <fcntl.h>
3084d9c625SLionel Sambuc #include <limits.h>
3184d9c625SLionel Sambuc #include <stdio.h>
3284d9c625SLionel Sambuc #include <stdlib.h>
3384d9c625SLionel Sambuc #include <string.h>
3484d9c625SLionel Sambuc #include <unistd.h>
3584d9c625SLionel Sambuc 
3684d9c625SLionel Sambuc #include "../common/common.h"
3784d9c625SLionel Sambuc 
3884d9c625SLionel Sambuc enum which {WN, WQ, WRITE, XIT};
3984d9c625SLionel Sambuc static int exwr __P((SCR *, EXCMD *, enum which));
4084d9c625SLionel Sambuc 
4184d9c625SLionel Sambuc /*
4284d9c625SLionel Sambuc  * ex_wn --	:wn[!] [>>] [file]
4384d9c625SLionel Sambuc  *	Write to a file and switch to the next one.
4484d9c625SLionel Sambuc  *
4584d9c625SLionel Sambuc  * PUBLIC: int ex_wn __P((SCR *, EXCMD *));
4684d9c625SLionel Sambuc  */
4784d9c625SLionel Sambuc int
ex_wn(SCR * sp,EXCMD * cmdp)4884d9c625SLionel Sambuc ex_wn(SCR *sp, EXCMD *cmdp)
4984d9c625SLionel Sambuc {
5084d9c625SLionel Sambuc 	if (exwr(sp, cmdp, WN))
5184d9c625SLionel Sambuc 		return (1);
5284d9c625SLionel Sambuc 	if (file_m3(sp, 0))
5384d9c625SLionel Sambuc 		return (1);
5484d9c625SLionel Sambuc 
5584d9c625SLionel Sambuc 	/* The file name isn't a new file to edit. */
5684d9c625SLionel Sambuc 	cmdp->argc = 0;
5784d9c625SLionel Sambuc 
5884d9c625SLionel Sambuc 	return (ex_next(sp, cmdp));
5984d9c625SLionel Sambuc }
6084d9c625SLionel Sambuc 
6184d9c625SLionel Sambuc /*
6284d9c625SLionel Sambuc  * ex_wq --	:wq[!] [>>] [file]
6384d9c625SLionel Sambuc  *	Write to a file and quit.
6484d9c625SLionel Sambuc  *
6584d9c625SLionel Sambuc  * PUBLIC: int ex_wq __P((SCR *, EXCMD *));
6684d9c625SLionel Sambuc  */
6784d9c625SLionel Sambuc int
ex_wq(SCR * sp,EXCMD * cmdp)6884d9c625SLionel Sambuc ex_wq(SCR *sp, EXCMD *cmdp)
6984d9c625SLionel Sambuc {
7084d9c625SLionel Sambuc 	int force;
7184d9c625SLionel Sambuc 
7284d9c625SLionel Sambuc 	if (exwr(sp, cmdp, WQ))
7384d9c625SLionel Sambuc 		return (1);
7484d9c625SLionel Sambuc 	if (file_m3(sp, 0))
7584d9c625SLionel Sambuc 		return (1);
7684d9c625SLionel Sambuc 
7784d9c625SLionel Sambuc 	force = FL_ISSET(cmdp->iflags, E_C_FORCE);
7884d9c625SLionel Sambuc 
7984d9c625SLionel Sambuc 	if (ex_ncheck(sp, force))
8084d9c625SLionel Sambuc 		return (1);
8184d9c625SLionel Sambuc 
8284d9c625SLionel Sambuc 	F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
8384d9c625SLionel Sambuc 	return (0);
8484d9c625SLionel Sambuc }
8584d9c625SLionel Sambuc 
8684d9c625SLionel Sambuc /*
8784d9c625SLionel Sambuc  * ex_write --	:write[!] [>>] [file]
8884d9c625SLionel Sambuc  *		:write [!] [cmd]
8984d9c625SLionel Sambuc  *	Write to a file.
9084d9c625SLionel Sambuc  *
9184d9c625SLionel Sambuc  * PUBLIC: int ex_write __P((SCR *, EXCMD *));
9284d9c625SLionel Sambuc  */
9384d9c625SLionel Sambuc int
ex_write(SCR * sp,EXCMD * cmdp)9484d9c625SLionel Sambuc ex_write(SCR *sp, EXCMD *cmdp)
9584d9c625SLionel Sambuc {
9684d9c625SLionel Sambuc 	return (exwr(sp, cmdp, WRITE));
9784d9c625SLionel Sambuc }
9884d9c625SLionel Sambuc 
9984d9c625SLionel Sambuc 
10084d9c625SLionel Sambuc /*
10184d9c625SLionel Sambuc  * ex_xit -- :x[it]! [file]
10284d9c625SLionel Sambuc  *	Write out any modifications and quit.
10384d9c625SLionel Sambuc  *
10484d9c625SLionel Sambuc  * PUBLIC: int ex_xit __P((SCR *, EXCMD *));
10584d9c625SLionel Sambuc  */
10684d9c625SLionel Sambuc int
ex_xit(SCR * sp,EXCMD * cmdp)10784d9c625SLionel Sambuc ex_xit(SCR *sp, EXCMD *cmdp)
10884d9c625SLionel Sambuc {
10984d9c625SLionel Sambuc 	int force;
11084d9c625SLionel Sambuc 
11184d9c625SLionel Sambuc 	NEEDFILE(sp, cmdp);
11284d9c625SLionel Sambuc 
11384d9c625SLionel Sambuc 	if (F_ISSET(sp->ep, F_MODIFIED) && exwr(sp, cmdp, XIT))
11484d9c625SLionel Sambuc 		return (1);
11584d9c625SLionel Sambuc 	if (file_m3(sp, 0))
11684d9c625SLionel Sambuc 		return (1);
11784d9c625SLionel Sambuc 
11884d9c625SLionel Sambuc 	force = FL_ISSET(cmdp->iflags, E_C_FORCE);
11984d9c625SLionel Sambuc 
12084d9c625SLionel Sambuc 	if (ex_ncheck(sp, force))
12184d9c625SLionel Sambuc 		return (1);
12284d9c625SLionel Sambuc 
12384d9c625SLionel Sambuc 	F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
12484d9c625SLionel Sambuc 	return (0);
12584d9c625SLionel Sambuc }
12684d9c625SLionel Sambuc 
12784d9c625SLionel Sambuc /*
12884d9c625SLionel Sambuc  * exwr --
12984d9c625SLionel Sambuc  *	The guts of the ex write commands.
13084d9c625SLionel Sambuc  */
13184d9c625SLionel Sambuc static int
exwr(SCR * sp,EXCMD * cmdp,enum which cmd)13284d9c625SLionel Sambuc exwr(SCR *sp, EXCMD *cmdp, enum which cmd)
13384d9c625SLionel Sambuc {
13484d9c625SLionel Sambuc 	MARK rm;
13584d9c625SLionel Sambuc 	int flags;
13684d9c625SLionel Sambuc 	char *name;
13784d9c625SLionel Sambuc 	CHAR_T *p = NULL;
13884d9c625SLionel Sambuc 	size_t nlen;
13984d9c625SLionel Sambuc 	const char *n;
14084d9c625SLionel Sambuc 	int rc;
14184d9c625SLionel Sambuc 	EX_PRIVATE *exp;
14284d9c625SLionel Sambuc 
14384d9c625SLionel Sambuc 	NEEDFILE(sp, cmdp);
14484d9c625SLionel Sambuc 
14584d9c625SLionel Sambuc 	/* All write commands can have an associated '!'. */
14684d9c625SLionel Sambuc 	LF_INIT(FS_POSSIBLE);
14784d9c625SLionel Sambuc 	if (FL_ISSET(cmdp->iflags, E_C_FORCE))
14884d9c625SLionel Sambuc 		LF_SET(FS_FORCE);
14984d9c625SLionel Sambuc 
15084d9c625SLionel Sambuc 	/* Skip any leading whitespace. */
15184d9c625SLionel Sambuc 	if (cmdp->argc != 0)
15284d9c625SLionel Sambuc 		for (p = cmdp->argv[0]->bp; *p != '\0' && ISBLANK((UCHAR_T)*p); ++p);
15384d9c625SLionel Sambuc 
15484d9c625SLionel Sambuc 	/* If "write !" it's a pipe to a utility. */
15584d9c625SLionel Sambuc 	if (cmdp->argc != 0 && cmd == WRITE && *p == '!') {
15684d9c625SLionel Sambuc 		/* Secure means no shell access. */
15784d9c625SLionel Sambuc 		if (O_ISSET(sp, O_SECURE)) {
15884d9c625SLionel Sambuc 			ex_wemsg(sp, cmdp->cmd->name, EXM_SECURE_F);
15984d9c625SLionel Sambuc 			return (1);
16084d9c625SLionel Sambuc 		}
16184d9c625SLionel Sambuc 
16284d9c625SLionel Sambuc 		/* Expand the argument. */
16384d9c625SLionel Sambuc 		for (++p; *p && ISBLANK((UCHAR_T)*p); ++p);
16484d9c625SLionel Sambuc 		if (*p == '\0') {
16584d9c625SLionel Sambuc 			ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
16684d9c625SLionel Sambuc 			return (1);
16784d9c625SLionel Sambuc 		}
16884d9c625SLionel Sambuc 		if (argv_exp1(sp, cmdp, p, STRLEN(p), 1))
16984d9c625SLionel Sambuc 			return (1);
17084d9c625SLionel Sambuc 
17184d9c625SLionel Sambuc 		/* Set the last bang command */
17284d9c625SLionel Sambuc 		exp = EXP(sp);
17384d9c625SLionel Sambuc 		free(exp->lastbcomm);
17484d9c625SLionel Sambuc 		exp->lastbcomm = v_wstrdup(sp, cmdp->argv[1]->bp,
17584d9c625SLionel Sambuc 		    cmdp->argv[1]->len);
17684d9c625SLionel Sambuc 
17784d9c625SLionel Sambuc 		/*
17884d9c625SLionel Sambuc 		 * Historically, vi waited after a write filter even if there
17984d9c625SLionel Sambuc 		 * wasn't any output from the command.  People complained when
18084d9c625SLionel Sambuc 		 * nvi waited only if there was output, wanting the visual cue
18184d9c625SLionel Sambuc 		 * that the program hadn't written anything.
18284d9c625SLionel Sambuc 		 */
18384d9c625SLionel Sambuc 		F_SET(sp, SC_EX_WAIT_YES);
18484d9c625SLionel Sambuc 
18584d9c625SLionel Sambuc 		/*
18684d9c625SLionel Sambuc 		 * !!!
18784d9c625SLionel Sambuc 		 * Ignore the return cursor position, the cursor doesn't
18884d9c625SLionel Sambuc 		 * move.
18984d9c625SLionel Sambuc 		 */
19084d9c625SLionel Sambuc 		if (ex_filter(sp, cmdp, &cmdp->addr1,
19184d9c625SLionel Sambuc 		    &cmdp->addr2, &rm, cmdp->argv[1]->bp, FILTER_WRITE))
19284d9c625SLionel Sambuc 			return (1);
19384d9c625SLionel Sambuc 
19484d9c625SLionel Sambuc 		/* Ex terminates with a bang, even if the command fails. */
19584d9c625SLionel Sambuc 		if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
19684d9c625SLionel Sambuc 			(void)ex_puts(sp, "!\n");
19784d9c625SLionel Sambuc 
19884d9c625SLionel Sambuc 		return (0);
19984d9c625SLionel Sambuc 	}
20084d9c625SLionel Sambuc 
20184d9c625SLionel Sambuc 	/* Set the FS_ALL flag if we're writing the entire file. */
20284d9c625SLionel Sambuc 	if (cmdp->addr1.lno <= 1 && !db_exist(sp, cmdp->addr2.lno + 1))
20384d9c625SLionel Sambuc 		LF_SET(FS_ALL);
20484d9c625SLionel Sambuc 
20584d9c625SLionel Sambuc 	/* If "write >>" it's an append to a file. */
20684d9c625SLionel Sambuc 	if (cmdp->argc != 0 && cmd != XIT && p[0] == '>' && p[1] == '>') {
20784d9c625SLionel Sambuc 		LF_SET(FS_APPEND);
20884d9c625SLionel Sambuc 
20984d9c625SLionel Sambuc 		/* Skip ">>" and whitespace. */
21084d9c625SLionel Sambuc 		for (p += 2; *p && ISBLANK((UCHAR_T)*p); ++p);
21184d9c625SLionel Sambuc 	}
21284d9c625SLionel Sambuc 
21384d9c625SLionel Sambuc 	/* If no other arguments, just write the file back. */
21484d9c625SLionel Sambuc 	if (cmdp->argc == 0 || *p == '\0')
21584d9c625SLionel Sambuc 		return (file_write(sp,
21684d9c625SLionel Sambuc 		    &cmdp->addr1, &cmdp->addr2, NULL, flags));
21784d9c625SLionel Sambuc 
21884d9c625SLionel Sambuc 	/* Build an argv so we get an argument count and file expansion. */
21984d9c625SLionel Sambuc 	if (argv_exp2(sp, cmdp, p, STRLEN(p)))
22084d9c625SLionel Sambuc 		return (1);
22184d9c625SLionel Sambuc 
22284d9c625SLionel Sambuc 	/*
22384d9c625SLionel Sambuc 	 *  0 args: impossible.
22484d9c625SLionel Sambuc 	 *  1 args: impossible (I hope).
22584d9c625SLionel Sambuc 	 *  2 args: read it.
22684d9c625SLionel Sambuc 	 * >2 args: object, too many args.
22784d9c625SLionel Sambuc 	 *
22884d9c625SLionel Sambuc 	 * The 1 args case depends on the argv_sexp() function refusing
22984d9c625SLionel Sambuc 	 * to return success without at least one non-blank character.
23084d9c625SLionel Sambuc 	 */
23184d9c625SLionel Sambuc 	switch (cmdp->argc) {
23284d9c625SLionel Sambuc 	case 0:
23384d9c625SLionel Sambuc 	case 1:
23484d9c625SLionel Sambuc 		abort();
23584d9c625SLionel Sambuc 		/* NOTREACHED */
23684d9c625SLionel Sambuc 	case 2:
23784d9c625SLionel Sambuc 		INT2CHAR(sp, cmdp->argv[1]->bp, cmdp->argv[1]->len+1,
23884d9c625SLionel Sambuc 			 n, nlen);
23984d9c625SLionel Sambuc 		name = v_strdup(sp, n, nlen - 1);
24084d9c625SLionel Sambuc 
24184d9c625SLionel Sambuc 		/*
24284d9c625SLionel Sambuc 		 * !!!
24384d9c625SLionel Sambuc 		 * Historically, the read and write commands renamed
24484d9c625SLionel Sambuc 		 * "unnamed" files, or, if the file had a name, set
24584d9c625SLionel Sambuc 		 * the alternate file name.
24684d9c625SLionel Sambuc 		 */
24784d9c625SLionel Sambuc 		if (F_ISSET(sp->frp, FR_TMPFILE) &&
24884d9c625SLionel Sambuc 		    !F_ISSET(sp->frp, FR_EXNAMED)) {
24984d9c625SLionel Sambuc 			char *q;
25084d9c625SLionel Sambuc 			if ((q = v_strdup(sp, name, nlen - 1)) != NULL) {
25184d9c625SLionel Sambuc 				free(sp->frp->name);
25284d9c625SLionel Sambuc 				sp->frp->name = q;
25384d9c625SLionel Sambuc 			}
25484d9c625SLionel Sambuc 			/*
25584d9c625SLionel Sambuc 			 * The file has a real name, it's no longer a
25684d9c625SLionel Sambuc 			 * temporary, clear the temporary file flags.
25784d9c625SLionel Sambuc 			 *
25884d9c625SLionel Sambuc 			 * !!!
25984d9c625SLionel Sambuc 			 * If we're writing the whole file, FR_NAMECHANGE
26084d9c625SLionel Sambuc 			 * will be cleared by the write routine -- this is
26184d9c625SLionel Sambuc 			 * historic practice.
26284d9c625SLionel Sambuc 			 */
26384d9c625SLionel Sambuc 			F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE);
26484d9c625SLionel Sambuc 			F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED);
26584d9c625SLionel Sambuc 
26684d9c625SLionel Sambuc 			/* Notify the screen. */
26784d9c625SLionel Sambuc 			(void)sp->gp->scr_rename(sp, sp->frp->name, 1);
26884d9c625SLionel Sambuc 		} else
26984d9c625SLionel Sambuc 			set_alt_name(sp, name);
27084d9c625SLionel Sambuc 		break;
27184d9c625SLionel Sambuc 	default:
27284d9c625SLionel Sambuc 		INT2CHAR(sp, p, STRLEN(p) + 1, n, nlen);
27384d9c625SLionel Sambuc 		ex_emsg(sp, n, EXM_FILECOUNT);
27484d9c625SLionel Sambuc 		return (1);
27584d9c625SLionel Sambuc 	}
27684d9c625SLionel Sambuc 
27784d9c625SLionel Sambuc 	rc = file_write(sp, &cmdp->addr1, &cmdp->addr2, name, flags);
27884d9c625SLionel Sambuc 
27984d9c625SLionel Sambuc 	free(name);
28084d9c625SLionel Sambuc 
28184d9c625SLionel Sambuc 	return rc;
28284d9c625SLionel Sambuc }
28384d9c625SLionel Sambuc 
28484d9c625SLionel Sambuc /*
28584d9c625SLionel Sambuc  * ex_writefp --
28684d9c625SLionel Sambuc  *	Write a range of lines to a FILE *.
28784d9c625SLionel Sambuc  *
28884d9c625SLionel Sambuc  * PUBLIC: int ex_writefp __P((SCR *,
28984d9c625SLionel Sambuc  * PUBLIC:    const char *, FILE *, MARK *, MARK *, u_long *, u_long *, int));
29084d9c625SLionel Sambuc  */
29184d9c625SLionel Sambuc int
ex_writefp(SCR * sp,const char * name,FILE * fp,MARK * fm,MARK * tm,u_long * nlno,u_long * nch,int silent)29284d9c625SLionel Sambuc ex_writefp(SCR *sp, const char *name, FILE *fp, MARK *fm, MARK *tm, u_long *nlno, u_long *nch, int silent)
29384d9c625SLionel Sambuc {
29484d9c625SLionel Sambuc 	struct stat sb;
29584d9c625SLionel Sambuc 	GS *gp;
29684d9c625SLionel Sambuc 	u_long ccnt;			/* XXX: can't print off_t portably. */
29784d9c625SLionel Sambuc 	db_recno_t fline, tline, lcnt;
29884d9c625SLionel Sambuc 	size_t len;
29984d9c625SLionel Sambuc 	int rval;
30084d9c625SLionel Sambuc 	const char *msg;
30184d9c625SLionel Sambuc 	CHAR_T *p;
30284d9c625SLionel Sambuc 	const char *f;
30384d9c625SLionel Sambuc 	size_t flen;
30484d9c625SLionel Sambuc 
30584d9c625SLionel Sambuc 	gp = sp->gp;
30684d9c625SLionel Sambuc 	fline = fm->lno;
30784d9c625SLionel Sambuc 	tline = tm->lno;
30884d9c625SLionel Sambuc 
30984d9c625SLionel Sambuc 	if (nlno != NULL) {
31084d9c625SLionel Sambuc 		*nch = 0;
31184d9c625SLionel Sambuc 		*nlno = 0;
31284d9c625SLionel Sambuc 	}
31384d9c625SLionel Sambuc 
31484d9c625SLionel Sambuc 	/*
31584d9c625SLionel Sambuc 	 * The vi filter code has multiple processes running simultaneously,
31684d9c625SLionel Sambuc 	 * and one of them calls ex_writefp().  The "unsafe" function calls
31784d9c625SLionel Sambuc 	 * in this code are to db_get() and msgq().  Db_get() is safe, see
31884d9c625SLionel Sambuc 	 * the comment in ex_filter.c:ex_filter() for details.  We don't call
31984d9c625SLionel Sambuc 	 * msgq if the multiple process bit in the EXF is set.
32084d9c625SLionel Sambuc 	 *
32184d9c625SLionel Sambuc 	 * !!!
32284d9c625SLionel Sambuc 	 * Historic vi permitted files of 0 length to be written.  However,
32384d9c625SLionel Sambuc 	 * since the way vi got around dealing with "empty" files was to
32484d9c625SLionel Sambuc 	 * always have a line in the file no matter what, it wrote them as
32584d9c625SLionel Sambuc 	 * files of a single, empty line.  We write empty files.
32684d9c625SLionel Sambuc 	 *
32784d9c625SLionel Sambuc 	 * "Alex, I'll take vi trivia for $1000."
32884d9c625SLionel Sambuc 	 */
32984d9c625SLionel Sambuc 	ccnt = 0;
33084d9c625SLionel Sambuc 	lcnt = 0;
33184d9c625SLionel Sambuc 	msg = "253|Writing...";
33284d9c625SLionel Sambuc 	if (tline != 0)
33384d9c625SLionel Sambuc 		for (; fline <= tline; ++fline, ++lcnt) {
33484d9c625SLionel Sambuc 			/* Caller has to provide any interrupt message. */
33584d9c625SLionel Sambuc 			if ((lcnt + 1) % INTERRUPT_CHECK == 0) {
33684d9c625SLionel Sambuc 				if (INTERRUPTED(sp))
33784d9c625SLionel Sambuc 					break;
33884d9c625SLionel Sambuc 				if (!silent) {
33984d9c625SLionel Sambuc 					gp->scr_busy(sp, msg, msg == NULL ?
34084d9c625SLionel Sambuc 					    BUSY_UPDATE : BUSY_ON);
34184d9c625SLionel Sambuc 					msg = NULL;
34284d9c625SLionel Sambuc 				}
34384d9c625SLionel Sambuc 			}
34484d9c625SLionel Sambuc 			if (db_get(sp, fline, DBG_FATAL, &p, &len))
34584d9c625SLionel Sambuc 				goto err;
34684d9c625SLionel Sambuc 			INT2FILE(sp, p, len, f, flen);
34784d9c625SLionel Sambuc 			if (fwrite(f, 1, flen, fp) != flen)
34884d9c625SLionel Sambuc 				goto err;
34984d9c625SLionel Sambuc 			ccnt += len;
35084d9c625SLionel Sambuc 			if (putc('\n', fp) != '\n')
35184d9c625SLionel Sambuc 				break;
35284d9c625SLionel Sambuc 			++ccnt;
35384d9c625SLionel Sambuc 		}
35484d9c625SLionel Sambuc 
35584d9c625SLionel Sambuc 	if (fflush(fp))
35684d9c625SLionel Sambuc 		goto err;
35784d9c625SLionel Sambuc 	/*
35884d9c625SLionel Sambuc 	 * XXX
35984d9c625SLionel Sambuc 	 * I don't trust NFS -- check to make sure that we're talking to
36084d9c625SLionel Sambuc 	 * a regular file and sync so that NFS is forced to flush.
36184d9c625SLionel Sambuc 	 */
36284d9c625SLionel Sambuc 	if (!fstat(fileno(fp), &sb) &&
36384d9c625SLionel Sambuc 	    S_ISREG(sb.st_mode) && fsync(fileno(fp)))
36484d9c625SLionel Sambuc 		goto err;
36584d9c625SLionel Sambuc 
36684d9c625SLionel Sambuc 	if (fclose(fp))
36784d9c625SLionel Sambuc 		goto err;
36884d9c625SLionel Sambuc 
36984d9c625SLionel Sambuc 	rval = 0;
37084d9c625SLionel Sambuc 	if (0) {
37184d9c625SLionel Sambuc err:		if (!F_ISSET(sp->ep, F_MULTILOCK))
37284d9c625SLionel Sambuc 			msgq_str(sp, M_SYSERR, name, "%s");
37384d9c625SLionel Sambuc 		(void)fclose(fp);
37484d9c625SLionel Sambuc 		rval = 1;
37584d9c625SLionel Sambuc 	}
37684d9c625SLionel Sambuc 
37784d9c625SLionel Sambuc 	if (!silent)
37884d9c625SLionel Sambuc 		gp->scr_busy(sp, NULL, BUSY_OFF);
37984d9c625SLionel Sambuc 
38084d9c625SLionel Sambuc 	/* Report the possibly partial transfer. */
38184d9c625SLionel Sambuc 	if (nlno != NULL) {
38284d9c625SLionel Sambuc 		*nch = ccnt;
38384d9c625SLionel Sambuc 		*nlno = lcnt;
38484d9c625SLionel Sambuc 	}
38584d9c625SLionel Sambuc 	return (rval);
38684d9c625SLionel Sambuc }
387