1dda95d6f4b1d7f4fb114894cff8e9e77b66deac
[platform/upstream/diffutils.git] / gnulib-tests / test-getcwd-lgpl.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Test of getcwd() function.
4    Copyright (C) 2009-2011 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (getcwd, char *, (char *, size_t));
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "macros.h"
32
33 int
34 main (int argc, char **argv)
35 {
36   char *pwd1;
37   char *pwd2;
38   /* If the user provides an argument, attempt to chdir there first.  */
39   if (1 < argc)
40     {
41       if (chdir (argv[1]) == 0)
42         printf ("changed to directory %s\n", argv[1]);
43     }
44
45   pwd1 = getcwd (NULL, 0);
46   ASSERT (pwd1 && *pwd1);
47   if (1 < argc)
48     printf ("cwd=%s\n", pwd1);
49
50   /* Make sure the result is usable.  */
51   ASSERT (chdir (pwd1) == 0);
52   ASSERT (chdir (".//./.") == 0);
53
54   /* Make sure that result is normalized.  */
55   pwd2 = getcwd (NULL, 0);
56   ASSERT (pwd2);
57   ASSERT (strcmp (pwd1, pwd2) == 0);
58   free (pwd2);
59   {
60     size_t len = strlen (pwd1);
61     ssize_t i = len - 10;
62     if (i < 1)
63       i = 1;
64     pwd2 = getcwd (NULL, len + 1);
65     ASSERT (pwd2);
66     free (pwd2);
67     pwd2 = malloc (len + 2);
68     for ( ; i <= len; i++)
69       {
70         char *tmp;
71         errno = 0;
72         ASSERT (getcwd (pwd2, i) == NULL);
73         ASSERT (errno == ERANGE);
74         /* Allow either glibc or BSD behavior, since POSIX allows both.  */
75         errno = 0;
76         tmp = getcwd (NULL, i);
77         if (tmp)
78           {
79             ASSERT (strcmp (pwd1, tmp) == 0);
80             free (tmp);
81           }
82         else
83           {
84             ASSERT (errno == ERANGE);
85           }
86       }
87     ASSERT (getcwd (pwd2, len + 1) == pwd2);
88     pwd2[len] = '/';
89     pwd2[len + 1] = '\0';
90   }
91   ASSERT (strstr (pwd2, "/./") == NULL);
92   ASSERT (strstr (pwd2, "/../") == NULL);
93   ASSERT (strstr (pwd2 + 1 + (pwd2[1] == '/'), "//") == NULL);
94
95   /* Validate a POSIX requirement on size.  */
96   errno = 0;
97   ASSERT (getcwd(pwd2, 0) == NULL);
98   ASSERT (errno == EINVAL);
99
100   free (pwd1);
101   free (pwd2);
102
103   return 0;
104 }