12 Simple shell - (actually this is not even a shell)
13 It just to make the testing more easy.
17 (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r')
22 size_t l = strlen(str);
23 while (l < (size_t)-1) {
25 if (!c || WS_CHAR(c)) {
35 strltrim_safe(char* str)
39 while ((c = str[l]) && WS_CHAR(c)) {
45 return strcpy(str, str + l);
49 parse_cmdline(char* line, char** cmd, char** arg_part)
52 line = strltrim_safe(line);
55 while ((c = line[l])) {
63 *arg_part = strltrim_safe(line + l);
73 printf("Error: Not a directory\n");
76 printf("Error: No such file or directory\n");
79 printf("Error: Invalid parameter or operation\n");
82 printf("Error: Not supported\n");
85 printf("Error: File system is read only\n");
88 printf("Error: Out of memory\n");
91 printf("Error: This is a directory\n");
94 printf("Error: (%d)\n", errno);
100 sigint_handle(int signum)
106 sh_exec(const char* name, const char** argv)
108 if (!strcmp(name, "cd")) {
116 if (execve(name, argv, NULL)) {
121 setpgid(p, getpgid());
130 signal(SIGINT, sigint_handle);
132 // set our shell as foreground process
133 // (unistd.h:tcsetpgrp is essentially a wrapper of this)
134 // stdout (by default, unless user did smth) is the tty we are currently at
135 ioctl(stdout, TIOCSPGRP, getpgid());
139 printf("[\033[2m%s\033[39;49m]$ ", pwd);
140 size_t sz = read(stdin, buf, 511);
142 printf("fail to read user input (%d)\n", geterrno());
146 parse_cmdline(buf, &cmd, &argpart);
152 if (*(unsigned int*)cmd == 0x74697865U) {
164 printf("\n Simple shell. Use <PG_UP> or <PG_DOWN> to scroll.\n\n");