usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-getusershell.c
bloba66479dbbb13a0bb8c1612004d320b5e896ab971
1 /* Test the getusershell, setusershell, endusershell functions.
2 Copyright (C) 2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation, either version 3 of the License,
7 or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Collin Funk <collin.funk1@gmail.com>, 2024. */
19 #include <config.h>
21 /* Specification. */
22 #include <unistd.h>
24 #include "signature.h"
25 SIGNATURE_CHECK (getusershell, char *, (void));
26 SIGNATURE_CHECK (setusershell, void, (void));
27 SIGNATURE_CHECK (endusershell, void, (void));
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
33 #include "macros.h"
35 /* The shell names in the order they appear in '/etc/shells'. */
36 static char **shells = NULL;
37 static size_t shell_count = 0;
39 /* Prepare the shells array. */
40 static void
41 first_pass (void)
43 char *ptr;
44 size_t i = 0;
46 /* Avoid reallocation. */
47 shell_count = 16;
48 shells = malloc (shell_count * sizeof (char *));
49 ASSERT (shells != NULL);
51 for (; (ptr = getusershell ()); ++i)
53 /* Make sure comments and empty lines are ignored. */
54 ASSERT (ptr[0] != '#');
55 ASSERT (ptr[0] != '\0');
56 if (i >= shell_count)
58 shell_count *= 2;
59 shells = realloc (shells, shell_count * sizeof (char *));
60 ASSERT (shells != NULL);
62 shells[i] = strdup (ptr);
63 ASSERT (shells[i] != NULL);
66 shell_count = i;
69 /* Assuming that '/etc/shells' does not change during the duration of this
70 test, check that setusershell puts us back at the start of the file. */
71 static void
72 second_pass (void)
74 size_t i;
75 char *ptr;
77 /* Return to the start of the file. */
78 setusershell ();
80 /* Make sure order is preserved. */
81 for (i = 0; i < shell_count; ++i)
83 ptr = getusershell ();
84 ASSERT (ptr != NULL);
85 ASSERT (STREQ (ptr, shells[i]));
86 printf ("%s\n", ptr);
87 free (shells[i]);
90 /* End of file. */
91 ASSERT (getusershell () == NULL);
93 /* Clean up. */
94 free (shells);
95 endusershell ();
98 int
99 main (void)
101 first_pass ();
102 second_pass ();
104 return test_exit_status;