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