sigprocmask: Fix configuration failure on Solaris 10 (regr. 2020-07-25).
[gnulib.git] / tests / test-read-file.c
blob7d01ec07c2f27b311417eaa562eb185df488095a
1 /*
2 * Copyright (C) 2006-2007, 2010-2020 Free Software Foundation, Inc.
3 * Written by Simon Josefsson
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "read-file.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
26 #include "macros.h"
28 #define FILE1 "/etc/resolv.conf"
29 #define FILE2 "/dev/null"
31 static int
32 test_read_file (int flags)
34 struct stat statbuf;
35 int err = 0;
37 /* We can perform the test only if the file exists and is readable.
38 Test whether it exists, then assume it is world-readable. */
39 if (stat (FILE1, &statbuf) >= 0)
41 size_t len;
42 char *out = read_file (FILE1, flags, &len);
44 if (!out)
46 perror ("Could not read file");
47 err = 1;
49 else
51 if (out[len] != '\0')
53 perror ("BAD: out[len] not zero");
54 err = 1;
57 if (S_ISREG (statbuf.st_mode))
59 /* FILE1 is a regular file or a symlink to a regular file. */
60 if (len != statbuf.st_size)
62 fprintf (stderr, "Read %lu from %s...\n",
63 (unsigned long) len, FILE1);
64 err = 1;
67 else
69 /* Assume FILE1 is not empty. */
70 if (len == 0)
72 fprintf (stderr, "Read nothing from %s\n", FILE1);
73 err = 1;
76 free (out);
80 /* We can perform the test only if the file exists and is readable.
81 Test whether it exists, then assume it is world-readable. */
82 if (stat (FILE2, &statbuf) >= 0)
84 size_t len;
85 char *out = read_file (FILE2, flags, &len);
87 if (!out)
89 perror ("Could not read file");
90 err = 1;
92 else
94 if (out[len] != '\0')
96 perror ("BAD: out[len] not zero");
97 err = 1;
100 /* /dev/null should always be empty. Ignore statbuf.st_size, since it
101 is not a regular file. */
102 if (len != 0)
104 fprintf (stderr, "Read %lu from %s...\n",
105 (unsigned long) len, FILE2);
106 err = 1;
108 free (out);
112 return err;
116 main (void)
118 ASSERT (!test_read_file (0));
119 ASSERT (!test_read_file (RF_BINARY));
120 ASSERT (!test_read_file (RF_SENSITIVE));
121 ASSERT (!test_read_file (RF_BINARY | RF_SENSITIVE));
123 return 0;