]> diplodocus.org Git - nmh/blob - uip/mkstemp.c
Simplified m_strn() per Ralph's suggestions.
[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 #include <h/utils.h>
23
24 #if ! defined HAVE_MKSTEMPS
25 # define HAVE_MKSTEMPS 0
26 #endif /* ! HAVE_MKSTEMPS */
27
28 char *build_template(const char *, const char *, const char *);
29 void process_args(int, char **, const char **, const char **, const char **);
30
31 /*
32 * Use a template of the form:
33 * [directory/][prefix]XXXXXX[suffix]
34 * where some of those named components might be null. suffix is only
35 * supported if HAVE_MKSTEMPS.
36 */
37
38 int
39 main(int argc, char *argv[]) {
40 const char *directory = "", *prefix = "", *suffix = "";
41 size_t suffix_len;
42 int fd;
43 char *template;
44
45 process_args(argc, argv, &directory, &prefix, &suffix);
46 if ((template = build_template(directory, prefix, suffix)) == NULL) {
47 return -1;
48 }
49
50 if ((suffix_len = strlen(suffix)) > 0) {
51 # if HAVE_MKSTEMPS
52 if ((fd = mkstemps(template, suffix_len)) < 0) { perror("mkstemps"); }
53 # else /* ! HAVE_MKSTEMPS */
54 fd = 0;
55 # endif /* ! HAVE_MKSTEMPS */
56 } else {
57 if ((fd = mkstemp(template)) < 0) { perror("mkstemp"); }
58 }
59
60 if (fd >= 0) {
61 (void) puts(template);
62 (void) close(fd);
63 }
64
65 free(template);
66
67 return fd >= 0 ? 0 : -1;
68 }
69
70
71 char *
72 build_template(const char *directory, const char *prefix, const char *suffix) {
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
124 #if HAVE_MKSTEMPS
125 # define MHFIXMSG_SWITCHES \
126 X("directory", 0, DIRECTORYSW) \
127 X("prefix", 0, PREFIXSW) \
128 X("suffix", 0, SUFFIXSW) \
129 X("version", 0, VERSIONSW) \
130 X("help", 0, HELPSW)
131 #else /* ! HAVE_MKSTEMPS */
132 # define MHFIXMSG_SWITCHES \
133 X("directory", 0, DIRECTORYSW) \
134 X("prefix", 0, PREFIXSW) \
135 X("version", 0, VERSIONSW) \
136 X("help", 0, HELPSW)
137 #endif /* ! HAVE_MKSTEMPS */
138
139 #define X(sw, minchars, id) id,
140 DEFINE_SWITCH_ENUM(MHFIXMSG);
141 #undef X
142
143 #define X(sw, minchars, id) { sw, minchars, id },
144 DEFINE_SWITCH_ARRAY(MHFIXMSG, switches);
145 #undef X
146
147 void
148 process_args(int argc, char **argv, const char **directory,
149 const char **prefix, const char **suffix) {
150 char **argp, **arguments, *cp, buf[100];
151 # if ! HAVE_MKSTEMPS
152 NMH_UNUSED(suffix);
153 # endif /* ! HAVE_MKSTEMPS */
154
155 if (nmh_init(argv[0], 2)) { done(NOTOK); }
156 arguments = getarguments (invo_name, argc, argv, 1);
157 argp = arguments;
158
159 /*
160 * Parse arguments
161 */
162 while ((cp = *argp++)) {
163 if (*cp == '-') {
164 switch (smatch(++cp, switches)) {
165 case AMBIGSW:
166 ambigsw(cp, switches);
167 done(NOTOK);
168 case UNKWNSW:
169 inform("-%s unknown", cp);
170 (void) snprintf(buf, sizeof buf, "%s [switches]", invo_name);
171 print_help(buf, switches, 1);
172 done(NOTOK);
173 case HELPSW:
174 (void) snprintf(buf, sizeof buf, "%s [switches]", invo_name);
175 print_help(buf, switches, 1);
176 done(OK);
177 case VERSIONSW:
178 print_version(invo_name);
179 done(OK);
180
181 case DIRECTORYSW:
182 /* Allow the directory to start with '-'. */
183 if ((cp = *argp++) == NULL) {
184 adios(NULL, "missing argument to %s", argp[-2]);
185 }
186 *directory = cp;
187 continue;
188
189 case PREFIXSW:
190 /* Allow the prefix to start with '-'. */
191 if ((cp = *argp++) == NULL) {
192 adios(NULL, "missing argument to %s", argp[-2]);
193 }
194 *prefix = cp;
195 continue;
196
197 # if HAVE_MKSTEMPS
198 case SUFFIXSW:
199 /* Allow the suffix to start with '-'. */
200 if ((cp = *argp++) == NULL) {
201 adios(NULL, "missing argument to %s", argp[-2]);
202 }
203 *suffix = cp;
204 continue;
205 # endif /* HAVE_MKSTEMPS */
206 }
207 }
208 }
209 }
210 #else /* ! NMH */
211 void
212 process_args(int argc, char **argv, const char **directory,
213 const char **prefix, const char **suffix) {
214 # if HAVE_MKSTEMPS
215 const char usage[] =
216 "usage: %s [-h] [-d directory] [-p prefix] [-s suffix]\n";
217 const char optstring[] = "d:hp:s:";
218 # else /* ! HAVE_MKSTEMPS */
219 const char usage[] = "usage: %s [-h] [-d directory] [-p prefix]\n";
220 const char optstring[] = "d:hp:";
221 # endif /* ! HAVE_MKSTEMPS */
222 int opt;
223
224 while ((opt = getopt(argc, argv, optstring)) != -1) {
225 switch (opt) {
226 case 'd':
227 *directory = optarg;
228 break;
229 case 'p':
230 *prefix = optarg;
231 break;
232 case 's':
233 *suffix = optarg;
234 break;
235 case 'h':
236 (void) printf(usage, argv[0]);
237 exit(0);
238 default:
239 (void) fprintf(stderr, usage, argv[0]);
240 exit(-1);
241 }
242 }
243 }
244 #endif /* ! NMH */