2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: cd_pwd.c /main/3 1995/11/01 16:27:31 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF *
29 * AT&T BELL LABORATORIES *
30 * AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN *
31 * ACCORDANCE WITH APPLICABLE AGREEMENTS *
33 * Copyright (c) 1995 AT&T Corp. *
34 * Unpublished & Not for Publication *
35 * All Rights Reserved *
37 * The copyright notice above does not evidence any *
38 * actual or intended publication of such source code *
40 * This software was created by the *
41 * Advanced Software Technology Department *
42 * AT&T Bell Laboratories *
44 * For further information contact *
45 * {research,attmail}!dgk *
47 ***************************************************************/
49 /* : : generated by proto : : */
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__ "C"
59 #define __PROTO__(x) x
61 #define __PARAM__(n,o) n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
74 #define __PROTO__(x) ()
75 #define __OTORP__(x) x
76 #define __PARAM__(n,o) o
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__ ...
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a) va_start(p,a)
92 #define __VA_START__(p,a) va_start(p)
98 #include "variables.h"
101 #include "builtins.h"
104 int b_cd __PARAM__((int argc, char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc; char *argv[];__V_ *extra;){
105 register char *dir, *cdpath="";
106 register const char *dp;
108 int rval,flag = (sh_isoption(SH_PHYSICAL)!=0);
110 Namval_t *opwdnod, *pwdnod;
112 if(sh_isoption(SH_RESTRICTED))
113 error(ERROR_exit(1),e_restricted+4);
114 while((rval = optget(argv,sh_optcd))) switch(rval)
126 error(ERROR_usage(2), opt_arg);
132 if(error_info.errors>0 || argc >2)
133 error(ERROR_usage(2),optusage((char*)0));
134 oldpwd = (char*)sh.pwd;
135 opwdnod = (sh.subshell?sh_assignok(OLDPWDNOD,1):OLDPWDNOD);
136 pwdnod = (sh.subshell?sh_assignok(PWDNOD,1):PWDNOD);
138 dir = sh_substitute(oldpwd,dir,argv[1]);
139 else if(!dir || *dir==0)
140 dir = nv_getval(HOME);
141 else if(*dir == '-' && dir[1]==0)
142 dir = nv_getval(opwdnod);
144 error(ERROR_exit(1),argc==2?e_subst+4:e_direct);
147 cdpath = nv_getval(nv_scoped(CDPNOD));
149 oldpwd = path_pwd(1);
155 /* test for pathname . ./ .. or ../ */
156 if(*(dp=dir+1) == '.')
158 if(*dp==0 || *dp=='/')
165 cdpath=path_join(cdpath,dir);
166 if(*stakptr(PATH_OFFSET)!='/')
168 char *last=(char*)stakfreeze(1);
169 stakseek(PATH_OFFSET);
172 stakputs(last+PATH_OFFSET);
178 stakseek(PATH_MAX+PATH_OFFSET);
180 if(!(cp = pathcanon(stakptr(PATH_OFFSET),PATH_DOTDOT)))
182 /* eliminate trailing '/' */
183 while(*--cp == '/' && cp>stakptr(PATH_OFFSET))
186 if(*(cp=stakptr(PATH_OFFSET))=='/')
187 if(!pathcanon(cp,PATH_DOTDOT))
189 #endif /* SHOPT_FS_3D */
191 if((rval=chdir(path_relative(stakptr(PATH_OFFSET)))) >= 0)
193 if(errno!=ENOENT && saverrno==0)
197 if(rval<0 && *dir=='/' && *(path_relative(stakptr(PATH_OFFSET)))!='/')
199 /* use absolute chdir() if relative chdir() fails */
204 error(ERROR_system(1),"%s:",dir);
207 if(dir == nv_getval(opwdnod) || argc==2)
208 dp = dir; /* print out directory for cd - */
211 dir = stakptr(PATH_OFFSET);
212 if (!(dir=pathcanon(dir,PATH_PHYSICAL)))
214 dir = stakptr(PATH_OFFSET);
215 error(ERROR_system(1),"%s:",dir);
217 stakseek(dir-stakptr(0));
219 dir = (char*)stakfreeze(1)+PATH_OFFSET;
220 if(*dp && *dp!= ':' && strchr(dir,'/'))
221 sfputr(sfstdout,dir,'\n');
224 nv_putval(opwdnod,oldpwd,NV_RDONLY);
228 /* delete trailing '/' */
229 while(--flag>0 && dir[flag]=='/')
231 nv_putval(pwdnod,dir,NV_RDONLY);
232 nv_onattr(pwdnod,NV_NOFREE|NV_EXPORT);
233 sh.pwd = pwdnod->nvalue.cp;
237 int b_pwd __PARAM__((int argc, char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc; char *argv[];__V_ *extra;){
238 register int n, flag = (sh_isoption(SH_PHYSICAL)!=0);
242 while((n = optget(argv,sh_optpwd))) switch(n)
254 error(ERROR_usage(2), opt_arg);
257 if(error_info.errors)
258 error(ERROR_usage(2),optusage((char*)0));
259 if(*(cp = path_pwd(0)) != '/')
260 error(ERROR_system(1), e_pwd);
264 if((flag = mount(e_dot,NIL(char*),FS3D_GET|FS3D_VIEW,0))>=0)
266 cp = (char*)stakseek(++flag+PATH_MAX);
267 mount(e_dot,cp,FS3D_GET|FS3D_VIEW|FS3D_SIZE(flag),0);
270 #endif /* SHOPT_FS_3D */
271 cp = strcpy(stakseek(strlen(cp)+PATH_MAX),cp);
272 pathcanon(cp,PATH_PHYSICAL);
274 sfputr(sfstdout,cp,'\n');