From: Michael Schroeder Date: Mon, 7 Mar 2011 18:40:33 +0000 (+0100) Subject: - change sat_xfopen_fd() to respect the file open mode X-Git-Tag: BASE-SuSE-Code-12_1-Branch~113 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f8fbf5ca792ea7ce16e67958d700caaec5863910;p=thirdparty%2Flibsolv.git - change sat_xfopen_fd() to respect the file open mode --- diff --git a/ext/sat_xfopen.c b/ext/sat_xfopen.c index 83335298..b745826b 100644 --- a/ext/sat_xfopen.c +++ b/ext/sat_xfopen.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "sat_xfopen.h" @@ -67,11 +68,19 @@ sat_xfopen_fd(const char *fn, int fd) char *suf; gzFile *gzf; - if (!fn) - return 0; - suf = strrchr(fn, '.'); + suf = fn ? strrchr(fn, '.') : 0; if (!suf || strcmp(suf, ".gz") != 0) - return fdopen(fd, "r"); + { + int fl = fcntl(fd, F_GETFL, 0); + if (fl == -1) + return 0; + fl &= O_RDONLY|O_WRONLY|O_RDWR; + if (fl == O_WRONLY) + return fdopen(fd, "w"); + else if (fl == O_RDWR) + return fdopen(fd, "r+"); + return fdopen(fd, "r"); + } gzf = gzdopen(fd, "r"); if (!gzf) return 0;