2222
2323using namespace std ;
2424
25- static const char * zcatExec (const string& path)
25+ static const char * wgetExec (const string& path)
2626{
2727 return
2828 startsWith (path, " http://" ) ? " wget -O-" :
2929 startsWith (path, " https://" ) ? " wget -O-" :
3030 startsWith (path, " ftp://" ) ? " wget -O-" :
31+ NULL ;
32+ }
33+
34+ static const char * zcatExec (const string& path)
35+ {
36+ return
3137 endsWith (path, " .ar" ) ? " ar -p" :
3238 endsWith (path, " .tar" ) ? " tar -xOf" :
3339 endsWith (path, " .tar.Z" ) ? " tar -zxOf" :
@@ -56,7 +62,8 @@ extern "C" {
5662 */
5763static int uncompress (const char *path)
5864{
59- const char *zcat = zcatExec (path);
65+ const char *wget = wgetExec (path);
66+ const char *zcat = wget != NULL ? wget : zcatExec (path);
6067 assert (zcat != NULL );
6168
6269 int fd[2 ];
@@ -127,8 +134,19 @@ FILE *fopen(const char *path, const char *mode)
127134 fprintf (stderr, " error: dlsym fopen: %s\n " , dlerror ());
128135 exit (EXIT_FAILURE);
129136 }
130- return zcatExec (path) == NULL ? real_fopen (path, mode)
131- : funcompress (path);
137+
138+ // open a web address
139+ if (wgetExec (path) != NULL )
140+ return funcompress (path);
141+
142+ // to check if the file exists, we need to attempt to open it
143+ FILE* stream = real_fopen (path, mode);
144+ if (!stream || zcatExec (path) == NULL )
145+ return stream;
146+ else {
147+ fclose (stream);
148+ return funcompress (path);
149+ }
132150}
133151
134152/* * If the specified file is compressed, return a pipe that
@@ -143,8 +161,19 @@ FILE *fopen64(const char *path, const char *mode)
143161 fprintf (stderr, " error: dlsym fopen64: %s\n " , dlerror ());
144162 exit (EXIT_FAILURE);
145163 }
146- return zcatExec (path) == NULL ? real_fopen64 (path, mode)
147- : funcompress (path);
164+
165+ // open a web address
166+ if (wgetExec (path) != NULL )
167+ return funcompress (path);
168+
169+ // to check if the file exists, we need to attempt to open it
170+ FILE* stream = real_fopen64 (path, mode);
171+ if (!stream || zcatExec (path) == NULL )
172+ return stream;
173+ else {
174+ fclose (stream);
175+ return funcompress (path);
176+ }
148177}
149178
150179typedef int (*open_t )(const char *path, int flags, mode_t mode);
@@ -161,8 +190,19 @@ int open(const char *path, int flags, mode_t mode)
161190 fprintf (stderr, " error: dlsym open: %s\n " , dlerror ());
162191 exit (EXIT_FAILURE);
163192 }
164- return zcatExec (path) == NULL ? real_open (path, flags, mode)
165- : uncompress (path);
193+
194+ // open a web address
195+ if (wgetExec (path) != NULL )
196+ return uncompress (path);
197+
198+ // to check if the file exists, we need to attempt to open it
199+ int filedesc = real_open (path, flags, mode);
200+ if (filedesc < 0 || zcatExec (path) == NULL )
201+ return filedesc;
202+ else {
203+ close (filedesc);
204+ return uncompress (path);
205+ }
166206}
167207
168208} // extern "C"
0 commit comments