xref: /freebsd/contrib/nvi/ex/ex_file.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 #ifndef lint
13 static const char sccsid[] = "@(#)ex_file.c	10.12 (Berkeley) 7/12/96";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 
19 #include <bitstring.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "../common/common.h"
27 
28 /*
29  * ex_file -- :f[ile] [name]
30  *	Change the file's name and display the status line.
31  *
32  * PUBLIC: int ex_file __P((SCR *, EXCMD *));
33  */
34 int
35 ex_file(sp, cmdp)
36 	SCR *sp;
37 	EXCMD *cmdp;
38 {
39 	CHAR_T *p;
40 	FREF *frp;
41 
42 	NEEDFILE(sp, cmdp);
43 
44 	switch (cmdp->argc) {
45 	case 0:
46 		break;
47 	case 1:
48 		frp = sp->frp;
49 
50 		/* Make sure can allocate enough space. */
51 		if ((p = v_strdup(sp,
52 		    cmdp->argv[0]->bp, cmdp->argv[0]->len)) == NULL)
53 			return (1);
54 
55 		/* If already have a file name, it becomes the alternate. */
56 		if (!F_ISSET(frp, FR_TMPFILE))
57 			set_alt_name(sp, frp->name);
58 
59 		/* Free the previous name. */
60 		free(frp->name);
61 		frp->name = p;
62 
63 		/*
64 		 * The file has a real name, it's no longer a temporary,
65 		 * clear the temporary file flags.
66 		 */
67 		F_CLR(frp, FR_TMPEXIT | FR_TMPFILE);
68 
69 		/* Have to force a write if the file exists, next time. */
70 		F_SET(frp, FR_NAMECHANGE);
71 
72 		/* Notify the screen. */
73 		(void)sp->gp->scr_rename(sp, sp->frp->name, 1);
74 		break;
75 	default:
76 		abort();
77 	}
78 	msgq_status(sp, sp->lno, MSTAT_SHOWLAST);
79 	return (0);
80 }
81