|
Packit |
6c4009 |
/* Basic test for scandir function.
|
|
Packit |
6c4009 |
Copyright (C) 2015-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <dirent.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef D
|
|
Packit |
6c4009 |
# define D(x) x
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void prepare (void);
|
|
Packit |
6c4009 |
static int do_test (void);
|
|
Packit |
6c4009 |
#define PREPARE(argc, argv) prepare ()
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test ()
|
|
Packit |
6c4009 |
#include "../test-skeleton.c"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static const char *scandir_test_dir;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
prepare (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t test_dir_len = strlen (test_dir);
|
|
Packit |
6c4009 |
static const char dir_name[] = "/tst-scandir.XXXXXX";
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
size_t dirbuflen = test_dir_len + sizeof (dir_name);
|
|
Packit |
6c4009 |
char *dirbuf = malloc (dirbuflen);
|
|
Packit |
6c4009 |
if (dirbuf == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("out of memory");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
|
|
Packit |
6c4009 |
if (mkdtemp (dirbuf) == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("cannot create temporary directory");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
add_temp_file (dirbuf);
|
|
Packit |
6c4009 |
scandir_test_dir = dirbuf;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The directory should be empty save the . and .. files. */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
verify_empty (const char *dirname)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
DIR *dir = opendir (dirname);
|
|
Packit |
6c4009 |
if (dir == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("opendir (%s): %s\n", dirname, strerror (errno));
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct dirent64 *d;
|
|
Packit |
6c4009 |
while ((d = readdir64 (dir)) != NULL)
|
|
Packit |
6c4009 |
if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("temp directory contains file \"%s\"\n", d->d_name);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
closedir (dir);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
make_file (const char *dirname, const char *filename)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *name = NULL;
|
|
Packit |
6c4009 |
if (asprintf (&name, "%s/%s", dirname, filename) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("out of memory");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int fd = open (name, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
Packit |
6c4009 |
if (fd < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("cannot create \"%s\": %s\n", name, strerror (errno));
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
close (fd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
free (name);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
remove_file (const char *dirname, const char *filename)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *name = NULL;
|
|
Packit |
6c4009 |
if (asprintf (&name, "%s/%s", dirname, filename) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("out of memory");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
remove (name);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
free (name);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
freelist (struct D(dirent) **list, size_t n)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
for (size_t i = 0; i < n; ++i)
|
|
Packit |
6c4009 |
free (list[i]);
|
|
Packit |
6c4009 |
free (list);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
select_a (const struct D(dirent) *d)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return d->d_name[0] == 'a';
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
verify_empty (scandir_test_dir);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
make_file (scandir_test_dir, "c");
|
|
Packit |
6c4009 |
make_file (scandir_test_dir, "aa");
|
|
Packit |
6c4009 |
make_file (scandir_test_dir, "b");
|
|
Packit |
6c4009 |
make_file (scandir_test_dir, "a");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* First a basic test with no select or compare functions. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct D(dirent) **list;
|
|
Packit |
6c4009 |
int n = D(scandir) (scandir_test_dir, &list, NULL, NULL);
|
|
Packit |
6c4009 |
if (n < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir failed on %s: %s\n",
|
|
Packit |
6c4009 |
scandir_test_dir, strerror (errno));
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (n != 6)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir returned %d entries instead of 6\n", n);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const char *name;
|
|
Packit |
6c4009 |
bool found;
|
|
Packit |
6c4009 |
} expected[] =
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
{ ".", },
|
|
Packit |
6c4009 |
{ "..", },
|
|
Packit |
6c4009 |
{ "a", },
|
|
Packit |
6c4009 |
{ "aa", },
|
|
Packit |
6c4009 |
{ "b", },
|
|
Packit |
6c4009 |
{ "c", },
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Verify the results, ignoring the order. */
|
|
Packit |
6c4009 |
for (int i = 0; i < n; ++i)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
for (size_t j = 0; j < sizeof expected / sizeof expected[0]; ++j)
|
|
Packit |
6c4009 |
if (!strcmp (list[i]->d_name, expected[j].name))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
expected[j].found = true;
|
|
Packit |
6c4009 |
goto found;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("scandir yields unexpected entry [%d] \"%s\"\n",
|
|
Packit |
6c4009 |
i, list[i]->d_name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
found:;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (size_t j = 0; j < sizeof expected / sizeof expected[0]; ++j)
|
|
Packit |
6c4009 |
if (!expected[j].found)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("failed to produce \"%s\"\n", expected[j].name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
freelist (list, n);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now a test with a comparison function. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
n = D(scandir) (scandir_test_dir, &list, NULL, &D(alphasort));
|
|
Packit |
6c4009 |
if (n < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir failed on %s: %s\n",
|
|
Packit |
6c4009 |
scandir_test_dir, strerror (errno));
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (n != 6)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir returned %d entries instead of 6\n", n);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
assert (sizeof expected / sizeof expected[0] == 6);
|
|
Packit |
6c4009 |
for (int i = 0; i < n; ++i)
|
|
Packit |
6c4009 |
if (strcmp (list[i]->d_name, expected[i].name))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir yields [%d] of \"%s\", expected \"%s\"\n",
|
|
Packit |
6c4009 |
i, list[i]->d_name, expected[i].name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
freelist (list, n);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now a test with a select function but no comparison function. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
n = D(scandir) (scandir_test_dir, &list, &select_a, NULL);
|
|
Packit |
6c4009 |
if (n < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir failed on %s: %s\n",
|
|
Packit |
6c4009 |
scandir_test_dir, strerror (errno));
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (n != 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir returned %d entries instead of 2\n", n);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (strcmp (list[0]->d_name, "a") && strcmp (list[0]->d_name, "aa"))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir yields [0] \"%s\", expected \"a\" or \"aa\"\n",
|
|
Packit |
6c4009 |
list[0]->d_name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (strcmp (list[1]->d_name, "a") && strcmp (list[1]->d_name, "aa"))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir yields [1] \"%s\", expected \"a\" or \"aa\"\n",
|
|
Packit |
6c4009 |
list[1]->d_name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (!strcmp (list[0]->d_name, list[1]->d_name))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir yields \"%s\" twice!\n", list[0]->d_name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
freelist (list, n);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now a test with both functions. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
n = D(scandir) (scandir_test_dir, &list, &select_a, &D(alphasort));
|
|
Packit |
6c4009 |
if (n < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir failed on %s: %s\n",
|
|
Packit |
6c4009 |
scandir_test_dir, strerror (errno));
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (n != 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir returned %d entries instead of 2\n", n);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (strcmp (list[0]->d_name, "a") || strcmp (list[1]->d_name, "aa"))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("scandir yields {\"%s\", \"%s\"}, expected {\"a\", \"aa\"}\n",
|
|
Packit |
6c4009 |
list[0]->d_name, list[1]->d_name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
freelist (list, n);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Clean up the test directory. */
|
|
Packit |
6c4009 |
remove_file (scandir_test_dir, "c");
|
|
Packit |
6c4009 |
remove_file (scandir_test_dir, "aa");
|
|
Packit |
6c4009 |
remove_file (scandir_test_dir, "b");
|
|
Packit |
6c4009 |
remove_file (scandir_test_dir, "a");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|