if no messages waiting, always display the file status line on
[nvi.git] / ex / ex_mkexrc.c
blob4f89d6a8f74cac23c43d23dee5455efc2db4b0ce
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: ex_mkexrc.c,v 8.6 1993/11/20 10:05:40 bostic Exp $ (Berkeley) $Date: 1993/11/20 10:05:40 $";
10 #endif /* not lint */
12 #include <sys/types.h>
13 #include <sys/stat.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 #include "vi.h"
22 #include "excmd.h"
23 #include "pathnames.h"
26 * ex_mkexrc -- :mkexrc[!] [file]
27 * Create (or overwrite) a .exrc file with the current info.
29 int
30 ex_mkexrc(sp, ep, cmdp)
31 SCR *sp;
32 EXF *ep;
33 EXCMDARG *cmdp;
35 struct stat sb;
36 FILE *fp;
37 int fd, sverrno;
38 char *fname;
40 switch (cmdp->argc) {
41 case 0:
42 fname = _PATH_EXRC;
43 break;
44 case 1:
45 fname = cmdp->argv[0];
46 set_alt_name(sp, fname);
47 break;
48 default:
49 abort();
52 if (!F_ISSET(cmdp, E_FORCE) && !stat(fname, &sb)) {
53 msgq(sp, M_ERR,
54 "%s exists, not written; use ! to override.", fname);
55 return (1);
58 /* Create with max permissions of rw-r--r--. */
59 if ((fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY,
60 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
61 msgq(sp, M_SYSERR, fname);
62 return (1);
65 if ((fp = fdopen(fd, "w")) == NULL) {
66 sverrno = errno;
67 (void)close(fd);
68 errno = sverrno;
69 goto e2;
72 if (abbr_save(sp, fp) || ferror(fp))
73 goto e1;
74 if (map_save(sp, fp) || ferror(fp))
75 goto e1;
76 if (opts_save(sp, fp) || ferror(fp))
77 goto e1;
78 #ifndef NO_DIGRAPH
79 digraph_save(sp, fd);
80 #endif
81 if (fclose(fp))
82 goto e2;
84 msgq(sp, M_INFO, "New .exrc file: %s. ", fname);
85 return (0);
87 e1: sverrno = errno;
88 (void)fclose(fp);
89 errno = sverrno;
90 e2: msgq(sp, M_ERR, "%s: incomplete: %s", fname, strerror(errno));
91 return (1);