xref: /minix3/external/bsd/nvi/dist/ex/ex_file.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_file.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_file.c,v 10.14 2001/06/25 15:19:16 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:16 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_file.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 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <errno.h>
2784d9c625SLionel Sambuc #include <limits.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <stdlib.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc #include "../common/common.h"
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * ex_file -- :f[ile] [name]
3684d9c625SLionel Sambuc  *	Change the file's name and display the status line.
3784d9c625SLionel Sambuc  *
3884d9c625SLionel Sambuc  * PUBLIC: int ex_file __P((SCR *, EXCMD *));
3984d9c625SLionel Sambuc  */
4084d9c625SLionel Sambuc int
ex_file(SCR * sp,EXCMD * cmdp)4184d9c625SLionel Sambuc ex_file(SCR *sp, EXCMD *cmdp)
4284d9c625SLionel Sambuc {
4384d9c625SLionel Sambuc 	char *p;
4484d9c625SLionel Sambuc 	FREF *frp;
4584d9c625SLionel Sambuc 	const char *np;
4684d9c625SLionel Sambuc 	size_t nlen;
4784d9c625SLionel Sambuc 
4884d9c625SLionel Sambuc 	NEEDFILE(sp, cmdp);
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc 	switch (cmdp->argc) {
5184d9c625SLionel Sambuc 	case 0:
5284d9c625SLionel Sambuc 		break;
5384d9c625SLionel Sambuc 	case 1:
5484d9c625SLionel Sambuc 		frp = sp->frp;
5584d9c625SLionel Sambuc 
5684d9c625SLionel Sambuc 		/* Make sure can allocate enough space. */
5784d9c625SLionel Sambuc 		INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
5884d9c625SLionel Sambuc 			    np, nlen);
5984d9c625SLionel Sambuc 		if ((p = v_strdup(sp, np, nlen - 1)) == NULL)
6084d9c625SLionel Sambuc 			return (1);
6184d9c625SLionel Sambuc 
6284d9c625SLionel Sambuc 		/* If already have a file name, it becomes the alternate. */
6384d9c625SLionel Sambuc 		if (!F_ISSET(frp, FR_TMPFILE))
6484d9c625SLionel Sambuc 			set_alt_name(sp, frp->name);
6584d9c625SLionel Sambuc 
6684d9c625SLionel Sambuc 		/* Free the previous name. */
6784d9c625SLionel Sambuc 		free(frp->name);
6884d9c625SLionel Sambuc 		frp->name = p;
6984d9c625SLionel Sambuc 
7084d9c625SLionel Sambuc 		/*
7184d9c625SLionel Sambuc 		 * The file has a real name, it's no longer a temporary,
7284d9c625SLionel Sambuc 		 * clear the temporary file flags.
7384d9c625SLionel Sambuc 		 */
7484d9c625SLionel Sambuc 		F_CLR(frp, FR_TMPEXIT | FR_TMPFILE);
7584d9c625SLionel Sambuc 
7684d9c625SLionel Sambuc 		/* Have to force a write if the file exists, next time. */
7784d9c625SLionel Sambuc 		F_SET(frp, FR_NAMECHANGE);
7884d9c625SLionel Sambuc 
7984d9c625SLionel Sambuc 		/* Notify the screen. */
8084d9c625SLionel Sambuc 		(void)sp->gp->scr_rename(sp, sp->frp->name, 1);
8184d9c625SLionel Sambuc 		break;
8284d9c625SLionel Sambuc 	default:
8384d9c625SLionel Sambuc 		abort();
8484d9c625SLionel Sambuc 	}
8584d9c625SLionel Sambuc 	msgq_status(sp, sp->lno, MSTAT_SHOWLAST);
8684d9c625SLionel Sambuc 	return (0);
8784d9c625SLionel Sambuc }
88