2 * Copyright (C) 1989-95 GROUPE BULL
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
26 /*****************************************************************************\
30 * Parse an XPM file and create the image and possibly its mask *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
35 /* October 2004, source code review by Thomas Biege <thomas@suse.de> */
44 #include <sys/types.h>
52 LFUNC(OpenReadFile, int, (char *filename, xpmData *mdata));
53 LFUNC(xpmDataClose, void, (xpmData *mdata));
55 FUNC(xpmPipeThrough, FILE*, (int fd,
65 XImage **image_return,
66 XImage **shapeimage_return,
67 XpmAttributes *attributes)
74 xpmInitXpmImage(&image);
75 xpmInitXpmInfo(&info);
77 /* open file to read */
78 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
81 /* create the XImage from the XpmData */
83 xpmInitAttributes(attributes);
84 xpmSetInfoMask(&info, attributes);
85 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
86 image_return, shapeimage_return,
87 &image, &info, attributes);
89 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
90 image_return, shapeimage_return,
91 &image, NULL, attributes);
93 if (ErrorStatus >= 0) /* no fatal error */
94 xpmSetAttributes(attributes, &image, &info);
95 XpmFreeXpmInfo(&info);
99 /* free the XpmImage */
100 XpmFreeXpmImage(&image);
102 return (ErrorStatus);
106 XpmReadFileToXpmImage(
114 /* init returned values */
115 xpmInitXpmImage(image);
116 xpmInitXpmInfo(info);
118 /* open file to read */
119 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
120 return (ErrorStatus);
122 /* create the XpmImage from the XpmData */
123 ErrorStatus = xpmParseData(&mdata, image, info);
125 xpmDataClose(&mdata);
127 return (ErrorStatus);
129 #endif /* CXPMPROG */
132 /* Do not depend on errno after read_through */
141 int status, fds[2], in = 0, out = 1;
153 if ( dup2(fds[out], out) < 0 )
156 if ( dup2(fd, in) < 0 )
164 execlp(cmd, cmd, arg1, (char *)NULL);
173 /* calling process: wait for first child */
174 while ( waitpid(pid, &status, 0) < 0 && EINTR == errno )
176 if ( WIFSIGNALED(status) ||
177 (WIFEXITED(status) && WEXITSTATUS(status) != 0) )
179 fp = fdopen(fds[in], mode);
182 close(fd); /* still open in 2nd child */
193 * open the given file to be read as an xpmData which is returned.
201 mdata->stream.file = (stdin);
202 mdata->type = XPMFILE;
204 int fd = open(filename, O_RDONLY);
205 #if defined(NO_ZPIPE)
207 return XpmOpenFailed;
209 const char* ext = NULL;
211 ext = strrchr(filename, '.');
212 #ifdef STAT_ZFILE /* searching for z-files if the given name not found */
215 size_t len = strlen(filename);
216 char *compressfile = (char *) XpmMalloc(len + 4);
218 return (XpmNoMemory);
219 strcpy(compressfile, filename);
220 strcpy(compressfile + len, ext = ".Z");
221 fd = open(compressfile, O_RDONLY);
224 strcpy(compressfile + len, ext = ".gz");
225 fd = open(compressfile, O_RDONLY);
228 XpmFree(compressfile);
229 return XpmOpenFailed;
232 XpmFree(compressfile);
235 if ( ext && !strcmp(ext, ".Z") )
237 mdata->type = XPMPIPE;
238 mdata->stream.file = xpmPipeThrough(fd, "uncompress", "-c", "r");
240 else if ( ext && !strcmp(ext, ".gz") )
242 mdata->type = XPMPIPE;
243 mdata->stream.file = xpmPipeThrough(fd, "gunzip", "-qc", "r");
248 mdata->type = XPMFILE;
249 mdata->stream.file = fdopen(fd, "r");
251 if (!mdata->stream.file)
254 return (XpmOpenFailed);
257 mdata->CommentLength = 0;
266 * close the file related to the xpmData if any
269 xpmDataClose(xpmData *mdata)
271 if (mdata->stream.file != (stdin))
272 fclose(mdata->stream.file);