std::string StatCache::readlinkImpl(const std::string& path) { // Punt if path is relative. if (path.size() == 0 || path[0] != '/') { return readlinkSyscall(path); } NodePtr p; { NameNodeMap::const_accessor acc; if (m_lpath2Node.find(acc, path)) { p = acc->second; } } if (p) { return p->readlink(path); } { SimpleLock lock(m_lock); if (mergePath(path, false)) { return readlinkSyscall(path); } { NameNodeMap::const_accessor acc; if (m_lpath2Node.find(acc, path)) { return acc->second->readlink(path, m_lastRefresh); } } } not_reached(); }
int StatCache::statImpl(const std::string& path, struct stat* buf) { // Punt if path is relative. if (path.size() == 0 || path[0] != '/') { return statSyscall(path, buf); } NodePtr p; { NameNodeMap::const_accessor acc; if (m_path2Node.find(acc, path)) { p = acc->second; } } if (p) { return p->stat(path, buf); } { SimpleLock lock(m_lock); if (mergePath(path, true)) { return statSyscall(path, buf); } { NameNodeMap::const_accessor acc; if (m_path2Node.find(acc, path)) { return acc->second->stat(path, buf, m_lastRefresh); } } } not_reached(); }
/* Check the given path for the given file. */ int checkPath(const char *path, const char *file) { char *merged = mergePath(path, file); /* Check if merged path exists. */ int r = fileExists(merged); free(merged); return r; }
void URI::resolve(const URI& relativeURI) { if (!relativeURI._scheme.empty()) { _scheme = relativeURI._scheme; _userInfo = relativeURI._userInfo; _host = relativeURI._host; _port = relativeURI._port; _path = relativeURI._path; _query = relativeURI._query; removeDotSegments(); } else { if (!relativeURI._host.empty()) { _userInfo = relativeURI._userInfo; _host = relativeURI._host; _port = relativeURI._port; _path = relativeURI._path; _query = relativeURI._query; removeDotSegments(); } else { if (relativeURI._path.empty()) { if (!relativeURI._query.empty()) _query = relativeURI._query; } else { if (relativeURI._path[0] == '/') { _path = relativeURI._path; removeDotSegments(); } else { mergePath(relativeURI._path); } _query = relativeURI._query; } } } _fragment = relativeURI._fragment; }
/* * Main Function: runs loop giving user a prompt to enter a command and its * arguments, until the keyword 'exit' is recieved */ int main(int argc, char *argv[]){ char buffer[BUFSIZE]; /* input buffer with command + args */ char cwd[DIRSIZE]; /* current working directory */ char *path = getenv("PATH"); /* PATH env var */ char *execPath; /* PATH where the command is located */ char *token; /* used for tokenizing args and paths */ int pid; /* parent process id (for forking) */ /* List of arguments parsed from buffer. */ char **arguments = malloc(sizeof(char*) * MAXARG); int maxArgs = MAXARG; int numArgs = 0; /* List of paths parsed from PATH. */ char **paths = malloc(sizeof(char*) * MAXPATH); int numPaths = 0; int maxPaths = MAXPATH; /* Split the path env var into an array with individual paths. */ token = strtok(path, PATH_DELIM); do { /* Double the size of the paths array if it's full. */ if (numPaths >= maxPaths - 1) { maxPaths *= 2; paths = realloc(paths, sizeof(char*) * maxPaths); } paths[numPaths] = token; numPaths++; } while ((token = strtok(NULL, PATH_DELIM))); /* Debug info for checking paths. */ if (DEBUG) { for (int i = 0; i < numPaths; i++) { printf("Path %d: %s\n", i, paths[i]); } } /* Continuously prompt for and execute commands until "exit" is input. */ while (1){ numArgs = 0; /* reset current arg count */ execPath = NULL; /* reset execution path */ /* Reset CWD. */ getcwd(cwd, DIRSIZE); /* Null out arguments list. */ for (int i = 0; i < maxArgs; i++) { arguments[i] = NULL; } /* Print prompt and read in command line. */ printf("esh/:-> "); fgets(buffer, BUFSIZE, stdin); /* Pull the command from the input buffer. */ token = strtok(buffer, ARG_DELIM); /* Tokenize arguments from buffer and store in arguments array. */ do { /* Double the size of the arguments array if it's full. */ if(token != NULL && numArgs >= maxArgs - 1){ maxArgs *= 2; arguments = realloc(arguments, (maxArgs * sizeof(char*))); } arguments[numArgs] = token; numArgs++; } while ((token = strtok(NULL, ARG_DELIM))); /* Check for exit, break the loop if specified. */ if(!strcmp(arguments[0], "exit")){ free(arguments); break; } /* Debug info for verifying command and arguments. */ if (DEBUG){ printf("Command: %s \n", arguments[0]); for (int i = 1; i < numArgs; i++){ printf("Argument %d: %s \n", i, arguments[i]); } } /* Check if command exists in environment paths. */ for (int i = 0; i < numPaths; i++) { if (checkPath(paths[i], arguments[0])) { execPath = paths[i]; } } /* Check if command exists in cwd (if not found in paths). */ if (!execPath && checkPath(cwd, arguments[0])) { execPath = cwd; } /* If the file was not found at any path, check for new input. */ if (!execPath) { printf("Command `%s' not found.\n", arguments[0]); continue; } /* If it was found, fork and execute. */ pid = fork(); /* * If this is the parent branch and & isn't last arg, wait for child * to die. */ if (pid != 0) { if(strcmp("&", arguments[numArgs - 1]) == 0){ continue; }else{ wait(NULL); } } /* If this is the child branch, execute the command. */ else { if (strcmp("&", arguments[numArgs - 1]) == 0) { arguments[numArgs - 1] = NULL; } char *merged = mergePath(execPath, arguments[0]); execv(merged, arguments); exit(0); } } return 0; }
int Directory::fill(CPCHAR fileSpec) { MStackCall(__FILE__,__LINE__); if (dirName != "") { HDIR hdir = HDIR_CREATE; ULONG count = 1000; char* buf = new char[BUF_SIZE]; int attrib = FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED; Array<RegExp> r; FileExp2RegExpArray(fileSpec, r); if (DosFindFirst((PSZ)(const char*)mergePath(dirName, "*"), &hdir, attrib, buf, BUF_SIZE, &count, FIL_STANDARD) == NO_ERROR) { do { MTrace(("%s, %i matching entries", (const char*)mergePath(dirName, "*"), count)); FILEFINDBUF3* pffb = (FILEFINDBUF3*)buf; entry.SetDynamic(TRUE); // fake . & .. entries in / dir under MS-DOS / FAT file systems if (strcmp(pffb->achName, ".") != 0) { if (match(".", r)) entry[entry.Size()] = Dirent(this, ".", pffb->fdateLastWrite, pffb->ftimeLastWrite, 0, FILE_READONLY | FILE_DIRECTORY); if (match("..", r)) entry[entry.Size()] = Dirent(this, "..", pffb->fdateLastWrite, pffb->ftimeLastWrite, 0, FILE_READONLY | FILE_DIRECTORY); } do { if (match(pffb->achName, r)) { Dirent aDirent(this, pffb->achName, pffb->fdateLastWrite, pffb->ftimeLastWrite, pffb->cbFile, pffb->attrFile); int size = entry.Size(); entry[size] = aDirent; } if (pffb->oNextEntryOffset) pffb = (FILEFINDBUF3*)(((char*)pffb) + pffb->oNextEntryOffset); else pffb = 0; } while (pffb); } while (DosFindNext(hdir, buf, BUF_SIZE, &count) == NO_ERROR); DosFindClose(hdir); entry.SetDynamic(FALSE); } delete buf; } return entry.Size(); }