missing pieces in previous patch
[nvi.git] / ex / ex_mkexrc.c
blobd6aa88e8d3f0bba5d9bd5564c099807cad961f86
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_mkexrc.c,v 10.12 2000/07/14 14:29:20 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:20 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "pathnames.h"
33 * ex_mkexrc -- :mkexrc[!] [file]
35 * Create (or overwrite) a .exrc file with the current info.
37 * PUBLIC: int ex_mkexrc __P((SCR *, EXCMD *));
39 int
40 ex_mkexrc(sp, cmdp)
41 SCR *sp;
42 EXCMD *cmdp;
44 struct stat sb;
45 FILE *fp;
46 int fd, sverrno;
47 char *fname;
48 size_t flen;
50 switch (cmdp->argc) {
51 case 0:
52 fname = _PATH_EXRC;
53 break;
54 case 1:
55 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
56 fname, flen);
57 set_alt_name(sp, fname);
58 break;
59 default:
60 abort();
63 if (!FL_ISSET(cmdp->iflags, E_C_FORCE) && !stat(fname, &sb)) {
64 msgq_str(sp, M_ERR, fname,
65 "137|%s exists, not written; use ! to override");
66 return (1);
69 /* Create with max permissions of rw-r--r--. */
70 if ((fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY,
71 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
72 msgq_str(sp, M_SYSERR, fname, "%s");
73 return (1);
76 if ((fp = fdopen(fd, "w")) == NULL) {
77 sverrno = errno;
78 (void)close(fd);
79 goto e2;
82 if (seq_save(sp, fp, "abbreviate ", SEQ_ABBREV) || ferror(fp))
83 goto e1;
84 if (seq_save(sp, fp, "map ", SEQ_COMMAND) || ferror(fp))
85 goto e1;
86 if (seq_save(sp, fp, "map! ", SEQ_INPUT) || ferror(fp))
87 goto e1;
88 if (opts_save(sp, fp) || ferror(fp))
89 goto e1;
90 if (fclose(fp)) {
91 sverrno = errno;
92 goto e2;
95 msgq_str(sp, M_INFO, fname, "138|New exrc file: %s");
96 return (0);
98 e1: sverrno = errno;
99 (void)fclose(fp);
100 e2: errno = sverrno;
101 msgq_str(sp, M_SYSERR, fname, "%s");
102 return (1);