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