1 /* $NetBSD: test.c,v 1.4 2003/07/26 19:38:46 salo Exp $ */
4 * Regression test for dirname(3).
6 * Written by Jason R. Thorpe <thorpej@NetBSD.org>, Oct. 2002.
21 * The following are taken from the "Sample Input and Output Strings
22 * for dirname()" table in IEEE Std 1003.1-2001.
24 { "/usr/lib", "/usr" },
31 * IEEE Std 1003.1-2001:
33 * If path is a null pointer or points to an empty string,
34 * dirname() shall return a pointer to the string "." .
39 * IEEE Std 1003.1-2001:
41 * Since the meaning of the leading "//" is implementation-defined,
42 * dirname("//foo") may return either "//" or "/" (but nothing else).
44 * The NetBSD implementation returns "/".
48 * Make sure the trailing slashes after the directory name component
49 * get trimmed. The Std does not talk about this, but this is what
50 * Solaris 8's dirname(3) does.
52 { "/usr///lib", "/usr" },
57 int main(int argc
, char *argv
[]);
60 main(int argc
, char *argv
[])
62 char testbuf
[32], *base
;
65 for (i
= 0; test_table
[i
].output
!= NULL
; i
++) {
66 if (test_table
[i
].input
!= NULL
) {
67 assert(strlen(test_table
[i
].input
) < sizeof(testbuf
));
68 strcpy(testbuf
, test_table
[i
].input
);
69 base
= dirname(testbuf
);
74 * dirname(3) is allowed to modify the input buffer.
75 * However, that is considered hostile by some programs,
76 * and so we elect to consider this an error.
78 * This is not a problem, as dirname(3) is also allowed
79 * to return a pointer to a statically-allocated buffer
80 * (it is explicitly not required to be reentrant).
82 if (test_table
[i
].input
!= NULL
&&
83 strcmp(test_table
[i
].input
, testbuf
) != 0) {
85 "Input buffer for \"%s\" was modified\n",
90 /* Make sure the result is correct. */
91 if (strcmp(test_table
[i
].output
, base
) != 0) {
93 "Input \"%s\", output \"%s\", expected \"%s\"\n",
94 test_table
[i
].input
== NULL
? "(null)"
95 : test_table
[i
].input
,
96 base
, test_table
[i
].output
);