creates output file with permissions of input file

- also some more detailed error messages, some duplication, though,
  maybe clean up later
- fixes #15
pull/43/head
Christian Krause 9 years ago
parent 115d6b2e5e
commit e5ba30f9c0

@ -24,7 +24,7 @@ PKG_CHECK_MODULES(LIBARCHIVE, libarchive)
PKG_CHECK_MODULES(LZMA, liblzma)
# Checks for header files.
AC_CHECK_HEADERS([stdint.h stdlib.h string.h unistd.h])
AC_CHECK_HEADERS([fcntl.h stdint.h stdlib.h string.h unistd.h])
# Checks for typedefs, structures, and compiler characteristics.
# add when travis has autoconf 2.69+ AC_CHECK_HEADER_STDBOOL
@ -43,7 +43,7 @@ AC_SYS_LARGEFILE
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_FUNC_STRTOD
AC_CHECK_FUNCS([memchr memmove memset strtol])
AC_CHECK_FUNCS([memchr memmove memset strerror strtol])
AC_CONFIG_FILES([Makefile
src/Makefile

@ -3,6 +3,8 @@
#endif
#include "pixz.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
@ -130,12 +132,33 @@ int main(int argc, char **argv) {
usage("Unknown suffix");
}
}
if (ipath && !(gInFile = fopen(ipath, "r")))
die("Can't open input file");
if (opath && !(gOutFile = fopen(opath, "w")))
die("Can't open output file");
if (opath) {
if (gInFile == stdin) {
// can't read permissions of original file, because we read from stdin,
// using umask permissions
if (!(gOutFile = fopen(opath, "w")))
die("can not open output file: %s: %s", opath, strerror(errno));
} else {
// read permissions of original file,
// use them to create / open output file
struct stat input_stat;
int output_fd;
stat(ipath, &input_stat);
if ((output_fd = open(opath, O_CREAT | O_WRONLY, input_stat.st_mode)) == -1)
die("can not open output file: %s: %s", opath, strerror(errno));
if (!(gOutFile = fdopen(output_fd, "w")))
die("can not open output file: %s: %s", opath, strerror(errno));
}
}
switch (op) {
case OP_WRITE:
if (isatty(fileno(gOutFile)) == 1)

Loading…
Cancel
Save