#define BUFF_SIZE 8192
#define MIN_SPONGE_SIZE BUFF_SIZE
-#define DEFAULT_TMP_NAME "/tmp/sponge.XXXXXX"
-char tmpname[] = DEFAULT_TMP_NAME;
+char *tmpname = NULL;
void usage() {
printf("sponge <file>: soak up all input from stdin and write it to <file>\n");
}
static void cleanup() {
- if (strcmp(tmpname, DEFAULT_TMP_NAME)) {
+ if (tmpname) {
unlink(tmpname);
}
}
int tmpfd;
FILE *tmpfile;
mode_t mask;
+ char *tmpdir;
+ char const * const template="%s/sponge.XXXXXX";
+
+ tmpdir = getenv("TMPDIR");
+ if (tmpdir == NULL)
+ tmpdir = "/tmp";
+ /* Subtract 2 for `%s' and add 1 for the trailing NULL. */
+ tmpname=malloc(strlen(tmpdir) + strlen(template) - 2 + 1);
+ if (! tmpname) {
+ perror("failed to allocate memory");
+ exit(1);
+ }
+ sprintf(tmpname, template, tmpdir);
trapsignals();
cs = cs_enter();
if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) {
usage();
}
+ if (argc == 2) {
+ outname = argv[1];
+ }
+
bufstart = buf = malloc(bufsize);
if (!buf) {
perror("failed to allocate memory");
perror("failed to read from stdin");
exit(1);
}
- if (argc == 2) {
- outname = argv[1];
- }
if (tmpfile) {
struct stat statbuf;