]> diplodocus.org Git - nmh/blob - uip/mkstemp.c
Added context_find_prefix().
[nmh] / uip / mkstemp.c
1 /*
2 * mkstemp.c -- create a temporary file
3 *
4 * This code is Copyright (c) 2014 by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
7 */
8
9 /* define NMH to 0 to remove dependencies on nmh. */
10 #ifndef NMH
11 # define NMH 1
12 #endif /* ! NMH */
13
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif /* HAVE_CONFIG_H */
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21
22 #if ! defined HAVE_MKSTEMPS
23 # define HAVE_MKSTEMPS 0
24 #endif /* ! HAVE_MKSTEMPS */
25
26 char *build_template(const char *, const char *, const char *);
27 void process_args(int, char **, const char **, const char **, const char **);
28
29 /*
30 * Use a template of the form:
31 * [directory/][prefix]XXXXXX[suffix]
32 * where some of those named components might be null. suffix is only
33 * supported if HAVE_MKSTEMPS.
34 */
35
36 int
37 main(int argc, char *argv[]) {
38 const char *directory = "", *prefix = "", *suffix = "";
39 size_t suffix_len;
40 int fd;
41 char *template;
42
43 process_args(argc, argv, &directory, &prefix, &suffix);
44 if ((template = build_template(directory, prefix, suffix)) == NULL) {
45 return -1;
46 }
47
48 if ((suffix_len = strlen(suffix)) > 0) {
49 # if HAVE_MKSTEMPS
50 if ((fd = mkstemps(template, suffix_len)) < 0) { perror("mkstemps"); }
51 # else /* ! HAVE_MKSTEMPS */
52 fd = 0;
53 # endif /* ! HAVE_MKSTEMPS */
54 } else {
55 if ((fd = mkstemp(template)) < 0) { perror("mkstemp"); }
56 }
57
58 if (fd >= 0) {
59 (void) puts(template);
60 (void) close(fd);
61 }
62
63 free(template);
64
65 return fd >= 0 ? 0 : -1;
66 }
67
68
69 char *
70 build_template(const char *directory, const char *prefix, const char *suffix) {
71 const char pattern[] = "XXXXXX";
72 size_t len, directory_len, pathsep_len, prefix_len, suffix_len;
73 char *template;
74
75 directory_len = strlen(directory);
76 if (directory_len > 0) {
77 pathsep_len = 1;
78 if (directory[directory_len - 1] == '/') {
79 /* Will insert a '/' separately, so truncate the one provided
80 in the directory name. */
81 --directory_len;
82 }
83 } else {
84 pathsep_len = 0;
85 }
86 prefix_len = strlen(prefix);
87 suffix_len = strlen(suffix);
88 /* sizeof pattern includes its final NULL, so don't add another. */
89 len = directory_len + pathsep_len + prefix_len + sizeof pattern +
90 suffix_len;
91
92 if ((template = malloc(len))) {
93 char *tp = template;
94
95 (void) strncpy(tp, directory, directory_len);
96 tp += directory_len;
97
98 if (pathsep_len == 1) { *tp++ = '/'; }
99
100 (void) strncpy(tp, prefix, prefix_len);
101 tp += prefix_len;
102
103 (void) strncpy(tp, pattern, sizeof pattern - 1);
104 tp += sizeof pattern - 1;
105
106 (void) strncpy(tp, suffix, suffix_len);
107 /* tp += suffix_len; */
108
109 template[len-1] = '\0';
110
111 return template;
112 } else {
113 perror("malloc");
114 return NULL;
115 }
116 }
117
118
119 #if NMH
120 #include <h/mh.h>
121
122 #if HAVE_MKSTEMPS
123 # define MHFIXMSG_SWITCHES \
124 X("directory", 0, DIRECTORYSW) \
125 X("prefix", 0, PREFIXSW) \
126 X("suffix", 0, SUFFIXSW) \
127 X("version", 0, VERSIONSW) \
128 X("help", 0, HELPSW)
129 #else /* ! HAVE_MKSTEMPS */
130 # define MHFIXMSG_SWITCHES \
131 X("directory", 0, DIRECTORYSW) \
132 X("prefix", 0, PREFIXSW) \
133 X("version", 0, VERSIONSW) \
134 X("help", 0, HELPSW)
135 #endif /* ! HAVE_MKSTEMPS */
136
137 #define X(sw, minchars, id) id,
138 DEFINE_SWITCH_ENUM(MHFIXMSG);
139 #undef X
140
141 #define X(sw, minchars, id) { sw, minchars, id },
142 DEFINE_SWITCH_ARRAY(MHFIXMSG, switches);
143 #undef X
144
145 void
146 process_args(int argc, char **argv, const char **directory,
147 const char **prefix, const char **suffix) {
148 char **argp, **arguments, *cp, buf[100];
149 # if ! HAVE_MKSTEMPS
150 NMH_UNUSED(suffix);
151 # endif /* ! HAVE_MKSTEMPS */
152
153 if (nmh_init(argv[0], 1)) { done(NOTOK); }
154 arguments = getarguments (invo_name, argc, argv, 1);
155 argp = arguments;
156
157 /*
158 * Parse arguments
159 */
160 while ((cp = *argp++)) {
161 if (*cp == '-') {
162 switch (smatch(++cp, switches)) {
163 case AMBIGSW:
164 ambigsw(cp, switches);
165 done(NOTOK);
166 case UNKWNSW:
167 advise (NULL, "-%s unknown", cp);
168 (void) snprintf(buf, sizeof buf, "%s [switches]", invo_name);
169 print_help(buf, switches, 1);
170 done(NOTOK);
171 case HELPSW:
172 (void) snprintf(buf, sizeof buf, "%s [switches]", invo_name);
173 print_help(buf, switches, 1);
174 done(OK);
175 case VERSIONSW:
176 print_version(invo_name);
177 done(OK);
178
179 case DIRECTORYSW:
180 /* Allow the directory to start with '-'. */
181 if ((cp = *argp++) == NULL) {
182 adios(NULL, "missing argument to %s", argp[-2]);
183 }
184 *directory = cp;
185 continue;
186
187 case PREFIXSW:
188 /* Allow the prefix to start with '-'. */
189 if ((cp = *argp++) == NULL) {
190 adios(NULL, "missing argument to %s", argp[-2]);
191 }
192 *prefix = cp;
193 continue;
194
195 # if HAVE_MKSTEMPS
196 case SUFFIXSW:
197 /* Allow the suffix to start with '-'. */
198 if ((cp = *argp++) == NULL) {
199 adios(NULL, "missing argument to %s", argp[-2]);
200 }
201 *suffix = cp;
202 continue;
203 # endif /* HAVE_MKSTEMPS */
204 }
205 }
206 }
207 }
208 #else /* ! NMH */
209 void
210 process_args(int argc, char **argv, const char **directory,
211 const char **prefix, const char **suffix) {
212 # if HAVE_MKSTEMPS
213 const char usage[] =
214 "usage: %s [-h] [-d directory] [-p prefix] [-s suffix]\n";
215 const char optstring[] = "d:hp:s:";
216 # else /* ! HAVE_MKSTEMPS */
217 const char usage[] = "usage: %s [-h] [-d directory] [-p prefix]\n";
218 const char optstring[] = "d:hp:";
219 # endif /* ! HAVE_MKSTEMPS */
220 int opt;
221
222 while ((opt = getopt(argc, argv, optstring)) != -1) {
223 switch (opt) {
224 case 'd':
225 *directory = optarg;
226 break;
227 case 'p':
228 *prefix = optarg;
229 break;
230 case 's':
231 *suffix = optarg;
232 break;
233 case 'h':
234 (void) printf(usage, argv[0]);
235 exit(0);
236 default:
237 (void) fprintf(stderr, usage, argv[0]);
238 exit(-1);
239 }
240 }
241 }
242 #endif /* ! NMH */