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