add checks for .exrc files -- don't source the startup files unless
[nvi.git] / ex / ex_mkexrc.c
blob5ab223236acac64fb4a12bda9b258dddb94bd28b
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.8 1993/12/02 10:49:53 bostic Exp $ (Berkeley) $Date: 1993/12/02 10:49:53 $";
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 "seq.h"
24 #include "pathnames.h"
27 * ex_mkexrc -- :mkexrc[!] [file]
29 * Create (or overwrite) a .exrc file with the current info.
31 int
32 ex_mkexrc(sp, ep, cmdp)
33 SCR *sp;
34 EXF *ep;
35 EXCMDARG *cmdp;
37 struct stat sb;
38 FILE *fp;
39 int fd, sverrno;
40 char *fname;
42 switch (cmdp->argc) {
43 case 0:
44 fname = _PATH_EXRC;
45 break;
46 case 1:
47 fname = cmdp->argv[0]->bp;
48 set_alt_name(sp, fname);
49 break;
50 default:
51 abort();
54 if (!F_ISSET(cmdp, E_FORCE) && !stat(fname, &sb)) {
55 msgq(sp, M_ERR,
56 "%s exists, not written; use ! to override.", fname);
57 return (1);
60 /* Create with max permissions of rw-r--r--. */
61 if ((fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY,
62 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
63 msgq(sp, M_SYSERR, fname);
64 return (1);
67 if ((fp = fdopen(fd, "w")) == NULL) {
68 sverrno = errno;
69 (void)close(fd);
70 errno = sverrno;
71 goto e2;
74 if (abbr_save(sp, fp) || ferror(fp))
75 goto e1;
76 if (map_save(sp, fp) || ferror(fp))
77 goto e1;
78 if (opts_save(sp, fp) || ferror(fp))
79 goto e1;
80 #ifndef NO_DIGRAPH
81 digraph_save(sp, fd);
82 #endif
83 if (fclose(fp))
84 goto e2;
86 msgq(sp, M_INFO, "New .exrc file: %s. ", fname);
87 return (0);
89 e1: sverrno = errno;
90 (void)fclose(fp);
91 errno = sverrno;
92 e2: msgq(sp, M_ERR, "%s: incomplete: %s", fname, strerror(errno));
93 return (1);