1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.14.6.1 */ 27*7c478bd9Sstevel@tonic-gate /* 28*7c478bd9Sstevel@tonic-gate * UNIX shell 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #include "mac.h" 32*7c478bd9Sstevel@tonic-gate #include <errno.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 35*7c478bd9Sstevel@tonic-gate #include <limits.h> 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #define DOT '.' 38*7c478bd9Sstevel@tonic-gate #define NULL 0 39*7c478bd9Sstevel@tonic-gate #define SLASH '/' 40*7c478bd9Sstevel@tonic-gate #define PARTLY 2 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate static void rmslash(); 43*7c478bd9Sstevel@tonic-gate #ifdef __STDC__ 44*7c478bd9Sstevel@tonic-gate extern const char longpwd[]; 45*7c478bd9Sstevel@tonic-gate #else 46*7c478bd9Sstevel@tonic-gate extern char longpwd[]; 47*7c478bd9Sstevel@tonic-gate #endif 48*7c478bd9Sstevel@tonic-gate extern char *getcwd(); 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate unsigned char cwdname[PATH_MAX+1]; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate static int didpwd = FALSE; 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate cwd(dir) 55*7c478bd9Sstevel@tonic-gate register unsigned char *dir; 56*7c478bd9Sstevel@tonic-gate { 57*7c478bd9Sstevel@tonic-gate register unsigned char *pcwd; 58*7c478bd9Sstevel@tonic-gate register unsigned char *pdir; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate /* First remove extra /'s */ 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate rmslash(dir); 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* Now remove any .'s */ 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate pdir = dir; 67*7c478bd9Sstevel@tonic-gate if(*dir == SLASH) 68*7c478bd9Sstevel@tonic-gate pdir++; 69*7c478bd9Sstevel@tonic-gate while(*pdir) /* remove /./ by itself */ 70*7c478bd9Sstevel@tonic-gate { 71*7c478bd9Sstevel@tonic-gate if((*pdir==DOT) && (*(pdir+1)==SLASH)) 72*7c478bd9Sstevel@tonic-gate { 73*7c478bd9Sstevel@tonic-gate movstr(pdir+2, pdir); 74*7c478bd9Sstevel@tonic-gate continue; 75*7c478bd9Sstevel@tonic-gate } 76*7c478bd9Sstevel@tonic-gate pdir++; 77*7c478bd9Sstevel@tonic-gate while ((*pdir) && (*pdir != SLASH)) 78*7c478bd9Sstevel@tonic-gate pdir++; 79*7c478bd9Sstevel@tonic-gate if (*pdir) 80*7c478bd9Sstevel@tonic-gate pdir++; 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate /* take care of trailing /. */ 83*7c478bd9Sstevel@tonic-gate if(*(--pdir)==DOT && pdir > dir && *(--pdir)==SLASH) { 84*7c478bd9Sstevel@tonic-gate if(pdir > dir) { 85*7c478bd9Sstevel@tonic-gate *pdir = NULL; 86*7c478bd9Sstevel@tonic-gate } else { 87*7c478bd9Sstevel@tonic-gate *(pdir+1) = NULL; 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate /* Remove extra /'s */ 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate rmslash(dir); 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate /* Now that the dir is canonicalized, process it */ 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate if(*dir==DOT && *(dir+1)==NULL) 99*7c478bd9Sstevel@tonic-gate { 100*7c478bd9Sstevel@tonic-gate return; 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate if(*dir==SLASH) 105*7c478bd9Sstevel@tonic-gate { 106*7c478bd9Sstevel@tonic-gate /* Absolute path */ 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate pcwd = cwdname; 109*7c478bd9Sstevel@tonic-gate *pcwd++ = *dir++; 110*7c478bd9Sstevel@tonic-gate didpwd = PARTLY; 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate else 113*7c478bd9Sstevel@tonic-gate { 114*7c478bd9Sstevel@tonic-gate /* Relative path */ 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if (didpwd == FALSE) 117*7c478bd9Sstevel@tonic-gate return; 118*7c478bd9Sstevel@tonic-gate didpwd = PARTLY; 119*7c478bd9Sstevel@tonic-gate pcwd = cwdname + length(cwdname) - 1; 120*7c478bd9Sstevel@tonic-gate if(pcwd != cwdname+1) 121*7c478bd9Sstevel@tonic-gate *pcwd++ = SLASH; 122*7c478bd9Sstevel@tonic-gate } 123*7c478bd9Sstevel@tonic-gate while(*dir) 124*7c478bd9Sstevel@tonic-gate { 125*7c478bd9Sstevel@tonic-gate if(*dir==DOT && 126*7c478bd9Sstevel@tonic-gate *(dir+1)==DOT && 127*7c478bd9Sstevel@tonic-gate (*(dir+2)==SLASH || *(dir+2)==NULL)) 128*7c478bd9Sstevel@tonic-gate { 129*7c478bd9Sstevel@tonic-gate /* Parent directory, so backup one */ 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate if( pcwd > cwdname+2 ) 132*7c478bd9Sstevel@tonic-gate --pcwd; 133*7c478bd9Sstevel@tonic-gate while(*(--pcwd) != SLASH) 134*7c478bd9Sstevel@tonic-gate ; 135*7c478bd9Sstevel@tonic-gate pcwd++; 136*7c478bd9Sstevel@tonic-gate dir += 2; 137*7c478bd9Sstevel@tonic-gate if(*dir==SLASH) 138*7c478bd9Sstevel@tonic-gate { 139*7c478bd9Sstevel@tonic-gate dir++; 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate continue; 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate if (pcwd >= &cwdname[PATH_MAX+1]) 144*7c478bd9Sstevel@tonic-gate { 145*7c478bd9Sstevel@tonic-gate didpwd=FALSE; 146*7c478bd9Sstevel@tonic-gate return; 147*7c478bd9Sstevel@tonic-gate } 148*7c478bd9Sstevel@tonic-gate *pcwd++ = *dir++; 149*7c478bd9Sstevel@tonic-gate while((*dir) && (*dir != SLASH)) 150*7c478bd9Sstevel@tonic-gate { 151*7c478bd9Sstevel@tonic-gate if (pcwd >= &cwdname[PATH_MAX+1]) 152*7c478bd9Sstevel@tonic-gate { 153*7c478bd9Sstevel@tonic-gate didpwd=FALSE; 154*7c478bd9Sstevel@tonic-gate return; 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate *pcwd++ = *dir++; 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate if (*dir) 159*7c478bd9Sstevel@tonic-gate { 160*7c478bd9Sstevel@tonic-gate if (pcwd >= &cwdname[PATH_MAX+1]) 161*7c478bd9Sstevel@tonic-gate { 162*7c478bd9Sstevel@tonic-gate didpwd=FALSE; 163*7c478bd9Sstevel@tonic-gate return; 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate *pcwd++ = *dir++; 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate if (pcwd >= &cwdname[PATH_MAX+1]) 169*7c478bd9Sstevel@tonic-gate { 170*7c478bd9Sstevel@tonic-gate didpwd=FALSE; 171*7c478bd9Sstevel@tonic-gate return; 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate *pcwd = NULL; 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate --pcwd; 176*7c478bd9Sstevel@tonic-gate if(pcwd>cwdname && *pcwd==SLASH) 177*7c478bd9Sstevel@tonic-gate { 178*7c478bd9Sstevel@tonic-gate /* Remove trailing / */ 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate *pcwd = NULL; 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate return; 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate void 186*7c478bd9Sstevel@tonic-gate cwd2() 187*7c478bd9Sstevel@tonic-gate { 188*7c478bd9Sstevel@tonic-gate struct stat stat1, stat2; 189*7c478bd9Sstevel@tonic-gate unsigned char *pcwd; 190*7c478bd9Sstevel@tonic-gate /* check if there are any symbolic links in pathname */ 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate if(didpwd == FALSE) 193*7c478bd9Sstevel@tonic-gate return; 194*7c478bd9Sstevel@tonic-gate pcwd = cwdname + 1; 195*7c478bd9Sstevel@tonic-gate if(didpwd == PARTLY) { 196*7c478bd9Sstevel@tonic-gate while (*pcwd) 197*7c478bd9Sstevel@tonic-gate { 198*7c478bd9Sstevel@tonic-gate char c; 199*7c478bd9Sstevel@tonic-gate while((c = *pcwd++) != SLASH && c != '\0'); 200*7c478bd9Sstevel@tonic-gate *--pcwd = '\0'; 201*7c478bd9Sstevel@tonic-gate if (lstat((char *)cwdname, &stat1) == -1 202*7c478bd9Sstevel@tonic-gate || (stat1.st_mode & S_IFMT) == S_IFLNK) { 203*7c478bd9Sstevel@tonic-gate didpwd = FALSE; 204*7c478bd9Sstevel@tonic-gate *pcwd = c; 205*7c478bd9Sstevel@tonic-gate return; 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate *pcwd = c; 208*7c478bd9Sstevel@tonic-gate if(c) 209*7c478bd9Sstevel@tonic-gate pcwd++; 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate didpwd = TRUE; 212*7c478bd9Sstevel@tonic-gate } else 213*7c478bd9Sstevel@tonic-gate if (stat((char *)cwdname, &stat1) == -1) { 214*7c478bd9Sstevel@tonic-gate didpwd = FALSE; 215*7c478bd9Sstevel@tonic-gate return; 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate /* 218*7c478bd9Sstevel@tonic-gate * check if ino's and dev's match; pathname could 219*7c478bd9Sstevel@tonic-gate * consist of symbolic links with ".." 220*7c478bd9Sstevel@tonic-gate */ 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate if (stat(".", &stat2) == -1 223*7c478bd9Sstevel@tonic-gate || stat1.st_dev != stat2.st_dev 224*7c478bd9Sstevel@tonic-gate || stat1.st_ino != stat2.st_ino) 225*7c478bd9Sstevel@tonic-gate didpwd = FALSE; 226*7c478bd9Sstevel@tonic-gate return; 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate unsigned char * 230*7c478bd9Sstevel@tonic-gate cwdget() 231*7c478bd9Sstevel@tonic-gate { 232*7c478bd9Sstevel@tonic-gate cwd2(); 233*7c478bd9Sstevel@tonic-gate if (didpwd == FALSE) { 234*7c478bd9Sstevel@tonic-gate if(getcwd(cwdname, PATH_MAX+1) == (char *)0) 235*7c478bd9Sstevel@tonic-gate *cwdname = 0; 236*7c478bd9Sstevel@tonic-gate didpwd = TRUE; 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate return (cwdname); 239*7c478bd9Sstevel@tonic-gate } 240*7c478bd9Sstevel@tonic-gate 241*7c478bd9Sstevel@tonic-gate /* 242*7c478bd9Sstevel@tonic-gate * Print the current working directory. 243*7c478bd9Sstevel@tonic-gate */ 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate cwdprint() 246*7c478bd9Sstevel@tonic-gate { 247*7c478bd9Sstevel@tonic-gate register unsigned char *cp; 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate cwd2(); 250*7c478bd9Sstevel@tonic-gate if (didpwd == FALSE) { 251*7c478bd9Sstevel@tonic-gate if(getcwd(cwdname, PATH_MAX+1) == (char *)0) { 252*7c478bd9Sstevel@tonic-gate if(errno && errno != ERANGE) 253*7c478bd9Sstevel@tonic-gate error("cannot determine current directory"); 254*7c478bd9Sstevel@tonic-gate else 255*7c478bd9Sstevel@tonic-gate error(longpwd); 256*7c478bd9Sstevel@tonic-gate } 257*7c478bd9Sstevel@tonic-gate didpwd = TRUE; 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate for (cp = cwdname; *cp; cp++) { 261*7c478bd9Sstevel@tonic-gate prc_buff(*cp); 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate prc_buff(NL); 265*7c478bd9Sstevel@tonic-gate return; 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate /* 269*7c478bd9Sstevel@tonic-gate * This routine will remove repeated slashes from string. 270*7c478bd9Sstevel@tonic-gate */ 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate static void 273*7c478bd9Sstevel@tonic-gate rmslash(string) 274*7c478bd9Sstevel@tonic-gate unsigned char *string; 275*7c478bd9Sstevel@tonic-gate { 276*7c478bd9Sstevel@tonic-gate register unsigned char *pstring; 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate pstring = string; 279*7c478bd9Sstevel@tonic-gate while(*pstring) 280*7c478bd9Sstevel@tonic-gate { 281*7c478bd9Sstevel@tonic-gate if(*pstring==SLASH && *(pstring+1)==SLASH) 282*7c478bd9Sstevel@tonic-gate { 283*7c478bd9Sstevel@tonic-gate /* Remove repeated SLASH's */ 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate movstr(pstring+1, pstring); 286*7c478bd9Sstevel@tonic-gate continue; 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate pstring++; 289*7c478bd9Sstevel@tonic-gate } 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate --pstring; 292*7c478bd9Sstevel@tonic-gate if(pstring>string && *pstring==SLASH) 293*7c478bd9Sstevel@tonic-gate { 294*7c478bd9Sstevel@tonic-gate /* Remove trailing / */ 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate *pstring = NULL; 297*7c478bd9Sstevel@tonic-gate } 298*7c478bd9Sstevel@tonic-gate return; 299*7c478bd9Sstevel@tonic-gate } 300