t_uchar run_exit(char **env, char **path, t_command *command, t_builtin_ptr **builtins) { char exit_status; if (count_args(command->argv_tmp) > 2) { my_dprintf(STDERR, "exit: Expression Syntax.\n"); return (EXIT_FAILURE); } if (command->argv_tmp == NULL || count_args(command->argv_tmp) == 1) exit_status = command->last_ret; else if (my_str_isnum(command->argv_tmp[1]) == FALSE) { my_dprintf(STDERR, "exit: Expression Syntax.\n"); return (EXIT_FAILURE); } else exit_status = my_atoi(command->argv_tmp[1]); if (command->interactive == TRUE) my_dprintf(STDERR, "exit\n"); my_free_2d_tab(env); my_free_2d_tab(path); my_free_2d_tab(command->argv); free(command->argv_tmp); free_builtins(builtins); exit(exit_status); return (EXIT_SUCCESS); }
int _get_lines_nbr(int argc, char **argv) { int lines; int idx; lines = 4; idx = 1; while (idx < argc) { if (my_cmp("-l", argv[idx])) { if (!my_str_isnum(argv[idx + 1])) { my_dprintf(STDERR, "Numeric argument required with \"-l\"\n"); return (-1); } lines = my_atoi(argv[idx + 1]); } idx += 1; } if (lines > 256 || lines <= 0) { my_dprintf(STDERR, "Number of lines out of range! {1..256}\n"); return (-1); } return (lines); }
t_uchar run_setenv(char ***env, char **argv) { char *tmp; if (count_args(argv) < 2) { my_printenv(*env, '\n'); return (0); } else if (count_args(argv) > 3 || !my_str_isalpha(argv[1]) || (!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0]))) { if (count_args(argv) > 3) my_dprintf(STDERR, "setenv: Too many arguments.\n"); else if ((!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0]))) my_dprintf(STDERR, "setenv: Variable name must begin with a letter.\n"); else if (!my_str_isalpha(argv[1])) my_dprintf(STDERR, ERALPH); return (1); } if ((tmp = malloc(sizeof(char) * (my_strlen(argv[1]) + 2))) == NULL) my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n"); tmp = my_strncpy(tmp, argv[1], my_strlen(argv[1])); tmp = my_strncat(tmp, "=", 1); my_setenv(env, tmp, argv[2]); free(tmp); return (0); }
t_uchar chck_exec_path(char *exec_path, char **argv) { struct stat file_data; if (exec_path == NULL || (exec_path[0] != '/' && exec_path[0] != '.') || access(exec_path, F_OK) == -1) { free(exec_path); my_dprintf(STDERR, "%S: Command not found.\n", argv[0]); return (1); } if (stat(exec_path, &file_data) == -1) my_exit(EXIT_FAILURE, "ERROR: stat() failed!\n"); if (!S_ISREG(file_data.st_mode)) { free(exec_path); my_dprintf(STDERR, "%S: Command not found.\n", argv[0]); return (1); } if (access(exec_path, X_OK) == -1) { my_dprintf(STDERR, "%s: Pemission denied.\n", exec_path); free(exec_path); return (1); } return (0); }
int redirect_output(char ***ptr, char **cmd, int fd, int idx) { while (cmd[idx]) { if ((my_cmp(cmd[idx], ">") || my_cmp(cmd[idx], ">>")) && cmd[idx + 1]) { if (fd != 0 || ((fd = open(cmd[idx + 1], (my_cmp(cmd[idx], ">>") ? O_CREAT | O_APPEND : O_CREAT) | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) || dup2(fd, STDOUT) == -1) { if (fd == -1) my_dprintf(STDERR, "%s: No such file or directory.\n", cmd[idx + 1]); else my_dprintf(STDERR, "Ambiguous input redirect.\n"); return (-1); } *ptr = remove_elem(cmd, idx); *ptr = remove_elem(cmd, idx); } ++idx; } return (fd); }
int redirect_input(char ***ptr, char **cmd, int fd) { int idx; idx = 0; while (cmd[idx]) { if (my_cmp(cmd[idx], "<") && cmd[idx + 1]) { if (fd != 0 || ((fd = open(cmd[idx + 1], O_RDONLY)) == -1) || dup2(fd, STDIN) == -1) { if (fd == -1) my_dprintf(STDERR, "%s: No such file or directory.\n", cmd[idx + 1]); else my_dprintf(STDERR, "Ambiguous input redirect.\n"); return (-1); } *ptr = remove_elem(cmd, idx); *ptr = remove_elem(cmd, idx); } ++idx; } return (fd); }
int redirect_input_double(char ***ptr, char **cmd, int fd) { int idx; idx = 0; while (cmd[idx]) { if (my_cmp(cmd[idx], "<<") && cmd[idx + 1]) { if (fd) { my_dprintf(STDERR, "Ambiguous input redirect.\n"); return (-1); } if (get_input_double(cmd[idx + 1])) { my_dprintf(STDERR, "FATAL ERR! Cannot create temparary file.\n"); return (-1); } *ptr = remove_elem(cmd, idx); *ptr = remove_elem(cmd, idx); } ++idx; } return (fd); }
int return_error(const char *msg, const char c) { my_dprintf(STDERR_FILENO, "%s", msg); if (c != END_CHAR) my_dprintf(STDERR_FILENO, " %c", c); my_dprintf(STDERR_FILENO, ".\n"); return (-1); }
t_bool check_empty_cmd(t_lexem *elem) { while (elem) { while ((elem) && (elem->type == STRING || (t_dir(elem->type)) || elem->type == COL)) elem = elem->right; if (!elem) return (CONTINUE); if (!((((elem->left && (elem->left->type == STRING || ((t_dir(elem->left->type)) && elem->left->left && elem->left->left->type == STRING)))) && (elem->right && (elem->right->type == STRING || ((t_dir(elem->right->type)) && elem->right->right && elem->right->right->type == STRING)))))) { my_dprintf(2, "Invalid null command.\n"); return (FALSE); } elem = elem->right; } return (CONTINUE); }
int main (int argc, char *argv[]) { /* Test behaviour for invalid file descriptors. */ { errno = 0; ASSERT (my_dprintf (-1, "test") == -1); ASSERT (errno == EBADF); } { close (99); errno = 0; ASSERT (my_dprintf (99, "test") == -1); ASSERT (errno == EBADF); } return 0; }
void aff_cmpl(t_shell *shell) { t_cmd *tmp; tmp = shell->com.cmd_l; check_pos(shell); shell->com.y[0] = get_actual_pos(shell); while (tmp) { if (tmp == shell->com.cursor) my_dprintf(1, PINK"%s"RES"\t", tmp->str); else my_dprintf(1, "%s\t", tmp->str); tmp = tmp->next; } shell->com.y[1] = get_actual_pos(shell); gotoyx(shell, get_actual_pos(shell) + (shell->com.y[0] - get_actual_pos(shell)), 0); }
static void check_pos(t_shell *shell) { int y; int cc; struct winsize ws; ioctl(1, TIOCGWINSZ, &ws); y = -1; cc = (count_char(&shell->com) / ws.ws_col); while (++y < cc) my_dprintf(1, "\n"); gotoyx(shell, get_actual_pos(shell) - (1 + y), 0); }
void check_status_cases(int status, pid_t ret, t_shell *shell, t_lexem *node) { if (WIFSIGNALED(status) && (status = WTERMSIG(status)) != 13) { my_dprintf(STDERR_FILENO, "(%d) %s Interrupted by %s (%d)\n", ret, *node->av, (status == 8 ? "Floating point exception" : status == 11 ? "Segmentation Fault" : "Signal"), status); shell->exit.status = 128 + status; } }
static int get_input_double(char *stop_str) { int fd; char *input; if ((fd = open("/tmp/mysh_tmp7y32g", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) return (1); while ((input = get_next_line(STDIN)) && !my_cmp(input, stop_str)) my_dprintf(fd, "%s\n", input); if (close(fd) == -1) return (1); if ((fd = open("/tmp/mysh_tmp7y32g", O_RDONLY)) == -1) return (1); return ((dup2(fd, STDIN)) == -1 || close(fd) == -1 ? 1 : 0); }
static char *give_it_to_guigui(t_shell *shell, char *buff, char *c) { char *ret; my_dprintf(1, "%s\n", shell->key.curs); if (*c == 4) return (NULL); if (shell->history_on == TRUE && shell->bufist) return (strdup(shell->bufist)); if (shell->com.on == TRUE && shell->com.cursor) { shell->com.on = FALSE; ret = strdup(shell->com.cursor->str); delete_list(&shell->com); return (ret); } return (strdup(buff)); }
int get_actual_pos(t_shell *shell) { char buff[3]; char trash[12]; int i; i = 0; my_dprintf(1, "%s\n", shell->key.pos); if (read(0, buff, 2) < 2) return (1); buff[0] = 0; if (read(0, buff, 2) < 2) return (1); buff[1] = !buff[1] || buff[1] < '0' || buff[1] > '9' ? 0 : buff[1]; read(0, trash, 12); buff[2] = '\0'; return (my_getnbr(buff, &i) == FALSE ? 0 : i); }
t_uchar run_setenv(char ***env, char **argv) { char *tmp; if (count_args(argv) < 2) { my_printenv(*env, '\n'); return (0); } else if (count_args(argv) > 3) { my_dprintf(STDERR, "setenv: Too many arguments.\n"); return (1); } if ((tmp = malloc(sizeof(char) * (my_strlen(argv[1]) + 2))) == NULL) my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n"); tmp = my_strncpy(tmp, argv[1], my_strlen(argv[1])); tmp = my_strncat(tmp, "=", 1); my_setenv(env, tmp, argv[2]); free(tmp); return (0); }
t_bool put_after(t_shell *shell, t_lexem *lex) { t_lexem *next; next = lex->right; if ((next->type >= PIPE) && (next->type < DRDIR_ERR)) { my_dprintf(STDERR_FILENO, "Ambiguous output redirect.\n"); return (FALSE); } lex->right = next->right; next->left = lex->left; if (lex->left) lex->left->right = next; if (next->right) next->right->left = lex; lex->left = next; next->right = lex; if (!(next->left)) shell->lexem = next; return (TRUE); }
static void test_function (int (*my_dprintf) (int, const char *, ...)) { /* Here we don't test output that may be platform dependent. The bulk of the tests is done as part of the 'vasnprintf-posix' module. */ /* Test support of size specifiers as in C99. */ my_dprintf (fileno (stdout), "%ju %d\n", (uintmax_t) 12345671, 33, 44, 55); my_dprintf (fileno (stdout), "%zu %d\n", (size_t) 12345672, 33, 44, 55); my_dprintf (fileno (stdout), "%tu %d\n", (ptrdiff_t) 12345673, 33, 44, 55); /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal output of floating-point numbers. */ /* Positive zero. */ my_dprintf (fileno (stdout), "%a %d\n", 0.0, 33, 44, 55); /* Positive infinity. */ my_dprintf (fileno (stdout), "%a %d\n", Infinityd (), 33, 44, 55); /* Negative infinity. */ my_dprintf (fileno (stdout), "%a %d\n", - Infinityd (), 33, 44, 55); /* FLAG_ZERO with infinite number. */ my_dprintf (fileno (stdout), "%010a %d\n", Infinityd (), 33, 44, 55); /* Test the support of the %f format directive. */ /* A positive number. */ my_dprintf (fileno (stdout), "%f %d\n", 12.75, 33, 44, 55); /* A larger positive number. */ my_dprintf (fileno (stdout), "%f %d\n", 1234567.0, 33, 44, 55); /* A negative number. */ my_dprintf (fileno (stdout), "%f %d\n", -0.03125, 33, 44, 55); /* Positive zero. */ my_dprintf (fileno (stdout), "%f %d\n", 0.0, 33, 44, 55); /* FLAG_ZERO. */ my_dprintf (fileno (stdout), "%015f %d\n", 1234.0, 33, 44, 55); /* Precision. */ my_dprintf (fileno (stdout), "%.f %d\n", 1234.0, 33, 44, 55); /* Precision with no rounding. */ my_dprintf (fileno (stdout), "%.2f %d\n", 999.95, 33, 44, 55); /* Precision with rounding. */ my_dprintf (fileno (stdout), "%.2f %d\n", 999.996, 33, 44, 55); /* A positive number. */ my_dprintf (fileno (stdout), "%Lf %d\n", 12.75L, 33, 44, 55); /* A larger positive number. */ my_dprintf (fileno (stdout), "%Lf %d\n", 1234567.0L, 33, 44, 55); /* A negative number. */ my_dprintf (fileno (stdout), "%Lf %d\n", -0.03125L, 33, 44, 55); /* Positive zero. */ my_dprintf (fileno (stdout), "%Lf %d\n", 0.0L, 33, 44, 55); /* FLAG_ZERO. */ my_dprintf (fileno (stdout), "%015Lf %d\n", 1234.0L, 33, 44, 55); /* Precision. */ my_dprintf (fileno (stdout), "%.Lf %d\n", 1234.0L, 33, 44, 55); /* Precision with no rounding. */ my_dprintf (fileno (stdout), "%.2Lf %d\n", 999.95L, 33, 44, 55); /* Precision with rounding. */ my_dprintf (fileno (stdout), "%.2Lf %d\n", 999.996L, 33, 44, 55); /* Test the support of the %F format directive. */ /* A positive number. */ my_dprintf (fileno (stdout), "%F %d\n", 12.75, 33, 44, 55); /* A larger positive number. */ my_dprintf (fileno (stdout), "%F %d\n", 1234567.0, 33, 44, 55); /* A negative number. */ my_dprintf (fileno (stdout), "%F %d\n", -0.03125, 33, 44, 55); /* Positive zero. */ my_dprintf (fileno (stdout), "%F %d\n", 0.0, 33, 44, 55); /* FLAG_ZERO. */ my_dprintf (fileno (stdout), "%015F %d\n", 1234.0, 33, 44, 55); /* Precision. */ my_dprintf (fileno (stdout), "%.F %d\n", 1234.0, 33, 44, 55); /* Precision with no rounding. */ my_dprintf (fileno (stdout), "%.2F %d\n", 999.95, 33, 44, 55); /* Precision with rounding. */ my_dprintf (fileno (stdout), "%.2F %d\n", 999.996, 33, 44, 55); /* A positive number. */ my_dprintf (fileno (stdout), "%LF %d\n", 12.75L, 33, 44, 55); /* A larger positive number. */ my_dprintf (fileno (stdout), "%LF %d\n", 1234567.0L, 33, 44, 55); /* A negative number. */ my_dprintf (fileno (stdout), "%LF %d\n", -0.03125L, 33, 44, 55); /* Positive zero. */ my_dprintf (fileno (stdout), "%LF %d\n", 0.0L, 33, 44, 55); /* FLAG_ZERO. */ my_dprintf (fileno (stdout), "%015LF %d\n", 1234.0L, 33, 44, 55); /* Precision. */ my_dprintf (fileno (stdout), "%.LF %d\n", 1234.0L, 33, 44, 55); /* Precision with no rounding. */ my_dprintf (fileno (stdout), "%.2LF %d\n", 999.95L, 33, 44, 55); /* Precision with rounding. */ my_dprintf (fileno (stdout), "%.2LF %d\n", 999.996L, 33, 44, 55); /* Test the support of the POSIX/XSI format strings with positions. */ my_dprintf (fileno (stdout), "%2$d %1$d\n", 33, 55); }
static int _usage(char *prog_name) { my_dprintf(STDERR, "Usage: %s [-m dumb|easy|medium|hard] [-l LINES]\n", prog_name); return (-1); }