1 /* $OpenBSD: ex_file.c,v 1.9 2016/05/27 09:18:12 martijn 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
ex_file(SCR * sp,EXCMD * cmdp)33 ex_file(SCR *sp, EXCMD *cmdp)
34 {
35 CHAR_T *p;
36 FREF *frp;
37
38 NEEDFILE(sp, cmdp);
39
40 switch (cmdp->argc) {
41 case 0:
42 break;
43 case 1:
44 frp = sp->frp;
45
46 /* Make sure can allocate enough space. */
47 if ((p = v_strdup(sp,
48 cmdp->argv[0]->bp, cmdp->argv[0]->len)) == NULL)
49 return (1);
50
51 /* If already have a file name, it becomes the alternate. */
52 if (!F_ISSET(frp, FR_TMPFILE))
53 set_alt_name(sp, frp->name);
54
55 /* Free the previous name. */
56 free(frp->name);
57 frp->name = p;
58
59 /*
60 * The file has a real name, it's no longer a temporary,
61 * clear the temporary file flags.
62 */
63 F_CLR(frp, FR_TMPEXIT | FR_TMPFILE);
64
65 /* Have to force a write if the file exists, next time. */
66 F_SET(frp, FR_NAMECHANGE);
67
68 /* Notify the screen. */
69 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
70 break;
71 default:
72 abort();
73 }
74 msgq_status(sp, sp->lno, MSTAT_SHOWLAST);
75 return (0);
76 }
77