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.
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.
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]
23 * Copyright 2015 PALO, Richard.
27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
41 #include <sys/types.h>
51 static void rmslash(unsigned char *string
);
53 extern const char longpwd
[];
55 extern char longpwd
[];
57 extern char *getcwd();
59 unsigned char cwdname
[PATH_MAX
+1];
61 static int didpwd
= FALSE
;
64 cwd(unsigned char *dir
)
69 /* First remove extra /'s */
73 /* Now remove any .'s */
78 while (*pdir
) { /* remove /./ by itself */
79 if ((*pdir
== DOT
) && (*(pdir
+1) == SLASH
)) {
84 while ((*pdir
) && (*pdir
!= SLASH
))
89 /* take care of trailing /. */
90 if (*(--pdir
) == DOT
&& pdir
> dir
&& *(--pdir
) == SLASH
) {
99 /* Remove extra /'s */
103 /* Now that the dir is canonicalized, process it */
105 if (*dir
== DOT
&& *(dir
+1) == NULLCHAR
) {
124 pcwd
= cwdname
+ length(cwdname
) - 1;
125 if (pcwd
!= cwdname
+1)
131 (*(dir
+2) == SLASH
|| *(dir
+2) == NULLCHAR
)) {
132 /* Parent directory, so backup one */
134 if (pcwd
> cwdname
+2)
136 while (*(--pcwd
) != SLASH
)
145 if (pcwd
>= &cwdname
[PATH_MAX
+1]) {
150 while ((*dir
) && (*dir
!= SLASH
)) {
151 if (pcwd
>= &cwdname
[PATH_MAX
+1]) {
158 if (pcwd
>= &cwdname
[PATH_MAX
+1]) {
165 if (pcwd
>= &cwdname
[PATH_MAX
+1]) {
172 if (pcwd
> cwdname
&& *pcwd
== SLASH
) {
173 /* Remove trailing / */
182 struct stat stat1
, stat2
;
184 /* check if there are any symbolic links in pathname */
189 if (didpwd
== PARTLY
) {
194 } while (c
!= SLASH
&& c
!= NULLCHAR
);
196 if (lstat((char *)cwdname
, &stat1
) == -1 ||
197 (stat1
.st_mode
& S_IFMT
) == S_IFLNK
) {
208 if (stat((char *)cwdname
, &stat1
) == -1) {
213 * check if ino's and dev's match; pathname could
214 * consist of symbolic links with ".."
217 if (stat(".", &stat2
) == -1 ||
218 stat1
.st_dev
!= stat2
.st_dev
||
219 stat1
.st_ino
!= stat2
.st_ino
)
227 if (didpwd
== FALSE
) {
228 if (getcwd((char *)cwdname
, PATH_MAX
+1) == NULL
)
236 * Print the current working directory.
245 if (didpwd
== FALSE
) {
246 if (getcwd((char *)cwdname
, PATH_MAX
+1) == NULL
) {
247 if (errno
&& errno
!= ERANGE
)
255 for (cp
= cwdname
; *cp
; cp
++) {
263 * This routine will remove repeated slashes from string.
267 rmslash(unsigned char *string
)
269 unsigned char *pstring
;
273 if (*pstring
== SLASH
&& *(pstring
+1) == SLASH
) {
274 /* Remove repeated SLASH's */
276 movstr(pstring
+1, pstring
);
283 if (pstring
> string
&& *pstring
== SLASH
) {
284 /* Remove trailing / */