From 06b323c1c267215197a3fdce52acb0690c65ce5c Mon Sep 17 00:00:00 2001 From: vyvyvyThao <wn73@drexel.edu> Date: Thu, 13 Feb 2025 23:15:19 -0500 Subject: [PATCH] update --- 3-Shell/dsh | Bin 24944 -> 24944 bytes 3-Shell/dsh_cli.c | 8 +++++++- 3-Shell/questions.md | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 3-Shell/questions.md diff --git a/3-Shell/dsh b/3-Shell/dsh index a099b0c121a54b24f307c08bab7b6e22a32a1ced..e6aa4094e2aeeffcf460bc6ff6ca9e00947661fb 100755 GIT binary patch delta 697 zcmexxi1EWA#tj^doRvxpU{JYPkTIP}q(8OZ!CsO<;qEUDlWh-DXKU6w9^Kr-JcnQL z#eNnp28I{C|Nj5?=yd&X-1PxS-*ML`Aj+fL^}}ROVO!}J`xqD*7(6<Uzu*BWY(2o= zvY3H^!K2&t#pH#;qV+F47%v=y@I5+Rf4pY(Xm)+U2$JUC#$cmR@&Uy3=oK~80hw|Q zWYFQi|NplhC>8YRcKre3zxnt7|4Zlp|Nnb*heDKhg4HogGca_zK54Ff@{hk&`Tzg_ z`#3;0_p-jy2AOqy2Ll6xD8mcazyJTgWc~mD{{)buK?YvlY#>r0#h5jDrJg-w*5sFZ z=9)}`U@b4W9{v9h(#P;piW%(c6CS;y%wS2domG=P^;H<JPR`Sp)!hE@|9_9(fd3xd zt`8u--0#51!0_TfNW9ba2UxuG_>2C@Tl7^Jy(Zt(Z{d5)53=Xjeoz>`h?tyhV8pi+ zEChBf!;2l0R~s0r^n-=K7T$gU4*nM&y{y$>QK;U-lMF?9BEceHbw!gE4XqiEO^!9R zVd}g;*}zbIa)Tky<b8%Z!rgozJE0b2-~a#rwe{o!hFaP&$l`kU|Nn1xy#tDw1KkXk zu6Ou*^8Z1@;^g7YAx2YpCpo;}TygpTe-TCohMdVC9kwgFGBPkQxbg|KF*)<HG21Zm zu!CZNfkA_Tf#D1T14GZ`laAVqDx1GLPUdFJ+1%+X&dMaAKY3A58Dq|5$>4a#kj<6B h;f#!~n=gj^=U~j4d@)v^Gsg%NI1CIqn>pjsnE?qM7LEV_ delta 669 zcmexxi1EWA#tj^doQX;dV34?3kTIP}MB+cwCXShN)>#Mkdlp<jpHQK=fn#$I^BjJ~ z5BpiT7#Lpk{`>#mqto@ran~0heaBtjfGCe{*B_HTg>9uj>|<bLVDRWX{(=Xju=N0c zOD6*ZgGaaPhsg_tMe9F!FkUzY;d}Iodh3AHUIQt=`}hC<)&r%2AeA6K+yDRnU&j9b z|KFoK^an%`td3clfuYm&O>^y=fBdbs|NsBr#{n|Gmz4!<*6|%6=e&sj`~Uw-`Tzg_ zPXM_LWZv69|Nrk`*z7M-EXCL|`LLcnW6NY_eREA2L68%UzfgGul4oFGcqzpUcHaq) zUQt=FB-q|5lauvT7~fCs(3jP``0)RKkKTa)9^I}FApY6!z{tSxLh=zLY{253$6u_R zd`@45F=g^+{T9Bz{2+Ud?FT!rXmYE85#N5W5Y)w&CLc90bX^G+0$cd?0obq?9=)tn z!J<&TcOl{*Jeq4?Fq8y&bi2Or=oKvnD*<clnQU!n&G=w)xuFf?;>lYK1B93If$WA_ z)O!E_|JT-&eGKI%|1{+0D?^s^nCx#P%f!I2Imc)U@8kna5|bQWaQ0pP|6hcWfgxz} zM~Cf-+Kdbg46b|vZA{L*Y|KiGJnW$GXJF7^U|=}Iz`#&6`J|&ZBiH7yj+40=LpFE% zinB8P)0?~~sEjdWvSe^Pqt)ih;BZDp?adcM{&O$}O}-eb&lzF_3Kj;2kj<QN>C6BD C8Vjrd diff --git a/3-Shell/dsh_cli.c b/3-Shell/dsh_cli.c index 86e76b4..7ad6b24 100644 --- a/3-Shell/dsh_cli.c +++ b/3-Shell/dsh_cli.c @@ -73,8 +73,14 @@ int main() cmd_buff[strcspn(cmd_buff,"\n")] = '\0'; //IMPLEMENT THE REST OF THE REQUIREMENTS + + // exit command + char *cmd_buff_original = cmd_buff; + + while (*cmd_buff == SPACE_CHAR) cmd_buff++; + if (strcmp(cmd_buff, EXIT_CMD) == 0) { - _exit(cmd_buff); + _exit(cmd_buff_original); } else { diff --git a/3-Shell/questions.md b/3-Shell/questions.md new file mode 100644 index 0000000..30d498d --- /dev/null +++ b/3-Shell/questions.md @@ -0,0 +1,35 @@ +1. In this assignment I suggested you use `fgets()` to get user input in the main while loop. Why is `fgets()` a good choice for this application? + + > **Answer**: `fgets()` is a good choice for this application since it takes an argument for max length, avoiding buffer overflow. It is also convenient since it reads the whole line, so it matches the behavior of a command shell (input finishes when user hits enter) + +2. You needed to use `malloc()` to allocte memory for `cmd_buff` in `dsh_cli.c`. Can you explain why you needed to do that, instead of allocating a fixed-size array? + + > **Answer**: Using malloc() for cmd_buff allows dynamic memory allocation based on ARG_MAX, which helps prevent excessive stack usage and potential overflow. Heap allocation also ensures persistence when the array (pointer) is passed in different functions and enables future scalability, unlike a fixed-size array. + + +3. In `dshlib.c`, the function `build_cmd_list(`)` must trim leading and trailing spaces from each command before storing it. Why is this necessary? If we didn't trim spaces, what kind of issues might arise when executing commands in our shell? + + > **Answer**: Trimming leading and trailing spaces from commands in build_cmd_list() is necessary to ensure accurate validation and execution of inputs. If spaces were not removed: + - Extra spaces might cause commands to be not recognized, which leads to problems like difficulty in string comparison (eg: " exit" vs "exit") + - Empty commands cannot be or takes more time to be detected + +4. For this question you need to do some research on STDIN, STDOUT, and STDERR in Linux. We've learned this week that shells are "robust brokers of input and output". Google _"linux shell stdin stdout stderr explained"_ to get started. + +- One topic you should have found information on is "redirection". Please provide at least 3 redirection examples that we should implement in our custom shell, and explain what challenges we might have implementing them. + + > **Answer**: + - Output Redirection (> and >>) – Saves command output to a file (ls > fileout). Challenge: Handling file permissions and overwriting existing files + - Input Redirection (<) – Reads input from a file instead of the keyboard (wc < filein). Challenge: Detecting missing or unreadable files + - Error Redirection (2>) – Captures error messages (gcc script.c 2> fileerror). Challenge: Ensuring errors are correctly separated from standard output + +- You should have also learned about "pipes". Redirection and piping both involve controlling input and output in the shell, but they serve different purposes. Explain the key differences between redirection and piping. + + > **Answer**: Redirection writes/reads output/intput to/from files, while piping passes output of one command as input to another. So the key difference is that redirection interacts with files, while piping connects processes dynamically + +- STDERR is often used for error messages, while STDOUT is for regular output. Why is it important to keep these separate in a shell? + + > **Answer**: Keeping them separate makes debugging more clear and prevents errors from mixing with regular output. This allows logging errors while still processing successful command results. + +- How should our custom shell handle errors from commands that fail? Consider cases where a command outputs both STDOUT and STDERR. Should we provide a way to merge them, and if so, how? + + > **Answer**: Our shell should detect failed commands and display specific error messages via STDERR. We should allow merging STDERR and STDOUT (2>&1) when needed for logging or processing combined output. \ No newline at end of file -- GitLab