1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <signal.h> #include <setjmp.h> #include <sys/types.h> #include <sys/wait.h>
#define NONE "\e[0m" #define RED "\e[0;31m"
char version[] = "1.0"; int pipe_flag = 0; static jmp_buf env; int sv;
void sig_handler(int sig) { if (pipe_flag) { wait(&sv); wait(&sv); } else { wait(&sv); } printf("\n"); siglongjmp(env, 1); }
void help() { printf("**********************************************************\n"); printf("%27sHELP%27s\n**********************************************************\n", "", ""); printf("Simple Shell %s by Sekiro May 21 2020\n", version); printf("Usage: [COMMAND] [OPTION]... (< [INFILE]) (| [COMMAND] [OPTION]... (> [OUTFILE])) (;...;...)\n"); printf("Simple shell with redirect and pipe(Can also run multiple commands)\nSpecial commands:\n"); printf("\thelp\tDisplay this help and continue\n"); printf("\texit\tExit simple shell\n"); printf("**********************************************************\n"); }
int main(int argc, char const *argv[]) { char buffer[256], *argv1[256], **p, *argv2[256], *buf, *end, *cmd2, *in, *out; int fd[2]; signal(SIGINT, sig_handler);
for (;;) { sigsetjmp(env, 1); printf("=> "); if (fgets(buffer, sizeof(buffer), stdin) == NULL) exit(0); if (strlen(buffer) == 1) { continue; } if (buffer[strlen(buffer) - 2] != ';') { buffer[strlen(buffer) - 1] = ';'; } for (buf = buffer; end = strstr(buf, ";"); buf = end) { *end++ = '\0'; pipe_flag = 0; cmd2 = strstr(buf, "|"); in = strstr(buf, "<"); out = strstr(buf, ">"); if (in != NULL) { *in++ = '\0'; in = strtok(in, " \t\n"); } if (out != NULL) { *out++ = '\0'; out = strtok(out, " \t\n"); } if (cmd2 != NULL) { pipe_flag = 1; *cmd2++ = '\0'; for (p = &argv2[0], *p = strtok(cmd2, " \t\n"); *p != NULL; *++p = strtok(NULL, " \t\n")) ; if (argv2[0] == NULL) { fprintf(stderr, RED "** No command 2 input!\n" NONE); continue; } } for (p = &argv1[0], *p = strtok(buf, " \t\n"); *p != NULL; *++p = strtok(NULL, " \t\n")) ; if (argv1[0] == NULL) { fprintf(stderr, RED "** No command 1 input!\n" NONE); continue; } if (strcmp(argv1[0], "exit") == 0) exit(0); if (strcmp(argv1[0], "help") == 0) { help(); continue; } if (pipe_flag) { pipe(fd); if (fork() == 0) { int fd0 = -1; if (in != NULL) fd0 = open(in, O_RDONLY); if (fd0 != -1) { dup2(fd0, 0); close(fd0); } else if (in != NULL) { fprintf(stderr, RED "** No such file or directory: %s\n" NONE, in); help(); exit(1); } dup2(fd[1], 1); close(fd[1]); close(fd[0]); execvp(argv1[0], argv1); fprintf(stderr, RED "** Bad command 1: %m\n" NONE); help(); exit(1); } else { if (fork() == 0) { int fd1 = -1; if (out != NULL) fd1 = open(out, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (fd1 != -1) { dup2(fd1, 1); close(fd1); } dup2(fd[0], 0); close(fd[0]); close(fd[1]); execvp(argv2[0], argv2); fprintf(stderr, RED "** Bad command 2: %m\n" NONE); help(); exit(1); } }
close(fd[0]); close(fd[1]); wait(&sv); wait(&sv); } else { if (fork() == 0) { int fd0 = -1, fd1 = -1; if (in != NULL) fd0 = open(in, O_RDONLY); if (fd0 != -1) { dup2(fd0, 0); close(fd0); } else if (in != NULL) { fprintf(stderr, RED "** No such file or directory: %s\n" NONE, in); help(); exit(1); } if (out != NULL) fd1 = open(out, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (fd1 != -1) { dup2(fd1, 1); close(fd1); } execvp(argv1[0], argv1); fprintf(stderr, RED "** Bad command 1: %m\n" NONE); help(); exit(1); }
wait(&sv); } } }
return 0; }
|