/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1989-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Eduardo Krell <[email protected]> *19* *20***********************************************************************/21#pragma prototyped2223#include "3d.h"2425int26chdir3d(const char* path)27{28register char* sp;29char buf[2 * PATH_MAX + 1];3031#if FS32if (!fscall(NiL, MSG_stat, 0, path, &state.path.st))33{34if (state.ret)35return -1;36if (!S_ISDIR(state.path.st.st_mode))37{38errno = ENOTDIR;39return -1;40}41state.level = 1;42}43else44#else45initialize();46#endif47{48if (state.level > 0 && state.pwd && !CHDIR(state.pwd))49state.level = 0;50if (!(sp = pathreal(path, P_SAFE, NiL)))51return -1;52if (CHDIR(sp))53return -1;54}55if (state.pwd)56{57/*58* save absolute path in state.pwd59*/6061if (*path != '/')62{63strcpy(buf, state.pwd);64sp = buf + state.pwdsize;65*sp++ = '/';66}67else68sp = buf;69strcpy(sp, path);70#if 071state.path.level = 0;72#endif73if ((sp = pathcanon(buf, sizeof(buf), 0)) && *(sp - 1) == '.' && *(sp - 2) == '/')74*(sp -= 2) = 0;75state.pwdsize = strcopy(state.pwd, buf) - state.pwd;76memcpy(state.envpwd + sizeof(var_pwd) - 1, state.pwd, state.pwdsize);77state.level = state.path.level;78message((-1, "chdir: %s [%d]", state.pwd, state.level));79}80return 0;81}828384