tests: fix license on several tests
[gnulib.git] / tests / test-getcwd.c
1 /* Test of getcwd() function.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "macros.h"
29
30 int
31 main (int argc, char **argv)
32 {
33   char *pwd1;
34   char *pwd2;
35   /* If the user provides an argument, attempt to chdir there first.  */
36   if (1 < argc)
37     {
38       if (chdir (argv[1]) == 0)
39         printf ("changed to directory %s\n", argv[1]);
40     }
41
42   pwd1 = getcwd (NULL, 0);
43   ASSERT (pwd1 && *pwd1);
44   if (1 < argc)
45     printf ("cwd=%s\n", pwd1);
46
47   /* Make sure the result is usable.  */
48   ASSERT (chdir (pwd1) == 0);
49   ASSERT (chdir ("././.") == 0);
50
51   /* Make sure that result is normalized.  */
52   pwd2 = getcwd (NULL, 0);
53   ASSERT (pwd2);
54   ASSERT (strcmp (pwd1, pwd2) == 0);
55   free (pwd2);
56   {
57     size_t len = strlen (pwd1);
58     ssize_t i = len - 10;
59     if (i < 0)
60       i = 0;
61     pwd2 = malloc (len + 2);
62     for ( ; i < len; i++)
63       ASSERT (getcwd (pwd2, i) == NULL);
64     pwd2 = getcwd (pwd2, len + 1);
65     ASSERT (pwd2);
66     pwd2[len] = '/';
67     pwd2[len + 1] = '\0';
68   }
69   ASSERT (strstr (pwd2, "/./") == NULL);
70   ASSERT (strstr (pwd2, "/../") == NULL);
71
72   free (pwd1);
73   free (pwd2);
74
75   return 0;
76 }