maint: update copyright
[gnulib.git] / tests / test-read-file.c
1 /*
2  * Copyright (C) 2006-2007, 2010-2014 Free Software Foundation, Inc.
3  * Written by Simon Josefsson
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 #include "read-file.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
25
26 #define FILE1 "/etc/resolv.conf"
27 #define FILE2 "/dev/null"
28
29 int
30 main (void)
31 {
32   struct stat statbuf;
33   int err = 0;
34
35   /* We can perform the test only if the file exists and is readable.
36      Test whether it exists, then assume it is world-readable.  */
37   if (stat (FILE1, &statbuf) >= 0)
38     {
39       size_t len;
40       char *out = read_file (FILE1, &len);
41
42       if (!out)
43         {
44           perror ("Could not read file");
45           err = 1;
46         }
47       else
48         {
49           if (out[len] != '\0')
50             {
51               perror ("BAD: out[len] not zero");
52               err = 1;
53             }
54
55           if (S_ISREG (statbuf.st_mode))
56             {
57               /* FILE1 is a regular file or a symlink to a regular file.  */
58               if (len != statbuf.st_size)
59                 {
60                   fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE1);
61                   err = 1;
62                 }
63             }
64           else
65             {
66               /* Assume FILE1 is not empty.  */
67               if (len == 0)
68                 {
69                   fprintf (stderr, "Read nothing from %s\n", FILE1);
70                   err = 1;
71                 }
72             }
73           free (out);
74         }
75     }
76
77   /* We can perform the test only if the file exists and is readable.
78      Test whether it exists, then assume it is world-readable.  */
79   if (stat (FILE2, &statbuf) >= 0)
80     {
81       size_t len;
82       char *out = read_file (FILE2, &len);
83
84       if (!out)
85         {
86           perror ("Could not read file");
87           err = 1;
88         }
89       else
90         {
91           if (out[len] != '\0')
92             {
93               perror ("BAD: out[len] not zero");
94               err = 1;
95             }
96
97           /* /dev/null should always be empty.  Ignore statbuf.st_size, since it
98              is not a regular file.  */
99           if (len != 0)
100             {
101               fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE2);
102               err = 1;
103             }
104           free (out);
105         }
106     }
107
108   return err;
109 }