OSDN Git Service

__FD_ZERO fixes for gnu libc1&2
[pf3gnuchains/gcc-fork.git] / gcc / fixinc / fixlib.c
1
2 #include "fixlib.h"
3
4 /* * * * * * * * * * * * *
5  
6    load_file_data loads all the contents of a file into malloc-ed memory.
7    Its argument is the file pointer of the file to read in; the returned
8    result is the NUL terminated contents of the file.  The file
9    is presumed to be an ASCII text file containing no NULs.  */
10
11 char *
12 load_file_data (fp)
13      FILE* fp;
14 {
15   char *pz_data = (char*)NULL;
16   int    space_left = -1;  /* allow for terminating NUL */
17   size_t space_used = 0;
18
19   do
20     {
21       size_t  size_read;
22
23       if (space_left < 1024)
24         {
25           space_left += 4096;
26          if (pz_data)
27             pz_data = realloc ((void*)pz_data, space_left + space_used + 1 );
28          else
29             pz_data = malloc (space_left + space_used + 1 );
30         }
31       size_read = fread (pz_data + space_used, 1, space_left, fp);
32
33       if (size_read == 0)
34         {
35           if (feof (fp))
36             break;
37
38           if (ferror (fp))
39             {
40               int err = errno;
41               if (err != EISDIR)
42                 fprintf (stderr, "error %d (%s) reading input\n", err,
43                          strerror (err));
44               free ((void *) pz_data);
45               fclose (fp);
46               return (char *) NULL;
47             }
48         }
49
50       space_left -= size_read;
51       space_used += size_read;
52     } while (! feof (fp));
53
54   pz_data = realloc ((void*)pz_data, space_used+1 );
55   pz_data[ space_used ] = NUL;
56   fclose (fp);
57
58   return pz_data;
59 }