readfile.c revision 100ae103
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#include <X11/Xos.h>			/* for types.h */
34#include <sys/stat.h>
35#include <stdio.h>
36#include <stdlib.h>
37
38#include "readfile.h"
39
40/*
41 * get_data_from_file - read data from a file into a single buffer; meant
42 * for small files containing messages.
43 */
44static char *
45get_data_from_file (char *filename, int *len_return)
46{
47    FILE *fp;
48    struct stat statbuf;
49    char *cp;
50    int count;
51
52    if (stat (filename, &statbuf) != 0 || statbuf.st_size < 0) {
53	perror(filename);
54	return NULL;
55    }
56
57    cp = malloc (statbuf.st_size + 1);
58    if (!cp) {
59	fprintf(stderr, "cannot get memory for message file\n");
60	return NULL;
61    }
62
63    fp = fopen (filename, "r");
64    if (!fp) {
65	perror(filename);
66	(void) free (cp);
67	return NULL;
68    }
69
70    count = fread (cp, 1, statbuf.st_size, fp);
71    if (count == 0 && statbuf.st_size != 0) {
72	perror(filename);
73	(void) free (cp);
74	(void) fclose (fp);
75	return NULL;
76    }
77
78    cp[count] = '\0';		/* since we allocated one extra */
79    *len_return = count;
80    (void) fclose (fp);
81    return cp;
82}
83
84/*
85 * get_data_from_stdin - read data from stdin into a single buffer.
86 */
87static char *
88get_data_from_stdin (int *len_return)
89{
90    char *cp;
91    int count;
92    int allocated;
93    int n;
94
95    allocated = BUFSIZ;
96    cp = malloc (allocated + 1);
97    if (!cp) {
98	fprintf(stderr, "cannot get memory for message file\n");
99	return NULL;
100    }
101    count = 0;
102
103    while ((n = fread (cp + count, 1, BUFSIZ, stdin)) > 0) {
104	count += n;
105	/* Here count <= allocated. Prepare for next round. */
106	if (count + BUFSIZ > allocated) {
107	    allocated = 2 * allocated;
108	    cp = realloc (cp, allocated + 1);
109	    if (!cp) {
110		fprintf(stderr, "cannot get memory for message file\n");
111		return NULL;
112	    }
113	}
114    }
115
116    cp[count] = '\0';		/* since we allocated one extra */
117    *len_return = count;
118    return cp;
119}
120
121
122/*
123 * read_file - read data from indicated file and return pointer to malloced
124 * buffer.  Returns NULL on error or if no such file.
125 */
126char *
127read_file (char *filename, int *len /* returned */)
128{
129    if (filename[0] == '-' && filename[1] == '\0') {
130	return (get_data_from_stdin (len));
131    } else {
132	return (get_data_from_file (filename, len));
133    }
134}
135