Back to index

plt-scheme  4.2.1
RdFToBuf.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1989-95 GROUPE BULL
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to
00006  * deal in the Software without restriction, including without limitation the
00007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00008  * sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017  * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00018  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00019  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020  *
00021  * Except as contained in this notice, the name of GROUPE BULL shall not be
00022  * used in advertising or otherwise to promote the sale, use or other dealings
00023  * in this Software without prior written authorization from GROUPE BULL.
00024  */
00025 
00026 /*****************************************************************************\
00027 * RdFToBuf.c:                                                                 *
00028 *                                                                             *
00029 *  XPM library                                                                *
00030 *  Copy a file to a malloc'ed buffer, provided as a convenience.              *
00031 *                                                                             *
00032 *  Developed by Arnaud Le Hors                                                *
00033 \*****************************************************************************/
00034 
00035 /*
00036  * The code related to FOR_MSW has been added by
00037  * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
00038  */
00039 
00040 #include "xpmP.h"
00041 #include <sys/stat.h>
00042 #include <unistd.h>
00043 #ifndef VAX11C
00044 #include <fcntl.h>
00045 #endif
00046 #ifdef FOR_MSW
00047 #include <io.h>
00048 #endif
00049 
00050 int
00051 XpmReadFileToBuffer(filename, buffer_return)
00052     char *filename;
00053     char **buffer_return;
00054 {
00055     int fd, fcheck, len;
00056     char *ptr;
00057     struct stat stats;
00058     FILE *fp;
00059 
00060     *buffer_return = NULL;
00061 
00062 #ifndef VAX11C
00063     fd = open(filename, O_RDONLY);
00064 #else
00065     fd = open(filename, O_RDONLY, NULL);
00066 #endif
00067     if (fd < 0)
00068        return XpmOpenFailed;
00069 
00070     if (fstat(fd, &stats)) {
00071        close(fd);
00072        return XpmOpenFailed;
00073     }
00074     fp = fdopen(fd, "r");
00075     if (!fp) {
00076        close(fd);
00077        return XpmOpenFailed;
00078     }
00079     len = (int) stats.st_size;
00080     ptr = (char *) XpmMalloc(len + 1);
00081     if (!ptr) {
00082        fclose(fp);
00083        return XpmNoMemory;
00084     }
00085     fcheck = fread(ptr, len, 1, fp);
00086     fclose(fp);
00087     if (fcheck != 1) {
00088        XpmFree(ptr);
00089        return XpmOpenFailed;
00090     }
00091     ptr[len] = '\0';
00092     *buffer_return = ptr;
00093     return XpmSuccess;
00094 }