int _builtin_cd(char **cmd, t_shenv *shenv) { int ret; char *cwd; static char *oldpwd = NULL; cwd = my_getcwd(); if (cmd[1] == NULL) ret = chdir_home(); else if (!strcmp(cmd[1], "-")) { if ((ret = my_chdir(oldpwd)) == EXIT_SUCCESS) { printf("%s\n", (oldpwd == NULL) ? my_getcwd() : oldpwd); xfree(1, &oldpwd); oldpwd = cwd; update_env(oldpwd, shenv); return (EXIT_SUCCESS); } return (EXIT_FAILURE); } else ret = my_chdir(cmd[1]); update_env(cwd, shenv); if (ret == EXIT_SUCCESS && xfree(1, &oldpwd)) oldpwd = cwd; return ((ret) ? (EXIT_FAILURE) : (EXIT_SUCCESS)); }
static FILE * open_random_sig( char *sigdir) { struct stat st; if (stat(sigdir, &st) != -1) { if (S_ISDIR(st.st_mode)) { srand((unsigned int) time(NULL)); my_chdir(sigdir); if (thrashdir(sigdir) || !*sigfile) { #ifdef DEBUG if (debug == 2) error_message("NO sigfile=[%s]", sigfile); #endif /* DEBUG */ return (FILE *) 0; } else { #ifdef DEBUG if (debug == 2) error_message("sigfile=[%s]", sigfile); #endif /* DEBUG */ return fopen(sigfile, "r"); } } } return (FILE *) 0; }
static int my_home(t_env *env) { t_env *home; if ((home = my_found_env(env, "HOME")) == NULL) return (my_put_error("no HOME set\n")); return (my_chdir(home->value)); }
static int my_old(t_env *env) { t_env *oldpwd; if ((oldpwd = my_found_env(env, "OLDPWD")) == NULL) return (my_put_error("no OLDPWD set\n")); return (my_chdir(oldpwd->value)); }
/*pre: takes in int argc and char **argv *post: runs the minishell program * with argc number of command line arguments defined by argv */ int main(int argc, char **argv) { int n; int pid; char *s; char **v; s = (char*)xmalloc(256*sizeof(char)); while(1) { my_str("minishell> "); n = read(0, s, 256); #ifdef DEBUG my_str("n= "); my_int(n); my_char('\n'); #endif s[n - 1] = '\0'; if(n > 1)/*1 character is just a \0 (read in \n user just hit enter)*/ { v = my_str2vect(s); if(my_strcmp(v[0], "cd") == 0) my_chdir(v[1]); else if(my_strcmp(v[0], "exit") == 0) break; else if(v[0] != NULL)/*if not just whitespace, we're going to need to fork*/ { #ifdef DEBUG my_str("command:>"); my_str(v[0]); my_str("<\n"); my_str("going to fork\n"); #endif if((pid = fork()) < 0) my_err("minishell: ERROR forking process!\n"); else if(pid > 0) wait(NULL); else { my_execvp(v[0], v); #ifdef DEBUG my_str("exiting forked process\n"); #endif exit(0);/*for processes that end in error*/ } } #ifdef DEBUG my_str("freeing vector\n"); #endif my_freevect(v); } else if(n < 0) my_str("minishell: ERROR reading command\n"); } free(s); my_str("Thank you for using myminishell, live long and prosper.\n"); return 0; }
static int chdir_home() { static struct passwd *p = NULL; if (p == NULL) p = getpwuid(getuid()); if (p == NULL) return (EXIT_FAILURE); else if (my_chdir(p->pw_dir) == EXIT_FAILURE) return (EXIT_FAILURE); return (EXIT_SUCCESS); }
void builtin_cd(char *str) { int i; int ok; ok = 0; i = 0; if (str == NULL) my_home(); while (g_tabfnct[i].detect != '\0') { if (strcmp(g_tabfnct[i].detect, str) > 0) { g_tabfnct[i].fct; ok = 1; } i = i + 1; } if (!ok) my_chdir(str); }
int my_cd(t_shell *shell, char **argv) { static char buf[2048 + 1]; int len; int ret; if (shell == NULL || argv == NULL) return (1); if ((len = my_len_tab(argv)) == 0) return (1); else if (len > 2) return (my_put_error("cd : too many arguments\n")); if (getcwd(buf, 2048) != buf) return (my_put_error("fail getcwd\n")); if (len == 1) ret = my_home(shell->env); else if (my_strcmp(argv[1], "-") == 0) ret = my_old(shell->env); else ret = my_chdir(argv[1]); if (ret != 0) return (1); return (my_change_env(shell, buf)); }
void msg_write_signature( FILE *fp, t_bool include_dot_signature, struct t_group *thisgroup) { FILE *fixfp; FILE *sigfp; char cwd[PATH_LEN]; char path[PATH_LEN]; char pathfixed[PATH_LEN]; #ifdef NNTP_INEWS if (read_news_via_nntp && 0 == strcasecmp(tinrc.inews_prog, INTERNAL_CMD)) include_dot_signature = TRUE; #endif /* NNTP_INEWS */ if (thisgroup && !thisgroup->bogus) { if (!strcmp(thisgroup->attribute->sigfile, "--none")) return; /* TODO: handle DONT_HAVE_PIPING case */ #ifndef DONT_HAVE_PIPING if (thisgroup->attribute->sigfile[0] == '!') { FILE *pipe_fp; char *sigcmd; char cmd[PATH_LEN]; fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n"); sigcmd = my_malloc(strlen(thisgroup->attribute->sigfile + 1) + strlen(thisgroup->name) + 4); sprintf(sigcmd, "%s \"%s\"", thisgroup->attribute->sigfile + 1, thisgroup->name); if ((pipe_fp = popen(sigcmd, "r")) != NULL) { while (fgets(cmd, PATH_LEN, pipe_fp)) fputs(cmd, fp); pclose(pipe_fp); } /* else issue an error-message? */ free(sigcmd); return; } #endif /* !DONT_HAVE_PIPING */ get_cwd(cwd); if (!strfpath(thisgroup->attribute->sigfile, path, sizeof(path), thisgroup)) { if (!strfpath(tinrc.sigfile, path, sizeof(path), thisgroup)) joinpath(path, homedir, ".Sig"); } /* * Check to see if sigfile is a directory & if it is * generate a random signature from sigs in sigdir. If * the file path/.sigfixed or ~/.sigfixed exists (fixed * part of random sig) then read it in first and append * the random sig part onto the end. */ if ((sigfp = open_random_sig(path)) != NULL) { #ifdef DEBUG if (debug == 2) error_message("USING random sig=[%s]", sigfile); #endif /* DEBUG */ fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n"); joinpath(pathfixed, path, ".sigfixed"); #ifdef DEBUG if (debug == 2) error_message("TRYING fixed sig=[%s]", pathfixed); #endif /* DEBUG */ if ((fixfp = fopen(pathfixed, "r")) != NULL) { copy_fp(fixfp, fp); fclose(fixfp); } else { joinpath(pathfixed, homedir, ".sigfixed"); #ifdef DEBUG if (debug == 2) error_message("TRYING fixed sig=[%s]", pathfixed); #endif /* DEBUG */ if ((fixfp = fopen(pathfixed, "r")) != NULL) { copy_fp(fixfp, fp); fclose(fixfp); } } copy_fp(sigfp, fp); fclose(sigfp); my_chdir(cwd); return; } } if ((sigfp = fopen(path, "r")) != NULL) { fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n"); copy_fp(sigfp, fp); fclose(sigfp); return; } /* * Use ~/.signature as a last resort, but only if mailing or * using internal inews (external inews appends it automagically). */ if ((sigfp = fopen(default_signature, "r")) != NULL) { if (include_dot_signature) { fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n"); copy_fp(sigfp, fp); } fclose(sigfp); } }
static int thrashdir( char *sigdir) { DIR *dirp; DIR_BUF *dp; char *cwd; int safeguard, recurse; int c = 0, numentries, pick; struct stat st; sigfile[0] = '\0'; if ((dirp = opendir(CURRENTDIR)) == NULL) return 1; numentries = 0; while ((dp = readdir(dirp)) != NULL) numentries++; /* * consider "." and ".." non-entries * consider all entries starting with "." non-entries */ cwd = my_malloc(PATH_LEN); #ifndef M_AMIGA if (numentries < 3 || cwd == NULL) #else if (numentries == 0 || cwd == NULL) #endif /* !M_AMIGA */ { CLOSEDIR(dirp); return -1; } get_cwd(cwd); recurse = strcmp(cwd, sigdir); /* * If we are using the root sig directory, we don't want * to recurse, or else we might use a custom sig intended * for a specific newsgroup (and not this one). */ for (safeguard = 0, dp = NULL; safeguard < MAXLOOPS && dp == NULL; safeguard++) { #ifdef DEBUG if (debug == 2) error_message("sig loop=[%d] recurse=[%d]", safeguard, recurse); #endif /* DEBUG */ #ifdef HAVE_REWINDDIR rewinddir(dirp); #else CLOSEDIR(dirp); if ((dirp = opendir(CURRENTDIR)) == NULL) return 1; #endif /* HAVE_REWINDDIR */ pick = rand() % numentries + 1; while (--pick >= 0) { if ((dp = readdir(dirp)) == NULL) break; } if (dp != NULL) { /* if we could open the dir entry */ if (!strcmp(dp->d_name, CURRENTDIR) || (dp->d_name[0] == '.')) dp = NULL; else { /* if we have a non-dot entry */ if (stat(dp->d_name, &st) == -1) { CLOSEDIR(dirp); return 1; } if (S_ISDIR(st.st_mode)) { if (recurse) { /* * do subdirectories */ if ((my_chdir(dp->d_name) < 0) || ((c = thrashdir(sigdir)) == 1)) { CLOSEDIR(dirp); return 1; } if (c == -1) { /* * the one we picked was an * empty dir so try again. */ dp = NULL; my_chdir(cwd); } } else dp = NULL; } else { /* end dir; we have a file */ get_cwd(sigfile); strcat(sigfile, "/"); strcat(sigfile, dp->d_name); #ifdef DEBUG if (debug == 2) error_message("Found a file=[%s]", sigfile); #endif /* DEBUG */ } } } } free(cwd); #ifdef DEBUG if (debug == 2) error_message("return 0: sigfile=[%s]", sigfile); #endif /* DEBUG */ CLOSEDIR(dirp); return 0; }
static void parse_args(int argc, char *argv[], const char *dir, char *lwin_path, char *rwin_path, int *lwin_handle, int *rwin_handle) { int x; int select = 0; (void)my_chdir(dir); /* Get Command Line Arguments */ for(x = 1; x < argc; x++) { if(!strcmp(argv[x], "--select")) { select = 1; } else if(!strcmp(argv[x], "--remote")) { if(!ipc_server()) { ipc_send(argv + x + 1); quit_on_invalid_arg(); } } else if(!strcmp(argv[x], "-f")) { cfg.vim_filter = 1; } else if(!strcmp(argv[x], "--no-configs")) { } else if(!strcmp(argv[x], "--version") || !strcmp(argv[x], "-v")) { show_version_msg(); quit_on_invalid_arg(); } else if(!strcmp(argv[x], "--help") || !strcmp(argv[x], "-h")) { show_help_msg(); quit_on_invalid_arg(); } else if(!strcmp(argv[x], "--logging")) { /* do nothing, it's handeled in main() */ } else if(!strcmp(argv[x], "-c")) { if(x == argc - 1) { puts("Argument missing after \"-c\""); quit_on_invalid_arg(); } /* do nothing, it's handeled in exec_startup_commands() */ x++; } else if(argv[x][0] == '+') { /* do nothing, it's handeled in exec_startup_commands() */ } else if(path_exists(argv[x]) || is_path_absolute(argv[x]) || is_root_dir(argv[x])) { if(lwin_path[0] != '\0') { parse_path(dir, argv[x], rwin_path); *rwin_handle = !select; } else { parse_path(dir, argv[x], lwin_path); *lwin_handle = !select; } select = 0; } else if(curr_stats.load_stage == 0) { show_help_msg(); quit_on_invalid_arg(); } else { show_error_msgf("--remote error", "Invalid argument: %s", argv[x]); } } }
/* * Main Loop * Everything is driven from this function with the exception of * signals which are handled in signals.c */ void main_loop(void) { LOG_FUNC_ENTER; int last_result = 0; int wait_enter = 0; int timeout = cfg.timeout_len; buf[0] = L'\0'; while(1) { wchar_t c; size_t counter; int ret; is_term_working(); #ifdef _WIN32 update_win_console(); #endif lwin.user_selection = 1; rwin.user_selection = 1; if(curr_stats.too_small_term > 0) { touchwin(stdscr); wrefresh(stdscr); mvwin(status_bar, 0, 0); wresize(status_bar, getmaxy(stdscr), getmaxx(stdscr)); werase(status_bar); waddstr(status_bar, "Terminal is too small for vifm"); touchwin(status_bar); wrefresh(status_bar); #ifndef _WIN32 pause(); #endif continue; } else if(curr_stats.too_small_term < 0) { wtimeout(status_bar, 0); while(wget_wch(status_bar, (wint_t*)&c) != ERR); curr_stats.too_small_term = 0; modes_redraw(); wtimeout(status_bar, cfg.timeout_len); wait_enter = 0; curr_stats.save_msg = 0; status_bar_message(""); } modes_pre(); /* This waits for timeout then skips if no keypress. */ ret = read_char(status_bar, (wint_t*)&c, timeout); /* Ensure that current working directory is set correctly (some pieces of * code rely on this). */ (void)my_chdir(curr_view->curr_dir); if(ret != ERR && pos != ARRAY_LEN(buf) - 2) { if(c == L'\x1a') /* Ctrl-Z */ { def_prog_mode(); endwin(); #ifndef _WIN32 { void (*saved_stp_sig_handler)(int) = signal(SIGTSTP, SIG_DFL); kill(0, SIGTSTP); signal(SIGTSTP, saved_stp_sig_handler); } #endif continue; } if(wait_enter) { wait_enter = 0; curr_stats.save_msg = 0; clean_status_bar(); if(c == L'\x0d') continue; } buf[pos++] = c; buf[pos] = L'\0'; } if(wait_enter && ret == ERR) continue; counter = get_key_counter(); if(ret == ERR && last_result == KEYS_WAIT_SHORT) { last_result = execute_keys_timed_out(buf); counter = get_key_counter() - counter; assert(counter <= pos); if(counter > 0) { memmove(buf, buf + counter, (wcslen(buf) - counter + 1)*sizeof(wchar_t)); } } else { if(ret != ERR) curr_stats.save_msg = 0; last_result = execute_keys(buf); counter = get_key_counter() - counter; assert(counter <= pos); if(counter > 0) { pos -= counter; memmove(buf, buf + counter, (wcslen(buf) - counter + 1)*sizeof(wchar_t)); } if(last_result == KEYS_WAIT || last_result == KEYS_WAIT_SHORT) { if(ret != ERR) modupd_input_bar(buf); if(last_result == KEYS_WAIT_SHORT && wcscmp(buf, L"\033") == 0) timeout = 1; if(counter > 0) clear_input_bar(); if(!curr_stats.save_msg && curr_view->selected_files && get_mode() != CMDLINE_MODE) print_selected_msg(); continue; } } timeout = cfg.timeout_len; if(is_redraw_scheduled()) { modes_redraw(); } pos = 0; buf[0] = L'\0'; clear_input_bar(); if(is_status_bar_multiline()) { wait_enter = 1; update_all_windows(); continue; } /* Ensure that current working directory is set correctly (some pieces of * code rely on this). PWD could be changed during command execution, but * it should be correct for modes_post() in case of preview modes. */ (void)my_chdir(curr_view->curr_dir); modes_post(); } }