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