void initializePaths() { #if RUN_IN_PLACE char buf[BUFSIZ]; infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl; bool success = getCurrentExecPath(buf, sizeof(buf)) || getExecPathFromProcfs(buf, sizeof(buf)); if (success) { pathRemoveFile(buf, DIR_DELIM_CHAR); std::string execpath(buf); path_share = execpath + DIR_DELIM ".."; path_user = execpath + DIR_DELIM ".."; if (detectMSVCBuildDir(execpath)) { path_share += DIR_DELIM ".."; path_user += DIR_DELIM ".."; } } else { errorstream << "Failed to get paths by executable location, " "trying cwd" << std::endl; if (!getCurrentWorkingDir(buf, sizeof(buf))) FATAL_ERROR("Ran out of methods to get paths"); size_t cwdlen = strlen(buf); if (cwdlen >= 1 && buf[cwdlen - 1] == DIR_DELIM_CHAR) { cwdlen--; buf[cwdlen] = '\0'; } if (cwdlen >= 4 && !strcmp(buf + cwdlen - 4, DIR_DELIM "bin")) pathRemoveFile(buf, DIR_DELIM_CHAR); std::string execpath(buf); path_share = execpath; path_user = execpath; } #else infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl; if (!setSystemPaths()) errorstream << "Failed to get one or more system-wide path" << std::endl; #endif infostream << "Detected share path: " << path_share << std::endl; infostream << "Detected user path: " << path_user << std::endl; }
char * getFullPath(char * name) { bool found = FALSE; char * pathlist = getenv("PATH"); // prepare the memory for the possible paths char * home = getenv("HOME"); char * homeCopy = malloc(MAXPATHLEN*sizeof(char*)); strcpy(homeCopy,home); char * pathCopy = malloc(MAXPATHLEN*sizeof(char*)); strcpy(pathCopy,pathlist); char * result = malloc(MAXPATHLEN*sizeof(char*)); // prepare memory to store the result char * current = getCurrentWorkingDir(); strcat(homeCopy,"/"); strcat(homeCopy,name); if (name[0] == '/') { // if it is an absolute path, store result. if (doesFileExist(name)) { strcpy(result,name); found = TRUE; } } else { if (doesFileExist(homeCopy)) { // If it is in the home directory strcpy(result,homeCopy); found = TRUE; } else { strcat(current,"/"); strcat(current,name); if (doesFileExist(current)) { // If it is in the current directory strcpy(result,current); found = TRUE; } else { // Else, check every path in PATH environment variable char* fullpath = strtok(pathCopy, ":"); while (fullpath != NULL) { char * path = malloc(MAXPATHLEN*sizeof(char*)); strcpy(path,fullpath); strcat(path,"/"); if (doesFileExist(strcat(path,name))) { strcpy(result,path); found = TRUE; } fullpath = strtok(NULL, ":"); free(path); } } } } free(current); free(pathCopy); free(homeCopy); if (found) { return result; } else { free(result); PrintPError(name); return NULL; } } /* getFullPath */
/* * changeWorkingDir * * arguments: none * * returns: outputs current directory * * Print Working Directory. */ void changeWorkingDir(char* path){ /*Change directory to given path and set environment "PWD" variable to match*/ if(path==NULL){ char* home = getenv("HOME"); chdir(home); } else chdir(path); //if(chdir(path)!=0) // printf("Error: %s\n", strerror(errno)); char *cwd = getCurrentWorkingDir(); setenv("PWD", cwd, 1); free(cwd); }/*changeWorkingDir*/
/* * ResolveExternalCmd * * arguments: * commandT *cmd: the command to be run * * returns: bool: whether the given command exists * * Determines whether the command to be run actually exists. */ static bool ResolveExternalCmd(commandT* cmd, char* path) { //Check to see if in home directory: if(*(cmd->argv[0])=='.'){ char* cwd = getCurrentWorkingDir(); //char* cwd; //getCurrentWorkingDir(cwd); sprintf(path,"%s/%s",cwd,cmd->name); free(cwd); return TRUE; } char** memLocs = getPath(); char dest[500]; int i=0; struct stat buf; /*If already absolute path*/ if(stat(cmd->name,&buf)==0){ /*Set path = to entered absolute path*/ strcpy(path,cmd->name); freePath(memLocs); return TRUE; } while(memLocs[i]!=NULL){ //Concatanate Paths with cmd->name: int size = (snprintf( dest, 499,"%s/%s",memLocs[i],cmd->name)+1)*sizeof(char); char* exeName = (char*)malloc(size); sprintf(exeName,"%s/%s",memLocs[i],cmd->name); //Check to see if exists and executable: if(stat(exeName,&buf)==0){ if(S_ISREG(buf.st_mode) && buf.st_mode & 0111){ strncpy(path,exeName,size); freePath(memLocs); free(exeName); return TRUE; } freePath(memLocs); free(exeName); return FALSE; } i++; free(exeName); } freePath(memLocs); return FALSE; } /* ResolveExternalCmd */
void initializePaths() { #if RUN_IN_PLACE char buf[BUFSIZ]; infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl; bool success = getCurrentExecPath(buf, sizeof(buf)) || getExecPathFromProcfs(buf, sizeof(buf)); if (success) { pathRemoveFile(buf, DIR_DELIM_CHAR); std::string execpath(buf); path_share = execpath + DIR_DELIM ".."; path_user = execpath + DIR_DELIM ".."; if (detectMSVCBuildDir(execpath)) { path_share += DIR_DELIM ".."; path_user += DIR_DELIM ".."; } } else { errorstream << "Failed to get paths by executable location, " "trying cwd" << std::endl; if (!getCurrentWorkingDir(buf, sizeof(buf))) FATAL_ERROR("Ran out of methods to get paths"); size_t cwdlen = strlen(buf); if (cwdlen >= 1 && buf[cwdlen - 1] == DIR_DELIM_CHAR) { cwdlen--; buf[cwdlen] = '\0'; } if (cwdlen >= 4 && !strcmp(buf + cwdlen - 4, DIR_DELIM "bin")) pathRemoveFile(buf, DIR_DELIM_CHAR); std::string execpath(buf); path_share = execpath; path_user = execpath; } path_cache = path_user + DIR_DELIM + "cache"; #else infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl; if (!setSystemPaths()) errorstream << "Failed to get one or more system-wide path" << std::endl; // Initialize path_cache // First try $XDG_CACHE_HOME/PROJECT_NAME const char *cache_dir = getenv("XDG_CACHE_HOME"); const char *home_dir = getenv("HOME"); if (cache_dir) { path_cache = std::string(cache_dir) + DIR_DELIM + PROJECT_NAME; } else if (home_dir) { // Then try $HOME/.cache/PROJECT_NAME path_cache = std::string(home_dir) + DIR_DELIM + ".cache" + DIR_DELIM + PROJECT_NAME; } else { // If neither works, use $PATH_USER/cache path_cache = path_user + DIR_DELIM + "cache"; } // Migrate cache folder to new location if possible migrateCachePath(); #endif infostream << "Detected share path: " << path_share << std::endl; infostream << "Detected user path: " << path_user << std::endl; infostream << "Detected cache path: " << path_cache << std::endl; #if USE_GETTEXT bool found_localedir = false; # ifdef STATIC_LOCALEDIR if (STATIC_LOCALEDIR[0] && fs::PathExists(STATIC_LOCALEDIR)) { found_localedir = true; path_locale = STATIC_LOCALEDIR; infostream << "Using locale directory " << STATIC_LOCALEDIR << std::endl; } else { path_locale = getDataPath("locale"); if (fs::PathExists(path_locale)) { found_localedir = true; infostream << "Using in-place locale directory " << path_locale << " even though a static one was provided " << "(RUN_IN_PLACE or CUSTOM_LOCALEDIR)." << std::endl; } } # else path_locale = getDataPath("locale"); if (fs::PathExists(path_locale)) { found_localedir = true; } # endif if (!found_localedir) { warningstream << "Couldn't find a locale directory!" << std::endl; } #endif // USE_GETTEXT }
/* * RunBuiltInCmd * * arguments: * commandT *cmd: the command to be run * * returns: none * * Runs a built-in command. */ static void RunBuiltInCmd(commandT* cmd) { if(strcmp(cmd->argv[0],"cd")==0){ changeWorkingDir(cmd->argv[1]); } /* "pwd" command*/ else if(strcmp(cmd->argv[0],"pwd")==0){ char *dir = getCurrentWorkingDir(); printf("%s\n",dir); free(dir); } else if(!strcmp(cmd->argv[0],"jobs")){ bgjobL* job = bgjobs; while (job != NULL){ if (!strcmp(job->status, "Running")){ printf("[%d] %s %s &\n", job->jid, job->status, job->name); } else{ printf("[%d] %s %s\n", job->jid, job->status, job->name); } job = job->next; } } else if (!strcmp(cmd->argv[0],"fg")){ bgjobL* job; if(cmd->argv[1] != NULL){ job = GetJobFromJid(atoi(cmd->argv[1])); } else{ job = GetMRJob(); } //bring bg process to foreground if (job != NULL){ tcsetpgrp(job->pid, STDIN_FILENO); crpid = job->pid; crName = job->name; DeleteJob(job->pid); kill(-crpid, SIGCONT); int status = 0; waitpid(crpid, &status, WUNTRACED); crpid = 0; crName = NULL; } } else if (!strcmp(cmd->argv[0],"bg")){ bgjobL* job; if(cmd->argv[1] != NULL){ job = GetJobFromJid(atoi(cmd->argv[1])); if (job != NULL){ kill(job->pid, SIGCONT); job->status = "Running"; } } else{ job = GetMRJob(); if (job != NULL){ kill(job->pid, SIGCONT); job->status = "Running"; } } } else if(!strcmp(cmd->argv[0],"alias")){ addAlias(cmd); } else if(!strcmp(cmd->argv[0],"unalias")){ deleteAlias(cmd->argv[1]); } else if(!strcmp(cmd->argv[0],"exit")){ return; } } /* RunBuiltInCmd */
void initializePaths() { #if RUN_IN_PLACE char buf[BUFSIZ]; infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl; bool success = getCurrentExecPath(buf, sizeof(buf)) || getExecPathFromProcfs(buf, sizeof(buf)); if (success) { pathRemoveFile(buf, DIR_DELIM_CHAR); std::string execpath(buf); path_share = execpath + DIR_DELIM ".."; path_user = execpath + DIR_DELIM ".."; if (detectMSVCBuildDir(execpath)) { path_share += DIR_DELIM ".."; path_user += DIR_DELIM ".."; } } else { errorstream << "Failed to get paths by executable location, " "trying cwd" << std::endl; if (!getCurrentWorkingDir(buf, sizeof(buf))) FATAL_ERROR("Ran out of methods to get paths"); size_t cwdlen = strlen(buf); if (cwdlen >= 1 && buf[cwdlen - 1] == DIR_DELIM_CHAR) { cwdlen--; buf[cwdlen] = '\0'; } if (cwdlen >= 4 && !strcmp(buf + cwdlen - 4, DIR_DELIM "bin")) pathRemoveFile(buf, DIR_DELIM_CHAR); std::string execpath(buf); path_share = execpath; path_user = execpath; } #else infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl; if (!setSystemPaths()) errorstream << "Failed to get one or more system-wide path" << std::endl; #endif infostream << "Detected share path: " << path_share << std::endl; infostream << "Detected user path: " << path_user << std::endl; bool found_localedir = false; #ifdef STATIC_LOCALEDIR if (STATIC_LOCALEDIR[0] && fs::PathExists(STATIC_LOCALEDIR)) { found_localedir = true; path_locale = STATIC_LOCALEDIR; infostream << "Using locale directory " << STATIC_LOCALEDIR << std::endl; } else { path_locale = getDataPath("locale"); if (fs::PathExists(path_locale)) { found_localedir = true; infostream << "Using in-place locale directory " << path_locale << " even though a static one was provided " << "(RUN_IN_PLACE or CUSTOM_LOCALEDIR)." << std::endl; } } #else path_locale = getDataPath("locale"); if (fs::PathExists(path_locale)) { found_localedir = true; } #endif if (!found_localedir) { errorstream << "Couldn't find a locale directory!" << std::endl; } }