]>
diplodocus.org Git - nmh/blob - uip/mkstemp.c
2 * mkstemp.c -- create a temporary file
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.
9 /* define NMH to 0 to remove dependencies on nmh. */
16 #endif /* HAVE_CONFIG_H */
22 #if ! defined HAVE_MKSTEMPS
23 # define HAVE_MKSTEMPS 0
24 #endif /* ! HAVE_MKSTEMPS */
26 char *build_template(const char *, const char *, const char *);
27 void process_args(int, char **, const char **, const char **, const char **);
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.
37 main(int argc
, char *argv
[]) {
38 const char *directory
= "", *prefix
= "", *suffix
= "";
43 process_args(argc
, argv
, &directory
, &prefix
, &suffix
);
44 if ((template = build_template(directory
, prefix
, suffix
)) == NULL
) {
48 if ((suffix_len
= strlen(suffix
)) > 0) {
50 if ((fd
= mkstemps(template, suffix_len
)) < 0) { perror("mkstemps"); }
51 # else /* ! HAVE_MKSTEMPS */
53 # endif /* ! HAVE_MKSTEMPS */
55 if ((fd
= mkstemp(template)) < 0) { perror("mkstemp"); }
59 (void) puts(template);
65 return fd
>= 0 ? 0 : -1;
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
;
75 directory_len
= strlen(directory
);
76 if (directory_len
> 0) {
78 if (directory
[directory_len
- 1] == '/') {
79 /* Will insert a '/' separately, so truncate the one provided
80 in the directory name. */
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
+
92 if ((template = malloc(len
))) {
95 (void) strncpy(tp
, directory
, directory_len
);
98 if (pathsep_len
== 1) { *tp
++ = '/'; }
100 (void) strncpy(tp
, prefix
, prefix_len
);
103 (void) strncpy(tp
, pattern
, sizeof pattern
- 1);
104 tp
+= sizeof pattern
- 1;
106 (void) strncpy(tp
, suffix
, suffix_len
);
107 /* tp += suffix_len; */
109 template[len
-1] = '\0';
123 # define MHFIXMSG_SWITCHES \
124 X("directory", 0, DIRECTORYSW) \
125 X("prefix", 0, PREFIXSW) \
126 X("suffix", 0, SUFFIXSW) \
127 X("version", 0, VERSIONSW) \
129 #else /* ! HAVE_MKSTEMPS */
130 # define MHFIXMSG_SWITCHES \
131 X("directory", 0, DIRECTORYSW) \
132 X("prefix", 0, PREFIXSW) \
133 X("version", 0, VERSIONSW) \
135 #endif /* ! HAVE_MKSTEMPS */
137 #define X(sw, minchars, id) id,
138 DEFINE_SWITCH_ENUM(MHFIXMSG
);
141 #define X(sw, minchars, id) { sw, minchars, id },
142 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
146 process_args(int argc
, char **argv
, const char **directory
,
147 const char **prefix
, const char **suffix
) {
148 char **argp
, **arguments
, *cp
, buf
[100];
151 # endif /* ! HAVE_MKSTEMPS */
153 if (nmh_init(argv
[0], 1)) { done(NOTOK
); }
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
160 while ((cp
= *argp
++)) {
162 switch (smatch(++cp
, switches
)) {
164 ambigsw(cp
, switches
);
167 advise (NULL
, "-%s unknown", cp
);
168 (void) snprintf(buf
, sizeof buf
, "%s [switches]", invo_name
);
169 print_help(buf
, switches
, 1);
172 (void) snprintf(buf
, sizeof buf
, "%s [switches]", invo_name
);
173 print_help(buf
, switches
, 1);
176 print_version(invo_name
);
180 /* Allow the directory to start with '-'. */
181 if ((cp
= *argp
++) == NULL
) {
182 adios(NULL
, "missing argument to %s", argp
[-2]);
188 /* Allow the prefix to start with '-'. */
189 if ((cp
= *argp
++) == NULL
) {
190 adios(NULL
, "missing argument to %s", argp
[-2]);
197 /* Allow the suffix to start with '-'. */
198 if ((cp
= *argp
++) == NULL
) {
199 adios(NULL
, "missing argument to %s", argp
[-2]);
203 # endif /* HAVE_MKSTEMPS */
210 process_args(int argc
, char **argv
, const char **directory
,
211 const char **prefix
, const char **suffix
) {
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 */
222 while ((opt
= getopt(argc
, argv
, optstring
)) != -1) {
234 (void) printf(usage
, argv
[0]);
237 (void) fprintf(stderr
, usage
, argv
[0]);