]>
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 */
21 #if ! defined HAVE_MKSTEMPS
22 # define HAVE_MKSTEMPS 0
23 #endif /* ! HAVE_MKSTEMPS */
25 char *build_template(const char *, const char *, const char *);
26 void process_args(int, char **, const char **, const char **, const char **);
29 * Use a template of the form:
30 * [directory/][prefix]XXXXXX[suffix]
31 * where some of those named components might be null. suffix is only
32 * supported if HAVE_MKSTEMPS.
36 main(int argc
, char *argv
[]) {
37 const char *directory
= "", *prefix
= "", *suffix
= "";
42 process_args(argc
, argv
, &directory
, &prefix
, &suffix
);
43 if ((template = build_template(directory
, prefix
, suffix
)) == NULL
) {
47 if ((suffix_len
= strlen(suffix
)) > 0) {
49 if ((fd
= mkstemps(template, suffix_len
)) < 0) { perror("mkstemps"); }
50 # else /* ! HAVE_MKSTEMPS */
52 # endif /* ! HAVE_MKSTEMPS */
54 if ((fd
= mkstemp(template)) < 0) { perror("mkstemp"); }
58 (void) puts(template);
64 return fd
>= 0 ? 0 : -1;
69 build_template(const char *directory
, const char *prefix
, const char *suffix
) {
70 const char pattern
[] = "XXXXXX";
71 size_t len
, directory_len
, pathsep_len
, prefix_len
, suffix_len
;
74 directory_len
= strlen(directory
);
75 if (directory_len
> 0) {
77 if (directory
[directory_len
- 1] == '/') {
78 /* Will insert a '/' separately, so truncate the one provided
79 in the directory name. */
85 prefix_len
= strlen(prefix
);
86 suffix_len
= strlen(suffix
);
87 /* sizeof pattern includes its final NULL, so don't add another. */
88 len
= directory_len
+ pathsep_len
+ prefix_len
+ sizeof pattern
+
91 if ((template = malloc(len
))) {
94 (void) strncpy(tp
, directory
, directory_len
);
97 if (pathsep_len
== 1) { *tp
++ = '/'; }
99 (void) strncpy(tp
, prefix
, prefix_len
);
102 (void) strncpy(tp
, pattern
, sizeof pattern
- 1);
103 tp
+= sizeof pattern
- 1;
105 (void) strncpy(tp
, suffix
, suffix_len
);
106 /* tp += suffix_len; */
108 template[len
-1] = '\0';
122 # define MHFIXMSG_SWITCHES \
123 X("directory", 0, DIRECTORYSW) \
124 X("prefix", 0, PREFIXSW) \
125 X("suffix", 0, SUFFIXSW) \
126 X("version", 0, VERSIONSW) \
128 #else /* ! HAVE_MKSTEMPS */
129 # define MHFIXMSG_SWITCHES \
130 X("directory", 0, DIRECTORYSW) \
131 X("prefix", 0, PREFIXSW) \
132 X("version", 0, VERSIONSW) \
134 #endif /* ! HAVE_MKSTEMPS */
136 #define X(sw, minchars, id) id,
137 DEFINE_SWITCH_ENUM(MHFIXMSG
);
140 #define X(sw, minchars, id) { sw, minchars, id },
141 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
145 process_args(int argc
, char **argv
, const char **directory
,
146 const char **prefix
, const char **suffix
) {
147 char **argp
, **arguments
, *cp
, buf
[100];
150 # endif /* ! HAVE_MKSTEMPS */
152 if (nmh_init(argv
[0], 2)) { done(NOTOK
); }
153 arguments
= getarguments (invo_name
, argc
, argv
, 1);
159 while ((cp
= *argp
++)) {
161 switch (smatch(++cp
, switches
)) {
163 ambigsw(cp
, switches
);
166 inform("-%s unknown", cp
);
167 (void) snprintf(buf
, sizeof buf
, "%s [switches]", invo_name
);
168 print_help(buf
, switches
, 1);
171 (void) snprintf(buf
, sizeof buf
, "%s [switches]", invo_name
);
172 print_help(buf
, switches
, 1);
175 print_version(invo_name
);
179 /* Allow the directory to start with '-'. */
180 if ((cp
= *argp
++) == NULL
) {
181 adios(NULL
, "missing argument to %s", argp
[-2]);
187 /* Allow the prefix to start with '-'. */
188 if ((cp
= *argp
++) == NULL
) {
189 adios(NULL
, "missing argument to %s", argp
[-2]);
196 /* Allow the suffix to start with '-'. */
197 if ((cp
= *argp
++) == NULL
) {
198 adios(NULL
, "missing argument to %s", argp
[-2]);
202 # endif /* HAVE_MKSTEMPS */
209 process_args(int argc
, char **argv
, const char **directory
,
210 const char **prefix
, const char **suffix
) {
213 "usage: %s [-h] [-d directory] [-p prefix] [-s suffix]\n";
214 const char optstring
[] = "d:hp:s:";
215 # else /* ! HAVE_MKSTEMPS */
216 const char usage
[] = "usage: %s [-h] [-d directory] [-p prefix]\n";
217 const char optstring
[] = "d:hp:";
218 # endif /* ! HAVE_MKSTEMPS */
221 while ((opt
= getopt(argc
, argv
, optstring
)) != -1) {
233 (void) printf(usage
, argv
[0]);
236 (void) fprintf(stderr
, usage
, argv
[0]);