Commit | Line | Data |
---|---|---|
395c4480 AM |
1 | /*- |
2 | * Copyright (c) 1992, 1993, 1994 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static char sccsid[] = "@(#)ex_mkexrc.c 8.10 (Berkeley) 3/22/94"; | |
36 | #endif /* not lint */ | |
37 | ||
38 | #include <sys/types.h> | |
39 | #include <queue.h> | |
40 | #include <sys/stat.h> | |
41 | #include <sys/time.h> | |
42 | ||
43 | #include <bitstring.h> | |
44 | #include <errno.h> | |
45 | #include <fcntl.h> | |
46 | #include <limits.h> | |
47 | #include <signal.h> | |
48 | #include <stdio.h> | |
49 | #include <stdlib.h> | |
50 | #include <string.h> | |
51 | #include <termios.h> | |
52 | #include <unistd.h> | |
53 | ||
54 | #include <db.h> | |
55 | #include <regex.h> | |
56 | #include <pathnames.h> | |
57 | ||
58 | #include "vi.h" | |
59 | #include "excmd.h" | |
60 | #include "seq.h" | |
61 | ||
62 | /* | |
63 | * ex_mkexrc -- :mkexrc[!] [file] | |
64 | * | |
65 | * Create (or overwrite) a .exrc file with the current info. | |
66 | */ | |
67 | int | |
68 | ex_mkexrc(sp, ep, cmdp) | |
69 | SCR *sp; | |
70 | EXF *ep; | |
71 | EXCMDARG *cmdp; | |
72 | { | |
73 | struct stat sb; | |
74 | FILE *fp; | |
75 | int fd, sverrno; | |
76 | char *fname; | |
77 | ||
78 | switch (cmdp->argc) { | |
79 | case 0: | |
80 | fname = _PATH_EXRC; | |
81 | break; | |
82 | case 1: | |
83 | fname = cmdp->argv[0]->bp; | |
84 | set_alt_name(sp, fname); | |
85 | break; | |
86 | default: | |
87 | abort(); | |
88 | } | |
89 | ||
90 | if (!F_ISSET(cmdp, E_FORCE) && !stat(fname, &sb)) { | |
91 | msgq(sp, M_ERR, | |
92 | "%s exists, not written; use ! to override.", fname); | |
93 | return (1); | |
94 | } | |
95 | ||
96 | /* Create with max permissions of rw-r--r--. */ | |
97 | if ((fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, | |
98 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { | |
99 | msgq(sp, M_SYSERR, fname); | |
100 | return (1); | |
101 | } | |
102 | ||
103 | if ((fp = fdopen(fd, "w")) == NULL) { | |
104 | sverrno = errno; | |
105 | (void)close(fd); | |
106 | errno = sverrno; | |
107 | goto e2; | |
108 | } | |
109 | ||
110 | if (abbr_save(sp, fp) || ferror(fp)) | |
111 | goto e1; | |
112 | if (map_save(sp, fp) || ferror(fp)) | |
113 | goto e1; | |
114 | if (opts_save(sp, fp) || ferror(fp)) | |
115 | goto e1; | |
116 | #ifndef NO_DIGRAPH | |
117 | digraph_save(sp, fd); | |
118 | #endif | |
119 | if (fclose(fp)) | |
120 | goto e2; | |
121 | ||
122 | msgq(sp, M_INFO, "New .exrc file: %s. ", fname); | |
123 | return (0); | |
124 | ||
125 | e1: sverrno = errno; | |
126 | (void)fclose(fp); | |
127 | errno = sverrno; | |
128 | e2: msgq(sp, M_ERR, "%s: incomplete: %s", fname, strerror(errno)); | |
129 | return (1); | |
130 | } |