From 980f692e6fcbc83fb41f831f1143b1e6ecf412c6 Mon Sep 17 00:00:00 2001 From: Josuer08 Date: Sat, 13 Feb 2021 21:10:50 -0500 Subject: [PATCH 1/1] first --- README.md | 1 + khe | 10 +++ test | 1 + tinysh | Bin 0 -> 17752 bytes tinysh.c | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 222 insertions(+) create mode 100644 README.md create mode 100755 khe create mode 100644 test create mode 100755 tinysh create mode 100644 tinysh.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d4d195 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# tinysh-parallel diff --git a/khe b/khe new file mode 100755 index 0000000..7a6346d --- /dev/null +++ b/khe @@ -0,0 +1,10 @@ +#!/bin/bash +# Basic while loop +counter=1 +while [ $counter -le 10 ] +do +echo $counter +((counter++)) +sleep 1 +done +echo All done diff --git a/test b/test new file mode 100644 index 0000000..97df509 --- /dev/null +++ b/test @@ -0,0 +1 @@ +./khe;./khe diff --git a/tinysh b/tinysh new file mode 100755 index 0000000000000000000000000000000000000000..9f97bcdb773c75926a4251664ce5dbe792dc20fe GIT binary patch literal 17752 zcmeHPe~c5?oqx7j0tCDZg_I^~n5LludN@D`CCNc-V3yft7qbE~=_KaynI9TdCVdpfsgm3f(0q?zH8iXw$o1eL2#z3r)89xyKQF zpYNOZ^^T2Q_3n?8PV!rMzVG|_{&;V`@6DSx^XBc>^!8S_%OyB@#Ckzo=Uf}{E5X9` zDg)vdYsJktep=ivrh}g*FA0#tQ>Puprm)=BFRZJiV|DBZCc8;xH>vEH z=BYwROgWwu6Z)M~eyiywGD2cXx|BNKvci-f(}=r~hHY3dWxFrIj_UcxCJy^HRbIWg zEL8Pps!mtj-52dzv-REbW z_`P=mKN$I^^)D>jeYH3@NA;#UkU!F)MEa^YiIa@WBl#bb&Ok}4q4+6a^J?IcI(TOt z{IhlN)phV^>)?m$;2Y}TpQwZX3b=+}b$SFqt@Hh29lW;=PJODDpV@Wr2kPLD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0ttwCm(mZa4}hQ?mAda61ugiSqk-8xn0kk{g>UA=JEo!0MzeQo@XcgH}-O ziKvK6*lz{9K(X;o z(jLyCvqcgE!Y42IJ+YLpD}^=E<=AXzLxBx#%jx>}k*p_}>=C~3U=03jY9(yR!% z8jn>lCS-|7Uyuw`ZlKQ+KH0`TE3y}Py6z#1%iivb^r$PnCk$KWYzzC6w=Fmi$HD$+ z2&Km13kRPpz;=e#7e4AS7*J{qi~nPY-VflGjyaN@=>A2CDZd}EQCpYuL_dokJ(IIN zKc6wB^;BmnkH-pJ1M0LdC`U{+r!n%?_M3oxh-Y*zpP%=ba{YOJ$L|Z+?G|Kc&cpWn zT*%a~1oS$9;>7kmZ+jRRZaHOpeow-b--|#+Ci3A-kAb0g5L`Y#XENpIWv)N#G3|vt zz0+WxpGTRpeRci04o@OOF=cyxZe^NNid=s6`5#dB8^B*{QS+7 zHY_+(b^kpIA=RJbFElWeG8NdN!< literal 0 HcmV?d00001 diff --git a/tinysh.c b/tinysh.c new file mode 100644 index 0000000..29c6a01 --- /dev/null +++ b/tinysh.c @@ -0,0 +1,210 @@ +#include +#include +#include +#include +#include +#include +#include + + +int COMMANDS_SIZE; +char **parse_command(char *my_line, char value) +{ + int buffer_size = 64; + int i = 0; + char *arg; + char **args = malloc(buffer_size * sizeof(char*)); + char *valuee = &value; + arg = strtok(my_line, valuee); + while (arg != NULL) { + args[i] = arg; + i++; + arg = strtok(NULL, valuee); + } + + args[i] = NULL; + COMMANDS_SIZE = i; + return args; +} +void trimTrailing(char * str) +{ + int index, i; + + /* Set default index */ + index = -1; + + /* Find last index of non-white space character */ + i = 0; + while(str[i] != '\0') + { + if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n') + { + index= i; + } + + i++; + } + + /* Mark next character to last non-white space character as NULL */ + str[index + 1] = '\0'; +} +////////////////////EXEC PROGRAM FUNCTION///////////// +// +// +// +static int exec_prog(int argc, const char **argv) { + + pid_t pid[argc]; + int status, timeout /* unused ifdef WAIT_FOR_COMPLETION */; + + // this block executes when pid=0, (I am the father process) + + for (int i =0; i < argc; i++) { + + if ((pid[i] = fork()) < 0) { /* fork a child process */ + printf("*** ERROR: forking child process failed\n"); + exit(1); + } else if (pid[i] == 0) { /* for the child process: */ + trimTrailing(argv[i]); + char **value = parse_command(argv[i], ' '); + if (strcmp(value[0], "")) { + if (execvp(value[0], value) < 0) { /* execute the command */ + printf("*** ERROR: exec failed\n"); + exit(1); + } + exit(0); + } else { + printf(""); + exit(0); + } + } + + } + + for (int i =0; i < argc; i++) { + do { + if ((pid[i] = waitpid(pid[i], &status, WNOHANG)) == -1) { + perror("wait() error"); + } + else if (pid[i] == 0) { + /*printf("child is still running ");*/ + sleep(1); + } else { + if (WIFEXITED(status)) + printf("child exited with status of %d\n", WEXITSTATUS(status)); + /*else puts("child did not exit successfully");*/ + } + } while (pid[i] == 0); + + } + + return 0; +} +// +// +// +////////////////////////////////////////////////////// + +////////////////////BATCH MODE PROCESS//////////////// +// +// +// +int batchModeStart(const char **argv) { + + + +} +// +// +// +////////////////////////////////////////////////////// + +void promptPrint(){ +printf("⌈[tinysh] \n↳>>> "); +} + + +char *read_command_line(void) +{ + int bufsize = 1024; + char *buffer = malloc(sizeof(char) * bufsize); + int c; + int i = 0; + + while ( c != '\n' ) { + c = getchar(); + buffer[i] = c; + i++; + } + + return buffer; +} + + +int main (int argc, char *argv[]){ + + char *prompt = argv[0]; + char *filename; + bool batchMode = false; + if ( argc == 2) { + filename = argv[1]; + batchMode = true; + } else if (argc != 1) { + printf("Wrong ammount of arguments, only one file per batch \n"); + } + + /*promptPrint();*/ + /*exec_prog(1, filename);*/ + /*exec_prog(&argv[1]);*/ + + + +//Oscar J Rodriguez B (@josuer08) + if (!batchMode) { + bool active = true; + char *arguments; + while (active) { + promptPrint(); + arguments = read_command_line(); + char **args = parse_command(arguments, ';'); + trimTrailing(args[0]); + if (strcmp(args[0], "exit") == 0) {/* is it an "exit"? */ + active = false; /* exit if it is */ + } else { + exec_prog(COMMANDS_SIZE,args); + } + /*printf("%d", COMMANDS_SIZE);*/ + /*printf("%s and %s", args[0], args[1]);*/ + } + } else { + char * buffer = 0; + long length; + FILE * f = fopen (filename, "rb"); + + if (f) { + fseek (f, 0, SEEK_END); + length = ftell (f); + fseek (f, 0, SEEK_SET); + buffer = malloc (length); + if (buffer) { + fread (buffer, 1, length, f); + } + fclose (f); + } + + if (buffer) { + char **args = parse_command(buffer, ';'); + trimTrailing(args[0]); + /*printf("%s", buffer);*/ + exec_prog(COMMANDS_SIZE,args); + } + + + return 0; + + } + + + + +} -- 2.25.1