Merge branch 'opendreambox' of /home/tmbinc/openembedded.git/ into opendreambox
[vuplus_openembedded] / packages / lesstif / files / 020_xpmpipethrough.diff
1 Index: lesstif2-0.95.0/lib/Xm-2.1/XpmRdFToI.c
2 ===================================================================
3 --- lesstif2-0.95.0.orig/lib/Xm-2.1/XpmRdFToI.c 2004-11-18 22:00:58.000000000 +0100
4 +++ lesstif2-0.95.0/lib/Xm-2.1/XpmRdFToI.c      2006-07-11 11:13:29.000000000 +0200
5 @@ -44,11 +44,15 @@
6     DebugUtil.h! */
7  #include <stdio.h>
8  #include <string.h>
9 +#include <errno.h>
10  
11  #include <ctype.h>
12  #ifdef HAVE_SYS_TYPES_H
13  #include <sys/types.h>
14  #endif
15 +#ifdef HAVE_SYS_WAIT_H
16 +#include <sys/wait.h>
17 +#endif
18  #ifdef HAVE_SYS_STAT_H
19  #include <sys/stat.h>
20  #endif
21 @@ -87,16 +91,6 @@
22             strcpy(dst, src); \
23         else return (XpmFileInvalid); }
24  #endif
25 -#include <sys/stat.h>
26 -#if !defined(NO_ZPIPE) && defined(WIN32)
27 -# define popen _popen
28 -# define pclose _pclose
29 -# if defined(STAT_ZFILE)
30 -#  include <io.h>
31 -#  define stat _stat
32 -#  define fstat _fstat
33 -# endif
34 -#endif
35  
36  LFUNC(OpenReadFile, int, (char *filename, xpmData *mdata));
37  LFUNC(xpmDataClose, void, (xpmData *mdata));
38 @@ -173,90 +167,131 @@
39  }
40  #endif /* CXPMPROG */
41  
42 -/*
43 - * open the given file to be read as an xpmData which is returned.
44 - */
45  #ifndef NO_ZPIPE
46 -       FILE *s_popen(char *cmd, const char *type);
47 -#else
48 -#      define s_popen popen
49 +/* Do not depend on errno after read_through */
50 +FILE*
51 +xpmPipeThrough(fd, cmd, arg1, mode)
52 +    int fd;
53 +    const char* cmd;
54 +    const char* arg1;
55 +    const char* mode;
56 +{
57 +    FILE* fp;
58 +    int status, fds[2], in = 0, out = 1;
59 +    pid_t pid;
60 +    if ( 'w' == *mode )
61 +       out = 0, in = 1;
62 +    if ( pipe(fds) < 0 )
63 +       return NULL;
64 +    pid = fork();
65 +    if ( pid < 0 )
66 +       goto fail1;
67 +    if ( 0 == pid )
68 +    {
69 +       close(fds[in]);
70 +       if ( dup2(fds[out], out) < 0 )
71 +           goto err;
72 +       close(fds[out]);
73 +       if ( dup2(fd, in) < 0 )
74 +           goto err;
75 +       close(fd);
76 +       pid = fork();
77 +       if ( pid < 0 )
78 +           goto err;
79 +       if ( 0 == pid )
80 +       {
81 +           execlp(cmd, cmd, arg1, NULL);
82 +           perror(cmd);
83 +           goto err;
84 +       }
85 +       _exit(0);
86 +    err:
87 +       _exit(1);
88 +    }
89 +    close(fds[out]);
90 +    /* calling process: wait for first child */
91 +    while ( waitpid(pid, &status, 0) < 0 && EINTR == errno )
92 +       ;
93 +    if ( WIFSIGNALED(status) ||
94 +        (WIFEXITED(status) && WEXITSTATUS(status) != 0) )
95 +       goto fail2;
96 +    fp = fdopen(fds[in], mode);
97 +    if ( !fp )
98 +       goto fail2;
99 +    close(fd); /* still open in 2nd child */
100 +    return fp;
101 +fail1:
102 +    close(fds[out]);
103 +fail2:
104 +    close(fds[in]);
105 +    return NULL;
106 +}
107  #endif
108  
109 +/*
110 + * open the given file to be read as an xpmData which is returned.
111 + */
112  static int
113  OpenReadFile(filename, mdata)
114      char *filename;
115      xpmData *mdata;
116  {
117 -#ifndef NO_ZPIPE
118 -    char buf[BUFSIZ];
119 -# ifdef STAT_ZFILE
120 -    char *compressfile;
121 -    struct stat status;
122 -# endif
123 -#endif
124 -
125      if (!filename) {
126         mdata->stream.file = (stdin);
127         mdata->type = XPMFILE;
128      } else {
129 -#ifndef NO_ZPIPE
130 -       size_t len = strlen(filename);
131 -
132 -       if(len == 0                        ||
133 -          filename[len-1] == '/')
134 -               return(XpmOpenFailed);
135 -       if ((len > 2) && !strcmp(".Z", filename + (len - 2))) {
136 -           mdata->type = XPMPIPE;
137 -           snprintf(buf, sizeof(buf), "uncompress -c \"%s\"", filename);
138 -           if (!(mdata->stream.file = s_popen(buf, "r")))
139 -               return (XpmOpenFailed);
140 -
141 -       } else if ((len > 3) && !strcmp(".gz", filename + (len - 3))) {
142 -           mdata->type = XPMPIPE;
143 -           snprintf(buf, sizeof(buf), "gunzip -qc \"%s\"", filename);
144 -           if (!(mdata->stream.file = s_popen(buf, "r")))
145 -               return (XpmOpenFailed);
146 -
147 -       } else {
148 -# ifdef STAT_ZFILE
149 -           if (!(compressfile = (char *) XpmMalloc(len + 4)))
150 +       int fd = open(filename, O_RDONLY);
151 +#if defined(NO_ZPIPE)
152 +       if ( fd < 0 )
153 +           return XpmOpenFailed;
154 +#else
155 +       const char* ext = NULL;
156 +       if ( fd >= 0 )
157 +           ext = strrchr(filename, '.');
158 +#ifdef STAT_ZFILE /* searching for z-files if the given name not found */
159 +       else
160 +       {
161 +           size_t len = strlen(filename);
162 +           char *compressfile = (char *) XpmMalloc(len + 4);
163 +           if ( !compressfile )
164                 return (XpmNoMemory);
165 -
166 -           snprintf(compressfile, len+4, "%s.Z", filename);
167 -           if (!stat(compressfile, &status)) {
168 -               snprintf(buf, sizeof(buf), "uncompress -c \"%s\"", compressfile);
169 -               if (!(mdata->stream.file = s_popen(buf, "r"))) {
170 +           strcpy(compressfile, filename);
171 +           strcpy(compressfile + len, ext = ".Z");
172 +           fd = open(compressfile, O_RDONLY);
173 +           if ( fd < 0 )
174 +           {
175 +               strcpy(compressfile + len, ext = ".gz");
176 +               fd = open(compressfile, O_RDONLY);
177 +               if ( fd < 0 )
178 +               {
179                     XpmFree(compressfile);
180 -                   return (XpmOpenFailed);
181 -               }
182 -               mdata->type = XPMPIPE;
183 -           } else {
184 -               snprintf(compressfile, len+4, "%s.gz", filename);
185 -               if (!stat(compressfile, &status)) {
186 -                   snprintf(buf, sizeof(buf), "gunzip -c \"%s\"", compressfile);
187 -                   if (!(mdata->stream.file = s_popen(buf, "r"))) {
188 -                       XpmFree(compressfile);
189 -                       return (XpmOpenFailed);
190 -                   }
191 -                   mdata->type = XPMPIPE;
192 -               } else {
193 -# endif
194 -#endif
195 -                   if (!(mdata->stream.file = fopen(filename, "r"))) {
196 -#if !defined(NO_ZPIPE) && defined(STAT_ZFILE)
197 -                       XpmFree(compressfile);
198 -#endif
199 -                       return (XpmOpenFailed);
200 -                   }
201 -                   mdata->type = XPMFILE;
202 -#ifndef NO_ZPIPE
203 -# ifdef STAT_ZFILE
204 +                   return XpmOpenFailed;
205                 }
206             }
207             XpmFree(compressfile);
208 -# endif
209         }
210  #endif
211 +       if ( ext && !strcmp(ext, ".Z") )
212 +       {
213 +           mdata->type = XPMPIPE;
214 +           mdata->stream.file = xpmPipeThrough(fd, "uncompress", "-c", "r");
215 +       }
216 +       else if ( ext && !strcmp(ext, ".gz") )
217 +       {
218 +           mdata->type = XPMPIPE;
219 +           mdata->stream.file = xpmPipeThrough(fd, "gunzip", "-qc", "r");
220 +       }
221 +       else
222 +#endif /* z-files */
223 +       {
224 +           mdata->type = XPMFILE;
225 +           mdata->stream.file = fdopen(fd, "r");
226 +       }
227 +       if (!mdata->stream.file)
228 +       {
229 +           close(fd);
230 +           return (XpmOpenFailed);
231 +       }
232      }
233      mdata->CommentLength = 0;
234  #ifdef CXPMPROG
235 @@ -273,15 +308,6 @@
236  xpmDataClose(mdata)
237      xpmData *mdata;
238  {
239 -    switch (mdata->type) {
240 -    case XPMFILE:
241 -       if (mdata->stream.file != (stdin))
242 -           fclose(mdata->stream.file);
243 -       break;
244 -#ifndef NO_ZPIPE
245 -    case XPMPIPE:
246 +    if (mdata->stream.file != (stdin))
247         fclose(mdata->stream.file);
248 -       break;
249 -#endif
250 -    }
251  }
252 Index: lesstif2-0.95.0/lib/Xm-2.1/XpmWrFFrI.c
253 ===================================================================
254 --- lesstif2-0.95.0.orig/lib/Xm-2.1/XpmWrFFrI.c 2005-04-13 20:03:27.000000000 +0200
255 +++ lesstif2-0.95.0/lib/Xm-2.1/XpmWrFFrI.c      2006-07-11 11:13:29.000000000 +0200
256 @@ -50,11 +50,15 @@
257     DebugUtil.h! */
258  #include <stdio.h>
259  #include <string.h>
260 +#include <errno.h>
261  
262  #include <ctype.h>
263  #ifdef HAVE_SYS_TYPES_H
264  #include <sys/types.h>
265  #endif
266 +#ifdef HAVE_SYS_WAIT_H
267 +#include <sys/wait.h>
268 +#endif
269  #ifdef HAVE_SYS_STAT_H
270  #include <sys/stat.h>
271  #endif
272 @@ -94,11 +98,6 @@
273         else return (XpmFileInvalid); }
274  #endif
275  
276 -#if !defined(NO_ZPIPE) && defined(WIN32)
277 -# define popen _popen
278 -# define pclose _pclose
279 -#endif
280 -
281  /* MS Windows define a function called WriteFile @#%#&!!! */
282  LFUNC(xpmWriteFile, int, (FILE *file, XpmImage *image, char *name,
283                           XpmInfo *info));
284 @@ -354,58 +353,48 @@
285      fprintf(file, ",\n\"XPMENDEXT\"");
286  }
287  
288 +
289 +#ifndef NO_ZPIPE
290 +FUNC(xpmPipeThrough, FILE*, (int fd,
291 +                            const char* cmd,
292 +                            const char* arg1,
293 +                            const char* mode));
294 +#endif
295 +
296  /*
297   * open the given file to be written as an xpmData which is returned
298   */
299 -#ifndef NO_ZPIPE
300 -       FILE *s_popen(char *cmd, const char *type);
301 -#else
302 -#      define s_popen popen
303 -#endif
304  static int
305  OpenWriteFile(filename, mdata)
306      char *filename;
307      xpmData *mdata;
308  {
309 -#ifndef NO_ZPIPE
310 -    char buf[BUFSIZ];
311 -
312 -#endif
313 -
314      if (!filename) {
315         mdata->stream.file = (stdout);
316         mdata->type = XPMFILE;
317      } else {
318  #ifndef NO_ZPIPE
319 -       size_t len = strlen(filename);
320 -
321 -       if(len == 0                        ||
322 -          filename[0] == '/'              ||
323 -          strstr(filename, "../") != NULL ||
324 -          filename[len-1] == '/')
325 -               return(XpmOpenFailed);
326 -
327 +       size_t len;
328 +#endif
329 +       int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
330 +       if ( fd < 0 )
331 +           return(XpmOpenFailed);
332 +#ifndef NO_ZPIPE
333 +       len = strlen(filename);
334         if (len > 2 && !strcmp(".Z", filename + (len - 2))) {
335 -           snprintf(buf, sizeof(buf), "compress > \"%s\"", filename);
336 -           if (!(mdata->stream.file = s_popen(buf, "w")))
337 -               return (XpmOpenFailed);
338 -
339 +           mdata->stream.file = xpmPipeThrough(fd, "compress", NULL, "w");
340             mdata->type = XPMPIPE;
341         } else if (len > 3 && !strcmp(".gz", filename + (len - 3))) {
342 -           snprintf(buf, sizeof(buf), "gzip -q > \"%s\"", filename);
343 -           if (!(mdata->stream.file = s_popen(buf, "w")))
344 -               return (XpmOpenFailed);
345 -
346 +           mdata->stream.file = xpmPipeThrough(fd, "gzip", "-q", "w");
347             mdata->type = XPMPIPE;
348 -       } else {
349 +       } else
350  #endif
351 -           if (!(mdata->stream.file = fopen(filename, "w")))
352 -               return (XpmOpenFailed);
353 -
354 +       {
355 +           mdata->stream.file = fdopen(fd, "w");
356             mdata->type = XPMFILE;
357 -#ifndef NO_ZPIPE
358         }
359 -#endif
360 +       if (!mdata->stream.file)
361 +           return (XpmOpenFailed);
362      }
363      return (XpmSuccess);
364  }
365 @@ -417,15 +406,6 @@
366  xpmDataClose(mdata)
367      xpmData *mdata;
368  {
369 -    switch (mdata->type) {
370 -    case XPMFILE:
371 -       if (mdata->stream.file != (stdout))
372 -           fclose(mdata->stream.file);
373 -       break;
374 -#ifndef NO_ZPIPE
375 -    case XPMPIPE:
376 +    if (mdata->stream.file != (stdout))
377         fclose(mdata->stream.file);
378 -       break;
379 -#endif
380 -    }
381  }