This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19 #include <unistd.h>
20
21 #include "signature.h"
22 SIGNATURE_CHECK (getcwd, char *, (char *, size_t));
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "macros.h"
30
31 int
32 main (int argc, char **argv)
33 {
34 char *pwd1;
35 char *pwd2;
36
37 if (1 < argc)
38 {
39 if (chdir (argv[1]) == 0)
40 printf ("changed to directory %s\n", argv[1]);
41 }
42
43 pwd1 = getcwd (NULL, 0);
44 ASSERT (pwd1 && *pwd1);
45 if (1 < argc)
46 printf ("cwd=%s\n", pwd1);
47
48
49 ASSERT (chdir (pwd1) == 0);
50 ASSERT (chdir (".//./.") == 0);
51
52
53 pwd2 = getcwd (NULL, 0);
54 ASSERT (pwd2);
55 ASSERT (strcmp (pwd1, pwd2) == 0);
56 free (pwd2);
57 {
58 size_t len = strlen (pwd1);
59 ssize_t i = len - 10;
60 if (i < 1)
61 i = 1;
62 pwd2 = getcwd (NULL, len + 1);
63 ASSERT (pwd2);
64 free (pwd2);
65 pwd2 = malloc (len + 2);
66 for ( ; i <= len; i++)
67 {
68 char *tmp;
69 errno = 0;
70 ASSERT (getcwd (pwd2, i) == NULL);
71 ASSERT (errno == ERANGE);
72
73 errno = 0;
74 tmp = getcwd (NULL, i);
75 if (tmp)
76 {
77 ASSERT (strcmp (pwd1, tmp) == 0);
78 free (tmp);
79 }
80 else
81 {
82 ASSERT (errno == ERANGE);
83 }
84 }
85 ASSERT (getcwd (pwd2, len + 1) == pwd2);
86 pwd2[len] = '/';
87 pwd2[len + 1] = '\0';
88 }
89 ASSERT (strstr (pwd2, "/./") == NULL);
90 ASSERT (strstr (pwd2, "/../") == NULL);
91 ASSERT (strstr (pwd2 + 1 + (pwd2[1] == '/'), "//") == NULL);
92
93
94 errno = 0;
95 ASSERT (getcwd(pwd2, 0) == NULL);
96 ASSERT (errno == EINVAL);
97
98 free (pwd1);
99 free (pwd2);
100
101 return 0;
102 }