summaryrefslogtreecommitdiffstats
path: root/test/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'test/stdlib')
-rw-r--r--test/stdlib/qsort.c66
-rw-r--r--test/stdlib/test-canon.c254
-rw-r--r--test/stdlib/testatexit.c58
-rw-r--r--test/stdlib/teston_exit.c57
4 files changed, 350 insertions, 85 deletions
diff --git a/test/stdlib/qsort.c b/test/stdlib/qsort.c
index 9e706c78d..abc505e2d 100644
--- a/test/stdlib/qsort.c
+++ b/test/stdlib/qsort.c
@@ -3,45 +3,43 @@
#include <stdlib.h>
#include <unistd.h>
-int select_files(const struct dirent *dirbuf)
+static int select_files(const struct dirent *dirbuf)
{
- if (dirbuf->d_name[0] == '.')
- return 0;
- else
- return 1;
+ if (dirbuf->d_name[0] == '.')
+ return 0;
+ else
+ return 1;
}
-
int main(void)
{
- struct dirent **array;
- struct dirent *dirbuf;
+ struct dirent **array;
+ struct dirent *dirbuf;
- int i, numdir;
+ int i, numdir;
- chdir("/");
- numdir = scandir(".", &array, select_files, NULL);
- printf("\nGot %d entries from scandir().\n", numdir);
- for (i = 0; i < numdir; ++i) {
- dirbuf = array[i];
- printf("[%d] %s\n", i, dirbuf->d_name);
- free(array[i]);
- }
- free(array);
- numdir = scandir(".", &array, select_files, alphasort);
- printf("\nGot %d entries from scandir() using alphasort().\n", numdir);
- for (i = 0; i < numdir; ++i) {
- dirbuf = array[i];
- printf("[%d] %s\n", i, dirbuf->d_name);
- }
- printf("\nCalling qsort()\n");
- qsort(array, numdir, sizeof(struct dirent *), alphasort);
- for (i = 0; i < numdir; ++i) {
- dirbuf = array[i];
- printf("[%d] %s\n", i, dirbuf->d_name);
- free(array[i]);
- }
- free(array);
- return(0);
+ chdir("/");
+ numdir = scandir(".", &array, select_files, NULL);
+ printf("\nGot %d entries from scandir().\n", numdir);
+ for (i = 0; i < numdir; ++i) {
+ dirbuf = array[i];
+ printf("[%d] %s\n", i, dirbuf->d_name);
+ free(array[i]);
+ }
+ free(array);
+ numdir = scandir(".", &array, select_files, alphasort);
+ printf("\nGot %d entries from scandir() using alphasort().\n", numdir);
+ for (i = 0; i < numdir; ++i) {
+ dirbuf = array[i];
+ printf("[%d] %s\n", i, dirbuf->d_name);
+ }
+ printf("\nCalling qsort()\n");
+ qsort(array, numdir, sizeof(struct dirent *), alphasort);
+ for (i = 0; i < numdir; ++i) {
+ dirbuf = array[i];
+ printf("[%d] %s\n", i, dirbuf->d_name);
+ free(array[i]);
+ }
+ free(array);
+ return (0);
}
-
diff --git a/test/stdlib/test-canon.c b/test/stdlib/test-canon.c
new file mode 100644
index 000000000..dd2f9bce3
--- /dev/null
+++ b/test/stdlib/test-canon.c
@@ -0,0 +1,254 @@
+/* Test program for returning the canonical absolute name of a given file.
+ Copyright (C) 1996,1997,2000,2002,2004,2005,2006
+ Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by David Mosberger <davidm@azstarnet.com>.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+/* This file must be run from within a directory called "stdlib". */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/param.h>
+
+/* Prototype for our test function. */
+extern int do_test (int argc, char *argv[]);
+#include <test-skeleton.c>
+
+#ifndef PATH_MAX
+# define PATH_MAX 4096
+#endif
+static char cwd[PATH_MAX];
+static size_t cwd_len;
+
+struct {
+ const char * name;
+ const char * value;
+} symlinks[] = {
+ {"SYMLINK_LOOP", "SYMLINK_LOOP"},
+ {"SYMLINK_1", "."},
+ {"SYMLINK_2", "//////./../../etc"},
+ {"SYMLINK_3", "SYMLINK_1"},
+ {"SYMLINK_4", "SYMLINK_2"},
+ {"SYMLINK_5", "doesNotExist"},
+};
+
+struct {
+ const char * in, * out, * resolved;
+ int error;
+} tests[] = {
+ /* 0 */
+ {"/", "/"},
+ {"/////////////////////////////////", "/"},
+ {"/.././.././.././..///", "/"},
+ {"/etc", "/etc"},
+ {"/etc/../etc", "/etc"},
+ /* 5 */
+ {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT},
+ {"./././././././././.", "."},
+ {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT},
+ {"./doesExist", "./doesExist"},
+ {"./doesExist/", "./doesExist"},
+ /* 10 */
+ {"./doesExist/../doesExist", "./doesExist"},
+ {"foobar", 0, "./foobar", ENOENT},
+ {".", "."},
+ {"./foobar", 0, "./foobar", ENOENT},
+#ifdef __UCLIBC__
+ /* we differ from glibc here, but POSIX allows it as it says that if we did
+ * not successfuly complete, the value of resolved_path is undefined */
+ {"SYMLINK_LOOP", 0, "", ELOOP},
+ /* 15 */
+ {"./SYMLINK_LOOP", 0, "", ELOOP},
+#else
+ {"SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
+ /* 15 */
+ {"./SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
+#endif
+ {"SYMLINK_1", "."},
+ {"SYMLINK_1/foobar", 0, "./foobar", ENOENT},
+ {"SYMLINK_2", "/etc"},
+ {"SYMLINK_3", "."},
+ /* 20 */
+ {"SYMLINK_4", "/etc"},
+ {"../stdlib/SYMLINK_1", "."},
+ {"../stdlib/SYMLINK_2", "/etc"},
+ {"../stdlib/SYMLINK_3", "."},
+ {"../stdlib/SYMLINK_4", "/etc"},
+ /* 25 */
+ {"./SYMLINK_5", 0, "./doesNotExist", ENOENT},
+ {"SYMLINK_5", 0, "./doesNotExist", ENOENT},
+ {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT},
+ {"doesExist/../../stdlib/doesExist", "./doesExist"},
+ {"doesExist/.././../stdlib/.", "."},
+#ifndef __UCLIBC__
+ /* we dont check for ENOTDIR in readlink() which causes failures to
+ * propogate up to realpath() ... so disable for now ... */
+ /* 30 */
+ {"./doesExist/someFile/", 0, "./doesExist/someFile", ENOTDIR},
+ {"./doesExist/someFile/..", 0, "./doesExist/someFile", ENOTDIR},
+#endif
+};
+
+
+static int
+check_path (const char * result, const char * expected)
+{
+ int good;
+
+ if (!result)
+ return (expected == NULL);
+
+ if (!expected)
+ return 0;
+
+ if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0'))
+ good = (strncmp (result, cwd, cwd_len) == 0
+ && strcmp (result + cwd_len, expected + 1) == 0);
+ else
+ good = (strcmp (expected, result) == 0);
+
+ return good;
+}
+
+
+int
+do_test (int argc, char ** argv)
+{
+ char * result;
+ int i, errors = 0;
+ char buf[PATH_MAX];
+
+ getcwd (cwd, sizeof(buf));
+ cwd_len = strlen (cwd);
+
+#ifndef __UCLIBC__
+ /* we choose to crash in uClibc when given a NULL */
+ errno = 0;
+ if (realpath (NULL, buf) != NULL || errno != EINVAL)
+ {
+ printf ("%s: expected return value NULL and errno set to EINVAL"
+ " for realpath(NULL,...)\n", argv[0]);
+ ++errors;
+ }
+#endif
+
+#if 0
+ /* This is now allowed. The test is invalid. */
+ errno = 0;
+ if (realpath ("/", NULL) != NULL || errno != EINVAL)
+ {
+ printf ("%s: expected return value NULL and errno set to EINVAL"
+ " for realpath(...,NULL)\n", argv[0]);
+ ++errors;
+ }
+#endif
+
+ errno = 0;
+ if (realpath ("", buf) != NULL || errno != ENOENT)
+ {
+ printf ("%s: expected return value NULL and set errno to ENOENT"
+ " for realpath(\"\",...)\n", argv[0]);
+ ++errors;
+ }
+
+ for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
+ symlink (symlinks[i].value, symlinks[i].name);
+
+ int has_dir = mkdir ("doesExist", 0777) == 0;
+
+ int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;
+
+ for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
+ {
+ buf[0] = '\0';
+ result = realpath (tests[i].in, buf);
+
+ if (!check_path (result, tests[i].out))
+ {
+ printf ("%s: flunked test %d (expected `%s', got `%s')\n",
+ argv[0], i, tests[i].out ? tests[i].out : "NULL",
+ result ? result : "NULL");
+ ++errors;
+ continue;
+ }
+
+ if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
+ {
+ printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
+ argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
+ buf);
+ ++errors;
+ continue;
+ }
+
+ if (!tests[i].out && errno != tests[i].error)
+ {
+ printf ("%s: flunked test %d (expected errno %d, got %d)\n",
+ argv[0], i, tests[i].error, errno);
+ ++errors;
+ continue;
+ }
+
+#ifndef __UCLIBC__
+ /* we choose to crash in uClibc when given a NULL */
+ char *result2 = realpath (tests[i].in, NULL);
+ if ((result2 == NULL && result != NULL)
+ || (result2 != NULL && strcmp (result, result2) != 0))
+ {
+ printf ("\
+%s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
+ argv[0], result2, result);
+ ++errors;
+ }
+ free (result2);
+#endif
+ }
+
+ getcwd (buf, sizeof(buf));
+ if (strcmp (buf, cwd))
+ {
+ printf ("%s: current working directory changed from %s to %s\n",
+ argv[0], cwd, buf);
+ ++errors;
+ }
+
+ if (fd >= 0)
+ {
+ close (fd);
+ unlink ("doesExist/someFile");
+ }
+
+ if (has_dir)
+ rmdir ("doesExist");
+
+ for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
+ unlink (symlinks[i].name);
+
+ if (errors != 0)
+ {
+ printf ("%d errors.\n", errors);
+ return EXIT_FAILURE;
+ }
+
+ puts ("No errors.");
+ return EXIT_SUCCESS;
+}
diff --git a/test/stdlib/testatexit.c b/test/stdlib/testatexit.c
index 3d4856df3..01874fdde 100644
--- a/test/stdlib/testatexit.c
+++ b/test/stdlib/testatexit.c
@@ -13,31 +13,38 @@ typedef void (*vfuncp) (void);
/* All functions call exit(), in order to test that exit functions can call
* exit() without screwing everything up. :)
*/
-static void exitfunc0(void) { printf("Executing exitfunc0.\n"); exit(0);}
-static void exitfunc1(void) { printf("Executing exitfunc1.\n"); exit(0);}
-static void exitfunc2(void) { printf("Executing exitfunc2.\n"); exit(0);}
-static void exitfunc3(void) { printf("Executing exitfunc3.\n"); exit(0);}
-static void exitfunc4(void) { printf("Executing exitfunc4.\n"); exit(0);}
-static void exitfunc5(void) { printf("Executing exitfunc5.\n"); exit(0);}
-static void exitfunc6(void) { printf("Executing exitfunc6.\n"); exit(0);}
-static void exitfunc7(void) { printf("Executing exitfunc7.\n"); exit(0);}
-static void exitfunc8(void) { printf("Executing exitfunc8.\n"); exit(0);}
-static void exitfunc9(void) { printf("Executing exitfunc9.\n"); exit(0);}
-static void exitfunc10(void) { printf("Executing exitfunc10.\n"); exit(0);}
-static void exitfunc11(void) { printf("Executing exitfunc11.\n"); exit(0);}
-static void exitfunc12(void) { printf("Executing exitfunc12.\n"); exit(0);}
-static void exitfunc13(void) { printf("Executing exitfunc13.\n"); exit(0);}
-static void exitfunc14(void) { printf("Executing exitfunc14.\n"); exit(0);}
-static void exitfunc15(void) { printf("Executing exitfunc15.\n"); exit(0);}
-static void exitfunc16(void) { printf("Executing exitfunc16.\n"); exit(0);}
-static void exitfunc17(void) { printf("Executing exitfunc17.\n"); exit(0);}
-static void exitfunc18(void) { printf("Executing exitfunc18.\n"); exit(0);}
-static void exitfunc19(void) { printf("Executing exitfunc19.\n"); exit(0);}
-static void exitfunc20(void) { printf("Executing exitfunc20.\n"); exit(0);}
-static void exitfunc21(void) { printf("Executing exitfunc21.\n"); exit(0);}
-static void exitfunc22(void) { printf("Executing exitfunc22.\n"); exit(0);}
-static void exitfunc23(void) { printf("Executing exitfunc23.\n"); exit(0);}
-static void exitfunc24(void) { printf("Executing exitfunc24.\n"); exit(0);}
+#define make_exitfunc(num) \
+__attribute__ ((__noreturn__)) static \
+void exitfunc##num(void) \
+{ \
+ printf("Executing exitfunc"#num".\n"); \
+ exit(0); \
+}
+make_exitfunc(0)
+make_exitfunc(1)
+make_exitfunc(2)
+make_exitfunc(3)
+make_exitfunc(4)
+make_exitfunc(5)
+make_exitfunc(6)
+make_exitfunc(7)
+make_exitfunc(8)
+make_exitfunc(9)
+make_exitfunc(10)
+make_exitfunc(11)
+make_exitfunc(12)
+make_exitfunc(13)
+make_exitfunc(14)
+make_exitfunc(15)
+make_exitfunc(16)
+make_exitfunc(17)
+make_exitfunc(18)
+make_exitfunc(19)
+make_exitfunc(20)
+make_exitfunc(21)
+make_exitfunc(22)
+make_exitfunc(23)
+make_exitfunc(24)
static vfuncp func_table[] =
{
@@ -72,4 +79,3 @@ main ( void )
return 0;
}
-
diff --git a/test/stdlib/teston_exit.c b/test/stdlib/teston_exit.c
index c5789b083..f7e8fd004 100644
--- a/test/stdlib/teston_exit.c
+++ b/test/stdlib/teston_exit.c
@@ -14,31 +14,38 @@ typedef void (*efuncp) (int, void *);
* exit() without screwing everything up. The value passed in through arg gets
* used as the next exit status.
*/
-static void exitfunc0(int status, void *arg) { printf("Executing exitfunc0 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc1(int status, void *arg) { printf("Executing exitfunc1 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc2(int status, void *arg) { printf("Executing exitfunc2 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc3(int status, void *arg) { printf("Executing exitfunc3 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc4(int status, void *arg) { printf("Executing exitfunc4 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc5(int status, void *arg) { printf("Executing exitfunc5 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc6(int status, void *arg) { printf("Executing exitfunc6 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc7(int status, void *arg) { printf("Executing exitfunc7 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc8(int status, void *arg) { printf("Executing exitfunc8 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc9(int status, void *arg) { printf("Executing exitfunc9 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc10(int status, void *arg) { printf("Executing exitfunc10 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc11(int status, void *arg) { printf("Executing exitfunc11 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc12(int status, void *arg) { printf("Executing exitfunc12 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc13(int status, void *arg) { printf("Executing exitfunc13 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc14(int status, void *arg) { printf("Executing exitfunc14 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc15(int status, void *arg) { printf("Executing exitfunc15 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc16(int status, void *arg) { printf("Executing exitfunc16 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc17(int status, void *arg) { printf("Executing exitfunc17 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc18(int status, void *arg) { printf("Executing exitfunc18 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc19(int status, void *arg) { printf("Executing exitfunc19 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc20(int status, void *arg) { printf("Executing exitfunc20 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc21(int status, void *arg) { printf("Executing exitfunc21 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc22(int status, void *arg) { printf("Executing exitfunc22 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc23(int status, void *arg) { printf("Executing exitfunc23 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
-static void exitfunc24(int status, void *arg) { printf("Executing exitfunc24 (status=%d, arg=%lu)\n", status, (unsigned long)arg); exit((unsigned long)arg);}
+#define make_exitfunc(num) \
+__attribute__ ((__noreturn__)) static \
+void exitfunc##num(int status, void *arg) \
+{ \
+ printf("Executing exitfunc"#num" (status=%d, arg=%lu)\n", status, (unsigned long)arg); \
+ exit((unsigned long)arg); \
+}
+make_exitfunc(0)
+make_exitfunc(1)
+make_exitfunc(2)
+make_exitfunc(3)
+make_exitfunc(4)
+make_exitfunc(5)
+make_exitfunc(6)
+make_exitfunc(7)
+make_exitfunc(8)
+make_exitfunc(9)
+make_exitfunc(10)
+make_exitfunc(11)
+make_exitfunc(12)
+make_exitfunc(13)
+make_exitfunc(14)
+make_exitfunc(15)
+make_exitfunc(16)
+make_exitfunc(17)
+make_exitfunc(18)
+make_exitfunc(19)
+make_exitfunc(20)
+make_exitfunc(21)
+make_exitfunc(22)
+make_exitfunc(23)
+make_exitfunc(24)
static efuncp func_table[] =
{