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