1/* $XConsortium: readfile.c,v 1.5 94/12/11 14:25:48 gildea Exp $ */ 2/* 3 4Copyright (c) 1988, 1991 X Consortium 5 6Permission is hereby granted, free of charge, to any person obtaining 7a copy of this software and associated documentation files (the 8"Software"), to deal in the Software without restriction, including 9without limitation the rights to use, copy, modify, merge, publish, 10distribute, sublicense, and/or sell copies of the Software, and to 11permit persons to whom the Software is furnished to do so, subject to 12the following conditions: 13 14The above copyright notice and this permission notice shall be included 15in all copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR 21OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23OTHER DEALINGS IN THE SOFTWARE. 24 25Except as contained in this notice, the name of the X Consortium shall 26not be used in advertising or otherwise to promote the sale, use or 27other dealings in this Software without prior written authorization 28from the X Consortium. 29 30*/ 31/* $XFree86: xc/programs/xmessage/readfile.c,v 1.2 2000/02/14 19:21:04 dawes Exp $ */ 32 33#ifdef HAVE_CONFIG_H 34# include "config.h" 35#endif 36 37#include <X11/Xos.h> /* for types.h */ 38#include <sys/stat.h> 39#include <stdio.h> 40#include <stdlib.h> 41 42#include "readfile.h" 43 44/* 45 * get_data_from_file - read data from a file into a single buffer; meant 46 * for small files containing messages. 47 */ 48static char * 49get_data_from_file (char *filename, int *len_return) 50{ 51 FILE *fp; 52 struct stat statbuf; 53 char *cp; 54 int count; 55 56 if (stat (filename, &statbuf) != 0 || statbuf.st_size < 0) { 57 perror(filename); 58 return NULL; 59 } 60 61 cp = malloc (statbuf.st_size + 1); 62 if (!cp) { 63 fprintf(stderr, "cannot get memory for message file\n"); 64 return NULL; 65 } 66 67 fp = fopen (filename, "r"); 68 if (!fp) { 69 perror(filename); 70 free (cp); 71 return NULL; 72 } 73 74 count = fread (cp, 1, statbuf.st_size, fp); 75 if (count == 0 && statbuf.st_size != 0) { 76 perror(filename); 77 free (cp); 78 fclose (fp); 79 return NULL; 80 } 81 82 cp[count] = '\0'; /* since we allocated one extra */ 83 *len_return = count; 84 fclose (fp); 85 return cp; 86} 87 88/* 89 * get_data_from_stdin - read data from stdin into a single buffer. 90 */ 91static char * 92get_data_from_stdin (int *len_return) 93{ 94 char *cp; 95 int count; 96 int allocated; 97 int n; 98 99 allocated = BUFSIZ; 100 cp = malloc (allocated + 1); 101 if (!cp) { 102 fprintf(stderr, "cannot get memory for message file\n"); 103 return NULL; 104 } 105 count = 0; 106 107 while ((n = fread (cp + count, 1, BUFSIZ, stdin)) > 0) { 108 count += n; 109 /* Here count <= allocated. Prepare for next round. */ 110 if (count + BUFSIZ > allocated) { 111 char *oldp = cp; 112 allocated = 2 * allocated; 113 cp = realloc (cp, allocated + 1); 114 if (!cp) { 115 free(oldp); 116 fprintf(stderr, "cannot get memory for message file\n"); 117 return NULL; 118 } 119 } 120 } 121 122 cp[count] = '\0'; /* since we allocated one extra */ 123 *len_return = count; 124 return cp; 125} 126 127 128/* 129 * read_file - read data from indicated file and return pointer to malloced 130 * buffer. Returns NULL on error or if no such file. 131 */ 132char * 133read_file (char *filename, int *len /* returned */) 134{ 135 if (filename[0] == '-' && filename[1] == '\0') { 136 return (get_data_from_stdin (len)); 137 } else { 138 return (get_data_from_file (filename, len)); 139 } 140} 141