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); }
char _get_mode(int argc, char **argv) { char mode; int idx; mode = 2; idx = 1; while (idx < argc) { if (my_cmp("-m", argv[idx])) { if (my_cmp("easy", argv[idx + 1])) mode = 0; else if (my_cmp("medium", argv[idx + 1])) mode = 1; else if (my_cmp("hard", argv[idx + 1])) mode = 2; else if (my_cmp("dumb", argv[idx + 1])) mode = 3; else return (_usage(argv[0])); } idx += 1; } return (mode); }
static char *get_exec_dir(char *input, char **path) { DIR *dirp; struct dirent *file; int idx; char *exec_dir; exec_dir = NULL; idx = 0; while (path != NULL && path[idx]) { if ((dirp = opendir(path[idx])) != NULL) { while ((file = readdir(dirp)) != NULL) { if (my_cmp(input, file->d_name)) exec_dir = path[idx]; } if (closedir(dirp) == -1) my_exit(EXIT_FAILURE, "ERROR: closedir() failed.\n"); } idx += 1; } return (exec_dir); }
vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int> > ret; int len = nums.size(); int i,value,count; count = 1 << len; sort(nums.begin(),nums.end()); for(i = 0; i < count; ++i){ vector<int> tmp; value = i; int j = 0; while(value){ if(value & 0x1) tmp.push_back(nums[j]); ++j; value >>= 1; } int r = ret.size(); int flag = 0; for(j = 0; j < r; ++j) if(!my_cmp(ret[j],tmp)) flag = 1; if(!flag) ret.push_back(tmp); } return ret; }
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); }
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 _is_help(int argc, char **argv) { int idx; idx = 1; while (idx < argc) { if (my_cmp("--help", argv[idx])) return (_usage(argv[0])); idx += 1; } return (EXIT_SUCCESS); }
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); }
t_uchar call_builtins(t_builtin_ptr **builtins, int *builtin_found, char ***env, char **argv) { int idx; idx = 0; while (builtins[idx]) { if (my_cmp(argv[0], builtins[idx]->command)) { *builtin_found = true; return (builtins[idx]->fct(env, argv)); } idx += 1; } *builtin_found = false; return (0); }
static bool equal(const Kmer& a, const Kmer& b) { return my_cmp(a, b); }