1da2e3ebdSchin /*********************************************************************** 2da2e3ebdSchin * * 3da2e3ebdSchin * This software is part of the ast package * 4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property * 5da2e3ebdSchin * and is licensed under the * 6da2e3ebdSchin * Common Public License, Version 1.0 * 77c2fbfb3SApril Chin * by AT&T Intellectual Property * 8da2e3ebdSchin * * 9da2e3ebdSchin * A copy of the License is available at * 10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt * 11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12da2e3ebdSchin * * 13da2e3ebdSchin * Information and Software Systems Research * 14da2e3ebdSchin * AT&T Research * 15da2e3ebdSchin * Florham Park NJ * 16da2e3ebdSchin * * 17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> * 18da2e3ebdSchin * David Korn <dgk@research.att.com> * 19da2e3ebdSchin * Phong Vo <kpv@research.att.com> * 20da2e3ebdSchin * * 21da2e3ebdSchin ***********************************************************************/ 22da2e3ebdSchin #pragma prototyped 23da2e3ebdSchin /* 24da2e3ebdSchin * K. P. Vo 25da2e3ebdSchin * G. S. Fowler 26da2e3ebdSchin * AT&T Research 27da2e3ebdSchin */ 28da2e3ebdSchin 29da2e3ebdSchin #include <ast.h> 30da2e3ebdSchin #include <error.h> 31da2e3ebdSchin #include <stk.h> 32da2e3ebdSchin 33da2e3ebdSchin #if DEBUG 34da2e3ebdSchin 35da2e3ebdSchin #undef PATH_MAX 36da2e3ebdSchin 37da2e3ebdSchin #define PATH_MAX 16 38da2e3ebdSchin 39da2e3ebdSchin static int vchdir(const char * path)40da2e3ebdSchinvchdir(const char* path) 41da2e3ebdSchin { 42da2e3ebdSchin int n; 43da2e3ebdSchin 44da2e3ebdSchin if (strlen(path) >= PATH_MAX) 45da2e3ebdSchin { 46da2e3ebdSchin errno = ENAMETOOLONG; 47da2e3ebdSchin n = -1; 48da2e3ebdSchin } 49da2e3ebdSchin else n = chdir(path); 50da2e3ebdSchin return n; 51da2e3ebdSchin } 52da2e3ebdSchin 53da2e3ebdSchin #define chdir(p) vchdir(p) 54da2e3ebdSchin 55da2e3ebdSchin #endif 56da2e3ebdSchin 57da2e3ebdSchin /* 58da2e3ebdSchin * set the current directory to path 59da2e3ebdSchin * if path is long and home!=0 then pathcd(home,0) 60da2e3ebdSchin * is called on intermediate chdir errors 61da2e3ebdSchin */ 62da2e3ebdSchin 63da2e3ebdSchin int pathcd(const char * path,const char * home)64da2e3ebdSchinpathcd(const char* path, const char* home) 65da2e3ebdSchin { 66da2e3ebdSchin register char* p = (char*)path; 67da2e3ebdSchin register char* s; 68da2e3ebdSchin register int n; 69da2e3ebdSchin int i; 70da2e3ebdSchin int r; 71da2e3ebdSchin 72da2e3ebdSchin r = 0; 73da2e3ebdSchin for (;;) 74da2e3ebdSchin { 75da2e3ebdSchin /* 76da2e3ebdSchin * this should work 99% of the time 77da2e3ebdSchin */ 78da2e3ebdSchin 79da2e3ebdSchin if (!chdir(p)) 80da2e3ebdSchin return r; 81da2e3ebdSchin 82da2e3ebdSchin /* 83da2e3ebdSchin * chdir failed 84da2e3ebdSchin */ 85da2e3ebdSchin 86da2e3ebdSchin if ((n = strlen(p)) < PATH_MAX) 87da2e3ebdSchin return -1; 88da2e3ebdSchin #ifdef ENAMETOOLONG 89da2e3ebdSchin if (errno != ENAMETOOLONG) 90da2e3ebdSchin return -1; 91da2e3ebdSchin #endif 92da2e3ebdSchin 93da2e3ebdSchin /* 94da2e3ebdSchin * path is too long -- copy so it can be modified in place 95da2e3ebdSchin */ 96da2e3ebdSchin 97da2e3ebdSchin i = stktell(stkstd); 98da2e3ebdSchin sfputr(stkstd, p, 0); 99da2e3ebdSchin stkseek(stkstd, i); 100da2e3ebdSchin p = stkptr(stkstd, i); 101da2e3ebdSchin for (;;) 102da2e3ebdSchin { 103da2e3ebdSchin /* 104da2e3ebdSchin * get a short prefix component 105da2e3ebdSchin */ 106da2e3ebdSchin 107da2e3ebdSchin s = p + PATH_MAX; 108da2e3ebdSchin while (--s >= p && *s != '/'); 109da2e3ebdSchin if (s <= p) 110da2e3ebdSchin break; 111da2e3ebdSchin 112da2e3ebdSchin /* 113da2e3ebdSchin * chdir to the prefix 114da2e3ebdSchin */ 115da2e3ebdSchin 116da2e3ebdSchin *s++ = 0; 117da2e3ebdSchin if (chdir(p)) 118da2e3ebdSchin break; 119da2e3ebdSchin 120da2e3ebdSchin /* 121da2e3ebdSchin * do the remainder 122da2e3ebdSchin */ 123da2e3ebdSchin 124da2e3ebdSchin if ((n -= s - p) < PATH_MAX) 125da2e3ebdSchin { 126da2e3ebdSchin if (chdir(s)) 127da2e3ebdSchin break; 128da2e3ebdSchin return r; 129da2e3ebdSchin } 130da2e3ebdSchin p = s; 131da2e3ebdSchin } 132da2e3ebdSchin 133da2e3ebdSchin /* 134da2e3ebdSchin * try to recover back to home 135da2e3ebdSchin */ 136da2e3ebdSchin 137da2e3ebdSchin if (!(p = (char*)home)) 138da2e3ebdSchin return -1; 139da2e3ebdSchin home = 0; 140da2e3ebdSchin r = -1; 141da2e3ebdSchin } 142da2e3ebdSchin } 143