xref: /openbsd/usr.bin/vi/ex/ex_file.c (revision 891d7ab6)
1 /*	$OpenBSD: ex_file.c,v 1.6 2009/10/27 23:59:47 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1992, 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 
17 #include <bitstring.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "../common/common.h"
25 
26 /*
27  * ex_file -- :f[ile] [name]
28  *	Change the file's name and display the status line.
29  *
30  * PUBLIC: int ex_file(SCR *, EXCMD *);
31  */
32 int
33 ex_file(sp, cmdp)
34 	SCR *sp;
35 	EXCMD *cmdp;
36 {
37 	CHAR_T *p;
38 	FREF *frp;
39 
40 	NEEDFILE(sp, cmdp);
41 
42 	switch (cmdp->argc) {
43 	case 0:
44 		break;
45 	case 1:
46 		frp = sp->frp;
47 
48 		/* Make sure can allocate enough space. */
49 		if ((p = v_strdup(sp,
50 		    cmdp->argv[0]->bp, cmdp->argv[0]->len)) == 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