]>
diplodocus.org Git - nmh/blob - uip/mkstemp.c
1 /* mkstemp.c -- create a temporary file
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.
8 /* define NMH to 0 to remove dependencies on nmh. */
15 #endif /* HAVE_CONFIG_H */
22 #if ! defined HAVE_MKSTEMPS
23 # define HAVE_MKSTEMPS 0
24 #endif /* ! HAVE_MKSTEMPS */
26 static char *build_template(const char *, const char *, const char *);
27 static 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';
124 # define MHFIXMSG_SWITCHES \
125 X("directory", 0, DIRECTORYSW) \
126 X("prefix", 0, PREFIXSW) \
127 X("suffix", 0, SUFFIXSW) \
128 X("version", 0, VERSIONSW) \
130 #else /* ! HAVE_MKSTEMPS */
131 # define MHFIXMSG_SWITCHES \
132 X("directory", 0, DIRECTORYSW) \
133 X("prefix", 0, PREFIXSW) \
134 X("version", 0, VERSIONSW) \
136 #endif /* ! HAVE_MKSTEMPS */
138 #define X(sw, minchars, id) id,
139 DEFINE_SWITCH_ENUM(MHFIXMSG
);
142 #define X(sw, minchars, id) { sw, minchars, id },
143 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
147 process_args(int argc
, char **argv
, const char **directory
,
148 const char **prefix
, const char **suffix
) {
149 char **argp
, **arguments
, *cp
, buf
[100];
152 # endif /* ! HAVE_MKSTEMPS */
154 if (nmh_init(argv
[0], 2)) { done(1); }
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
161 while ((cp
= *argp
++)) {
163 switch (smatch(++cp
, switches
)) {
165 ambigsw(cp
, switches
);
168 inform("-%s unknown", cp
);
169 (void) snprintf(buf
, sizeof buf
, "%s [switches]", invo_name
);
170 print_help(buf
, switches
, 1);
173 (void) snprintf(buf
, sizeof buf
, "%s [switches]", invo_name
);
174 print_help(buf
, switches
, 1);
177 print_version(invo_name
);
181 /* Allow the directory to start with '-'. */
182 if ((cp
= *argp
++) == NULL
) {
183 adios(NULL
, "missing argument to %s", argp
[-2]);
189 /* Allow the prefix to start with '-'. */
190 if ((cp
= *argp
++) == NULL
) {
191 adios(NULL
, "missing argument to %s", argp
[-2]);
198 /* Allow the suffix to start with '-'. */
199 if ((cp
= *argp
++) == NULL
) {
200 adios(NULL
, "missing argument to %s", argp
[-2]);
204 # endif /* HAVE_MKSTEMPS */
211 process_args(int argc
, char **argv
, const char **directory
,
212 const char **prefix
, const char **suffix
) {
215 "usage: %s [-h] [-d directory] [-p prefix] [-s suffix]\n";
216 const char optstring
[] = "d:hp:s:";
217 # else /* ! HAVE_MKSTEMPS */
218 const char usage
[] = "usage: %s [-h] [-d directory] [-p prefix]\n";
219 const char optstring
[] = "d:hp:";
220 # endif /* ! HAVE_MKSTEMPS */
223 while ((opt
= getopt(argc
, argv
, optstring
)) != -1) {
235 (void) printf(usage
, argv
[0]);
238 (void) fprintf(stderr
, usage
, argv
[0]);