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