Sie sind auf Seite 1von 2

CS 314 Fall 2013 HW #2 Due: Thursday, October 10th, 2013 at 11:5 !

!" Simple Shell For th#s ass#$%"e%t, you &#ll #"!le"e%t a s#"!le #%teract#'e shell !ro$ra"( Th#s ass#$%"e%t #s ada!ted )ro" a% e*erc#se #% Operating Systems by +ary ,utt( 1( -ead throu$h the Executing Commands in Unix ha%dout a%d the Simple Shell ha%dout you rece#'ed #% class( These ha%douts !ro'#de a lot o) hel!)ul #%)or"at#o% that &#ll hel! you co"!lete th#s ass#$%"e%t( 2( .% the Executing Commands in Unix ha%dout, the author !rese%ts the #"!le"e%tat#o% o) a shell/l#0e !ro$ra" that reads a l#st o) co""a%ds )ro" a )#le the% e*ecutes each co""a%d 1ust as a shell &ould( 2lthou$h the code !rese%ted &ould tech%#cally &or0, so"e o) #t #% !art#cular the parsePath() a%d parseCommand() )u%ct#o%s are $ood e*a"!les o) bad C code( .%stead o) these )#les, you should use the three C )#les 3minishell.c, minishell.h, a%d parse.c4 located #% "y d#rectory( The )#les are located here: /home/christman/public_html/CS314/HWs/hw2/ -ead throu$h these )#les a%d make sure you ha'e a $ood u%dersta%d#%$ o) &hat they are do#%$( 5ou &#ll %eed to thorou$hly u%dersta%d these )#les to co"!lete th#s ass#$%"e%t( We &#ll d#scuss these )#les dur#%$ class as &ell( These C )#les re!rese%t the start o) a solut#o%, bas#cally a main() !ro$ra" a%d re&r#tte% 'ers#o%s o) the parsePath() a%d parseCommand() )u%ct#o%s( 3( 5our "#%#/shell !ro$ra" &#ll !ro"!t the user )or a co""a%d, read a%d !arse the co""a%d, a%d )#%ally e*ecute the co""a%d( To e*ecute the co""a%d, your !ro$ra" "ust search the user6s P !H 'ar#able to )#%d the a!!ro!r#ate co""a%d, the% call the e"ec#() syste" call to e*ecute the co""a%d( 5our !ro$ra" should e*#t &he% the user ty!es #% the co""a%d 7e*#t8( 4( So"e #"!orta%t #ssues: 314 5ou MUST use the e"ec#() syste" calls a%d NOT the e"ec#e()or e"ec#p() syste" calls to e*ecute the co""a%ds( See here )or the d#))ere%ce bet&ee% the calls: htt!:99e%(&#0#!ed#a(or$9&#0#9:*ec;3co"!ut#%$4 324 The author su$$ests read#%$ #%!ut )ro" the 0eyboard us#%$ $ets(bu%%er)( The C co"!#ler &#ll tell you %ot to use th#s, s#%ce #t #s u%sa)e 3#t ca% read "ore #%!ut tha% &#ll )#t #% the bu))er, &h#ch ca% cause a 'ul%erab#l#ty 0%o&% as a 7bu))er o'er)lo& attac084( 2 better )u%ct#o% a!!roach #s as )ollo&s:

char bu%%er&2'()* + + + %$ets(bu%%er, 2'(, stdin)* 334 ,ote that %$ets() does ,OT tr#" the %e&l#%e 3<=%64 character )ro" the e%d o) the str#%$, so #) you &a%ted to co"!are the co%te%ts o) the bu))er to the str#%$ 7e*#t8 3to see #) the "#%#/shell should e*#t4 you should e#ther "a%ually tr#" the %e&l#%e 3<=%64 character or co"!are the co""a%d l#%e to the str#%$ 7e*#t=%8( To co"!are t&o str#%$s s1 a%d s2, use strcmp(s1, s2), &h#ch retur%s /1 #) s1 > s2, 0 #) s1 ? s2, a%d 1 #) s1 @ s2( For "ore deta#ls o% th#s )u%ct#o%, use the A#%u* "a%ual !a$es, #(e( 7man strcmp8( 344 The author "e%t#o%s the )u%ct#o% access(-) as a &ay to see #) a co""a%d )#le e*#sts #% a !art#cular d#rectory( 5ou &#ll also %eed to chec0 #) you ha'e e*ecute !er"#ss#o% o% the co""a%d )#le( For deta#ls o% ho& to call th#s )u%ct#o% a%d &hat #t retur%s, read about the access() )u%ct#o% #% cha!ter 2 o) the B%#* "a%ual by ty!#%$ 7man 2 access8( 354 B%l#0e Ca'a &h#ch has auto"at#c $arba$e collect#o%, &he% you create structs or str#%$s #% C, you "ust allocate a%d de/allocate "e"ory )or these 'ar#ables( -ead throu$h the On to C Manual to see ho& to do th#s correctly( 3D4 Co""a%ds that are absolute !ath %a"es, s!ec#)#cally, those that be$#% &#th <96, <E6, <(96 <((96, or <996 3e($( (9a(out4 are %ot e*ecuted throu$h a !ro$ra" a%d there)ore do %ot reFu#re search#%$ the d#rector#es l#sted #% GH2TH( These co""a%ds ca% be e*ecuted d#rectly us#%$ e"ec#e()( There)ore, you &#ll ha'e to deal &#th these co""a%ds d#))ere%tly tha% the other "ore $e%eral co""a%ds( 3I4 B%l#0e "ost B%#* co""a%ds, the cha%$e d#rectory co""a%d 3cd4 #s %ot the %a"e o) a )#le, but #s actually #"!le"e%ted &#th#% the shell rather tha% #% a )#le( There)ore, your !ro$ra" does not ha'e to ha%dle the cd co""a%d( 3J4 5our !ro$ra" should be able to deal &#th #%'al#d co""a%ds( .) the user e%ters a% #%'al#d co""a%d, your !ro$ra" should !r#%t a 7Co""a%d ,ot Fou%d8 error "essa$e( 3 4 -e"e"ber that &he% !ro$ra""#%$ #% C, you should )ree a%y dy%a"#cally allocated "e"ory &he% you are do%e &#th #t( Submission Hlace your )#les #% a d#rectory labeled <h&26( Sub"#t your d#rectory us#%$: submit314 hw2