return NULL;
}
-static char *temp_name = NULL;
-
static void
print_usage()
{
return 0;
}
-static void
-exitfunc(void)
-{
- if (temp_name)
- remove(temp_name);
-}
-
#include "getopt_long.h"
void
{
FILE *outfile;
- temp_name = tmpnam(NULL);
- outfile = fopen(temp_name, "wb");
+ outfile = tmpfile();
if (outfile == NULL)
error("Can't open temporary file");
- atexit(exitfunc);
return outfile;
}
if (strcmp(archive_file, "-") == 0)
opts.archive_to_stdio = 1;
+ if (opts.archive_to_stdio)
+ opts.quiet = 2;
argv++;
argc--;
case 'a':
case 'u':
case 'c':
- if (opts.archive_to_stdio)
- opts.quiet = 2;
-
outfile = open_tempfile();
wp->fp = outfile;
wp->buf = 0;
fputc(0, outfile); /* end of archive */
if (ferror(outfile))
error("Can't write");
- fclose(outfile);
+ rewind(outfile);
if (opts.archive_to_stdio) {
- if (move_file_to_stream(temp_name, stdout) == -1)
- error("fail to move_file_to_stream(): %s -> %s",temp_name,"stdout");
+ if (copy_stream(outfile, stdout) == -1)
+ error("fail to copy_stream_to_file(): temp -> %s","stdout");
}
else {
- unlink(archive_file);
- if (xrename(temp_name, archive_file) == -1)
- error("fail to rename(): %s -> %s",temp_name,archive_file);
+ if (copy_stream_to_file(outfile, archive_file) == -1)
+ error("fail to copy_stream_to_file(): temp -> %s",archive_file);
}
exit(0);
break;
error("Can't write");
if (!opts.archive_to_stdio)
unlink(archive_file);
- fclose(outfile);
+
fclose(arcfile);
+ rewind(outfile);
if (cmd == 'd') {
if (arc_count > count) {
- if (xrename(temp_name, archive_file) == -1)
- error("fail to rename(): %s -> %s",temp_name,archive_file);
+ if (copy_stream_to_file(outfile, archive_file) == -1)
+ error("fail to copy_stream_to_file(): temp -> %s",archive_file);
}
else {
message("The archive file \"%s\" was removed because it would be empty.", archive_file);
}
}
else {
- if (xrename(temp_name, archive_file) == -1)
- error("fail to rename(): %s -> %s",temp_name,archive_file);
+ if (copy_stream_to_file(outfile, archive_file) == -1)
+ error("fail to copy_stream_to_file(): temp -> %s",archive_file);
}
exit(0);
}
int file_mtime P_((char *file, time_t *t));
int copy_stream P_((FILE *rfp, FILE *wfp));
int move_file_to_stream P_((char *file, FILE *wfp));
+int copy_stream_to_file P_((FILE *rfp, char *file));
int xrename P_((char *from, char *to));
/* header.c */
int read_header P_((FILE *fp, struct lzh_header *h));