xref: /netbsd-src/external/bsd/nvi/dist/ex/ex_file.c (revision 2f698edb5c1cb2dcd9e762b0abb50c41dde8b6b7)
1 /*	$NetBSD: ex_file.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 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 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_file.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 #endif
21 
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 
25 #include <bitstring.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include "../common/common.h"
33 
34 /*
35  * ex_file -- :f[ile] [name]
36  *	Change the file's name and display the status line.
37  *
38  * PUBLIC: int ex_file __P((SCR *, EXCMD *));
39  */
40 int
ex_file(SCR * sp,EXCMD * cmdp)41 ex_file(SCR *sp, EXCMD *cmdp)
42 {
43 	char *p;
44 	FREF *frp;
45 	const char *np;
46 	size_t nlen;
47 
48 	NEEDFILE(sp, cmdp);
49 
50 	switch (cmdp->argc) {
51 	case 0:
52 		break;
53 	case 1:
54 		frp = sp->frp;
55 
56 		/* Make sure can allocate enough space. */
57 		INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
58 			    np, nlen);
59 		if ((p = v_strdup(sp, np, nlen - 1)) == NULL)
60 			return (1);
61 
62 		/* If already have a file name, it becomes the alternate. */
63 		if (!F_ISSET(frp, FR_TMPFILE))
64 			set_alt_name(sp, frp->name);
65 
66 		/* Free the previous name. */
67 		free(frp->name);
68 		frp->name = p;
69 
70 		/*
71 		 * The file has a real name, it's no longer a temporary,
72 		 * clear the temporary file flags.
73 		 */
74 		F_CLR(frp, FR_TMPEXIT | FR_TMPFILE);
75 
76 		/* Have to force a write if the file exists, next time. */
77 		F_SET(frp, FR_NAMECHANGE);
78 
79 		/* Notify the screen. */
80 		(void)sp->gp->scr_rename(sp, sp->frp->name, 1);
81 		break;
82 	default:
83 		abort();
84 	}
85 	msgq_status(sp, sp->lno, MSTAT_SHOWLAST);
86 	return (0);
87 }
88