* lib/stat-time.h: (get_stat_birthtime): Check for zero-valued
[gnulib.git] / tests / test-read-file.c
1 /*
2  * Copyright (C) 2006-2007 Free Software Foundation
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 2, or (at your option)
8  * 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "read-file.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29
30 #define FILE1 "/etc/resolv.conf"
31 #define FILE2 "/dev/null"
32
33 int
34 main (void)
35 {
36   struct stat statbuf;
37   int err = 0;
38
39   /* We can perform the test only if the file exists and is readable.
40      Test whether it exists, then assume it is world-readable.  */
41   if (stat (FILE1, &statbuf) >= 0)
42     {
43       size_t len;
44       char *out = read_file (FILE1, &len);
45
46       if (!out)
47         {
48           perror ("Could not read file");
49           err = 1;
50         }
51       else
52         {
53           if (out[len] != '\0')
54             {
55               perror ("BAD: out[len] not zero");
56               err = 1;
57             }
58
59           /* Assume FILE1 is a regular file or a symlink to a regular file.  */
60           if (len != statbuf.st_size)
61             {
62               fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE1);
63               err = 1;
64             }
65           free (out);
66         }
67     }
68
69   /* We can perform the test only if the file exists and is readable.
70      Test whether it exists, then assume it is world-readable.  */
71   if (stat (FILE2, &statbuf) >= 0)
72     {
73       size_t len;
74       char *out = read_file (FILE2, &len);
75
76       if (!out)
77         {
78           perror ("Could not read file");
79           err = 1;
80         }
81       else
82         {
83           if (out[len] != '\0')
84             {
85               perror ("BAD: out[len] not zero");
86               err = 1;
87             }
88
89           /* /dev/null should always be empty.  Ignore statbuf.st_size, since it
90              is not a regular file.  */
91           if (len != 0)
92             {
93               fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE2);
94               err = 1;
95             }
96           free (out);
97         }
98     }
99
100   return err;
101 }