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