xref: /illumos-gate/usr/src/lib/libc/port/gen/getcwd.c (revision d4039345c8fe6e54a31d17d91e86e393fdcf401b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
26  */
27 
28 /*
29  * getcwd() returns the pathname of the current working directory.
30  * On error, a NULL pointer is returned and errno is set.
31  */
32 
33 #pragma weak _getcwd = getcwd
34 
35 #include "lint.h"
36 #include <sys/param.h>
37 #include <sys/syscall.h>
38 #include <sys/types.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 char *
46 getcwd(char *pathname, size_t size)
47 {
48 	int alloc = 0;
49 
50 	if (size == 0 && pathname == NULL) {
51 		/*
52 		 * If no size was provided, start with a buffer that should
53 		 * accommodate any normal path and, if it is not big enough,
54 		 * keep doubling it to try and make enough space.
55 		 *
56 		 * Any non-global zone path is longer when observed from the
57 		 * global zone, and some filesystems, including ZFS, support
58 		 * paths much longer than MAXPATHLEN/_PC_PATH_MAX.
59 		 *
60 		 * To protect against unbounded memory usage, cap to 128KiB.
61 		 * This is an arbitrary limit which is far bigger than the
62 		 * length of any practical path on the system.
63 		 */
64 		if ((size = pathconf(".", _PC_PATH_MAX)) == -1)
65 			size = MAXPATHLEN;
66 
67 		while (size <= 0x20000) {
68 			if ((pathname = reallocf(pathname, size)) == NULL) {
69 				errno = ENOMEM;
70 				return (NULL);
71 			}
72 			if (syscall(SYS_getcwd, pathname, size) == 0) {
73 				char *ret;
74 
75 				/*
76 				 * Shrink the buffer to the length actually
77 				 * required to hold the final path.
78 				 */
79 				ret = realloc(pathname, strlen(pathname) + 1);
80 				if (ret == NULL)
81 					return (pathname);
82 
83 				return (ret);
84 			}
85 			if (errno != ERANGE)
86 				break;
87 			size <<= 1;
88 		}
89 		free(pathname);
90 		return (NULL);
91 	}
92 
93 	if (size == 0) {
94 		errno = EINVAL;
95 		return (NULL);
96 	}
97 
98 	if (pathname == NULL)  {
99 		if ((pathname = malloc(size)) == NULL) {
100 			errno = ENOMEM;
101 			return (NULL);
102 		}
103 		alloc = 1;
104 	}
105 
106 	if (syscall(SYS_getcwd, pathname, size) == 0)
107 		return (pathname);
108 
109 	if (alloc)
110 		free(pathname);
111 
112 	return (NULL);
113 }
114