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