Linux C 函数参考手册


BIB %#Ma@& isalnum ( BliitFRfiF %!%*?@3&?- 1 ... 2 isalpha ( @]i$?8e7!7 3 %*?qj ............... 3 isascii ( @! 3.S 8f 2% % ASCII I%? R ..ern..0 4 isblank l #l]i$T$jEB% 2%915 ............... 5 iscntrl $!!Nlf.BE$% ASCII @3$tlE$J +$j' ) ............................................................... 5 isdigit ( @!l%?fi%B%lWt.ffi%? ...*......... 6 isgraph ( #!4%9fia%% qfl€P?R ........... 7 islower ( #!li3FRR5%/J~%%A?f$) a.a.... 8 isprint @~i$~fl&F%irlfi@~~) ............. 8 isspace ( fiIJ%TBf.%Ej$ $J&TR ............... 9 ispunct (~ijft?REBk~fiR'+~%R R?, ............................................................. 10 isupper (@JiitZflZE%~k~?&fr:~@~ +.... 11 isxdigit ( #!I i$? fl f$ P? %J 16 %$!!#? 1 ....... 12 Be* &B%&fiB atuf ( $jFfi$ ~!IZB} ................... 15 doi (%TR$~@L~P~'J 1 ....................... 15 ~~1 (#~~$4f#&~f#k!&~ ................... 16 ecvt (%%fi?I!!#V&R7-R%, RIW hh) ............................................................. 17 fcvt Gw?&$B%#%?R*t Ra$ ZA) ............................................................. 18 gcvt (#P?A@%R#&%9R$, 4Wl.g &A j ............................................................. 19 sm')'-j (&+ic); $rg#&jjfi, ga) a*.............. 20 m1 (##Tfl $$$&&*gg3gP: ................. 2 1 smoul ( %?R$ %R&%R4K@$# 1 ... 22 toascii (#P3B%#&&i&H ASCll bg ) ............................................................. 23 tolower ( #fJr5?+3%%&~113S~ +.a+....* 24 toupper ( %/l~%?$~R&;kRS?@ ) ++.+...+. 25 %3* ~#EEm& alloca (g~zbgq(q > ................................... 27 bfk (&s&#g$?~t~gfi) ........................... 27 callw ( ~g fi 8214 1 .......................... ........ 27 frse (#$&@&$~ftq~*) ........................... 28 getpagmize (m~ fiA#$ik/J\ j .................. 28 (Ef finq1'fl ) .................................. 29 m,-,, ap (~~P~J~@IJ~~) ................~...+.~...++.++.+.. 29 r-unmap ( MR lf~ j'!j@~ 1 .......~........~...~+......... 32 ~.eaIIrx: ( E& EBlfim fi gP(fl a7 +................. 33 sbrk (j#fiR$g#qaj~~g~[gj ) .................. 34 %4B HRaa axtime ~~W~;lTa7~.sIE3M~~~~.8$d??5~ ... 36 clock (XI#iBB&a3 CPU &I kEtWfs7 1 ..... 36 ctime {%fl;STe7NEMUSR%&RS%j +. ++37 difftime (it# W[@EE > ................................ 38 ftjtime (RB ~mffj~fq8 FJ jgj gl .................... 38 geaimeofday (@#I fimwrq ) .................... 39 grntime €I T$JKIIHIRJN H Mgl +...+..+....++..+ 4 1 localtime (~#%&hHiiifb!lFJ$Ts7&lEl~g3) +*-++.42 mktime I #Ff~~%@%k#f$$#Ek%%~@#k 1 ... 43 =timfday ( BE Sifi&tlB;f(q) .................... 44 strftime (#$k B m#Q[q > ........................ 45 lime (R# H%#Jw[q) d................................. 47 lrset (~jgs;l~ p~#+s;lp~gjt&> ..................... 48 935s ?#cfrfiB@# hemp ( kkE&&fig) .................................... 50 hopy @EPCJG~@) ................................... 50 b (~-gp~$fpJg*jg~~) ................ 5 1 ffs ($E-!$$%qf!3%%-+~kJ%b!l@l -52 index (g)%f ?$$~%-~-~&N~%$F' ) ................................................................. 53 memccpy (g 8 fi 8 ) .....................+..+.... 53 memchr i ZE&-.-h&Bl3lqB f%-l$?Z% 8) ................................................................. 54 ............. cmp ( &@fi@h@) ".. ............. 55 ............................... memcpy (E fijeb~ j 56 mtmfmb (j$h&E@&kL j .......................... 5 7 memmove ( fi Ah ....................*....... 5 8 memset r %-,% h GP[$$lA%B ...........a 59 rindex ~$~???$~&6-~$@!~%% 9.8) ............................................................. 59 strcasecrnp $2 @A/hS bL%%!f+ 1 .+......+4. 60 . strcat @+gpj?,q g j ...++.++..+...+,+..++..+. .......... 6 1 strchr (. &4&??$*+%.-. /i'.MFlSgE? fi > ................................................................. 62 ,kc, ( bt;e~~*j ...................................... 63 strc~il r%M ~g~@EtWRflk~lJ&#sRtt: SF$$$ 1 ..................................................... 64 strcpy (~p-qq*) ....................................... 64 strcspn ~&H$R$4+&i%$3%Z9?"8 fi@QqSq$$J} ............................................. 65 strdup { R*J+fi* ) ....................................... 66 strfry ( @#L~~~B$ fi Kj98 1 .......*....P.m. 67 strien ~+$~TR*J~T)E) ................................ 66 stmcasecmp ,@? @%+% bki!???? @ .a*.....+ 68 ( gg$gq~$ ) .................................. 69 strncmp k$q~$ 1 .................................... 70 ................ strncpy ($$ R?$$?$ $ ) +.+. ................. 7 1 strpbrk ( &;Pft?$!f* $%-'?-~%ft!l$$%? R) ................................................................. 71 stmh ~BE%~@P@G-?~~R~Ki~ 2~ j ............................................................. 72 StflQn ~~~$7~$~%!~~%%~?8$ ............... pJB(fjFB&) .............................. 73 strstr (B-BR* +B#~E~~KI$.R$I 1 ..... 74 smok (#BJTR*) ........................................ 75 %6$ B+itBfiB (itg4t$jy4f~gs~f~) +.......................... 78 a,-oS (~&sff34;@y~!@) .................................. 78 asin ( RfijF .................................. 79 atan (~g~ggqa#j@> .................................. 80 ntan2 (~~fi gm&@@ ...................~+.+.~+.~ 81 ceil ( @FYJ\*@~&($JE /Jig!?$& ) .*..*..*.....*. 81 ....................................... ,, & 82 cash (~i(q@@+3$~&@1 .......... ............... 83 ........ div (@!@mg@%!@k6mfifi&%) 84 exp ( itgaB kP) ............................................... 85 fabs (itggfi ga~e~m ) ...................... 85 frexp (I%$$ fi@!?&%%l&%%$k% ) ..++....++.+ 86 hypot ( i+s~fi 1% jf$~&fi) .................... 87 labs (ifgeBa&m@w@} ...................... 88 ................................ ldexp (it# 2 &1&3&) 89 ldiv %@EE@$k#!%g k!ll%iB&& 1 ....... 89 log (itg~j :Re ft~@y$t&g) ....... ................. 90 loglo (if$$ I;R 10 $~&&Mfpe@) .................. 91 ........ modf (%#A!tJ@fiH&P!&5~J~% 1 92 pow (it$>Xjy@) .......................................... 93 sin (&E$&$jPBfi@) ........................................ 94 S, & (R~rn%E%gq&E) .......................... 95 sqrt (~$f+3#@) ....................................... 95 tan (~$fzt~gj~~) ........................................ 96 tanh (&qa$&~+Jj@&fi) .......................... 97 7 ajPiFDB@& ......................... cuerid (@#flp%%g#&> 99 ,endgent ( ~~$~*{~) ................................... 99 ( $iJ * j .................. ..+...+, . 100 ( 3 [d utmp } ......................... 100 fgergrent ( M%%N2#~8@?1&$1 .... 101 fgetpwent ( ME$~X#~@@%W&& ) ... 103 (!@J#gBmaiEBJ@$ >' ................... 104 geteuid ( %$$a% b$N P iF.916q 1 ..+..+......... 1 05 ptgid ( &#&$maisXr]@) ..................... 1Q6 gttgreni ( Mt~*l+Z1+ +%R~$.@IB j@ , ............................................................... 106 getgrgid I M%*!qP%!f3$3% gid m%! #, ............................................................... 1 08 getgranm ( MWZiTWK@#$%HB%) *.. 109 gelgroups (@#Q{t&q) ............................... I IO getlogin { Rf#BSKtttlaj P N8I&fi 1 ....+..... 11 I getpw (JEfRTB$%I P bQ%@+*i4%@ ) ..... 1 12 getpwent M%@ft#EPRf%Pk%!i%% ... 1 13 getpwnam i M%W*(VPRRifEZK%& ........................................................... %#f ) 115 getpwuid I M%@*#$ R#lEZ uid W %#) ........................................................... 116 getuid I R#agmm piJ!#]@ 1 ........m........ 117 getutent (M utmp ftiWRR%?E%B #$) ............................................................... 117 gerutid ( M utrnp ft#+g~%%$533) ... 1 19 getutline (M utmp *WBI$%ZBtli2, 31 .++ 120 initgmups (m%egg$) ........................... 12 1 logwtrnp C #-B3% wtmp 2 ............................................................... $1 122 pututltne ( #% utmp 23% hA.I+> ............ 1 22 %legid ( Eg$Bm$li, XI]@ ) ................... 123 sekuid ( gB&8!@JH P iEBJ@ 1 ............... 1 24 setfsgid ( agx#%$$&@iP, RqH 1 ..+..++.++ 1 24 setfsuid ( t!YE*# %6Ht98] ,@ iF! Rflfig 1 .++.++ 125 setgid ( $Ba$&&i~, %]a > ..................... 125 setgrent ( 4 $42 II 8 2 fk CP b!~ BE 1 .ma... 126 ............................... setgroups ( '$kg'#\@ 1 126 setpwcnt (M&$R86924+$&1E9% #) ............................................................... 127 setregid ( ~E!B&,R~B@!I~H~P, XIJR j ..A*.* 128 setreuid ( ~~~~~l%'!&b!l P iff84 W 1 ... I28 setuid ( fk $&% M p i,0, 81 fi ................. 129 setutent M%@R utmp *W!?EIB~& , ............................................................... 129 updwunp (%-g%&#Z2FI] wimp * I+} ............................................................... 130 utmpname iQE utmp *#$8@ > ............+. 131 8s %&&*&& Crypt c#$@@$s$j#jsp, 1 ........................... 133 gelpass ( @#-@@$&A > ........................... 134 %9 RB&;)$ElB bsmh (~x#~j ....................................... 137 hcMte ( j$%@rftfa) ................................... 139 Mestory (lffj@e$s > ................................. 139 hearch {@$$%%% j ................................... 139 insgne ( 8nA-G gflyl] $I 1 .................... 140 \find ( k&B@B ) .........................................+. 141 Isear& {$4k@sj ...................................+.+. 141 qsort ( $J~~~~~~%HF~II~~ > ...+..+........ 143 xu fi 1- > ................... ' 144 delete ( MZX#+~B&B ) .................... 145 tfind (@g=Xfl) ....................................... 145 Iseach (=aw) ........................................... 146 ($i~--xH) ...................................... 146 0 Bbi&%fi& drand48 I P!~-+~E~!II~.~.ZB#~&) +.+..+ 149 erand48 7?k-?iEb!J@.6 gh8fiB 1 +..... 150 initstate r gZE#l'g(t%&&%l 1 .................. 151 jrand48 ( P&-'?fi@!@aBfi# 1 ........... 151 lcong48 SE! 48 ~Z#b'!?$&&#?) .a.m 152 ~~UIC ( #-@$FRS A&#$tp I ........... 1 84 ~PUIS (%-BiZMZR%S A*IYh) ....... 185 fread ( ~~{~$$~@~~ ........................... 1 86 freopen ($Jj+*j$) ....................................... 187 fseek ( @aft{TRm$% ) .................. 187 fsetpos ) ............... 189 fkll (~fq*pp&~j$@fif 1 .................... 189 b"te (#a#'ll;r*#g) ......................... 190 getc ( ~ft(++~~-+~~~ .................... 191 getchar ( bk@%AB&h &%-9-?3 i ... 192 gets (&h%$#h@&kl&~-?fl~> .... 192 mktemp ( P 9%- w 2 ie *I+ % ) ...... 193 putt ( #@-@aFn 5 AT! > ................ 194 putchar i %f~~K!???%F~~~#~ 5! &) ... " ......... '." ............................................. 194 puts ~%#~WK!?R*SP~J~~%I~~B &) ............................................................... 195 rewind (3$6!*#%&@%&Z%%#-R A) ............................................................... 195 setbur ( @~ft#fi~~#~ ) ..................... 196 serbuffer ( ~E*#inkL~~@E ................. 1% scrlincbuf ( iftBk#%%&#@*E 1 ....... 197 sehbuf ~~gq&*(+~~~~~~ ................... 197 tmpfile {@albwz{+) ............................. .... 198 ungetc l %-%@FR%ElX14iR+ j ........ 198 %I3 s i!E@BBR@H abort ( ~W~~~$~~@ ) ....................... 201 assert %$~MH%B;T.~~~F!I~%~R 1 ... 20 1 atexit ( i$f EEER%?kSi;r%Mm&% 1 ..+ 202 exec! (Nfift# j .......................................... 203 ex& (&fix#) ......................................... 203 execlp (M PATH G%%f +B%ftl+# fifi, ........................................................... 204 execv (h~g#) ......................................... 205 execve ( &fi&(+) . ............. 206 scandir (@&Rzm a 3## ) ................... 270 seewir i!$2?il7;El@@ 3NfiE 1 ........+.. 272 ..................................... 1 (& 27 3 symlink (@2*{*@4$;4i%) ...................... 277 telldir ( @# $fim@RQg ) ................. 278 m,y a~ ( &%*#k,J\ > .............................. 279 umask ( Bf Z%~%Jift~~~~@~~~) .... 280 unlink ( fl]@ 3 {+ > ......................................... 280 utirne r ~&ftl+mhrDlWCs7fiE&Nral j .+. 28 1 utimes ri&A%f%WGRWI'87@E&NIU 1 .... 281 B9@& a]m (egp {$j&/aH> .......................... a3 284 kin ( ~~~~$$@~~~@) ..................... 285 pause ( ~~~##~EIJ{~~~~~ .............. 286 psignal ($4 %E?#%$RfkF29?5 b 1 ....... 287 raise ~figg~& 3~3~) ................... 288 sigaction Bi$%%Zfi9fiBE?$ j ........ 288 sigaddset ( @l JR - +!E $ Z ($$% 1 ............ 29 1 sigdelset C ME'i$%WJB-+E% ......... 291 sigemptyxt I @@fi~3g ) ........................ 292 sigfillset ( %fiGE$fiR hgmq% ) .....++.. 292 sigismember (8!d%X'tE9EE EhiAE jgqgg ) ................................................... 292 signal r ~~~~&~~~~ ......................... 293 sigpause ( ~~~~~jfi~~~$-) ..................... 294 sigpending ( Bfi@flE$~Eq) ................. 294 sigprwmask ( ~i'&li$%!~~~i!&~ ........... 294 sigsuspend ( B#B9llE?BJ% ) ................. 295 sleep ( i&&~Q$&fi-~~m 1 +.++..+..++ ..+. 295 isdigit ( jadi&%REG%W4$1fi!&? A*....... 2% %lm %EfiH@& €error r ~~*~%%~~~~~~~ 1 .......... 299 perror ( fJ f$*%Smi,EiE. b?RT$ ) ......... 299 stremr c %HagRasrJ%B?R* 1 ....... 300 % * @ZMSb&& rnkfif0 ( Bi'JRg @% > ................................ 303 pclosc (gfjjpa u,o > ................................. 304 pipe C gzEs) ............................................ 305 ppen yo) ++ 4++..+.++4.++4++.++4.+4.++4++n.+ 306 #$I9 $ Socket MRfiR accepl ( @ 9 socks sg ) ............................ 309 bind ( t;fc a@) ................................... 309 connect ( ga socket j$$ j .......................... 311 endprotoent ( ki!~~%fii~~#ffI!l~@! ) +... 3 13 endservent I %% ~%B%!!@Bt%&R~ ..... 313 gethosrbyaddr ( IP ~hkB@ PI $!i &# ) .+. 3 14 getbmtbpame < &!&#I, %%R#m&&%f) .A+ 315 getpmtchyname ( EII F4 %$ W iI$ %@@Mi.X gj# ) ........................................................... 317 getprotobynumber & M !%kb$!%%&#W Ngfrjg > ....................................................... 317 getprotoent < @#F!I%UI~!%#~ 1 +..++..+...+..+.. 318 getsewbyname (!?k%%@@l%%%%l##k j ............................................................... 320 getsewbyport (# port ?!2%k#m%!k%m &# ........................................................... 321 gekncat (R@&#M#BFt$$lk!?&%~ ...... 321 getsockopt (RB socket ..................... 323 herror ( $lv m!i#g@mE. @%n$ 1 ... 324 hstremr (BBP6Q%%~@B~#BT# $ ) ............................................................... 324 htonl ($ij 32 ~%#l?~J@~%#&l%% flR ) ................................................... 325 htons (# 16 @&#l%?M@$%%&R% Tflj@@) ....................................... *+.*4,.* .... 326 inet-ad& d#jPW%~%&R%=%$.lb!~% ............................................................... , 326 inetaton RM%M~%~M%~%II]$9% FjT ) ............................................................... 326 iwt-ntoa (#Fl%3tE$JmB?%#&M% ~jjt) ........................................................... 3 27 .......................................... listen r $.fa&& g: 328 ntohl 32 !CW%?fi!RR%#&.HL 5;, ................................................... 33 1 ntohs (% 16 f$M%?R#%%&fi$fl ................................................... TfljDrq) 33 1 recv r gg g@~# 1 .......................... 332 recvfmm ( g$ socket #@&@ 1 .................. 333 recvmsg ( 2 &~@%) ................... 335 send ( g. socket 1 ......................... 3 36 ................... sendmsg (tg . socket {%z%itt#> 336 (t? socket f+ggj~) ...................... 338 ..... setprotwnt ( fIH PIgjhi.2 m!&# kt4 340 setservent i $TA%#, W%16i%@J%@i2 f't: ) ..............................'.... ............................ 340 setsockopt ( $3 mket %& ) .................... 340 shutdown ( $$lh socket BE ) ..................... 34 1 sw ket ~gh- .- + .................. 342 %20$ i!H28lE (If'C) BlC fiok ~%X4+fifQSflt+Ell4tY%k System V IPC key j ................................................. 345 msgctr (g%Jfi ,'&.N$~jp~&fi) .................... 345 msgget ( g$fg, BBAFI] ) ............................... 348 ,, (Mfi,@hgj@m{g, g) ................... 349 rnsgsnd (%E,BjgA(.$A $\pApI] > ................... 350 semct~ ~~$rJf~.!$~~~~&@) .................... 35 1 semget ( RJ~S~ a711 ) .... ..+..+..+4.+.b.'.-... 353 ........................................ stmop { j 354 s.,mat (attach g3 fi fi 1 .............................. 356 shmc[l ( fPi*l]*-+ ~GVJE~~ j .................... 357 shmdt (detach #$fig j ............................. 359 shmger (flzg#qfi& ) .... ".+ ....................... 360 %21@ ZS@& cog . , ............................. 362 ( %.&{&E.g.i~z 1 .......................... 362 syslog (%($.@.,.iz%E gih fl ;L .......... 372 Itgexec ( ~~~&~~J&$+~@J#!! > .............. 374 regfree c $$A,kiEJ!l]$&&I$FH@.lh.f$ ....... 375 %24$ 81&8B d]dose (Xw,j$~&&aj$*fT . ................... 378 dlemr < rjtl$j$~~gg~s> ........................ 378 dlopen (fJRj&&fi&gfr:#$j .................... 379 dlsym C M39#@+@%$~;&& % 1 ........+ 380 3325s &Ni%!!.k getopt ( ++fiFiQ+j~g& j ............................. 383 isatty ~PlWrZl+#ke~12S~k%%#l I ... 384 select s~fi'$~j) .................................. 385 ayfiame (&n.--%sfigfi I; . ........ ............. 386 ir#s A @&@*-gcc 25a& MW czsa Mik D Linm E3#J% Hit E *RBERfi&EM @X # : isalpha, isdigit, islower, isupper gx&a : int isalnum (int cl; &#%W : &&%& c EB%3!2?+itS'tP~Rft%k+, i!Et??E C +#llZ~TttRl (isalpha (c) II isdigit (c 1) 5 H f& : g$& c %%@$&?, !j!q&H TRUE, BNjjaB NULL (0)o /* Mi str ?V%CP%%X?@$R?HTR */ #include main (1 i char str[]-"23c@#FDsP[e?"; int i; for (i = 0; str[iJ != 0; i++) if (isalnum (str [i] ) printf ("%c is an alphanumeric character\nW . str [i] 1 ; 1 1 is an alphabetic character 2 is an alphabetic character 3 is an alphabetic character c is an alphabetic character F is an alphabetic character D is an alphabetic character s is an alphabetic character P is an alphabetic character e is an alphabetic character mx&# : isalnum, islower, isupper &x&# : intisalpha Cintc); di!#W?El : BB@# c E7S%%Z~E17 &6@ C +&l%T@H (isupper (cj 11 islower cc>> I'&#[l80 @ : g@%! c %%*?@, flll&m TRUE, sfllJ&B NULL (O), /* 4%~ str TR$~YJ?&*+@&?R */ #include main 0 I char str [ 1 ="123c&#FDsP [e?I1; int i; for (i = 0; str[i] != O;i++) if (isalpha (str[il) ] grintf Iu%c is an alphabetic character\nn, str[i]) ; 1 c is an alphabetic character F is an alphabetic character D is an alphabetir character s is an alphabetic character P is an alphabetic character e is an alphabetic character @%&@ : iscntrl &$# : #include ex@B : int isascii (int c): a&aR : &g@@ c ASCII @3?$. &$!kt#& c &%%aE'd;& 0 $d 127 2 I's7 6 a : g%!# c % ASCII $!I+?$, !lj&fl TRUE, %!lb!%B NULL WMNgj : Ik%%ZsL wEEfi&o /* #j@ int i %sg%X$l&m ASCII #TR */ #include main (} i int i; for (i = 125: i < 130, i++) if {isascii (i) ) printf ("%a is an ascii character : %cinn , i, i) ; else printf Iv%d is not an ascii character\nn, i) ; 3 125 is an ascii character : 1 126 is an ascii character ; - 127 is an ascii character : 128 is not an ascii character 129 is not an ascii character ' : intisblank (intc); : Uf &t c t$%PISR, B%RYllWE&%P& (spsce) &EWQ%R , ., . . ,, ,. (tab), Z& (space) H ASCII W% 32, Z@?R (tab) WJ ASC~RR~J% 9. :i lEil @': %@!& c %%#$qfl, gqsg TRUE, BIl(%B NULL (O), /* #FRS str [ 1 EppJ$m9#ZZ%B&, #E5P%?Rm ASCII a*/ #include main (1 I char str [ 1 ="123c B# FD sP[e?* ; int i; for (i = 0; str[i] != O;i+t) if (isblank (str[i]) )printf IHstr[%d] isblankcharacter: %d\nm, i. str[i]) ; 1 str[4] is blank character: 32 str[7] is blank character: 32 str[l0] is blank character: 9 a&*+# : #include &xa& : int iscntrl (int cl: @#k#M : &B&& c sB% ASCII g%q@, i!l%E?u@ c b!l%mEB& 0 gu 31 2id0 a fi : %%% c h ASCII @%{fit R'l&m TRUE, g!r]%fl NULL (01, W&I&W : lk%%%X* 4k&iEBi#o : isxdigit 3% k .PF : #include sx@a : int isdigit (int c): @##&I : &$%a c EB%PVt$fBBF FIJ 9, 5 a : gB& c %6a743f1&?, !ll%H TRUE, ??~IJ&B NULL MaaW : hkA%ZX9 4F&iEi%&o /* ;f.likb str SR$$%R6le%Tm?R */ #include main (1 i char str [I ="l23c@#FDsP[e?": int i; for (i = 0: str[i] != O;i++) if (isdigit lstr[i] J ) printf I"%c is an digit charactcr\nu, strii]) ; rv4-& .... ..... 1 is an digit character 2 is an digit character 3 is an digit character : int isgraph (intc); i@1&%91: &Bt#r c RB%WW-P3.?$, % c %%@Kt ASflIFfW@J, 892# %fiR!J%@ TRUE0 s & .: c Iflim TRUE, gFq&B NULL (0). I char str[] ="a5 @; ': int i; for (i = 0; str[i] != O;i++) if (isgraph (str lil 1 . . .-. ... . printf Imstr [%dl is printable character, %d\nM . . f. i, , str I' [i]) ; . . .. .. . -. . . . .. .. . str[O] is printable character, a str[ll is grintable character, 5 str[3] is printable character! @ str 141 is printable character: : i I . .. . . . isalpha, isupper . . # : int islower (intc); /* str PB@+%~J~Z#RF%~Pfi *, #include main () { char' str [ J =" 123c@#FDsP [e?'; int i; for (i = 0; str[i] != O;i++) if {islower (str [i] I 1 grintf ImBc is a lower-case character\nm, str[if) : 1 c is a lower-case character s is a lower-case character e is a lower-case character 1 %Re&:: . ......... int isprint (int c); ~~~~t awa TRUE. 'a .m-# :: g$FJr c hWU$PB, lg%H TRUE. BB!J%B NULL (01, ............. ir #%41~, j~g&aa~ i -.,.,. ............. /* M6 str ?B#+mXTn$+R 18$P&Tfl, */ #include xctype. h, main (1 I char str [ 3 =*a5 @; "; int i; for (i = 0; str[il != O;i++) if (isprint (str[i]) ) grinti (wstr[%dl is printable aharactar, %d\nu. i, str[i]) ; 1 str[O] is printable character: a str[ll is printable character, 5 str[21 is printable character: strl31 is printable character, B strC41 is printable character: ; # : c %q&??$, !$IlB@ TRUE, gFI]&H NULL (O), /* #PRS sw3 Ffi&$@2#PfiB$, #E~~&SRIB ASCII W*/ #include main 0 { char strfl="123c Q# FD\tsP[e?\nn; int i; for li ;- 0; strlil != O;i++} if (isspace (str[il) ) printf ("str [%dl is a white-space character, %dinn, i, str [il l ; 1 str[4] is a white-space character: 32 str[7] is a white-space character, 32 str[lOl is a white-space character, 9 /* 'it *! str[l6] is a white-space character, lo/* in */ - at&*# : #include cctype.h> sx@& : intispunct (intc): a##&&J : &B%% c i$~%6,&8%@?%%%??o %@ TRUE &%@if2k@& c % 3% BI : %OB c %~$%,$,?$%%%%%3, !l~J~~ TRUE, STJ!IJ&H NULL (0). ,'* Pj&?R@ str Pmh,6R*%#%Be */ #include main 0 I char str[1="23c@ #FDsP[e?"; int i; for {i = 0; str[i] != O;i++) if [ispunct (str[i]) ) printf (m%c\nl', str[i]) ; 1 m*@# : isalpha, islower @aft# : #include &xi#j& ': int isupper Iint c): &#a!!lj : &B%# c EB%A%%JW@~ @ @ $.: 88% c %ASBffP@, !WJ&IEI TRUE. $IU%H NULL (0). MBi#M : Bk4XW7 4WiElB. #include main I} C char str[l=*123c@#FD~P[e?~; int i; for (i = 0; str[il != O;i++) if (isupper (str [il ) ) grintf (*%c is a an uggsrcase characterln* , str [ill ; 1 P is a an uppercase character D is a an uppercase character P is a an uppercase character m& : int isxdigit ( int c ): : @6)# c E8% 16 %%lRP8, Rf c )57;##9-+RlRR~lD TRUE0 163$J%?: 0123456789abcdefABCDEF g m7 In; %St c % 16B$!J&F, RlJ1lBl TRUE, GABB NULL (0). w&'~m a%znn, rlbmaa. /* #& 3tr ?R@$%tk%M#*rn*R #include main 0 I. i . ., , . ~ char str[]="123c@#FDsP[e?"; . . .. . . ,.,,,< ' I: . . * -1 , .:. . . .. int i; , I ... . . . : .:.,.~ , for Ii = 0; str[il != O.J~++) ... .. . . I. . if ( isxdigit (str [i] 1. )-printf .. . ("%c is a hkiedecimal digits\nn , str [i] ) ; . . . .. ,. . 1 . . . . '.;, , . I ,A'.'.' ' I 1 is a hexadecimal digits 2 is a hexadecimal digits 3 is a hexadecimal digits c is a hexadecimal digits F is a hexadecimal digits D is a hexadecimal digits e is a hexadecimal digits. I-I HBSBmR I @EJ~ : double atof ( const char *npa) ; P[t&i#l': atof ( 1 $@# strtod (optr, (char **I NULL) t %%#dm 4 /* 18$R$ a -4?R$ b %Bfi%+gHh~ */ #include main 1) { char *a=m-100.23m; char *b=" 200e-2"; float c; c = atof (a) + atof (b1 ; printf ("c = %.2f\nUr c) ; 1 : atof, atol, strtod, strtol, stttoul .*** A ,. I:: nclude ............ : int atoi (const char *nptr): ' atoi 0 $EM strtol hpa, (char**) NULL. 10); !$%$llHO /* RSR@ a ST+?* b S@@BPEHAu */ #include cstdlib.hr main (1 C char a[ ]=n-lOOn; char b[]=" 456"; int c; c = atoi (a) + atoi (b) ; grintf ('c = 3djn3, c) ; 1 ........... . 4. atof, ai,. sM, strtolt sQctoul .................... ............... ; *&*# : #include Cstd1ib.b : %# . . : long at01 (const char *nptr): i &&a@: atol o w~t~ nptr TR+, ~~2iiiimme~q3;f* IZIJ~~Z~~PSE fiR9$*&MR&, ~R~~IJ~F~+%*R@%%H (7VI mi*%%, #%BR%DQ #i: &@$$~1mtc;%lgO ...................... --A. ,< .? . maam am^ ................ ,....r at01 o %EN ~~lt01 (nptr, (char**) NULL, 10); %%!mmo main I) i char a[]=mlOOOOO~OOO~; char b[J="2234563890n; lag c; c = at01 (a) 9 at01 Ib1 ; printf lmc = %dinn, c) ; 1 !'l#m#.i: l ......-........ .... fcvt, gcvt, sprintf , .............. i 1. a# . a& .: j : char *ecvt (double number, int ndigits, int *decpt, int *sip 1 ; 1 m#f;l;: .-,. A - avt ( 1 81 f number %#1 ASClI R8.flB. B& ndigils a7JCB 18S8Bo bR@BG9, @t dqt %ftfi%M2~&1E4%@EP~J~#k85 tpik ~M2H6BB)$ ifi+B sip ~StR%@$%JgitS&BiE$fiI % ;lii # : &HL3?$'1;%+, &373@$!4% static, g@i%aj ecvt > 3 fcvt ( 1. ~%R*~~~~@~o : i$FB&ffl sprintf ( ) {ki$$@, #include main { j i double a = 123.45; double b = -1234.56; char "ptr; int decpt, sign; ptr = ecvt (a. 5, &decpt, &sign) ; printf {"decept = %d, sign = %d, a value = %s\nw, decpt, sign, ptrl ; ptr = ecvt (b, 6. &decpt, &sign) ; printf (*decept = %d. sign = %d. b value = %sinu, decpt. sign, ptr) ; decept = 3, sign = 0, a value = 12345 decept = 4, sign : 1, b value = 123456 mg@@ : ecvt, gcvt, sprintf : #include cstd1ib.b ex # : char *fcvt (double number, int ndigits, int *decpt, int *sign ); &##w : fcvt 0 H*$f%# number #-&a ASCII @?fi*, $!!$? ndigits %%/I\ &AG%%@fi!&o %%@&@, B# decpt EHfiak!l%iii&&@&#& B a @ : BB-?Bl$l?#+, k?R$Ff!Ajtl static, %R$laJ ecvt 1 a fcvt ( 1 &??$* bB+E@%o k#&BY : i8K$&ffl sprintf (1 {&@@o #include main 0 I double a = 123.45; double b -1234,567; char *ptr; int decpt , aim; ptr = fcvt (a, 2, &decpt, &sign) ; /* IJ\&,~EETJ~ 2 G% *I printf (Wecept = %d, sign = %d, a value = %s\nm, decpt, sign, ptr] ; ptr = fcvt (b. 3, &dccpt. &sign] ; I* t]1&,%63% 3 3& */ grintf (*decept = %d, sign = %d, b value = %s\nm, decpt, sign. ptr) ; 1 decept 3. sign = 0, a value = 12345 decept = 4, sign = 1, b value = 1234567 ma@%': mvt, f~t. sprintf ex&# . : char *gcvt (double number, size-t nhgits, char *buf 1 : : fimi#BJj: gcvt 0 B]*@1$P@ number ASCII i@%!f$, %# ndigik %%g ~&B@. pvt 0 3 ecvt 0 @ fcvt 0 ;I;AMtg3B;f. gcvt 0 FrK ~Grn?R$@8fJ~%fiSiEfiR5* g%#&a* %#Grn?R*&rnE b~f B%tfiBb!I!W~~ s''~ ... : ID-TR@~BH, wktw buf %+to #include cstdlib.hr main () C double a = 123.45; double b = -1254.56; char *gtr t int decpt, sign; gcvt Ia, 5, gtr) ; printf ("a value = 8s\nn, ptrl ; prr = gcvt (b, 6, ptr) ; printf ("b vhlue = %s\nu , gtr) ; 1 a value - 123.45 b value = -1234.56 .............. ., ......... .. j&g@# :: double svtod (const char *nptr, char **end@); ............._. _ ....... #%i$!$!&D. g endpv 4% NULL, Rrl~#je3l$%&f~fi%k~ nptr Wt!SiSWWI endpa %Do %#!k np@ +RPW!k$Efi4. ~14&.6r3i E (e) 3/$?%#$&@%0 h 123.456 8 123e-2, i* %T%?Eg, a, b, c fiR'I%R 10, 2, 16 %#J%#fMkP */ #include 28W& npb ?R$, @bi$T%i@&@&?$$t g%ll&k #+%EfiR9*Rai&%%P RWU4F%Y%w?f*%%@ (WI %.R% @, ##$$i%E@. endptr $% NULL, !i!d]b#fBFc)$&&#fi%k @I nptr ~b!I?flf%tb endptr &Do P[tftli#y : ERANGE %%B(l$$@q$f$j&&lf?%%Ho &)l!a# : int toascii (int c): &&~m : toascii ( ) &-%@$& c $$%a 7 unsigned char fi, %/\nl!q&&#kkp bk?-fi~P&&%& ASCII @?Ro a # : %%#tbk~h~ ASCII @$?R@t%D main (1 I int a = 217; char b; pt-intf {''before toascii 0 : a value = %d {%cJ inn, a, a) ; b = toascii (a) ; printf {-after toascii 0 : a value = B;d (%c) In". b, b) ; 1 before toascii I) : a value = 217 (1 after toascii I) : a value = 89 (Y) a# : int tolowr (int c ); /* Pi +R*F~~!~AST@R&J~~IGFW~ */ #include main 0 { char s[l=na~cbeFgH12345;!#$u; int i; grintf ("before toupper I) : %s\nn, s) ; for (i = 0; i < sizeof Is) : it+] s[il = toupper [s[i]) ; printf {"after toupper () ! %sinm, s) ; I before toupper (1 : a~cDeFgH12345; !#$ after toupper I) : ABCDEFGH12345;!%$ APTE : malloc, free, reallm, brk a : void *allma ( size-t size 1 ; SIB~M~ : allma 0 H*BZZ size +?TmhG91'4t %ii3@ malloclcalloc $MKt E, allma 0 BMNt$9Ts3 Istack) Wk2i!t~Gt H~E@%Z~slS~R 3lR&&qi4 0 23 w #i : zRzaaRq%H-lkft+ %J!kQl&H NULL0 - : malloc, calloc, free, dloc, sbrk &a @& : int brk ( void *end-data-segment): : brk ( 1 fflf%fl%.% end-data-segment fifib!J%.liEtff&%m$fr6$?%%a Zi H #i : %@I@~H%QRI]BEI oo &BfiHXWIfiIJZB -1, 44 emo $% ENOMEM. aB : malloc, free, realIoc, brk a&*# : #include struct test C int ail01 ; char b [2 01 ; j; main 0 { struct test *ptr = calloc (sizeof (struct test) , 10) ; 1 mg@& : rnallcw;, callw, realloc, brk %&A## : #include main I) i printf ("Page Size = %d\nn. getpagesize 11 1 ; &l% : calloc, free, reaIloc, brk : #include &xE&& : void *malloc I size-t size 1: @#ki3Z@l : malloc (1 ffl~BEBhfiqls7, #kll\~M659!1 size &Z0 EI @ : 2iEiE@i-zhRrl$B-Ei#, %%RlJ&H NULL, Lma void *p = maLLoc (1024l : I* fizR 1K @h.k */ : munrnap, open & P[ void *mmap (void *statt, sizet length, int prot, int flags, int fd, off-t offset ); M AP-FIXED ~IJ%BR start &Bm&MZ&%aBZ&% w, X!IBS@%, +HMBfi@Eo a%$3!Eb @W&B&ia MAP-SHARED wadlZ#~%A~#&~$~~*!?&, Tfm3t ~+-#fi@N%k!+rn8@%30 MAP-PWVATE %@HE@N%A#fi%?%-+@#%%mB 81. 4%AH "ShN~tl'' (copy-on-write). Xd ~K@*rnffrnI@&@~%%BR*~*f~h 80 MAP-ANONYMOUS &ZLE% dkH$B-@@& fdt T@B.X#v fiH&HEx%a&#aE%s@ MAP-DENYWRITE ,q$iTN@NEX%m%ABd$, J%h!3XI*PFB# %AMB#%&&E%Q MAP-LOCKED #$@$jE@@z@+ i%$?i~i%@@~~~@~#& (swap ), &flH mrnrtp (1 B$ah%B@$ MAP-SHARED 3 AP-PRIVATE, Z$% fd 31 open ( ) ZB@ZZT@Uii;l, ~~K~@!$$E~I~B~Z~+~ $BSr offset 2 ~~Y@H@I%o%B~ ZR~!~BYJ 0, ~t%M~t+~BdiZ%MiNEv offset 43 m#B*fJ\rnE&!go a ffi : ~&~~~~lI&~~~E~~~&&~~I BRlJ&IEI MAP-FAILED (-1 ), %%EBGT errno q0 EACCES @Ib$lEg$?, %s MAP-PRIVATE t6X-F gfT4&$$?qi?k, @ M MAPSHARED J!I]%?% PROT_WRITE H&%ftf$gfik%ho EINVAL &fk start, length % offset a-$.$f&o EAGAIN *#@%E- GkSfiAg 6J@@tfiB* /* $11M mrnap 0 5kBialDI /etc/passwd fi#d3$ */ #include #include #include #include #include main () i int fd; void *start; struct stat sb; fd = open ("/etc/passwd", 0-RDONLY} ; If fSR /etc/passwd *I fstat (fd, ~sbl ; /* B%*#kd\ */ start = map (NULL, sb.st-size, PROT-READ, MAP-PRIVATE, fd, 0) ; if (start == MAP-FAILED) I* $J%EBR&#BQ */ return ; printf ("%s*, start) ; mumap [start, sb.st-size1 ; /* *! close (fd) : /"MfcW *, 1 root: x: 0: 0: root, /root: /bin/bash bin: x: 1. 1, bin, ibin. daemon- x. 2- 2- daemon. Isbin, adrn, x: 3- 4, adm: ivar/adml lp: x. 4. 7. Ip: /varlspool/Xpd, sync, x: 5: 0: sync, /shin; /bin/sync shutdown, x: 6: 0: shutdown: /shin: /sbin/shutdawn halt, x, 7; 0; halt /shin. isbinlhalt mail: x, 8. 12: mail: /varlspool/mail, news- x 9 13. news /var/spool/news. uucp: x: 10: 14: uucp- Ivarlspoolluucp, operator: X. 11: 0: operator: lrcot, games. x: 12. 100: games, iusrlgames, gopher, x: 13: 30, gopher, /usrlliblgopher-data: ftp: x: 14, 50: FTP User, Ihome/ftp: nobody, x: 99. 99, Nobody: I: xfs, x, 100, 101: X Font Server: /etc/Xll/fs: ;bin/false gdm. x. 42: 42: : /homelgdm, lbinlbash kids, x: 500! 500! , /hornelkids, /bin/bash #I%iaB : mmap s% 2 # : #include anistd. h> #include &x : int munmap I void %tart, size_t length 1: @i#kam : munmap 0 ffl%%fif%k start fi#~hfl@8th6&#i&ktI 8% length Rd E#Q?i!l m h@A4\0 33%%%S$~Jffl exec m%g%*h4YB%@Ru+ @%&fi& EmHR* @%mHEm*!+%$fl#4;2@R@Ho @ : ~II~~~~~&zJJ!~~~~ 0, GRlJ8lEI -1, %$2RBIG3' enno 9" ##RtB ; EINVAL P& start & length $e#. iES+ mmap (L #% # : mallw , callw , free, brk : void *realloc ( void *ptr, size-t size): g#i#W: %& ptr hfEl;;l%%EB malloc 0, calk 0 3 realloc 0 %~B~%fi@ Eft, m$% size X%!i~~h@A4~l #iiTilkL%h&A%~Jj0 i$@B size #t@RRZ2rfl@j1k9 h@ti1%#+2&?5, -R&HHtEH%@*hG E%&hik;4B% size @@RBZE9fdA9 Ffl realloc j 7F---Z2&@G%& t9&7 Ei%BtJA83i;1;QHr!%, @$ri$~t:8(1~9tliSFfl*iQ%IEv%~9tp~ % ft% NULL, JkflNmq? rnalloc (size> ;gBa size &k 0, kflN#€l 3T free (ptr); #include main (1 char *per1 = malloc {lo) ; char *ptr2; memset IptrP. 'AL. 10) ; * (ptrl+lO) = '\0' : printf ("before realloc ! ptr = %x [%s]\n", ptrl, ptrl) ; ptr2 = realloc {ptrl, 40960) : printf ("after realloc : ptr = %x [%s] \nu, ptr2, ptr2) ; before realloc : ptr - 8049840 [-I after realloc : ptr - 8049840 I-I #zaa : mallw , calloc, free, realloc, brk s&*# : #includecstdlib.h> &x@& : void *sbrk ( ptrdiff-t increment): B&i#m : sbrk 1 ~~#fiLI~~~ffl~~#f~I'~, &f hf.\l%# incre meat a M fig : %@BflN&QN(&B-BW, $Brn%rn~G2~~oG&flH%RR~l%B -19 % errno @% ENOMEMo #Isha : time, ctime, gmtime, localtime : char "time (const struct trn *tirneptr): &#BW : asctime ( > #%& timeptr fiE@!~ tm %fi+139E,B%#&&%BRfi@H mwl'aHm%%%?&P %G%%RU?R*%%&Do lka#kEf?EkmK$$ Ba5~Hld* w?*&S%: "Wed Jun 30 21 : 49: 08 1993h" (B : %R~H@K&WTdElRa&, Itt;?fl*fl%2%@GO .~EIB% ctime (1 ~M&a?~$Am~#E~Hm%@n M&i.QEI: ~D-?R*%?SHrn3%rnMMBMe #include main () I time-t timep; time [&t imep] ; printf ("%sH, asctime (gmtime (&timep) ) } ; 1 Sat Oct 28 02: 10- 06 2000 msm& : time : cl~k-t clock I void ) iih&i!&!!kf : clock 0 H%@@%@%&M CPU &iSfr$tJWTsJd 5 la E : &D8#2%&8i] CPU mAtqWl',70 ms@a : time, asctime, gmtime, localtime : #include $& : char *ctime (const time-t *timep); : @J#i#Yl : ctime %%% timep time-t %#+$9E.B%2%j$tS%~R%1283 Il!JWdHJ@%%Z%* %G@%%U?R*%%BH* k&!&E@&WE% %L&%aNblii1, ?fi?@#&%%: "Wed Jun 30 21: 49: 08 IW3W"' j!$%MH M%Nt3lTa7 El%@&, Jk?R*~%&.i$i@%o & H # : ~H--~~@~~H~3~~~Ps7R~o ('T-Iq #include main (1 I time-t timep; time (& t imep) ; printf I*%sl'. ctime (&timep) } ; 1 Sat Oct 28 10. 12, 05 2000 #%a& : time, ctime, grntirne, localtime ex &# : double difftime ( time-t timel , time-t time0); ~&@,m : difftime H%id-$$$& timel time0 Wi!-k%@N1'4%9F, %!$!u double !i!%@ffi%Bo fi~&!!k~Ftl's7!@~ 1970 % 1 8 1 d%lFf%*%@k$$ &@I UTC ~l'do ig a #$ : %a%Rm~s~~E~J&~ : time, ctime, gettimeofday : in1 ftime i struct timeb "p); stmct timeb { time-t time; unsigned short millitm; short timezone; short dstflag; 1; time %M2%1970% 14 1 H3~&fi&o timezone k R %ll$Em Greenwich mg@JB$l's7, ${z% fi-Vo #include main I) { struct timeb tp; ftime (&tpj ; printf (mtime: %d\n\ ttg.tirne) t print€ ( "rnillitm: %d\nm , tg.millitrn) ; print f (" timezone, %dinu, tg. timezone) ; printf (*dstf lag, %d\nM , tp.8stflag) ; 3 time: 974857398 millitm, 215 timezone: -540 dstflag, 0 ................... I ##%ma ................. i : time, ctime, ftime, settimeofday ................... i *&*# . . .... i: #include SxajlB : int gettimeofday I smct timeval "tv, struct timezone *tz); & : gettimeofday 0 %$!~?#'83#l'dh tv 818CJ%@@n. %MFfE&fi.@W &$fl&l tz %@ft!J%&% timevd %#ax%; long tv-sec; I* #!!*I long tv-usec; I* @if"!*/ 1; timemne 2 X 3j : struct timemne ( int tz-minuteswest; I* 8l Greenwich El;f 5 !b$j@*/ int tz-dsttime: P ET&Pi@ll;lTa7@#& */ 1; k % fi 't !$&a % & /usr/includP/sydtime. h tz-dsttirne ffr % m# & @ 7;: DST-NONE I* $4$R4 *I DST-US A /* %El $1 DST-AUST /* @#l */ DST-WET I* fi @ ( Western European *I DST-MET I* *% (Middle European) *I DST-EET P b €% (Eastern European ) *I DST-CAN I* ;fiRWC *I DST-GB I* %$m@ */ DST-RUM I* ~~~~ *I DST-TUR I* k$# *f DSTAUSTALT I* #!M 1936 qufi 1 *I a I B ~J~JBEI o, ~RBH -1, awwar em,,. @BRIE$ EFAULT Wf t~ 3 tz WiWHplJ@Qle7B$8RRM0 #include main 0 I char *~day[]={~Sun*, 'Mon*. *%em. "WedM, "Thuu, *Frin, "Satn); time-t timep; struct tm *p; time I&timep) r p = gmtime (~tirnep) ; printf ("%%d/%d/%d \ {1900+p-rtm~ear) , (l+p->twon) . p->trnsday} ; printf ("%s %d, %d: %d\nm , wday [p->t~wday], p->tm-hour, p->tuin, g->tm-see) ; 1 2000!10/28 Sat 8: 15: 38 : time, asctime, ctime, gmtime gg a & : struct tm *localtime ( const time-t *timep ); & : locauime ( 1 %$B timep fitP,& time-t %#WJE.RH~&BP~$W~ ~~M&NI's7HM%53%l %G%%SBh%N tm %Elo %& tm Kt%XiEi $.% gmtime 0. &l%%HBBJis7BlfBZ43%@$3~#E, #include etime.h> main () I char *wday[] ={*Sunr. *MonW, *Tue*. #Wed". Thun, ,"Fin. "at* 1; time-t timep; struct tm *p; time (& t imep 1 ; p - localtime {&timep) ; /* I$#%NBfTe7 */ printf ("%d/%d/%d ", II9OOtp->tm_yearl , (ltg->tm-mon) , p->tm-mday) ; printf ("3s %d, %d, %d\nH , wday [p-rt~wday 1 , p->tmhour. p->t-in, p->trn-stcI : 2000/10/28 Sat 11: 12: 22 : mg@# : time, asctime, gmtime, localtime i $&*# I: #include , . . . . , . . . . . : dx a& : time-t mktime (stmet tm *timeptr): - . $#alJ': . .. mktime ( ) H%.I$$& timeptr ff-r$kK~ tm U#@@t$kW&M$Z 1970 $1 B 1 El 0 w 0 !d o@@@$+Bfi UTC wlqR%Bm*@Q I* 4 time I) Rj+N[fj (@&I , #1]H localtime I) $$a& struct tm @$lJM mktime [) 8 struct tm %f!%JjkJEk%l@& * / #include main (1 I time-t timep; struct tm *p; time (&timepl ; printf ("time 0 : &d\nn, timep) ; p = localtime (&timep) ; timep = mktime (p} ; printf ("time 0 -> localtime 4) -r mktime 0 : %dinu, timep) ; 1 time {) : 974943297 time 0 -> localtime 0 -> mktime () : 974943297 settimeofday (af Ijil&wri) "< ..,.-,. #% a& ~: time, crime, €time, gettimeofday @#aw : int settimeofday l const struct timeval *tv, const struct timezone *tz 1 ; E#J~MW : settimeofday () ~fii%&lFll'@J~&EB' tv %#b!I@~fi,@, %iI!l@E(fjE ,RRIh!i!& k. %$E&J%fi. i%lBIS(li#Yi98% gettimeofday ( )* @%, AB root ~I&!~k@H~~%B&srl~~~ #$#$Re : EPERM #qkm root @EflH settimeofday 0, &!@6'&0 &f &#k : time, asctime, grntime, localtime : size-{ strftirne (char 3, size-t max, const char "format + const struct tm *tm) ; fi#&g : strftime (1 $%%% tm IBa;fI'q%&, KK@& format FT$EBH???*# &i&B%, ~BJEWJ?R*F~@%!Z~~B~SB s Pi#E@9?7+*@33$7 3 ?R@WBAEB%S@ max Wi@%llo BB format b9&&@-&&&R?E@@3%.51;rt $CIBU I%' ?RW@ $549 TrnE!#&&B+: #include ctime.h> main (1 C char *format[] = { "$1: %M: %S %p %m/%d %a ", "%x %x %Y ", NULL 1; char buf E301; int i; time-t clock; Struct tm *tm; time (&clock) ; tm = gmtirne (&clock) ; for ( i = 0; format[i] != NULL: i++) i strftime (buf, sizeof (buf) , format[il, tm) : printf ("%s => %sin" format [il . buf I ; 1 1 %I1 8M: %S %p %m/%d %a => 02: 13: 15 AM 10/28 Sat %x %X %Y -> 10/28/00 02: 13, 15 2000 time (RAHaWMifliij) : . ... . ': ctime, ftime, gettimeofday : &x$a :: time-t time (time-t *I): ~#i#w: EfiB2i!ZHM/4\Z 1970$1 E! 1 Elk9 UTC EI$I'~~~~o~F~o+o$~B&BJ ~&fit$~rn@BO a%! t #4b2aHrn%, lkia@&&%%D@@9U t @ *fi@rn&G* : @ # it :hh!!BDb@+ XARIJPIEI ((time-t 1 -1 ) @. Bijl@HB;f ermo fP. main (I { int seconds = time ( {time-t *I NULL) ; print f ( " %dinn, seconds ) ; 1 mza# : ctime, ftirne, gettimeofday : voidtzset (void) ; extern char *tzname[2]; I.' El ~@#-iitM : tzset (1 BJ%%4;M2f TZ tB%+H?!?f tzname, &,&~M%l$;%g@ @I%%&rnBjEo lMd%@mGa2haaMk~&* %%%%E Tz * %E9 &E%S tznarne $$#&R/etc/loca~time ~kh~~ili~&~EFflZd $1158 f$?%gg TZ mfi% NULL* %@%$&%A, Nl/#!fl UTC B$Eo & : bcrnp, strcasecmp, strcmp, strcoll, strncmp, strncasecmp ataft* : #include x@& : int bcmp (const void *sl , const void *s2, int n); B#i#m : bcmp 0 M%M;@sl @ s2 REt3P31SFi41 n .f?-%, %Z%k J[ % 25, F'J%El 00 : 8S;Pt sl s2 fits"BcJi+?Fi%~b%%h+EIFlflrIBD @t. G11H85 EL r#mi#a ... : BalaM memcmp o it^ ii$@% memcmp 0 &% @B : memccpy , memcpy , memmove, strcpy , strncpy : #include fi @ : void bcapy ( const void *src, void *dest, int n ) ; g#i$&l : bcopy 0 $ memcpy 0 --#ib8BHdt%R src &PWht3Ah%t n /i'q T94 dest BiEWhkt, $i$. D@ src 3 dest bl~%G&@EHEHBZO aElB:% Rii#&j : ZiMCH memcpy (1 Rit. main 0 i char dest [30] ="stwing (a] q; char src[30]="string\0stringm; int i; bcopy Isrc, dest, 30) ;/* src $i€t$$E$~ **I printf {*bcopy () *) ; for (i = O;i < 30; i++) printf ("%c *, destri]) ; memcpy (des t , src, 30 : I * des t * / printf ("\nmemcpy (1 : "1 ; for (i = 0: i< 30; i++) printf ("%c ", destfi]) ; 1 ~C~PY (1 string string memcpy (1 I string string : : mernset, swab &#j#j# :: void bzero (void *s, int n): Pliltm.: bzero O %@BB s fitlW~&&E$P n 3 h@$?%%Qo &ZIT memset (void *s), 0 I size-t n): : Rt#i#SI : 1iXEH memset ( 1 alt: &x@# : intffs Cint i); aBHlJij t ffs 0 2@i&E3&l2, FflFif~B& i ?Tf9@--E9 #&R$%flhE% (11 rnGE&@O main (1 C ink ill = {O, 1, 2, 4. 8, 16,32. 64);: int j; for (j = 0: j i 8; jtt) printf (*%d : %d \nM, i[jl, ffs (iIj1) 1 ; 1 $A*# : #~nclude : char 'index const char *s, int c 1; GaaM : index 0 M%RkbB& s F~@~%-'Fkli@fK@@ c Wkr %G#3%,? q$&@ft!ffbhhifd% ?$$$g%??$ (NULL) &#!%?7@8-3%0 main I) { char *s = w0123456789012345678901234567890n; char *p; p= index Is, 15') ; printf (*%s\nm, p) ; 1 @a : void *memccpy C void *dest , const void *src, int c , si~t n ; &#kaa]CJ : memccpy 0 H%%fl src fi83&fi@fi@% n 'i'-?fr!BJ dest fi%mh!~hk 1. 5 memcpy C 1 ;I;FI&E, memccpy 0 2BB$JlJBif&&@B c kB %f,, %~~~~~ dat $EL% c mTd+?3fhhk0 % M : i$i@@fi dest $@% c mT-'p?-?##v &@@ 0 src fig& h@rn nw+;38m% c m?Po #include main (1 { char a[]="string[ a 1"; char b[]=*string { b ) *; memccpy (a, b, 'b', sizeof (b) } ; print€ Iumemccpy (1 : %sinn, a] ; 1 rnemccpy I1 : string ( b j : index, rindex, strchr, strpbrk, shrchr, s~p. strspn, strstr %**# : #include : void *memchr (eonst void *s, intc, size-t n): main () I char *s = u0123456789012345678901234S67890*; char *p: p = memchr (s, '5', 10) ; printf ("%s\nn, p) ; 1 sg aa !: int memcmp ( const void '$1, const void *s2, size-t n ); P##a:: ~IIHIMII~ 0 BIJRkR si fiBB(Ih.fiEWiiS n +FRO 9WBhdl @J t:kBk!2 ASCII w%kb9MB%&E, Irt;r%i@$%?R&&a memcmp ( 1 7$R% sl %-+FR@@& sz %-+?Riii, %%@% 0 !QlJBB%k @T+?F$t %SBT% 0 fl%#ffSlBZIZI~ #!!&I, ?$I* "Ac" W nba'' k @Fq+i&HqB 'A' (65) @ %' (98) EJZB (-3310 #include ?R$ b, 1 */ memcmp (a , c) ; -1 /* FR* a < Tfi* b, ZPl -1 */ rnemcmp (a , dl : 0 /* $f$Ef! a = fFJ@ b. 0 */ m[IX$# : bcopy, memccpy, memcpy, memmove, strcpy, stmcpy : #include s%$# : void *memcpy ( void *deft? const void *src, size-t n ) ; I P#ZSrl : memcpy 0 H%AW src FrBBtlABfiBO n +8ii%J dest RGAakt 1. 3 strcpy 0 $Miletlk, memcpy 0 $%EBtli%i n +Pi;? $2H i?Hl]+R*%% lot fi%%o a B # : &flfB6l dest m%H0 W : src dest R%b9hffE&t&flS&. #include main (1 i char a[30]=nstring (a) "; char b[301=nstring\0string"; int i; strcpy la, b) ; printf {"strcpy (1 , ") ; for (i = O;i < 30, it+) print f (*%c 'I, a [il ) ; memcpy {a. b, 30) ; printf ("\nmemcpy (1 ; "1 ; for (i = 0; ic 30, i++) printf ("%c 'I, a[il ) ; 1 strcpy [) : string a ) memcpy () : string string #I% iiia : strfry %4;2# : #include ex a a : void *memfrob ( void *s , size-t n ) ; $Lag : mernfrob (1 H%%&& s jjf@#jfi~~I'iJ~ n +9R$42 XOR mmi#ljl8 : kiifi&% Linux #include main {) i char aC1=wThis-is-memfrob"; printf ("~efore first memfrob () : %s \nM, a) ; memfrob Ia, strlen (a) ) ; printf ["After first mernfrob 0 : %s in". a) ; memfrob {a, strlen (a) ) ; printf ("~fter second memfrob () : 8s in", a) : 1 Before first memfrob 0 I This-isaemfrob After first memfrob (1 : -3CYuCYuGOGLXEH After second memfrob (1 : his-is-memfrob : bcopy , memccpy , merncpy , strcpy, stmcpy &xa& : void *rnemmove hid *dest, const void *src, size-t n 1; 1 : memmove O 5 memcpy O -#-BPtH*tfR srr: WiEB(1PtlPABft n +%%PI] dest W~%IYI~&~L~~ +FJI$IE+ 3 src @ dest fiEHh6E% B&sil, mernmove (> fi%?ilUiE%M@HL T3fifiB%k%k#M memcpy (). @EXo g Ej #J : gafi;l;;l dest mwt-0 : Rai#l : tEfF src I dest BRB(JhGE~4USB. SB% memcpy ( ), MSagepl : bzem, swab a& : void *memset ( void *s, int c, size-t n ); @#aIPJ : mernset 0 &#jB!$k s fiEHfi&E%3 n /I'?7jU%B c %A, RE &El%@ s &%$to &%3E@W3 E%9%%-B&#$AMk9 mem=t 0 42H33Eo a M #: ZHBh s Mfk%to Mmi#l : && c lbaA% int, {H4@E unsigned char, ljri 0 $4 255 main (1 i char sC301; memset (s. 'A', sizeof (s) ) ; srso~ *\om; /* q~ag.mk1 0\0*.gs+~ */ printf IM%s\n@, s) ; 1 #g@B : index, memchr, strchr, strrchr &x @ & : char *rindex I const char *sl int c ): #include main () char *s = "0123456789012345678901234567890"; char *p: p = rindex Is, '5'1 ; printf ("%s\nM. p) ; 3 BJ6 a : bcmp, memcmp, strcmp, strcoll, strncmp : int strcasecmp i const char *sl , const char *s2 ); iMW!Jl.: swasecmp i H*bL%B& sl 3 s2 ?R@, tt%We B3B@Af.h%199 ZRP 2Z Bl #i : %@&sl fls2~?~@~~!l~~2ZHOc sl KBgA? s2 KB81]&HAT 0 mE0 sl EES4\T s2 #iL!iEKU&HfJ\T 0 ma0 #include main (1 i char *a = =aBcDeFh; char *b = "AbCdEf"; if (!strcasecmp (a, b) printf ("%s = %s\nr, a; b).; 1 aB : bcopy , memccpy , memcpy , strcpy , strncpy : char *strcat (char *dest, const char *src); : swat O 2gSgyl SIC 9R@RW41!%# dest Wi%H7R4RO 3%-f 8 B dest ~*~~m~l's7stE%~B~~m~R~O % a': glBL dest l#SB*&%aRto main (} C char a[30]-"string (1) '; char Is[ 1 =''string (2) "; printf ("before strcat () : %sfnu. a) ; printf ("after strcat 0 : %sinn. strcat la, b) 1 ; 1 before strcat (1 : string (I} after strcat 0 : string (1) string (2) : index, memchr, rindex, strpbrk, strsep, strspn, strstr, strtok main I) I char *s = "0123456789012345678901234567890'; char *p; a# : int strcmp (const char *sl, const char *s2 ): a#am : strcmp (1 M%tt;@$@ sl N s2 ?@$o ?-R$k/l\sr3tL$i!EU ASCII 6YE+LIC)@R*&t. &@R%%PRmB. swmp 0 t%B sl W-4' ?RB@k% $2 %-+FRiL ZZE% 0 N~l@%%tLBT+Fflp %2B $% 0 fl~~#?j~@~~~ @I@, ?%f$ "Ac" 8 "ban k%!@&~~?-?? 'A' (65) $U 'b' (98) Mgffi (-3310 #include main I) I char *a = "aBcDeFm; char *b = *AbCdEfn; ..,... .... char *c = "aacdcfm; char *d = %BcDeFn; printf Instrcmp [a , b) : %din". strcmp (a. b] ) ; printf tmstrcmp (a . c) : %d\nL, strcmp (a, c) ] : printf ("strcmp (a . dJ ! %d\nu, strcmp (a, d) ) ; strcmp (a , b) : 32 /* 3R$ a r qR$I b. 'a' 3P 'A' &lXE 32 */ strcmp (a , c) : -31 /*FR* a <$#& c, Bn '13' $1 'a' &%@ -31 *, strcmp (a , dl 0 /* ?R$ a TR$ d, 0 */ @ 2 @ : strcmp, bcmp, memcmp, strcasecmp, stmcasecmp ex @I.& : int strcoll i const char *sl + const char *s2 1; Il#&i#a : % LC-COLLATE % ''POSIY 3 "CM, pj 11trrcolI ( ) -4 strcmp ( ) ft BIZ +tflHe [3Ed # % a # : bcopy rnemccpy + memcpy , mernmove ex a & : char *strcpy (char *dest, const char *src): a&Bta : strcpy (1 2%+& src 8Ff$Efl341P dest Wi%K~&kt. B @ : &El@& dest ftfl%%F@@&j&iko Mrba#y : &%$% dest &Em F$&q/q$@A+ W6k2%l;k%#i&h (Buffer vetfl flow) IYJ+EGTE)S~, k%5WFEFfiE%3JB39 6883 strncpy 0 S(F main () I char a[30l="string (1) "; char b[]="string (2) "; printf ('before strcpy () : %s\n3. a) ; printf (*after strcpy () : %s\nm, strcpy la, b) I ; 1 before strcpy 0 string (1) after strcpy 0 : string (2) &x .: size-t strcspn ( const char %, const char *reject >; $&#g : strcspn ( ) M%t s SW+@ffXitBi%@tF~S7+, BSE9RBZ4768 SB reject &EN?@*+o Ri$&ifi+ Z strcspn 0 %DIFJB@% n, W~litS?%$ s 7f%S%%i n $ZRB+$?R$l reject h&SR4 main I) { char *str = "Linux was first developed for 386/486-based PCs."; printf ("%d\nM, strcspn {s~r, ) ; printf (*%d\nm, strcspn {str. '/-") ) ; printf ('%d\nU, strcspn {str, '1234567890*) ) ; 1 : calloc, rnalloc, reallocj free : strdup ! 1 malloc ( fldZ5BB s ?R++UFJ@J~T~A/J\, %G% @!& s TR*&F'~I?~B$J$JJ%~I%HM~ EGR~H!I~~~!~~EI~ %WkEG TiJU$JM free 0 8 M B : &H--%E@IBHj ~EW~ElB$1]G~%~R$~k4.0 Z&H NULL ?!? Z~tJ5En main ( ) I char a[] ="strdupW; char *b: b = strdup la) : printf (*bC1 = \"%s\'\n", b} ; 1 &Kiiiiii & : char *strfry (char *suing 1; $##Q PI; strfry 0 C$]H rand 0 ~l$tS6%81+B string %?f$~~f@p $8 B&@El;ilbgll[ string ?RbLft3Ho s @ #&: %aBm&s~G&~R~%RQ Bfttal: L3 strfry (1 &$$+a string 8RSA% 89h+B string NF@*B m&%%nq%ANh*&Mo main (1 i char a[]="strfryn; int i; for (i=O;i main I) { char *str = "12345678"; printf ("str length = %din", strlen Istr) ) ; \ str length = 8 @j & : int strncasecmp ( const char *sl + const char *s2, size-t n ); #include main I) { char *a = "aBcDeFm; char *b = "AbCdEfm; if ( ! strcasecmp (a, b) ) printf {"Bs = %s\nM, a, b) ; 1 I , , . , ,-. , , - . , , . , , - , , , , , , . . . strncat (j#$#Q+@$T gx : char *strncat (char *dest, const char *src, size-t n 1; @j&aW : strcat 0 2#f@!!$1 src BR*$R n +TR3JJB% dest fiamFR%E. %--*+B& dest ~~h!@~~l'~%~~~~!%t!!~fl#o 5 H : &I%%% dest FJ?~@&!&~~~ main {) I char al30]='string (1) "; char b[l="string 12) "; .I . printf ("before strcat () : %s\nm, a) ; printf ("after strcat I) : %s\n\ strncat {a, b, 6) ) ; 1 before strcat (1 ; string (1) after strcat {) : string (1) string ,t int strncmp (const char *sl , const char *s2, si~t n >; jg a & : bcopy , memccpy , memcpy , mmmove @& : char 'stmcpy (char *deest, const char *src, size-t n ); :&Baf&j : strncpy 0 388% ssr ?P.frfOBf;W@ n +i-YRZ%%k dest WiBQ9Mkt. ' jhi El : dest h!~B?!f%&!&&u~ main I) I char a[301="string (I) '; char b[l="string (21 m; printf (''before strcpy I) : %s\nm, a) ; printf ("after strcpy (1 : %sinn, strcpy (a, B) j ; 1 before strcnpy 0 ; string (1) after strcnpy () : string (I) B*$&, : index, memchr, rindex, strpbrk, strsep, strspn, ma, smdc 846 &# : #include gx : char *supbrk ( const char *st const char *accept ); @&i#B : snpbrk 0 H$@$b@ s 8RS+@ffbfRAEBa accept ?-8$$B(1 i%Bf.Ro #inzlude main (} i ckax *s G "0123456789012345678901234567890w; char *p; P = strpbrk (s, "a1 839") ; /* 1 ea%& s ?R$l+~@] */ print£ (*%s/nn, p) ; p = strpbrk Is, "4398") ; 1" $$S&a s ?eE$*j&W */ printf ( "s\nu, p) ; : index, memhr, rindex, sopbrkf strsep, strspo, smtr, stnok exma : char *strrchr (const char's, int c); ITBaIRW : ~chr 0 Bl%&&S% s ?A991%-+~1RW$t c t8.tlt9 %G#i *FRt~~EEJ&&5Ho El a : PnSBEgll%Bm+RgIJ%Hi4;%R%S~~ B!JJBIEI 0. #include . . . . . . . # : char *strstr ( const char "haystack. const char *needle ); !'@-&.& . .. .. :: strstr ( 'I QM8flS haystack q#+qfl$ needle, #%%-i%bls(l& hMD* main 0 { char % = *0123456789012345678901234567890"; char *p; p = strstr (s, "901") ; printf ("%s\n6, p) ; : index, memchr, rindex, strpbrk, strsep, strspn, strstr a : char *strtok (char *s, const char *delim ); i@Bl#w : strtok () ~%%?@@$%~@~-?~%@!P B& s #6l%#$!!l%Bfl$+ @%! delirn !@~??~%%l]%?$*~ 3 strtok (1 ?€@% s @l?~#~$%!~@ % delirn FI#~~~@IF!~~~&#$%?-RBS~~ \O %?fo $%--?kfl8ifb$, strtok 0 il3%%3%%!? s ?fl$* @~@l$l~R~~#~& s g.& NULLe @Rfl M~~hR4%B7;-*+#%11GH?7$$,tEWo ilj% @ : &~T-~~%4G~~R%E~t~ @%C%h!&~~~NYZEl NULLo #include dstring.h> main (1 { char s[l="ab-cd, ef;gh: i-jk1;mop;qrs-tu, vwx-y;zW; char *delim="-, '; char *p; printf In%$ ', strtok {s, delim) } ; while ( (p=strtok (NULL, delirn) } ] printf (w%s \ pp) ; prfntf (*\nhj ; 1 mgm# : labs, fabs a$&*# : #include cstdlib.b> &x@a : int abs (intj>: #include main I) { double angle; angle = acos (0.5) ; printf ("angle = %f\nn. angle) ; 1 angle = 1.047198 : KOS, atan, atan2, cos, sin, tan : #include tmath.h> sx @a :: double asin (doubie x 1: #include main I) double angle ; angle = asin (0.5) ; printf ('angle = %finm, angle) ; 1 angle = 0.523599 MJ6@j# : rtcos, asin, atan2, cos, sin, tan . .. . ,,. .., . : double atan (double x 1; #include main I] I double angle; angle = atan (1) ; printf (*angle = %f \nc, angle) ; 1 angle -1.570796 -: acos, asin, atan, cos, sin, tan : : double atan2 ( double y . double x 1; @gku/W: atan2 0 Bl%itPFHk J~/X B(IBEtllB. XG#gRIH. :a fl #J : -PY2 P PY2 2l6IBtl#$g~Z. R&@&m : f2R GCC %i$Hia;baA -Im, #include ~&~?T;~J~~&& x H&4B3kB1 %J$.l double jf?&&Ho jg ,B :: &N+fj~T-B# x. rn@~j~@gk@o &)Sjba%R : !MI GCC %%Wii5A~h -Im, #include main [} I double value[] = { 4.8. 1.12, -2.2, 0); int i; for (i = 0; value[i] != 0; i++) printf ("%f => %finn, value[il, ceil (value[i]l ) ; 1 : acos, asin, am, atan2, sin, tan sxa# : doublecos (double x); -: cos (> H%it-B%B x M&i;E@, %GH%R&Ho #j : &H -1 3 1 2FlrnitBG%O #include main ) double answer = cos (0.5 1 ; printf ("cos (0.51 = %f\nm, answer) : 1 cos (0.5) = 0.877583 : sinh, tanh a$!&@ : #include ex a : double cosh i double x ); a#am : cash () aJ%itS%% x @JX!&%&!&ffi~ %GWi$R&EIo BWXS %: (exp (XI tap (-x)I /2, s @j # : %ElB@ x &%$%&3%@* #include imath,h> main I) ( double answer = cosh (0.5) ; printf {"cash (0.5) = %f\nh, answer) ; 1 l#@jlf;, : ldiv . . .... : aaft91;' : #include a# : di~ div (int numer, int denom 1: : div ( ) fi&&#$$g& numer/dewm, %~%#ll%G~fifi~&& div-t %$ &I%@ div-t %%$x$lT: typedef struct i int quot; * */ int rem; /* A% */ 1 div-t, /"tg 67/4 HEZ*% **I #include : ~X@!J# : doobleenp (doublex); &&i#l:: exp 0 MAfitBU :l, %EM x EihAZ$. EP ex %. EEJBBJICIB. B 18 : 1H e Btl x &fti+%%%. mm#g : @Rj GCC 9$i+FJiBfi~A -lm, #include imath,hz main (1 , . ,. ... , I double answer; answer = exp (10) ; printf l"eA1O = %finu, answer) ; 1 \ mftam:; abs, labs . . . . . . . . . . . . . . . i f 9;*# j: #include u118th.b ..~. . # : double fabs (double x I: @&am ,: fabs 1 H%it$$$?,&!!d% x m%@@, #6%$%%mg #include main (1 { double answer; answer = fabs I-3,1415921 ; printf ("1-3.1415921 = %f\nH, answer) ; 1 exa# : double frexp (double x, int *exp): a&aB ; f~xp 0 BlJR%%PJI x #J3Pb@LtII$llfdiEBIWEBo btl%f %B8, B8dftQ4l88 exp %St%Ei~ %&Bffi%U 2 exp &AaP% x m.IB. . %El@% x Bcl@DSr%#, R#SfR!?%+ exp B#Bf%P&hh. main 0 I int exp; double f rack ion; fraction = frexp (1024, &exp) ; printf ('exp = %d\nn, exp) ; printf ("fraction = %f \nm, fraction) ; 1 exp = 11 fraction = 0+500000 /* 0.5' (2^11) = 1024 */ . . $ @ : double hypot ( double x, double y 1 ; iiij&#M : hypt 0 EflH sqrt (x*x + y*y) %G%itB%gfCED+ iiBRMX#gB fhEfi%#BE, B& x y %fi%i&E, %EitEEi,GFM {x, y} &!I 0 'P'H .. . a .. : &a (x*n + y*y ) t1(173#34. :: @W GCC Ui$l.I$%j~/h -lm, . . main 0 I double distance: distance = hypot ( 3. 4) ; printf (*distance of the point (3, 4) from the origin is %fin'. distance) : distance of the point (3, 4) from the origin is 5.000000 labs (i-~#~%#'kI&&X#fi%) #gas : abs, fabs %**# : #include cstd1ib.b &x : long int labs ( long int j 1 : @@.iQzR : labs () FR*itBP!& j H%Mp ;PRG%%%%Ho % IEI B : %El j 85%jM.@;t.IURo main (1 long int answer; answer = labs (-2000) ; printf (" 1-20001 = %d\n", answer) ; 1 a : double ldexp ( double x , int exp ) : @#!CRY : ldexp I) H#E%ff !& x %k 2 H exp &3ffi9 @ x * kxpo /* its 3* (2"21 = 12 */ #include aath+h> main 0 i int exp; double x. answer; answer = ldexp (3. 2) ; printf ("3*2" (2) = %fina, answer) I Mz&& : div sY;;gf$ : #include ~ & : ldiv-t ldiv I long int numer, long int denom 1; typedef struct i long int quot; /* B& */ long int rem. /* *& *I 1 ldiv-t; main (1 I ldiv-t answer; answer = ldiv 12653589, 79323) ; printf (*Quotient = %d, remainder = %d\nM . answer.guot, answer .rem) ; 1 Quotient = 33, remainder = 35930 %#&.a:: . , exp, loglo, pow Ex : double log { double x) : 4a#m : log 0 H*itBH e %E,M x N!!kIL %E%%%%DQ main (1 i double answer; answer = log (100) ; print£ I "log (100) = %f \nw, answer] ; 1 log (100) = 4.605170 : exp, log+ pow sjb*# : #include cmath,h> sx&B : double log10 (double K): : loglo 0 HjW$$lZI 10 %&ffi x M&@, %G%gEi!ZH. #j : %HB4k a El 10 %&mnjm@uo @Bfi@ : EDOM $42 x %fi@o RANGE %&x$J%B, %l%~BB%~X~ w&!#m : GCC %i$Elrfi$B~A -]ma main 0 i double answer; answer = log10 [I001 ; printf (mlog10 (100) = %finH, answer) ; 1 & Xfi : double modf ( double x, double *iptr 1 ; @&iRM : mdf < 1 M?k%$B x EtW%,6.~B+M&%B@~J~%o ~J4933+B@&H* %&%%R4IES%k ip %#&Ele ill3 #i : l2@@@ x ~~I~B35+, EB9p#RI1G'f iptr B%tFr4EW&Mo main (1 { double integral; double fractional; fractional = mdf (3.141592, &integral) ; printf ("integral = %f\nm, integral) ; printf ("1 ractional = Bf in@, fractionall ; 1 integral = 3,000000 fractional = 0.241532 #%a# : exp, log, log10 : #Include &a & : double pow ( double x, double y 1 ; s~wm : pow o m~it~u x %am ~km BP 'Py yB, tmsammb a m r : im x n y aljitnan. : EDOM @!& x %fi%Ii+$ y ~E@?!k0 m#ii#w : @H GCC %%rt;liSfi~A -lmo main 0 { double answer; answer = pow (2, 10) ; printf ("2^10 = %f\n*, answer) ; &ljb&& : acos, asin, atan, atan2, cos, tan %.3b;*# : #include a & : double sin ( double x 1 ; @#%!!4 : sin 0 H%iWB& x &E3Effip %G%@%&Dv ig El : &El -1 2 1 2bawR%%* WMNM : @aJ WC %~+W%PUA -Im, main () I double answer = sin (0.5) ; printf (*sin (0.5) = %f\n\ answer} ; sin (0.5) = 0,479426 #%&a : cosh, tanh a& : double sinh (double x ); @&ij&!Jl : sinh () H%itB%B x HXt~%Z$%iXy %G#%2&Hn BW2XZh: Iexp (x) -exp (-XI) 12, H B : &HZ!%& x kitXibh%E!~Z&~ MhaM : GCC %%lFl%BUA -lmo #include main 0 i double answer = sinh (0.5) : printf ("sinh 10+5) = %f\nW , answer) ; 1 sinh (0.5) - 0.521095 N%&@k : hypo4 #&A# : #include gx@& : double sqrt (doublex); &glPtaIPJ : sqrt 0 H35tBB!& x WFBRI %G#%%ZHd x &t%Q%iE&o B Dl B : B8Bt x #~Tft#!l~ #B.fe@ : EDOMf%x.%&@. mhl@W : .fgM GCC %%Bili~B~h-lrn~ /* i+g 200 &T3@li3 "1 #include aath.h> main 0 I double root; root = sqrt (200) ; printf ("answer is %f \nu. root) ; 1 answer is 14.142136 : atan, atan2, cos, sin : #include : doubletan (doublex): Pt@m : tan O M$it@$Mt x HiEtlJQ, PRBlftfUlBBO &: &rnB.&x ~iFM@o f#m#&m : GCC $i$fii%~~A -1m. main I) L double answer = tan (0.51 ; printf ("tan (0.5) = %f\nn, answer) ; 1 i.tpH&:*-. >. . . . A . . . . tan (0.5) = 0.546302 #xi@# i: cosh, sinh %&*# : #include fi a : double tanh (double x); P&%M : tanh ) NgitB@& x HXI~kkiE@IB, %E%$R&H. &%$X&%: sinh (x) /cosh (XI, IBI : &FIB& x Mfl~Q%iEtJiiZ~ M&@M : GCc %i$s;fifi;baA -Im, #include main 0 I double answer = tanh (0.51 ; printf ("tanh (0.5) = %£\nu, answer) ; 1 tanh (0.5) - 0.462117 'ER &x&a : char * cuserid (char *string); B#j&lgl : cuserid (1 ~fl-~Pr)li3%%~~J~I]$3& string fi%N?R*%B+, fU!% @& string %qt8& (NULL). cuserid ( ) $2 $%?lCeE---#$l?!J%R$& a* EG%%m&?fl*M%++5H, lk l%!lRSH9r~AfJ~m&xa s1dio.h tfi L-cuserid E@z (POSIX %%% 9)b @&? cuserid ( ) $3 Ei~$fl@#~$&flH getlogin ( 1 E$k&%IaJ cuserid ( ) W%@So #include main (1 t printf ("I am %$.in*. cuserid () 1 ; 1 I am root. /* 2$fl8] root %filAfi%$J */ MJbfiB : getgrent, setgrent : #Include #include & fi a : void endgrent void 1 : h#k@,M : endgrent c) B]%kbflEh getgrent 0 WitT;fFMIW2#. igH@:% MiJUiltB ! Ti i%&!$ getgrent ( 1 3 setgrent ( 1. mxa@ : getpwenty setpwent *&jr# ; #include #include ex@ja : void endpwent (void); ff!j#%M : endpwent ( 1 ffl%#WI& getpwent ( ) BinKBCJ%@*f$Q ~~~:~ W&%M : z %% % getpwent ( ) 8 setpwent ( ) . I- endutent (Xm utmp *#) I : getutent, setutent %%*# : #Include : void endutent ( void ): @&al : endutent 0 B]%#WB getutent 0 6UKm utmp fCi+o 3Lgmm: z MB#a!M : 2 ia"$% getutent ( ), %**# : #include #include #include ex a& : struct group "fgetgrent (FILE *strerun); @j&#y : fgetgrent C 1 %A@# stream EZsrJ*1F@$@--~7@#3, #JE k2 pup %El %%atr@&H. &B stream fitB~~*~~iZ% letclgroup &l(IAPhP3. group %&tSXSB% getgrent ( )o a # : goup %&%#3* ?$%%El NULL N~J%%EZBB~ ZER%gE&V #include #include #include main I) { struct group *data; FILE *stream; int i; stream = fopen ("letc/groupW, "rn) ; while I (data - fgetgrent (stream) ) != 0) { i = 0; printf {"%s: %s! %d: ", data->gr-name, data-rgr-passwd, data->gr-gid) ; while (data->gr~ernI i] I printf IM%s, ". data->gr~nern[i+e] ) ; printf ('\n') ; 1 fclose (stream) ; 1 root: x: 0: root, bin, x. 1: root, bin, daemon, daemon: x: 2, root, bin, daemon, sys, x: 3, root, bin, adm. adm: x: 4, root. adm, daemon. tty, x: 5: disk, x; 6: root, lp, X, 7 ; daemon, I@, mem: x: 8: krnern: x: 9: wheel, x: 10: root, mail, x: 12: mail, news: x: 13: news. UUCP: X: 14: UUCP. man: x: 15: games: x: 20: gopher: x: 30: dip, x, 40, ftp, X, 50: nobody: x: 99: gx a& : struct passwd 'fgetpwent (FILE *stream 1; fiaIRrW : fgetpwent ( &&%B stream #$EHZ!$BR-fiB##, RE W passwd i$!i @#J$&#%li8. $% stream BfJ*fk&8%jk@ letclpasswd #I@ l$l& $. passwd %#il~W%,f getpwent Oo H : I@ passwd %B$h@, b%&m NULL F!~]BZBZ&JI%, @l%#R8*. #include . . . .. ., . .. ._I. . . ', .L . ' #include . . main 0 I struct passwd *user; FILE "stream; stream = fopen {"/etc/passwdm, #rn) ; while [ (user = fgetpwent (stream] ) != 0) { I: printf (9s: %d: %d: %s: %s: %s\n9, . . .I ' user->pw-name, user->pw,uid, u$er->gw-gid. user->pw-gecos , user->pw-dir , iiser ->pw_shelll ; I 1 root: 0: 0: root, /root: ibinlbash bin! l! 1; bin: /bin: daemon, 2, 2: daemon, /shin: ah: 3: 4: adm: /var/adm: lp, 4: 7: lp, Ivar/spool/lpd; sync, 5, 0: sync, /shin, /bin/sync shutdom: 6: 0: shutdown; /sbin /sbin/shutdown halt, 7: 0: halt, /shin: lsbinlhalt mail: 8, 12: mail, /var/spool/rnail, news, 9 ; 13 ; news, /var/spool /news, uucp, 10: 14 : uucp; /var/spool/uucp: operator: 11, 0: operator: /root: games, 12: 100: games, /usr/games, gopher: 13: 30: gopher: /usr/lib/gopher-data: ftp, 14: 50: FTP User, /home/ftp: nobody: 99: 99: Nobody, /, xfs, 100; 101, X Font Server: /etc/xlI/fs: lbinifalse gdm, 42, 42, : /horne/gdm: /bin/bash kids, 500; 500, : /home/kids: /bin/bash : getgid, setgid, setregid %Y( fit : gid-t getegid ( void 1; @ai&s :: getegid! )A*IWWW8 a3%98@4t8(18iffPi1640 RBB(l/%RUW (effective group ID) ~%&ZiB%!#,~fl@Fl&F@o main { ) ( printf fcegid is %d\nu, getegid I) 1 ; egid is O /* %@I root 51-i381,fi?E@JEBElj */ : getuid, setreuid, setuid : #include #indude sx@& : uid-tgeteuid (void): BBt#M : geteuid ( 1 HJtE@@hfi €I @i&@RB~RPi.BJR ?%kNaJPiR%JR (effecrive user ID) HJtE&Z%$&fi&aR, ~~~~&ffi+ B@WU@R %$b&&~oji$$%l,fi*#b!J set ID~~%~~,%~~~~;5?~,~~@b!I euid @@I$?&%ft#%fttlrl uid. @I@ 61. &fi*f~lusrhidpasswd m& -r-s--x--a, #s PBP% setD (SUID) 8, fi%~ZBMP&WfpasswdFtJZ ;fSB@fflPiRR~jRN2#i2 passwd fi~~~ uid a, RP mt uid fi (O), main 1) ,, . .. , . { printf ("mid is %dinM, geteuid I) ) ; 1 euid is 0 /* 3EH root 9B#1fi%@l@$srf *i mj6 &a : getegid, setregid, setgid %&*# : #include #include ay&ypes.h> a# : gid-t getgid ( void 1: .................. @#BIB : ptuid 0 ffl$tR%GW~B~%R~~iR%-@O a H # : &EI&iRf;lIW main 0 E printf ("gid is %dinu, getgid 0 1 ; 1 sx $@ ': srrungroup *getgront (void): BRillW : getgent ( ) H dtRB*A (leagroup) 9 8JW-Qk€i%B. i28AM group E#Q&EIo %-&BlM~&lX%%-~~##t, 2G@8BS-&2&BDT -@%a7 B%li%kEfl#%ktgH NULLo struct group ( char Qr-name; I* 48% */ char *gr_passwd; /* imig *I gid-t gr-gid; /* fii5%1]# */ char ** gr-mem ; /* mi!?E#? */ 1; R&#W . . . . t getgrent ( ) &%-&4JaB;f2MRB*i+, @IW&BR96q@H endgrent () *#m%a2!+0 #include . . . .. . . . . . . : ' I 'main (1 . . , . . .. * ,' . struct group *data; int i; while ( (data - getgrent 0 1 != 0) ( i = 0; printf ("%s; $3, %d, ". data->gr,name, data-rgr_paa.awd. data->gr,gid} ; while (date->gr_mern[i J l printf (.%s, * , data-&r-mem~i++l~ ; grintf ("\nn] ; I . . endgrent I) : 1 r0oC: x: 0: root. bin, x: 1: root, bin, daemonr daemon, x: 2, root, bin, daemon sys, x: 3, root, bin, a&, ah- x- 4. root, adm, daemon, tty. x 5. disk: x: 6: root. lp: x: 7, daemon, Ip, mem: x: 8: kmern, x : 9 : wheel, x. 10: root, mail. x, 12, mail, news, x- 13, news, uucp, x, 14: uucp. man, x: 15: games, x 20, gopher: x, 30. dip, x- 40; ftp, x: 50: nobody, x- 99: 1.. getgrgid !MR.*#Wlk## &a # : smct group *getgrgid ( gid-t gid ) ; e~wrill ; getgrgid ( 1 HMSB gid 1aam~~~11~~--~2~*1+~ t~uwt~ ~lBtltlW group %#&El. group $#t%&!$ getggrent (). B H ffi : &D group !#kl&% @!%lin NULL J!ll%%E%%fBi, E&~#%%!%. /* I$!(# gid=3 Ma&#$ */ #include gr-name, data->gr_passvd, data->grgid) ; while {data->gr~em[il) printf ("8s. " , data->gr-mem[i++l) ; printf ("\n*) ; 1 sys, x 3, root. bin, adm fQ #& : struct group "getgrnam (const char *name 1; iSi@ikaPJ : getgmam 0 H%B-@%@# name ~~~~Z#v EF~JEFHER%am% #El group %&&El, group %NiA@% getgrent (), B H 9 : t~ group %fit#+ fiuziai~ NULL fillzrnzzm, 8% BSE+-. / * @4!$ adm I%$H&B */ #include #include main I1 I struct group *data; int i=O; data = getgrnam {*adma) ; pwintf "$st %s: %d, " , data->gr-name, data->gr_passwd, data->gr-gid) ; while (data->gr-mem[il) printf ('%s, " data->gr=etn[i++j 1 ; printf ("inM) ; adm. K: 4: root, ah, daemon #include tsydtypes.h> $& : int getgroups ( int size, gid-t list[] 1: $@IIJII ... . : getgroups () H~$WHiii~P%~~IH4~t:~30 SB size jb list[] Wi%d #IFJ gid-t %El 0 lmS8BB size Q%FV &lL@~&El~P6'7AH~Ba 8 . & . : &@&~.#4R, @%+igF4&H -1 0 w#$fi@ : EFAULT list %%&!lhk?$7$o ElNVAL %& size @~~!%%~f!81?~&o #include main 0 I gid-t list[5001; int x. i: x = getgroups (0, list] ; getgroups (x. list) ; for [i = 0; i < xi i++) printf Iqd : %d\nn, i. listti]) ; 1 ax&& : char*getlogin (void): #include main { prinkf ("1 art %s.\nn. getlogin (j j ; 1 I am root. /* Yl*R root Aj$&fiEPIE$Eid */ %& &# : #include #include sx&@ : intgetpw (uid-tuid, char*buf); e&$&W : getpw (.) &k /etclpasswd uid %5E@h!JHpfk9%#37 -l %$i@H buf ?8?@&&$0': %? giq, MpiR8ljR {uid) #3iJ?E1kj (qidl !t&: tEB3: shell : I. getpw 0 ~~@&II!J~+~BW~. iiSBfbfflRYBFIB&R+ko 2. 1$M shadow Bfi %%CENP%@ftB & /etc/passwd+ UbLjfM ptpw ( ) &GW%63%31 ''~"0 jg #g : ZH 0 S%~J~I, %-%g%*a%@ -10 caK4 #include #include main (1 char bufferC801; getpw (0. buffer) ; printf ("%s\n", buffer] ; 1 roat, x, 0: 0: root: /root, /bic/bash &Jftaa : getpw, fgetpwent, getpwnarn, getpwdd, setpwent. endpwent ax : struct passwd *getpwent ( void ): &&aM : getpwent ( ) 83~~~~~~~ (/etc/passwd) ---J~MP&#~, %mP m.fJrEU passwd gttldFl. I--iARHV4%0(A%-iZHP%#1 tG$ - J NULL, passwd %&@Xa-F: struct passwd C char * pw-name: /* ~PbK? */ char + pwjasswd; /* ajlpmq */ uid-t gw-uid , /* RPiO%ll$g */ gid-t pw-gid: /* &iR%l]fl */ char * pw-gecos: /* W=.f% */ char * pw-dir, /* AH3 */ char * pw-shell: /* fi@flfi shell g$$ */ ; F#tmil/&I : getpwent () &%-&flHld-&flR%a3Z!+q ~@B#fZWGPJf$aj endpwent ( ) 3f%8ikx%@ft.fB0 #$$&fia : ENOMEM b?F$E, %%Bi!f passwd $$&, /* 43M1?R%?zt#$ */ #include #include main 0 C struct passwd *user; while I (user = getpwent I) != O} { printf (*%s, %d, 8d: %s: %s: %sinH, user-rpw-name, user->pw-uid, user->pw-gid, user-rpw-gecos, user->pw-dir, user->pw-shell) ; 1 endpwent (1 ; root: 0, 0; root; /root: /bln/bash bin: 1: 1: bin, /bin: daemon: 2, 2: daemon, /shin: adm, 3: 4: ah, /var/adm: lp, 4: 7: lp: /var/spool/lpdI sync. 5: 0; sync, Isbin: /bin/sync shutdown, 6; 0: shutdown, isbin: /sbin/shutdokn halt, 7: 0: halt, /$bin: /sbin/halt mai 1, 8 ; 12 : mail : /var/spool /mail, news; 9 : 13, news, /var/ spool /news, uucp, 10: 14 : uucp. /var/spool/uucp~ operator, 11: 0: operator, /root! games, 12, 100 : games : /usr/games: gopher, 13 , 30. gopher : /usr/lib/gopher-data: ftp, 14, 50: FTP User: /horne/ftp; nobody, 99: 99: Nobody; /; xfs 100. 101: X Font Server; /etc/Xll/fs: /bin/false gdm, 42, 42: , /home/gdm: /bin/bash kids: 500: 500, : /home/kids, /bin/bash ,, : getpw , fgetpwent, getpwent, getpwuid sx : smct passwd *getpwnam (const char * name 1; :ae'j&,~ l: getpwnam () JRf B-B%@#@ name #BRBtlb9%.fftrp i&3$M%%fbH P &J&@ cl, passwd %#dB. passwd i%./$%$% getpwmt ( 1, , * rW# root %+fiE%~JllWNRR3 */ #include #include pw-uid) ; grintf I "home ; %sinn, user-rpw-dir) ; 1 name : root uid , 0 home . /root #€I# a & : getpw, fgetpwent, getpwent, getpwnam gx a & : struct passwd *getpwuid i uid-t uid ); iii!j~~~ : getpwuid () M5kB--B%@%k uid %$$cJHP%XI1P$, &ZJJM5%f%HP lKt%#N passwd %#%Ho pmswd %#%b% getpwent ( ). /* Re uid=6 HlYaT$5%. iR5;'JW$n@HSt */ #include main I) I struct gasswd *user; user = getpwuid (61 ; print f ( "name : %s\nu , user->pw~lame) ; printf ("uid : %d\nw. user-rpw-uid) ; printf ("h~me -: %s\nw, user-rpw-dir) ; 1 name : shutdown uid : 6 home - isbin ; geteuid, setreuid, setuid ss * # : #include anistd.h> #include : uid-t getuid i void) ; @&i#18 : getuid ( 1 R$Bf+!$%%7 I3 %%&!@MP iEX1JNO a a fi : fflPiE9Jfiqo [-3 main 0 I printf ("uid is %d\nR, getuid 0 1 ; 1 - ., ;@"-' -. ...... ,.- -- - uid is 0 I* root %!fi$h??EfidWjf;H *I ##a# : getutent , getutid, getutline, setutent , endutent , pututline, utmpname %x a# : struct utmp *getutent I void j: id!!&aM : getutent ( ) M*M utmp (/var/mn/utmp) $3 - %L EL! utmp @&&El. %-XiRdM@&IW%%-EHPSB. 264ilrlH-- &%%5@~-~%#% BB/E%f3ma%Et%m NULLv utmp %N5iXPAT;: struct utmp I short int ut-type; /* %R&% *I pid-t utjid; I* login ppi */ char ut-l ine IUT-LINESIZE] ; /* gsgs&, &@,T " ,'devlU *I char ut-id(41; /* Inittab ID. */ char ut-user [UT-NAMESIZE] : /+ - * / char ut-h~st IUT-HOSTSIZE] ; / "3+@jf%@3E&3A@& * / struct exit-status ut-exit ; / * 3233 DEAD-PROCESS @*@#J%% "/ long int ut-session; /* Session ID * I struct timeval ut-tv; / I */ int32-t ut-addr-v6C41; jh &e#mJm%%id */ char unused 129 I ; * % */ I; ut-type RTF!IAH%@: EMPTY lk%~&Z?k RUN-LVL iZ2%% run-level WBg. BOOT-TIME iZ3%%khIa;fl'sld NEW-TIME E%%%~I'e7896ii8tlNfqm OLKT IME ~$%&%%%w~dwHHrq+ INIT-PROCESS i?$-+& init ?jik8%@jii%Ev LOGIN-PROCESS 33 login USER-PROCESS 33-E%@+ DEAD-PROCESS iz$-%%@l%@. ACCOUNTING mu%i@M. exit-status %$kI&X: struct exit-status I short int eferminltion; * @ */ short int e-exit: /* %~~~~& */ 1: timeval W %&I$XilS.S% gettimeofday ( ). : MM'rJtl : getuent (1 %%-&flHW+flR utmp *f+, @8!&@%FGW@H endutent ( 1 %#I%% utmp fC8 %El : &El utmp %t;f$tB. B%%@ NULL ~1J2?7?;EZ@#f9 $i2ia%%f&. #include cutmp.h> main I) C struct utmp *u; while ( Iu = getutent I} ) ) I if (u->utfype == USER-PROCESS) printf ("$d %s %s %s\nn, u->ut-type, u-rut-user, u->ut,line. u->ut-h~st) : I endutent 0 ; 1 /* %%BE+ root Ipti9.ff-XqS3 /dev/pts/O, /dev/pts/l. /dev/pts/2 */ 7 root pts/O 7 rmt ptsll 7 root pts/2 i #Jsaa . .. . . : getutent, getutline ; %%*# : #include j&xa@ : struct utmp *getutid (struct utmp *ut): dQ&&!Bj : getutid ( ) a]*& 1 i% urmp Zi+&SSfiEB--f%GB~%& ut P&& 2 , #I 2 ut->ut-type 3 RUN-LVL . BOOT-TIME , NEW-TIME . 3 OLD-TIME # $2--Rl B tfiti-f ut->ut-type #El 8 : % ut->ut-type INIT-PROCESS 9 LOGIN-PROCESS USER-PROCESS 9 8 DEAD-PROCESS 3&+ 27 !lJB#@j ut->ut-id B74NiZ3 R$H€lfiW3k@%%&B U utrnp G&%B, utmp %#~i8$5 gemtent ( 1. #include ut-type, u->ut-user, u->ut-line, u->ut-host] ; 1 1 , . . . . . , , . . . : . .. : getutent , getutid , pututline . .. . ,. *&*# : #mclude .. ...,- nfi mt ; smct utmp *getutline ( struct utmp *ut ): $&am : getutliw () Jflj€Ma~# utn~p 3C4+FI@$l?tiZB-@GBB ~t-tyyp % USER-PROCESS % LOGIP-PROCESS , fi A. ut-line #I ut->uc-line mR. ~~~Il~BCJQJRIIER~~B H utmp U~BEI. utmp % &5i$Pf getutent )q a # : $&l utmp G&&E, ~u%&D NULL J!jJ3%EZBEv S!G%BE?k0 #include main () I struct utmp ut, *u; strcpy {ut .ut-line, "pts/lH 1 ; while { Iu = getutline (&ut) ) 1 { 7 root pts/l : setgrent, endgrent &g $# :: int initemups (const char *user, gid-t grap); .-. ,-.- .a,. . . allti#&: . .. initgroupst (1 H%M/Xff (/etc/group) Cei&lW-JF!$H!&f, 2?%Q&#Z Bcl&E +I?@& user #, @%St group ~H%jjllWh~AEl&##+- 1 .._____ ms.&& , ..........__ _,. .: updtmp, getutent *&*# : rVinclude ........ ! $a : void logwtmp (const char *line, const char *name, const char *host ); ......, .,, . : alltam : logwtmp ( ) QQ+B line, name fl host %B%stl%E%, i#i33WS #I%&@ $3Ba- utmp %@. %hi19H updwtmp ( %%lkBJitSAEl wtmp gff (Jvarfloghtrnp). #include sutmp,h> main () { logwtmp ("pts/2", "kids", "www.gnu,orgmj : 1 /* z%%Z?' root %?*81lg$ /dev/pts/O, /dev/pts/l. /dev/pts/2 */ 7 root pts/0 7 root pt;s/l 7 root pts/2 f&a : void pututline ( struct utmp *ut ); @#am ;: pututline ( ) W $t%&B ut BCJ utmp %#Z%9I ump biq $0 &fig% %M gttutid ) 3k&#$iE-lif[lb!I%hfiE9 hl%!fi!fX$lJ#i$f~%%flI]%&h 24 utmp X14E. utmp %#l+% getutent ( ). #include main [I C struct utmp ut; Ut.Ut,type = USER-PROCESS; ut-utjid = getpid () ; strcgy {ut . ut-user. "kids ) ; strcpy (ut .ut-line, *pts/ln ) ; strcpy (ut . ut-host, "www. gnu. orgH) ; pututline (&ut) ; 1 /* who -1 fls * / root pts/O Dec9 19: 20 kids pts/l Decl2 10: 31 (www.gnu.org) root pts/2 Decl2 13, 33 : setgid, setregid, setfsgid : int setegid (gid-t egid); . ,Pai#9] . i: setegid ( 1 H%f %Sf @l,fi @I ~jttEhQBB4HiP,PPJ@0 .$ Linux T, ~gid Cagid) #tl%'F- setregid (-1, egid), B B : &ehE%RMiEI 0, %%RYBH -I+ EG!t@GT moo : m#@& : #setuid, sctreuid, setfsuid : # : int seteuid ( uid-t euid ); &#@&I : seteuid () H$Z%~~~SJJ~~~H~~~~B~ABFBP~RRI~~~~ i9 Linux +, seteuid ( euid ) # %T setreuid ( - 1 , euid ) % 9' : A4TbIhRlllH 0, XLRI&B -1, %Sff@G;f ermo. WwOail/M : in",% setuid. 8 #z$B : setuid, setreuid, seteuid, setfsuid ex : int setfsgid C ui&t fsgid ): --< : serfsgid 0 H~~WiftPB3B@flX!+%%~%%%1fi0 --~'k&%T7 d I%%%k!l%i..%qW (fsgid) ~~~BtllRR164 (egid) &~[lR~. @%Bi@ IHPflmkgB. @& fsgid VU%f€m@. BYll)%& fsgid @A% reaveffec tivehaved N &iR #46!12 - # : fi68~hRlJi!iEl 0, X@RIZH -1, fEB4t648'f errno- #RR,@ : EPERM I5!RT?@, %&%f&,EZo MMaEYJ : k@%% tinux %Go #lg &w : setuid, setreuid, seteuid, setfsgid %%*# : #include @B : int setfsuid ( uid-t fsuid 1 ; e&i#!!Fl : setfsuid ( ) JWk3%$kZ? H ljSl%@~ft#%R~~PiR#~I@~ -&t#iRT+ H+%%mFBPiRRI]@ (fsuid) S%??k&ttlajPi.RXI]i4 (euid> E&M#t!Ja %i$@&HPGIH&Ei%- B%f! fsuid STJ!2%iZ~iB, RFU$@ fsuid k realleffectivelsaved &I P i! 5qi - ig la #i : %47aJhRu&Pl 07 *&RqEH -1, %i2{tR.GT em00 iSHRH: EPERM HFRT@, X%Z&iftf* : R&atJ!j : &@%%I Linux $$go @ : setgid, setregid, getegid, setegid : #include ax&& : int setgid (gid-t gid) : setgid (1 ~%%Hfj;j~E~~~~iRBJb4 (real gid) B&&& gid B. %!E u&%HP 9 #@,47lk%H, I real, effective 5 saved gid Q%&%B %!& gida 8 a #j : ~~&@Fll&@ 0, %%JlJ%B -1, %iHf@7?T errno @%R@ : EPERM #dku%gmP2%fi?iRlHf ma$% gid #~F%@IFI effective gid 3 saved gid #$;1--, : #/%a& ... : getgrent, endgrent : #include . . : $1B[ : Xlnclude void setgrent ( void ; .............. i @&#m ..~.. i: setgrent ( 1 81s1 getgrent ( il(lP5&~)OHgHfEiTKX. .%:. 81.:~:: A ... R&i#M ...... ; iB$+ sefpwent (). ..................... -. ... - ......................... - - .. 1: set groups ( BBBM )_ , . - -, : initgroups , getgroup, getgid, setgid .... ; ?&*&* : #include &X@# : int setgroups (size-t size, const gid-t *list): $&BIB : s~gmups (1 H%%f list %tR~A)i$58aefil$AF11 W R8ENgEL8B+d I size % list[] gid-t a , gha% NGROUP ( 32 ) . W;bli#M : /a\% root &lW~iisk@aj~G~O \a m+a : 8E~JhflqgB 0, BR#EJd&H -10 'mBfc~9 : EFAULT P& list gt~~ht~&&. EINVAL size @A? NGROUP ( 32 ), 8% a# i: getpwent, endpwent , . . : #include ex : void setpwent (void ): &agM : setpwent ( )aj%% getpwent () P~@5it!ihhriiPl%WTI?-RAa #include main ( ) I stwuct passwd *user; int i; .: .. 4I ,. . . .. . . ,. for {i = 0;i c 4: i++) { user = getpwent () ; printf (*%s: %d. %d: %s: %s; %sjnn, user->pw-name, user->pw-uid, user->pw-gid, user->pw-gecos, user->pw-dir. user-rpw-shell) ; 1 setspwent (1 ; /* M%fl@BR */ user = getpwent I) ; printf ("%s: %d: %d: 8s: %s: $$\nu, user->pwjame, user->pw-uid, user->pw_gid, user-rpw-gecos, user-rgw-dir, user->pw-shell) ; endpwent 0 ; 1 root: 0: 0: root: /root: /bin/bash bin, 1. 1: bin, /bin, daemon: 2. 2: daemon; /shin, adm, 3, 4, adm: /var/adm: root. 0. 0. root: /root, /bin/bash I* setpwent 0 P,LYM%R% *,' mg@# : setgid, setegid, setfsgid *%*# : #include ex a# : int setregid i gist rgid, gid-t egid ): @j&E#R : setregid ( ) NX#&!$? rgid iSi% Ei %iB#ElPBC8%tBiAX1J54, %@& egid 8 P% H~%EBSBBli,D,BJb~O CIBB rgid 3 egid B% -1, PINIiYQ ~EXIJ@$$$&~~ 0 @ @ : &f.?EriSJ?,1EH O, XR!4&B -I+ %WRRiSrT em00 #%a& : setuid, seteuid, setfsuid : #include cunistd. h> gx : int setreuid i uid-t mid, uid-t euid ); $&BI I setreuid ( H?&#&& mid 3% HljttRKJZ%$RilPiA811@, %$& wid @f %Bi$~$BS~~HP'iARIJRg @%@& ruid E!?, euid Eh -1, HrjW E~~JRRII@*&&~C @ ba: #,$7E~hJ!lMiEl 0, XARljBB -1, li%ltR$T emu. mMBw : i$B% setuid >. : getuid, setreuid, seteuid, setfsuid s&*-Qf: : #include ex& : int setuid (uid-t uid) @&H&j ': senrid ! H%S%8gafiW'iiB%&HPifi#1RP $5, Zikk@&6@ &fi&l%~Pt!X~& (effective-userid) &@%I 0 $E Linux T, 3 root f5!H setuid 5k%&hk#@ajP8XlJ.64.f% root &PF&i&#h%, 3 *%%&%Mp3#, %~@&~$$~%~~~ *hid 1 &&I$), fin !?R%#%Ff&% root 8? !B+ #6%!~%~@@~, !$lb%fi{$H seteuid C lo I B Q : hfJsti2@!U&El 0, 8llfrRYI8 -1, +WW,WB;f ermo. R&lfiM : -&ass8 setuid root m@@W. %@$&%f!?ffW%M%%%+F1B9 $E-f$R% root @N%&Xqk#,ff setuid (getuid ( )); ?k#&+ mt I&,#, i!fj%t!l uid a euid $-3@$, Linux j$&#$;f;%P$ core dump, : : getutent, endutent . . ,. 1 %&&# :: #include ' an :: void ~et~ tat ( yoid ); @ai#M :: setutent ( al*% getutent ( 1 BcJ$41RttE@ utmp X{+Ffft. i&%!+ setpwent ( 1 3 setgrent ( ), : logwunp, pututline gx$j@ : void updwtmp (const char *wtmp-file, cwst struct utmp *ut 1; : a#1#a : updwmp () 814tRffdil logwtmp (1 17iBBm utmp gtdJTA%l*#A. $# ut 3 ~ogwtmp i ) j$bH ump @N@Bp $B wtmp-file FU%%S Am wvnp XA (/var/loglwtmp), #include #include gx.$ # : void utmpname ( const char *file): @@iaM : utmpname 0 HdFiftB utrnp *j$B(lbB. UB#! utmp #H#I!&b9BB! R@. @SBtBCR utmpname ( Qll%H utmp *#%f2% lvarhdutmp. Bma,rZ . . ex @j& : char *crypt (const char *key, const char *salt 1; . . . . . . . i&##l': crypt () %%I DDataEncryption Standard (DES) !j%$$%#fP@ key fi%m ?-%f#fi~k@ifi, key ??ff@E@fK@S 8 -???% &&.k~@IK@@i!l %&Xg B% salt %?W$Wld&~%R$7 EII a-2% A-2, 0-9, I.' f4 '/' R tR&t H*&ZZ@H 4096 @;f;IFI~~3!?~II!J~b~~~~ iEtB#t~?bk~E&B ElEikl64~~?33QfBH, 01 key RPBCJ%B$;~;BR%Z%!J~ fEiW iIh?!l?fq@kE% 13 'P+%, iiJi%+%R%%& salt it$?b!l?fi@o B H #j : %l6l-+%lGlU NULL ~~t%%R?fi$o #include main 0 I char passwd[l3]: char *key; char slat [2] ; key = getpass ("lnput First Password : ") ; slat [O] = key[0] ; slat[ll = keyll]; strcpy (passwd, crypt (key, slat) 1 : key = getpass {*~nput Second Password : "1 ; slat [03 = passwd[Ol; slat[l] = passwd[ll; printf ("~fter clrypt 0 . 1st Passwd : %sinM, pasawdl ; printf ("After crypt 0 , 2nd Passwd : %$\nu. crypt {key, slat) ; Input First Password ; /* %A test. %@%;k? passwdtl *I Input Second Password : /* %h test. %#@n%a$Eb%flm */ After crypt (1 , 1st Passwd : teHOwLIpWOyyQ After crypt () , 2nd Passwd teHOwLIpWOgyQ &X : char *getpass ( const char * prompt); &#l&j ; ptpasr ( 1 %SXB@ prompt WiBBSR 0, %EM /dev/tty +i$rWRtef AM$W, %X%k Idevltty ~BrWRl&%MS@dBA~&~~R%~O Ri ~AIYJ%~~~@~%~JE 128 'i'?fi, $$%!i$!??? NULL? @i$%@&? @BLl'i?t (In') WB@lB. B$hAf WR getpass () QXI39RB Elt $kg@-!%IS~$Lt CTRL-C S, CTRL-Z RP%ElGT 0 #include main (1 char passwdCl = "asswordn; char *ptr: ptr =. getpass ("Input password : "1 ; if (!strcmp (passwd, ptr) } . . print f (l'Correct\nn) ; else print f ("1ncorrect\nw) ; ~nput assw word, /* @A test, T&B$ */ Incorrect /* #qN%iZ */ Input: Password: /*!#Apassword*/ correct /* @GE% */ APTE I bsearch (~z@%) I @&aM : void *bsearch (const void *key, const void *base. size_t nmemb, size-t size. int *campar ) C const void * , const void * ) ); % : bsearch ( ) $IIM-E@~&fi~@~@~%~&#&%~. SOB( key B@$Xf & m&@$!7#, 8% base ~~t%!!%#%t%%&fl%&kl:* B& rimernb ff$ @iHcfi13!~X%&E, @-Z%&A~J~Rll&B& size &2+ SG-@B# compar % --Ei%Wtt. S+GBH*#J%fi+Z%I's7HAfJ~%%+ ZI%% compar Brl%--/i'P~~~~7cff~~k'f%~+~~&~~Z%%R~~l' 5BDi$*T- 0 ffia rn+Z%@Brn%NflHf* 00 #include #include #define NMEMB 5 #define SIZE 10 int compar (const void *a, const void *b] { return (strcmp ( (char *) a, (char *) bJ 1 ; 1 main 0 ( char data[50] {SIZE] = { "linux', "freebsd" , "solaris", "sunosn, "windows' 1; char key [80] , *base, *off set; int i, nmemb = NMEMB. size = SIZE; while (1) { printf (">"I ; fgets (key, sizeof (key) , stdin) ; keyistrlen [key) -11 - \O *; if I!strcmp (key, \exitu) j break; if I!strcmp (key. ".listM) ) ( for li = 0; i < me&; i++) pr~intfi ("ts'ir~", data[i]) ; continue; 1 base - data [O] ; qsort (base. nmemb. size, compar) ; offset = (char *) bsearch {key, base, nmemb, size, comparl ; if {offset == NULL} ( printf (h%s not found! \n*, key) ; strcpy (data[nmemb++]. key) ; printf ("Add 8s to data array\nw, key) ; 1 else ( printf ("found : %sinw, offset] ; 3 3 1 >hello /* #A hello Te$ */ hello not found! /* 3TElI hello f fl* */ Add hello to data array I* ,q hello $f+$finA */ r. list /* Y'J&FfrdZ&B */ freebsd 1 inux solaris surtos windows he1 lo >hello found . hello /* $%$$A-* hello FB* */ /* S*B$lJT */ @gi$~& : hsearch, hdestroy *& *# : #include a a : int hcreate i unsigned nel): ih&i#W : hcreate () H%@&%rff;%, @B. nei %%~%BkEt~~@it@~ a l@l : &%%?&~J.h~~@%%!@~%~ NULLo m36@& : hcreate, hsearch ; #include 2 & a : void hdcstroy (void 1 : $#i#18 : hdestory ( ) M%Wfr1l%H%~2~~%%%, @%'~h%~b%%+ !lq&i%i %$UfflkfiBNU% H3rn%*%o jg@&:% #ijsma : hcreate, hdestroy s* 2 # : #include ex &# : ENTRY *hsearch ( ENTRY item, ACTION action 1; : hsearch i ) $M iE/ Zstl%$%qBjiB& item %&I$& key x@#k#!L 41133$3@ action % FIND, N~~E~~]%#%%&@%@&%R&M%@I % 6gUNIJgH NULL, !AU!$!%@ action %I ENTER, ~~44#@'%%~& %b~fi@BfJnAG%%FPo %&J ENTRY ZXBT: typedef struct entry { char *key; char 'data; ) ENTRY; key ,%--?$f$a#h jE6Jk4 NULL %%Em??$%, H%#%@Mo data W#tJJ!ljfs"k+ key &##l@d?Whko .a @J B !: #&BJ#@@#RU%R~2drn&~, ~EE%%S+~+3~3@B%~~%H NULL, : remque : void insque (struct qelem *eelem, struct qelem *prev 1: &&a&j : insque (> $J remque t 1 M%&#-~~;;~%B*S@IRT~J~ struct qelem Bxfr117;: struct qelem I struct qelem * q-forw; struct qdem *%back; char xdata [I 1 ; 1; insque ( ) %P& elem filB(lJREl%PJRP~+@~ prev Fhf38SJ@E!B~, Itt;B& prev +ijJk2$BH {NULL>. #%ijfg# : lsearch @g[I : void *lfind (const void *key, const void *base, size-t "memb sizct size, int *cornpar 1 (const void *, const void * 1 1 : & : In - &% key fstkl8 gfi&#@&3$, @& base f~lkliJ#$@%~&~l~~~~ht:, &@ nmemb ~~%&~A+WJZZ%B., Q-Z%IFIAJJ~J!~EIIBB size &$, EG--JG@ 21 compar %--@B18+t9 ~/i'~~FB~~I]~~'t~~EB~H E*K% compar ~%-+%&fiE~7G%&EjFD%:+S&%fB~Z%&B@MW Jll]BH 0, jFj+~&~~;s;~~R~J&~~~ 0 a. lfind () % lsearch 0 6 Hfi$ET9 %$k$%lj%@%!@iPf lfind 1 R$$gB NULL, %$$$&fd!fii% %&#$JUA&~~R%~ 23 H fig : fiFII#~&@Fll%~#k~IIm~%n;%m~kE, PR%&&~CPSt$ZlJ#BBB Y!ljiliH%%f (NULL)o -4 @xiiEjB : lfind ex e@ ; void *lsearch ( const void *key, const void *base, size-t *nmemb, size-t size, int *cornpar) ( const void * ccost void * 1 1; fiB*M : lsearch 0 $ll~%#@%h%&~M;?;t~~-----r 8% key #5 h%Bfi&#@&#+ 8B base BI;iJs#@%m&%fi%a~+ B@ nmemb 4%f2&&q ft!IZ%%db @-%%mkfhMIJ&$# size &% g6 -@$& compar %-E&t%#, jh/l-lBM%?d$Wf X%Br$mFlt Z 6% cornpar m%-+BtWir;iEpllZ%@#$~%~+BBfiER?i%&# ~fl~!Vh!%~ 0, ~'f%%@%$7f;&lflRI]&~4b 0 @lo llsearch 0 fi ~FI]%@&EW~&~E&J@@BBIA~&~ #include #include #define NMEMB 50 #define SIZE 10 int compar (const void *a, const void *bl I return Istrcrnp (char *) a, (char *I b) ) ; /* $JM straw* 0 k%?fl@ */ ,I main (1 i chardata[NM~~~][S1ZE]=( "linuxm. "freebsdM, "solaris*, "sunosH, "windows" 1; char key[803 , *base, *offset; int i, memb = NMEMB, size - SIZE; for (i = 1: i < 5, it+) I fgets (key. ,sizeof [key) . stdin) ; /* #A%@? .!:/ key(str1en (key) 1 = '\O*; /* *# ,\n *R *I base = data[Ol; /* data H%?f&#% */ offset = (char *) lfind (key, base. &nmemb, size, cornpar) ; if (offset == NULL) I printf ("%s not foud!\nu, key) ; offset = (char *) lsearch (key, base, &me&, size, cornpar) ; printf ("~dd %s to data array\nW, offset) ; ) else { printf ("found I %s\.nn. offset) ; linux /* %ATFJ$ linux */ found : linux /* linux TR* *I os/2 /* $i$A?a% 0.~12 */ osi2 not found! /* %TEJ os/2 */ Add os12 to data array I* 8 os/2 TZq*j)~h$] data */ 0$/2 /* @%,A os/2 */ found : os/2 /* 32#$117 */ mga & : bsearch &xaB : void qsort C void "base, size-t nmemb, size-t size, int ( *cornpar) ( const void * , const void * > 1: a#aW . . . . . . . . . . . : base @h&#F@b@kkHfi%hhhk, 8& nmemb !t%&gfl+bilZS@ f 9-%%Mk4\lU &$!& size &Zl @hi-@@& compar %-a& 1kl-t. S+fitR%$ltR+Z%lW~Af1~#%, 813% compar W%--+ %&FfiBm3%&@%T%~+B@%%mz%%BR'la~~9Hi~%T%H B? fi+Z%,&@@%F4Bf$ 00 sm#:x #define nmemb 7 /* @$H$G 7 +zg */ #include int cornpar (const void *a, const void *b] { int *aa - (int '1 a, *bb = (int *I b; if Ifaa > *bb} return 1; if (+aa := *bb) return 0; if Ifaa < *bb} return -1; J main () I int base [nmembl = ( 3, 102, 5, -2, 98. 52, 18 1; /* 7 7g33 */ int i; for li=O; i ffl%#~F--~l;il%~+Sb9PAPI], fi9Q struct qelem ZX4nT: struct qelern I struct qelern *q_forw; struct qelern *=back; char %data [ 11 ; 1; #saa : [search, tfind, twalk SS* ##= : #include csearch.b t void "delete ( const void *key + void ""rootp, int *cornpar 1 ( const void * + const void * ) ) ; $&@JJj : tdelete ( ) MAC%@ @ key Br.l B(J%4@Rf rootp tk?h H~Rtafh 1%. @@I cmpa )CI--BIBW+~~ S+@@H%11Rfi+%%Is7~A~5~# % Ti{$% cornpar ~%--~~~~fP~~A&#$ kT%~+Bb%Bib9 7cl%%Bt !@11&4@Hi%AT%~B7 @+X%&B#%R~IHi$ 0, %LEI fi : fkFU3;@BEFU8J%, #%~2~kE#&Ho BD%?ZITIMP~%+~IX @&@, !@j%iM NULL, : tsearch, tdelete, twalk $x # : void *tfind (const void "key, const void **rootpt int ('cornpar) (const void *, const void *) ); a&#,9rJ , . : tfind (1 $P wmh ( ) 4k?8%!!XO s?% key $%il%fi%% g%? rootp ~8~1X~/(J@~kb%~, 5 tsearch 0 $mmgt fi~%!E~~%k~%$ %IJX@&&t J!~~$~,~~uAL~B#EIXWF$~ @& COOI~N i'g-BiBB#a mg : qsort, bsearch, hsearch, Isearch, tfind, tdelete, twalk ax # : void *tsearch ( const void *key, void **rootp, int ( *cornpar) ( const void *, const void * 1 : @h&%m : iqearch 0 ~%@%fl~~~~#%%~X#% @@ key fi36J#B&k!J%#!& Et Pa motp ~~~X~~RPik&kt:~ $R%&O!&%J NULL+ %%% ~%BCI-W@. %L cornpar %-B&J11W9 Z+l&HdtY.lJ@m+Z%Is7 Hk/l\K% 234% cornpar H%-+$%fiE~Z%%EIr;f%~+B% fiErnZ%&E* Y4&~RBi*A+%mB, fi'P%%B#M%RIJME 00 as&# : tsemch, tftnd, tdelete a& : void twalk const void *root, void *action 1 ( const void *nodep, const VISlT which, const int depth ); iiQ&Wll : twalk ( ) H%%i2 E@B3FCXMC $B KMI~ %I~~X#FJ@T;,+,M~~, BB action %-iiF~&%q, B+ZJ%%~E@%, @BBA-+3k$P% i4HkfiB. If% action i$~%-++#k nodep %ldBPAHWI-, I which %--@%m, {f,zm@$%Z$: 3 preorder: %%TtfA I #Xft%&?fiv @gffihbPfi. postorder: RPitjZiFB, #EhZ%T.k E68~fj&trb. endorder: 6 #b~ifj&T,&~ @GQ%B&. leaf: KR%I-L%iiiH, Bb##T%A. i#l# a ; rand, erand48, lrand48, nrand48, mrand48, jrand48, srand48 &# : double drand48 I void >; #include main 0 I int i; for (i 2 0; i<10; ica) printf IM%f\na, drand48 0 1 ; as @j : rand, drand48, lmnd58, nrand48, mrand48, jrand4k smnd48 &x # : double erand48 ( unsigned short in t xsubi[3] ) : a#Nw -: emnd48 C &%~--E~%,6~%$II&@, %@$E 0.0 E 1.0 I'q. xsubi &&&f$r!&fi%k385!%%%24~@~ seed48 ( )o #include main (1 I unsigned short int xsubil31 = I 1. 2. 3 1; int i, j; for (i = 0; i<10; i++) { for {j=O: j<3; j++] printf ("%6d ", xsubiEj1) ; printf ("random number : %finn, erand48 (xsubi] ) ; 1 1 /A m~+m~~+n~mm */ 1 2 3 random 59000 43374 28966 random 61731 23903 17244 random 7666 39619 42869 random 11285 43283 27567 random 41724 4128 1538 random 52567 63651 50102 random 38934 50798 65040 random number : 0.441996 number ! 0.263128 number : 0.654138 number : 0.420649 number ; 0.023469 numbsr ; 0.764511 number , 0.992443 number 0.053484 34153 8706 3505 random number:0.117352 9152 51643 7690 random number ; 0.437939 m*i$lB : random, setstate *&*$# : #include ex : char 'initstate ( unsigned int seed, char "state, int n ): @&BRFj : initstate ( 1 M%#J&!k random ( ) %@Ril&@fBI B@ n AWIkrl~, S B ssed %?I1&fkNmm#&HTo a M # : 6EilW83 initstate 0 T$j random 0 figHsfi&Bo m&am : EINVAL state &ak/bTg 8 +Tf?fE. ms@j& : rand, erand48, drand48, nrand48, lmnd48, mrand48, $rand48 R$ft# : #include &ff & # : long int jrand48 l unsigned short int xsubi[3] 1 ; &&sm : jrand48 ( ) $$Li@-'P~@@!%l%#l,%f!, $6m$-231 91) 231 iI'% @& xsubi ffi %N seed48 ( 1, & : -231 S 231 2l'S7HmH,%0 #include main I) { unsigned short int parami71 = { 2, 2, 3. 11. 22, 12, 20 3: int i; lcong48 {param) ; for (i - 0; i < 10; it+) C printf ("%d\nx, lrand48 (} 1 ; 1 ; #sac : rand, erand48, drand48, nrand48? mrand48, jrand48, srand48 ax a@ : long int lrand48 ( void); main (I I int i; for (i = 0; i<10; i++) @ & # : rand, erand48, drand-48, nrand48, lrand48, jrand48, srand48 sx & : long int mrand48 ( void 1 ; #include main (1 { ink i; for {i = 0: i<10; it+) printf ("%d\nV, lrand48 () ) ; 1 # : rand + erand48, drand48, lrand48, rnrand48, jrand48, srand48 gx a# : long int nrand48 (unsigned short int xsubi[3] 1: #include main I) { unsigned short int xsubi E31 = { 1, 2, 3 ) ; int i, j; for (i = 0; i<10; it+) { for [j=O; j<3, j++} printf [*%6d ", xsubi Ij 1) ; printf ("random number : 8d\nn, nrand48 {xsubij I ; 1 1 1 2 3 random number : 949179875 59000 43974 28966 random number : 565063343 61731 23903 17244 random number . 1404751201 7666 39619 42869 random number: 903337097 11285 43283 27567 random number : 50399248 41724 4128 1538 random number: 1641774161 52567 63651 50102 random number : 2131256119 38934 50798 65040 random number: 114856193 34153 8706 3505 random number : 252011741 9152 51643 7690 random number : 940424678 ~S@J # : srand, random, srandom exas : int rand (void): igj&il/l : rand o BfH-I&%%. %Ha o g RAND-MAX liil. annag9 P%m;fn%ml '%$%$um srand ( g!@B&&f$?, $~%*%k@&a# 7, md 0 &flHW2h3EE&@@3% lo 3+@&&HTi%@% srand ( ), a M : &B 0 3 RAND-MAX Z14mPN&B7 RAND-MAX ;SX7!E std1ib.h. g@% 2 147483647 /* Pikfi-7 1 91 10 FM!iEVL&B. &Z3fl~iElB#I%@7-, EB@!B~~P* ig@?& srand ( 1 * / main () I int i, j; for li = 0; i < 10: i++) { j=l+ Iint) (lO.O*rand (I / (RAND-MA.X+~.O) ) ; printf {"%d ", jl ; #I%@& : rand, srandom $a*# : #include ex@& : longintrandcim Cvoid): @!1#19rl : random () 4&8-1199rI$, 0 E RAND-MAX fs7. &flM&i !&~~~&%~, &@%$JM srandom I) $i&fkb#fi%f$?, @%5k$?Fd3#l, #f$T9 random aflfflH2 &?I@@&##$?% lg %?I%&&#Tifi @% srandom a a : &a 0 3 RANEMAX ib%!b%&&#!k RANRMAX Z%!E stdlib.h, #@% 2 147483647, .ii$&!$ rand C 1, ex a & : unsigned short int *seed48 unsigned short int seed16v[3] : Eih!&%w : 48 BP~B()A$#@@@%@@i%f%fl@ (linear congruential) &#i&%P!k %fl&: Xn+l = (aXn + c) mod m , (n >= 0) #+ %& a = Ox5DEECE66D9 c = OxB, m = 24a0 @@ seedl6v Fa&Zq unsigned short int m&f.., fi@iETJ,% 48 fl, &!$h@%fi5!1k%l@&&3 #.!i!,Pffi$~J@~ seed48 ( ) m%@@h$U seedl6v ;/;WRWJ!~~?J&B~ kflm seed48 ) FimE@%83m$IIBo #include main 0 unsigned short, int *pa seed16v13) -= I 2, 2, 3 I; /* #a$ 1, 2, 3 */ int i = 0; p seed48 Iseedl6v) ; /* iR%mC6* *P *, for (i = 0: i < 3; i++) printf ("%dl', * (p+iJ ) ; printf ["\nn) ; for {i = 0; i < 10; i++) { print f "%d\na , lrand48 (1 1 ; 1 1 : random, initstate @ : char "setstate (char *state 1; @aa!d : setstate (> 83'3kg* random (1 ~fi~~~k!i~%%&&410 @??& state #3 I;iJ%B(IE&&&&, ik&a&#P:%E~%mm&@Ho a @ : &H%N setstate (1 iiif random () fi@FBm#ao #xaa : rand, random, srandom SFJ fi : void srand (unsigned int seed 1; Ejail&m : srand () R?k8%' rand () P~~~~@~~~fi@#$70 %@? seed &~%2 +!#-&, 8%muwH getpid i 1 3 time (01 ~~@~~~# seed, @R @?k seed %%&Imf&, rand 0 fr~!k~@&@@@&$kk-#$o /* ?*fiT 1 10 (qmk#A&@, &%@jS#lfi%Z!JS rand (1 */ #include #include main () I int i. j; srand ( {int) time (01 1 ; /* $lJR time (0) %f$B$#L%? */ for (i 0; i < 10; it+) { j=1+ (int) (lO.O*rand (1 / (RAND-MAx+1.0) ) ; printf ("Bd ", j) ; 1 1 ex : void srand43 ( long int seedval ): @j#a : sxand48 ( ) %%z drand48 I ) hand48 ( ) lrnrand48 1 ~!ik~#L&F/ &#JL&@?. B#k seed &%@+!E@. ifi%iTEl$l]# getpid ( 1 Zk time (0) ffi%Bf&k%if seedd fi~%%& seed #i?@Fl@9 %P%B(1!%3fl%@@& i!2-&fi jgM#$:% Bxagegt : rand, random % : void srandom ( unsigned int seed ); @i&i#m : sandom (1 H#@g random () P&@~A&~~l%$II%~To @& seed &%!,@?!!&!$* %'ifqu$qM getpid ( ) % time (0) b!l%@g3k%{$ seed. $!R%6& seed @$$#Ha, random l) fip%m@#fi&fi@?k$$$ .Rc %WB:% I close (XGRRB) I : open* fcntI* shutdown, unlink, fclose &x : int close ( int fd>; aBaM : Y~!MZ3C#G%E+B%BRY~@BiJ close i) %H%Xf+, KI close (1 4?ikLBSHt&* #Rtdr%Bf+B&W#B#O %L fd %%tl open ( ) $t Great ( F!%H&2i+&&EJo 8 M B : S~CPFJI!WJ#HIR~&H 0, E!ik$WJFl&B -lo @iBR@ : EBADF %@7 fd 4F6BKJ*4~@i&GIE!8Z~~i2#W0 Mliai#M : HRBZt#Z%%ut A%$ $%XI%! EMRIB*!+, @#@iX I3 51.X~%~+~ #~%&&%HBe : read, write, fcnt1, close, link, stat, urnask, unlink, fopen : #include #include Cfcntl. hr sx : int creat I const char *pathname, mode-tmode 1; $&#l : %%!I pathname #i?fi~%$$B(lft#@@$.fi$d creat i 1 @J-fdffl7;FlJb!~ ;WHX3flBJ open 0: open (const charApathname. (0-CREATIO-WRONLYIO-TRUNC) mode-t model ; $&Eft@ : &.FB%k mode if$$% open 0 $ H : creat ! ) 4i!ZH%BfJZd$%lfi (filedescriptor), gfi%iZ&!kll&@-l. #R%Slffl@% errno, EEXl ST 8% pathname %%m?tff2&i!E0 EACCESS B% pathname fi%%~~~~$~~~%~~$!I]% rn@P@o EROFS %n%SA&BH*I+8G?E@*!?%%he EFAULT @% pathname E1#~di%%kh6?2I‘~~ ElNVAL 8% mode XE@t0 ENAMETOOLONG @& pathname k#. ENOTDIR pathname %--I%& ENOMEM &dLbh@$hio ELOOP Ba pathnarne ~~~?$~~P&I'~~~O EMFILE ~&~~~~~~~~T%~~!~~k~~o ENFILE ~%%~~%%~~~$~flh*~~&k~~ is$% open C 1, : open, close, fcntl. dap2 a : int dup ( int oldfd 1; &Barn : dup ( 1 Fd%%k'l@%k oldfd %f%Jft#@%fll #%%%ao kk%Bfi*# @%4?@# oldfd ]bmSM-+*#-, $!$%fih%di% i&%fiB@%@ : open, dose, fcntl, dup s&*.fE : #include sx fie : int dup2 ( int oldfd, int newfd 1: &#i#g : dup2 {) ~%~%~Z%!$ oldfd fiB~2fT~~G11 %#~~b!$?%@newfd G--.&$@lo g$S& newfd Yj-E4J%&X.C$&%G17 newfd l%Bb!lZ #%X#t#WI. dup2 1 RiBLK!*W6@It -%R%@Jf .MB%flR$% #*.IT%$* WI%~$% dup ()o a H #j : Z~B%fl%lhW, R~l~B~~J~RH%~~R&Z~+#%H~ %'~~~~~%i~-l~ emo +G@%S4.t:a?$0 safilcrg : EBADF S!& fd IFG%k&Xf$%%d, &SX#EJ;Ro m*%& : open, flock *&*# : #include #include &x aa : int fcntl i int fd, int cmd) ; int fcntl C int fd, int cmd, long arg 1; int fcntl (int fd, int cmd, struct flock Vock): i&&%W : fcntl (1 H~~#Xf+@B4H-%%#g 83% fd ~~%%$ZHXf+fi$ $1, cmd R$?@&fim%& %TYdA#4t31R: F-DUPFD aj%Z!Zj&jT?$%?%@! arg ~I%/J'~#%@~~!~%!Y@~ id, M~IIB~ fd mz4++3sa0 RIIZHRBRM f10 %&%dup2 F-GETFD @# close-on-exec &#IT;, xlkE$%f$l FD-CLOEXEC fi% O+ 1PS@iRH exec ( 1 @#~~HZ1+%~~% W F-SETFD iif!?!? close-on-exec #4b? %@~ u$3% arg FD-CLOEXEC fi F-GETFL &@ft#%!%fl%&&k &#%% open ( rn$#k flags, F-SETFL iQ~fC~#6%id#&&&E7 %%! arg %%&#%, {HgfCR RAPPEND, RNONBLOCK @ RASYNC &2, g$hfi rn&%%T9%Qo F-GETLK N#*#%em#& F-SETLK @8*#$!8~sfi~&, flock %@@J 1-type @'%% F-RDLCKI F-WRLCK 5 F-UNLCKo @%ZSBS@$7 RII %H -1, gsfl6qk EACCES 3 EAGATN, F-SETLKW F-SETLK f$Rj@fl, 1B~%&@2%~~7 &naj&-$%%IJ ~B%ai$a~h%ko EB%Rmzm3@9#!E%+&H* &P&m-1, %g{t@% EINR~ @#! lock a!#% flock %m%% %XPRT: struct flock { short int 1-type, /* @$hza */ short int 1-whence, I* 1-start EB *.I off-t 1-start , /* $4ZEi?t@~HAfiiS */ off-t 1-len, /* @SE%stJA)1\ */ pid-t ljid, I* %$@f$mBg */ SEEK-SET ~*#~%%~~fp5&%fi~o SEEK-CUR U %*~~$&%~!i!ii!%~h~~~~~~ SEEK-END U*i$%E%%zb!J&f6flgo a # : ~~%I!J!IJBH 0, ~fiR%J!lJi!ZEl -1, ~%RIZl~T errno , : open, fcntl %%%#$ : #include csys/file.h> fi & : in[ flock (int fd, int operation ): &&i#W : flock 0 &fiB& operation %%%atJ33W$B fd FiJi%m*f+fR&H% @%#R%Zrndi~o kGBHEB$B+Z!+? %&+fiZ*Pt-rn%-ERo BB operation %7;511PSH%R: LOCK-SH @ %4%@qm@Nm-%4$f'F#k$+&$ LOCK-EX &~3@@$n -~*~~~~,?fi--'?~fi%& LOCK-UN #%%f*@$@&O LOCK-NB ZEgZ%ZH, k@.fi~$BPI!Mt qk&@%@o NRS LOCK-SH 3 LOCK-EX fi OR (I ) %-f; @3F;;&# : #include cunistd+h> gx@B : int fsync (int fd): &&sm : fsync 0 fi5R@B fd @i%~9f+%R9 Ufif% %RR$a a @ : &a!Ui!@l 0, %%%El -1, em0 31%iNt% : dup, open, fseek .4) : #include csys/ty pes. h> #include & ff % : off-t lseek ( in t fildes , off-t offset, int whence 1 ; i%&#M i: Q--+ctIRm*!+B%-+B3@s, 2i~~R*~+~~~~~S~~EE *% T* (@ 0-APPEND), !!lls%@ge $s"$Jrkf%Eo 3 read i ) 3 write i j IF/, $%3!?E&$62#$hl I lseek I > REHH3%l%Zi+mB5fizo @& fiides % E#JRKJZ~~B%~~~ SB offset %@@i@%! whence ~f$~@~!~?!?~@@@O %% whence hT?1] Bcp-N: SEEK-SET %& offset ~lJ%%Jifi~i~F~@E~ 4" SEEK-CUR U QEH&5@ZfZG#;tln offset +1C$3Eo SEEK-END %~%fi~~E~%!~RGB@fi~offset~iG#$~~ 3 whence fi$% SEEK-CUR 3 SEEK-END kt, @g offset fti?&&@Jdj fR 0 c 1 1 %%i$342E%F1J2iT%%ll4: lseek ( int fildes, 0, SEEK-SET) ; (2) Rfii$~fif!$?$f~jfr{~~ll$: lseek (intfildes, 0, SEEK-END) ; (3) %g&@H%,x#fif!?F$: lseek (intfildes, 0, SEEK-CUR): jg Jg @ : %.iEjHj$JhaRg$k! Hzmg%fiB* &%EEs$*~+%%$9+?Bo E ~wWll&H -1, em0 26B+E$iltti% fitfi '" %%fta5 : EwrR &fiM%EY%+W* EAGAlN %{$8j$~PH&UO~(O_NONBLOCK)~~%BB~$RI'J &HEGO EBADF @# fd ~k%&k!l~f~%%~, 3G*fqc%m0 ESPIPE &!& fildes fFrW@Z!yk-f 8 (pipe), Sodret 2 FIFO. EINVAL g@ whence qkg%fio R&i#B : Linu %%i[;fii'f lseek () fl tty %g$W, kJ$&#$+ lseek () gB ESPIPE, gxP& : int mkstemp (char *template): I4kiltrJI : dstemp ( ) FB%@PB-HlbB;f?CiTe 48 template %tFi@~*l+%%W+ *q3~6)-;+??8&%& XXXXXX, mkstemp t 1 k!$qi%T##r'2# 0600 8!~~?TR~SC~~7 @R~~~~+T~&EFJJ%@GS*{$~ TTfi &*(+%&* ~+~$fl15~o *~+m+~~IRG&BNs5~2i+B&~o ~~2~+tInk@B4GH NULL, jf'.ik%i%{tfi@& emo 5$i$$fi#$ : ElNVAL && template 7fl $l gG?i'?'+ 7734b XXXXXX, EEXIST &$&@~I~$II$*{~~ m@aW : S& template fi%~*!~~;F%?ff$~s5fi~~3~&%, Bu: char template[]-"template-XXXXXXn; -Fa~~u~~H-F~!Jm3&5& char *template = "eemplate-xxxxxx"; #include main {) I int fa; char template[3='template-XXXXXXo; fd = mkstemp (template) ; printf { " templa:e - %s\nn . template) ; close Ifdl : 1 template = template-IgZcbo ! * Tfi template- lgZcbo 3&!@5 *! #E@B : read, write, fcntl, cbse, link, stat, umask, unlink, fopen @j& : int open (const char "pathname, in1 flags j; int open (const char "pathname, int flags, mode-t mode 1; &#kBW : && pathname fEl;il%fIK$9f i+%Q2?H@0 T3dS+@ flags fiBkf$H rn@t$: R*i$%R0 Linux 22 LlG%Gm&$%, UB% --E%%24?FJE9 @!& mode !~JfiTP1]%t+4#~~ R ~&SQ%Zi+fl7f'&*%, &?I.aiE32*~:wBt3*XE 29FIJ umask ii!ifiBnliiJ, H~ZX#HPBE~%%J ( rnude-urnask 1, S-IRWXU00700 @R* {f%$%XfFfiB%RRTi.~~ W%B.FJNITH& Ro S-IRUSR 8 S-IREAD, 00400 @bE* df%8x#fig%R%q 2RsS&6B0 S-IWUSR 94 S-mm, W2W &!I@, ~fS8iZf~FAfi8~6~ 5As5@1rFO S-IXUSR 8 S-IEX-ECF OlW &R, dt??C%*W%fi%R%$ &4?rn@Fo S-IRWXG m070 RR, ~%%*4+~~t~~%qi3~ V%E?WR ~rn@IRO S-IRGRP @P&C* ~f%SZf~HP~WGTili~~&PBo S-IWGRP 00020 flRt ~~~~Z*#HP~R~~SAKJ&BE~ S-IXGRP mlo RR* K%%%!qMPtHW6SrlhgM@Wo S-IRWXO W RE* 4t%&fhHPRRqi% iT5R%kfiF~ N 6E 0 S-IROTH 00004 &R9 ~t%X4hHPfifi~i%B~@R~ S-IWOTH 00002 &If!, ?G5#~&~PR%q'rg'hm@bB0 S-IXOTH MMlol @E* w?&#HPfi~~&$?~@@o S H @ : %~~~%&B~RPB#S~T@BYU&HQB, %%~ZX,, RS@-+&B %%lk (denied) Rl]gn -la #HRti& : EEXIST @& pathname fi%BCJ2#2,7?B7 MCM 7 0-CREAT 0-EXCL RjZ EACCESS %@ pathname fiE@ IFI ~#$~?$?%Z$@!I~$WJ& Ro EROFS ~#d~%A&~~~f~6&~~i%*f%%%~o EFAULT @& pathname %#@~~6@h6$%l0 EINV4L %& mode $E@. ENAMETOOLONG $#$7 pathname ENOTDIR s!& pathname $%El%, ENOMEM @wit6$%q ELOOP 8% pathname fii%%#'#jp&lb3&. EIO uo Gat%%, #include #include #include #include main ( ) 1 int fd. size; char s[l = ''Linux Programmer!\nu, buffer[80]; /* IT* /tmp/temp I%A, ?h%iZftW;I;@Bfl~JBhi3ZZW */ fd = open ("//tmp/cempM, 0-WRONLY I 0-CREAT) ; write (Ed, s, sizeof {s} ) ; close (£dl : /* )SR fFtm~/temp %%#BR~IT+ */ fd = open ("/tmp/tempn. O-RWNLY) ; size = read {fd, buffer, sizeof [buffer) ) ; close Ifd) ; printf ("8s'' buffer) ; . . ~,- Linux Programmer ! #% ; readdir , write, fcntl , close, lseek , readlink, fread G#am : ssize-t read < int fd, void *buf, size-t count 1; & : read { j 2$223& fd l!fr$Rm*f?@% count +?-'621 bur f!?$tfi?%$JB@ d;ro .E%@ count .% 0, NJ read () +2%fiH.%ZH 0, i&El@%%F%@ RHWT3Bt hi%iiiD 0, S%EZ~&A~+E$E%P~S%H%B, k9b 2i+S%i$E&@SR~~rn~T&~~ M&%M : ~uRN!WJ read < %~~~Ei3~413WT!!!i, IBB%%&D(&~SB count #k@+ E&HP9%i%k tL%33$i~RH?T&9j !4U%W3ki$3U7Z1+E> LARS (pipe) ~&%%&i%@, EMZ read 0 @E?+M7&4X%k#o 3 $j@% E$Wj!Il&H -1 + +E$jtE$&A enno $, fiTi+i~SfiZRlJZ% firno : EINTR lk%H#E$.%q@p EAGAIN %@H$qPH& I/0 (0-NONBLOCK), ~k&%~~&!N~j BBkkBo EBADF %@ fd dER@IXlZf#BZW, $%*#E%bfIo i$@$ open i 1. : int sync (void>: #%,#&a : open, read. fcntl, close, lseek, sync, fsync, €write %&A#- : #include a : ssize-t write ( int fd , const void *buf, size-t count 1 ; &#k#M : write () buf RH!tfi63A count +SPFII@%? fd RfKibW !+h - 3fiT *~+@S@Z&2EZ@30 # : ~U%~~I!Z~?I write 0 ~i5H%P5~A~~~$kp ~~~#~~~~R~~Z~-I~ +E-iHtfiqGn errno cflo $&@RrrJ : EINTR kflH%iG?%q Ro EAGAIN %@~$~P~@?I/OB~ 10-NONBLOCK)v Ex#@iqg 8.l J!IJZDkE* EBAPF B% fd %k%%f%E%mo iZ@+ open (), wz@a : feof a3; 2 # : #include &x @ @ : void clearerr ( FILE *stream ) : GpgPrifirPf : clearerr r > 8PtSB stream EB~*l+t%fi1$HH#8E60 ga#i: -k mz@@ : close, fflush, fopen, setbuf gx a& : int fclose ( FILE *stream ); @ai#l : fclose (1 FBft%rd%P fopen (1 tTfFB5Pl+. R$f'F2itB@EC5m%E 5AXiW #W3%%fiRgNZ6B%o : Z%.&#~~~~?,~~~i&@ 0, 6ifiGE5kWFlJZH EOF #@%#iZiffiqG$lJ #$i$lR@ : EBADFS%S& stream 4h2fTR&ft4+o H%@jB : fopen, open, fclose &X a : FILE *fdopen C int Bides, const char *mode j: &#k%M : fdopen (1 %??t$%k fifildes @l*#fii$Q3 K@%X$EFIZ##BQGZH~ %& mode ?f?f$ !J!1144?,%%2f+ifEHW~?fi%&- &%&d*%fi$~k% %#@@ 4&915fR1I4. X-f mode Pt$$&~ti3g3$ fopen ( 1. /* j%6%$&AiB&W2#%Sig 0, %%T?Am2%#4%t */ #include main 0 I FILE *fp - fdopen (0, "t") ; fprintf (fp. "%s\nU, "hello!") : fclose (fp) : 1 hello! sx @# : int feof C FILE *stream 1; : write, fopen, fclose, setbuf %*# : #include sx &a : int fflush ('FILE *stream : Biai&!Yl : fflush C ) 2~3@%%fiEptJHBB5B%% scream EZB!J*.IY+ BRB !& stream 33 NULL, fflush () 2#%%)S%b9ftiWkEE%d & @ : &T.,.&H 0, #I#%@ EOF, t$E!t@&'f. ermo 9, #HRP3 : EBADF B& sham tEQBI1*A%hBnff, $nFF#f %Rig. XP, #S{t.f4&!$ write lo . .- fgetc ('l4f*&*@@-+p~) 8% : fopen, fread, fscanf, getc sgC;&# : #include gS(@# . . . . . . . , . . : int fgetc (FILE *saearnj; aa#&M : fgetc 0 aj~M$% stream %B~2i++@@-+?R0 Ei%3lW%Rili !%&%Hf2!%m EOFo & #j : fgetc 1 $5t@ISR$l]~??$, ZBB EOF QIl%%FI17Z!%Bo * / #include main I) I FILE *fp, int c; fp = fopen ("existn, "rn} ; while (c = fgetc (fp) ) != EOF) printf (u%c", cl : fclose (fpl ; 1 m*a& : fseek, rewind, ftell, fsetpos ex : int fgetpos ( FILE *stream, fwt *pos ): &&%la : fgetpos ( 1 81 hi1 i ) -WBI(BFB~%W2!$%Ht~i39RB0 PL stream )tl anfimfiqtP+to wzremtl pas ~PHZB. 8 : 3$fIMj$ta@Rd&D 0. %8@BR!fB -1 emo &@@QR4+,Wo #&!RR : EBADF && stream ~F~%%~@d~~~Bib!~~l+%~ i3$3$ fseek ( jo : fopen, fread, fscanf, getc : char "fgets C char *s, int size, FILE *stream) : &&ii#M : fgets 0 HftM$@ stream %%WZ~+F~BA~.BRGF~]D& s &%BFJh @Wd7 ,B~JJ&Rfifi?B> B%UZ.I+EZE~$ ES7 size-I +%R%k, &&%Ilk NULL #%??%$%go @ H f& : fgets !) g&J#lljgg s @f$, gB NULL Ffl$?%6%%@g5k0 #include main 0 C char s[80]; fputs (fgets (s, 80, stdin) , stdout) : this is a test /* %A */ this is a test /* */ - -- fino (gm*.pI:~~i~~s*.pi:~;zeiii7) : open, fopen &*f$ : #include ex fi& : int fileno FILE *stream ); iih#ki#M : fileno (1 NjtE@@@B stream %G%~Ti+%fi@.MStlZf+fi$fl~ 2s 'Bl #$ : ZB*!+B$30 c-1 #include main (1 I FILE *fp: int fd: fp = fopen (m/etc/passwdH, "rm) ; fd = fileno (Epl ; printf [*fd = %d\nh, fdl ; fclose (fp) ; : open, fclose &x @ : FILE *fopen const char *path, const char *mode): %ATi+A& 9AB(1%E2Ukul*4+RG7 ~P?U+R%BA%& %i%$JP .k%~%&%"?$@TiJ~@h~-'b b ??$, 4~ rb, w+b d!, ab+ %a+, BIA b +flm*sim &~~Tflm*f+%~B~~2f+7 fi4L%*T*i+P rl;j%?f POSIX %a, @a Linur $P4,%@3ZE0 & fopen ( ) WiBZB %*{++ 8 % S-IRUSRlS-WSRlS-IRGRPISSMrGRPISS IROTHl S-WOW M66) ik*%J!F$I&%% umask ffi, & H B : Zi$llRNlTRG+ ~EI~I~~%IB~~+%#$~&&%EI 0 E%!~PHJR%!&Y~J& H NULL, #RfgiZ{ffi9@$E ermo q, /* M at noexist */ #include main I) { FILE *fp; fp fopen ("noexist". "a+") ; if [fp == NULL) return; fclose (fp) : 1 . . . . . . . &'xa# : int fputc (int c, FILE *sueam): @.#Bt.bZfB : fputc 8%$.& c $$% unsigned char 89AQ% stream jRi?m*#q a #f : fputc 0 ki3D5AFik~hBtJ??31 co EOF !l1{f,3%A*!!ka /* #ilI\Sg29$.a g'l 2. -+?-+StihSAfY2++ noexist + +/ #include main () I FILE *fp: char a[26]="abcdefghijklrnnopqrstuvwxyzM: int: i; fp = fopen ("noexist", "w") ; for (i = 0: i < 26, i++j fputc (a[il, fp) ; fclose (fg) : 1 Ms@Q& : fopen, fwrite, fscanf, fputc, putc : #include exma : int fputs con st char*^, FILE *stream): &&a98 : fputs () W%lB& s fiRtB&'~flSF~h$d%@ sueam #iBtB*#A. B B : %RrfiA~~~BSttlBcJ?P4+&, f EI EOF ld%%8E@E!kQ $@$% fgets ( )* ms@& : fopen, fwrite? fseek, fscanf @j # : size_t fread ( void *ptr, size-t size, size-t nmemb, FILE *stream ) : iii@j&M&j : fread 0 aj3k&zl%%i%$a%%0 $& stream J~E!dTfil!t!J%f%B% @# ptr %fi&%7@i$%%tffi%@i~~~~ @Rl?!l?8&w%@ size*nmemb 4: &go fread ( ) ~~H~~~~@~JJ~ nmemb H ? @%k@k@@% nmemb ;stF.BlJ\? R1JR%qhki%$l1 T*i~E8*@%%&, SN!iZ@a3 fwf( )3 ferror 1 *az2%itk1r6N0 : : 4 nmemb @ido /.* @rW& fwrite I) %tBj@WfigR */ #include main 0 t FILE *fp, fp = fopen ("/etc/passwdH , "r"] ; /* )J# /etc/passwd */ fp = freopen ("letclgroup", #r", fp) ; /* &%tTR /etc/group*/ fclose Ifg) ; 1 : int fseek ( FILE *stream, long offset, int whence); B3BNW : fseek 0 H$%3*i+%BSiA3fiB0 stream %E~J~FI%I*~+%#, @& offset ?I&!&$@!& whence %4$d~i%3{9E&$@&~ .& whence %FFjg+-$$: SEEK-SET hkBF*#Tf% offset fi8@%%~i%Zi~Z!q SEEK-CUR %Tm@%EZ&h?% ha offset )i.@@f SEEK-END %@ 3~:EfE~iJ3rti+RGB4$hn offset +B@So 3 whence @% SEEK-CUR SEEK-END IF), @& offset hi?fiBm$ 1x0 TF~]&%%XIJ~~~H~S: 1 j % % i fi % f R : fseek FILE *stream , 0 , SEEK-SET ): (2) %%i#~~~%$IJ*f~~~: fseek CFILE *stream, 0, SEEK-END j ; ef R@ : IBADF B!& stream ~bfi@$~@?hi&%fiE~%#% EINVAL @ 5!$ whence qbfi%l$o W&l%M : fseek C 1 $@ ]seek ( 1 $i%Hi$Tf23?, l%lk~~%fi!2J#j ftell ( 1 %R@ H -be-+ au~&tYI@~ #include main (1 FILE *stream; long offset; fpos-t pos, stream = fopen (*/etc/passwdn. 'r") ; fseek (stream, 5, SEEK-SET} ; printf ['offset = %d\nY ftell (stream) ) ; rewind (stream) ; fgetpos (stream, &posJ ; /* M fsetpos 0 R%i%5El!t */ printf ('offset = %d\na, ~OS) , pcs = 10: f setpos (stream, &pos) : /* 81 fsetpos 0 i2;tf$Sfif */ print f ("of £set = %d\nu . ftell I stream) 1 : fclose (stream) ; offset = 5 /* fseek (stream, 5, SEEK-SET) : */ offset = 0 /* rewind (stream) : *I offset = 10 /* pos = 10, fsetpos (stream. &pos) ; *I : rewind, ftell, fgetpos, fseek *&*# : #include ex a& : int fsetpos ( FILE *stream, fpos-t *pas): @#i&g : fsetpos i ) fseek ( 1 --#@~~%@$1*~~%,~@5fi~~ $S!& stream 3 dtJRBcJXfftPtt. 4% pos h%ikbaJIWR&%@% : H 0, ~R+iW!lJ%H -1, errno $G&E%4t#o #$%RE$ : EBADF 3$% stream jki%8!%Sil%?1i%%!!LE@fr:!~%~ %B% fseek { jo mx @QB : fseek, rewind, fgetpos, fsetpos ' $X @B : long ftell ! FILE *stream ) : : fi&NM : ftdl (1 H#S!@Xf+%H$&i%5BBn BB stream %i2tJfiHZi+& ttv ##HR@ : EBADF %% stream ~%%W~@,?I@~I'~~@J*#~,%~ ms@j & : fopen, fread, fseek, fscanf lg& : size-t fwrite ( const void *ptr, sizet size, size-[ nmemb? FILE *stream ): I $llri#M : fwrite ( 814F%tW4A*f+R+. 8& stream %I ZfT'fFt!i9Bf+EH, B #k ptr Efi%%A~&#!i&kEJ JElRTAHBR&U8!!tk size *nmemb &Zo fwrite ( > &$H%b%hb!J nmemb !!&H, % a # : i&?la%b$%hb!J nmemb !$kH, #include #define sets (x, y) I strcpy (s [x] .name, y) ; s [XI . size = strlen (y) ; 1 #define nmemh 3 struct test C char nameC201, int size; , ) s [nmembl : main 0 { FILE "stream, see-s (0. "~inux! ") ; set_$ (1. "~reeBsD! "1 ; set-s (2, "windows2000") ; stream = fopen ("/tmp/fwrite'. "w") : fwrite (s, sizeof (struct test} , nmemb, stream) : folose (stream) : 1 #Jsjjij& : read, fopen, fread, fgetc ex a# : int getc FILE *stream ): iiiai#rW : getc i) HiRM%9 stream Witk~Stk+@l--+9T+d ~i%flff!+EK~ k@#s;t@BH EOF. %% geet 1 -4 fgetc ( ) f"FM.ttlA. {El getc ( 1 )tl EZX, 4EaZma&flMo @I r : getc i ) &gB%RLlm?f$+ g%B EOF N~Jd~~~JTB1+R. : fopen+ fread, fscanf, getc & # : int getchar C void 1: i&BaM : getchar ( 1 W%&6%% ($u@&) 93%. -+??!fo %%%8??? unsigned char %#@ int %&ao $ H f& : getchar 0 %%@BRP118587+. %%B EOF !l~??%%%%~!kO P#wa#M : getchar (1 4E$%dl&, fiZ getc (stdin) 221. #include &x a : int putc ( int c, FILE *stream 1; : putc & c %% unsigned char %%hD% stream fi5%324+$ L&% putc () -4 fputc ( 1 f'FBl@Fll, @ putc () %ZRB. ~F~F~EBC~BIB~Q~H. a @ : putc 0 ZGH%ArJiXhm373. PP%& c. %%g EOF !$~df,W~)\%!@. %P* fputc ( )* : fopen, fwrite, fscanf, fputc %&%# : #include sx@& : intputchar (intc): fi&$hm : pukhar 0 W*%%#k c %%fT?d&%$h$@& (~UB@)+ ~ H # : putchar ( ) %@l%lk&?.hk!J%% PPPiSP cb S&H EOF R!if,%$hLk!% Pko wM#a : putchar ( dbgEi%jfitl, fig putc (c, stdaut) %%x0 &lESj & : fopen, fread, fscanf, €puts a&*# : #include : int puts (const char *s); eai#m : pub ( ) W~@.BB s P~+*S~JI~ZBM,~:W (a638)o @ : puts (1 ~J%X~~~&~%I%&%?$~@, i@l EOF R~%%~%$#!ko 8 & : void rewind ( FILE'stream ): St&&!Jj : rewind ( 1 8j3k~*~~%~~~~iS!@~~~~~~0 %I stream kEfJK l%lft##$$t. Jk&~&#i%"ffl fseek stream, 0, SEEK-SET), BSB:X - - - . . -. . . . . . , , setbuf (E@**$&&#$) mx*a : setbuffer, setlinebuf, seivbuf ex a : void setbuf FILE *stream, char *buf 1: PBaB : %flffft#%6, @@Ad2l, IN setbuf () qI1IHf @Sf #%Hg @EXo @B stream .%%ZI#~~+R. $!& buf ET;il I3 ZBtl@iFPE&&akt:o 1% buf % NULL @#, gI]%%j!# 10,xtbufl setvbuf (stream, buf, buf ?-IOFBF : -IONBF, BUFSZZ); zm#i:z . . . . , . ., . m# : setlinebuf, setbuf, ~vbuf : void setbuffer ( PILE *stream, char *buf, size-t size); @!~&@,m : &fi%*f+%%. i%&F$@2%, fi83 setbuffer ( 1 qH5k@gft#%&% : setbuffer, setbuf, setvbuf a& : void setlinebuf (FILE *stream 1; $&#w : setlinebuf ( 1 B]%$P*!q%!&&fT%fi#bfJ%%@ 10, #[IJ;ffiH: setvbuf ( stream, I char * NULL, _IOLBF, 0): setvbuf()* SHE:% : setbuffer, setlinebuf, setbuf sx : int setvbuf (FILE *stream char *buf, int mode , size-t size): : -err~ni+w~, PIWJW~~, HH ~t~buf o mi~~iftzft~+~m %REo SB stream %Ei%BA!+%, BB buf m13 Q iZm%*K&&& ik, 9% size %BREJ~JI\, B% md FJVdhH: IONBF 10, - - IOLBF u?%fT%%@l&%gifp 10, - IOFBF %&B,W 10, fr~%H buf 31 NULL @+I, flfl%%@@ 10, ex&# : FILE*ttmpfile (void); &#sm : tmpfile 1 B]f BT~lfi%W2f+*i+~ lhwftff$$B(1 R%u usrl indudel 5tdio.h ZKm P-tmpdir (Amp) %&b. %l%#f #JlSJ&3G$$WJiR% #SmT**1% #%31f+%*&H. B rn : ~hW*~+Jrn$lB2G, EEl~%H*,f+%~7#2~%Bo E%*#+J8%Rk FIl&H NULL, #R%i%4t@GB errno +, : EACCES X%BITGiU*I$~ EEXIST lki El$*!? i?&$Ee EMFILE E%$ljB#?Rl' HFtnfl KI.fc4MkkBl2~ ENFILE &.&3~%%TiTmNnR&*#%I:RQ #include main 0 C FILE *fp: . fp = tmpfile 0 : fclose (fd) ; : fputc, getchar, getc sx fi# : int ungetc ( int c, FILE *stream): &&gg : ungetc (1 %@!& c ?RSHS% stream fiEZm3rf+%a jh+%HH? fi2&tT-4%@*@ifim&%R?30 3 H f& : b~~h!ll~~~ c %?$, EOFo MZijfja : exit, -exit, assert : #include cstd1ib.b S~@J# : voidabort (void): & # : void assert l int expression 1: Pt~!!l : assert ( 1 %Flbfr8& expression 3, ~$&YL!W]+B~EBN#+E iZ,%* fiGflH abort { *%JkBf% i&M#:Z #icclude csys/types.hr #icclude main 0 assert (open "!tmp/noexist " , 0-RDONLY) >=O J ; printf ("open ok\nm) ; 1 assert a5sert.c: 9: main, Assertion 'open {"itmp/noexist", 00) >=Of failed* Aborted (core dumped) #%@# : -exit, exit, on-exit sx &a : int atexit (void *function > void> 1; i!lj#%~.': atexit ( ) M~iQZ-+RffiE#?L~i$VflM ~l9iZ1@~ %j!i!@~B~~ exit 0 %M main +%El@, @!& function EM$Z&@B2%Bflff19 &Em exit (1 23 BI B : t~%hf7&rh!rIi@lo, BYllifilEl -1, XytkRH@'f. ermo 9, #include void my-exit (void) C printf ["before exit () !\nn) ; 1 main (1 ( atexit (my-exi t) ; exit (0) ; before exit 0 ! a%@# . . : fork, execle, execlp, txecv, execve, execvp &# : int exec1 (const char *path, const char *q, ... ): d@#i#rJl : execl (> H3k&45P% path %'?f$%ff%Mft!4%$4t %Tsffki@B!i~% &$T%ft#W~S~ik% argu[O]. argv[lIvv****, &8-'P@&B5fiR 5% $f (NULL) @sRa : iff@% execve 0, /* wfi /bin/ls -a1 /etc/passwd *I #include cunistd.h> main () C execl {*/bin/lsa, "Isu, "-alr. "/etc/gas,swdh, ((char *) 0) ; 1 -rw-r--r-- 1 root root 705 Sep 3 13: 52 /etc/passwd #%a# : fork, execl, execlp, execv, execve, execvp %%A# : #include , #include main (int argc, char *argvtl, char *env[l 1 I execle ("/bin/lsW. 01~3, "-aln, "/etc/passwdm. 0, env) ; 1 rw-r- 1 root root 705 Sep 3 13: 52 /etc/passwd : fork, exec1 execle, execv, execvel execvp sx : int execlp (const char *file, const char ", ... ): &&la : execlp ( ) 5% PATH q%%f R%P&l k?kq&kk?%&%% file b!Iftff%. +fi~~J6@&~7%9~+, %G%%3UG~B&~~~Z~+~ W~V[O] argv[l]h*****. f 6-f $@B@MQ$$# (NULL) i s r : hznt-?rw;mnllimwra, nmawdrgim-l. ikanmar emo +o I" hfi 1s -a1 /etc/passwd, execlp 0 &$jf PATH 3f q&J /bin &$I /bin/ls */ #i~clude main 0 ( execlp I"lsn, "ls", "-al", "/etc/passwdm. (char *) 0) ; -rw-r--r-- 1 root root 705 Sep 3 13, 52 /etc!passwd I execv (#,~*#) I #ftP& : fork+ execl, execle, execlp, execve, execvp : int execv ( const char *path, char *const qv[] ); : execv 0 ~~~~~~# path 37+bBRZHBi+@I(P. 5 exec1 {) Bihz$~f execve ) R%F!j+&&, %1'P@&,%$~HBB$k!#dt4$24 %Rfi*f+o B B : ~D%&C~-&~~FU~&$&~B* %4T*aR1J&@GH-l9 5~EBGT errno +o $!#BR@ : tg$% execve ( >, /* Rfi /bin/ls -a1 letclpasswd */ #include main 0 char *argv[] = { wls", "all'', Vetc/passwd', (char *) 0 ) ; execv ('/bin/lsh. argv) ; 1 -rw-r--~'-- I roct root 705 sep 3 13; 52 /etc,/passwd : fork, execl, execle, execlp, execv, execvp %%*# : #include . . , . . . - . . . . . . < i sx a# : int execve const char *filename, char *const argv [I, char *const envp[] 1: $Raw : execve 1) H%&fi%& filename ?R$l%4t%rnX!+%$3, %3@% %~UMB%EH ~~~%WA~TXI?$-, @G-+@BRIJ~~-$&%~~~XI+~~@ wmBB&Q R B : 4~~~~7~~~~~~~2~H~ &47%%!l~JB@@fl-1+ %%EUG?emo $0 #gRa : EACCES 1. &~~~?HZ!+$RRHP~&~JHHE~ 2. %&firn24+fiR rn*i+%&EU nwxm 5ZBkO 3. @Ml71?!12fWt script S#Sig4E-&*#. EPERM 1- i?L@fiT#&G#$, &j?%#W r~t&l6!~ % #k4'il%~if~~% SUID 3 SGID {go 2. &#&??b!l*#fiBm*#%%EH nosuid 2z@ kg %$h,4?H*{qg% SUID 3 SGID @%, #!& V%#W&* root E2BIG 2Sw!k%fikg ENOEXEC X%3J@#fifiTi4H%472f+&&7 %qfiE@&8# i23mkGkqe&fiO EFAULT ENAMETOOLONG ENOENT ENOMEM ENOTDIR EACCES EIO ENFILE EMFILE EINVAL EISDIR ELIBBAD 2% fi~ename REm%R@Nkt@&qfi@qTs7%@. filename fi#BM?R*Mo B& filename J.?$*fitk5?gtJZl$4;@XEO E~L~hG$Eo @% filename ?R$tfiB.$W H 3Z8@#lk%5$ I3 go @& filename Wii @%~GJFI l33~~~Xik6R~ & lR+,% ~gmR%s@o R%$TH*i+Z@Efi%$2UR-miHEE&#%AS *Pwo uo @&q$iz0 C&$u%%Fh*iFrntT*2d$,Q+&* c$$u%%fi~i?@-B@fifik+~Rm*~+~etBo #&fT*#b!I ELF #4,~&8$- PLINTERP 9 E* ELF @%%%-H3a ELF @S#-RfelEo main (1 I char *argv[] = I "Is", "-dl" n"/tc/passwdn. (chau *) 0 ) ; char *envp[] = ( *PATH=/binH. O 1 ; execve ("/bin/lsn, argv, envp) ; -rW-r--r-- 1 root root 705 Sep 3 13: 52 /etc/passwd : fork, execl, execle, execlp, execv, execva ex@# : int execvp (const char*file+ char *const argv[]?: &&illy : execvp ( 1 24 PATH WU9tWiP85 8 h#%&PB file Kif #%. fi$li6CAfi%QB9 %6%%-+$% argv .KE%HiftWfis(J3~1+ H. : ~D%&~~E~R~J~%~~%H, h47%%N4&B&D-17 %@@B&T emo 410 #$%Re : ff@% execve ( > , /* iB5 execlp I1 EPrIWR7 A/ #include main 0 { char *argv[I = { 'Ismr "-al". "/etc/passwd", 0); exccvp (" 1s". argv) ; -rw-r--r-- 1 root root 705 Sep 3 13: 52 ietclpasswd #&a& : -exit, atexit, on-exit : #indude a : void exit int status 1 : @B#y : exit (1 H~iE%%%H~XiiEf!Zb9~fi~ #ES@ status %D%X%@+ Ti a~fi~m~~~~~2~~3~#~m~3mm2i+~ SME:Z i#@% wait I>, : exit, wait, abort %&*# : #include ex@# : void _exit (int status); &@#+lPiJ : sxit ( 1 M%2%ll@% El i3~@@&47, #eB@ status BH&2i%WI # #I'd*#m7fJBt3*!$0 ki&!&d~6;f;&%@, #H&fiE SIGCHLD E% %2BE7 XitLEWEh wait &&RBT%RE%#%o 3biHB:Z : it 1 2 0 , DEE exit (), : wait, execve 3ti; & @ : #include axa&': pid-tfork (void): maaM : fork { 2F$-+%HTi3E, X7B%&B%q2%Em@@3#&Q1q9 +tiwixmmajpit:aq, mtay, ~;B%B, ~~mfti+it~> I* H ?&$~BWELIW. Linux I'eH copy-on-write (COW) M, R$TSff+-- %@XB@&8B$UH2(4N$&4&&EmE%!a#3 @ TS!!5@#3WE,El ZB%Wi%, #4FtB#HM&G2Ml* KhkTB@*2%*BH@&NX% @%$+M$O &9\* ~~@~2%~~~t~m*~+~~~*~BrnE%a , nu 2 tLXBRk&4'iSs4eatT9 BR%5@PW22 @&%% (deadlock) %j%%%# (race condition) a #f : $R fork &~hAllltKjfl@25H%@2mTBBRH (PID). fi$&BP @I& Oo !dl% fork %Y!kJ!4J#%BH -1, %!%%H?ST errno tff * main 0 if [ fork {) == 0 ( printf his is the child process\nu) ; 1 else { print f his is the parent process\nm) ; 1 3 This is the parent process This is the child process a : pid-t getpgid ( pid-t pid ) ; a#Ba : getpgid ( ) HdF&@@% pid B~d%!%&$b!/~iP%~Rd !~uR%?% pid 3 0, J!1]k&B H ~~~m~z~BJ~o @HR8 : ESRCH ft4;3J7$&@B pid @$IB%@~ I* m# initl SjS (pid = 1) */ #include main 0 C pr-ntf ('finit gid = %d\nM , getpgid (1) } ; 1 init gid = O mg @j# : setpgid, getpgid , getpgrp %$&jr# : #include & # : pid-t getpgrp ( void : : getpgrp ( ) H $3X# 1 %BE%firnaiRR~J@~ &@BN%FflaJ getpgid (0); a Q : %@H%jteEfiEBfJIA%IJWo #include main (1 { printf ("my gid = %dinu, getpgrp I) 1 ; 1 my gid = 28546 : : fork, kill, getppid sX@jdaJ[ : pid-t getpid (void); #include main (1 i printf ICpid = %dinn. getpid [I ] ; 1 I pid - 1494 /* @%a?<%%%$$-Zflfl */ & : fork, kill, getpid R a& : pid-t getppid ( void ) ; @ja~w : getppid ( 1 ffl%%l49 W %8+2&1XiBf2iERrI6% #include .. . .. . .r main (I I.. pxintf ("MY parent' pid = %dinn, getppid (1 ) ; 1 My parent' pid - 463 : setpriority, nice gx : int getpriority iint which, int who): &&ij&w : getpriorityOqffl%R%%& i&$%l@HP ffi8$!i!&fifi%@o @#!! which ~ZVBB @a who WR aamsx: which who jt%&l%%x PRIO-PROCESS who JJ PRIO-PGRP who h%f3!!l%L$?#lj@ $!#@R@ : ESRCH $#I which 8 who Til;fk%%, fi&$~fl$?-%~%!k@O EINVAL @a which Egg, : setjrnp, siglongjmp, sigsetjmp %**@ : #include ax@& : void longjmp (jmp-buf env, int val); , : longjmp04&RrlZ h setjmp()@ft etJ#&4;&, %GNk%Bj setjmp02G% %@Mt!t%#LSo B& env % setjmp 0 WiB#&@64;%, BB vaI iR setjmp ( 1 KtBEIL, &PF"J'% 0. 8% 0 %%Q $3 U 1 4Clito BHB:X WdalR : longimp ( ) siglongjmp () 6$@E;1;84AB8, f @t;l;B%H. #include jmp-buf em; test (1 { printf ("Before longjmp 0 \nu) ; longjmp'(env. 1111) ; /* ifii setjmp (1 1111 */ print£ ("Aftex longjmp (1 \nu) ; 1 main ( ) 1 int val ; int i = 1234; if ( lval =sstjmp (env) ) != 0) I print f ("longjmp call! inm) ; grintf ("val = %dr i = %d\nu. val, i) ; return; /* Mmain 0 p&N. $%B$ */ 1 i = 5678; test 0 ; Before longjmp (1 longjmp call ! val - 1111, i - 5678 &x&& : int nice (intinc); e@aY : nice 1 H~$~~~$~@B@%WR%JIE@~ %@ inc !&E@MUfi%IlR@ #FB%GKI, EP%%iE@fi4'i2@Eo R%BBmP$fiE@H&m inc itBw%m@flF@3m~ 8@&t?+@WQ a @ : @!$&%e&j~fllJBH 0, BglJ&D -1, %~~~~Terrn~~~ #%$fife3 : EPERM ~-.-&H,%kB%ajjfi4_B% inc ~&~~#F~%I~~@o @%&a : -exit, atexit, exit gx &a : int on-exit ( void I *function) { int , void Vq void %rg ): &Bki.%M : on-exit ( > H%iEE- -+@@X$$%%Ti;lfiH&$~&~ 3jT@B3$jffl exit 1 C!fi main $6HWp BB function fi#$$R~ij#2k@fiH, #$%$ BIE B exit ( j %%t5@. d#tr arg BHB14%BL function H 8. #I tgi32i8am11 #i~clude void my-exit (int status, void *arg) { printf ("before exit (1 !\nu) ; printf Ibexit (%dl inm, status] ; printf harg. = .%sinm. (char *) arg) ; 3 main 0 { char *str = "testM; on-exit (my-exit, (void *I str) ; /* str ~$fj$fi!$ my-exit: ffj arg $gft */ exit (1234) : /* 1234 &4fg my-exit status @& */ 1 before exit I) ! exit (1234) arg = test & : fork, wait, signal ex a& : int pace int request, int pid, int addr, int data ): $&iaI : ptrace () B#@MIR%iklljlt@%N'F.BE1$gEQ #PEHf LW*T & 2 BB request aj?R!E$% miumawas.: PTRACETRACEME bki?k@#j &28@&f$ PTRACE-PEEKTEXT fi pid F8,@ft!l(1@Mkt: addr +$m-/I' wordo PTRACE-PEEKDATA PTRACE-PEEKTEXT, PTRACE-PEEKUSR M pid ?%@& USER B#hit!lkt addr 'f' wordo PTRACE_PUKETEXT #data~hpid~~~#~fi@M~addr~, PTRACE-POKEDATA PTRACE-POKETEXT, PTRACE-PUKEUSR fi data 3A pid USER E@R&ik addr 410 PTRACE-SY SCALL %$k pid ?8%!&kfi0 ETRACE-CONT PTRACE-SYSCALLA PTRACE-SINGLESTEP gB pid ?%@~@$~%@6 6 PTRACE-ATTACH m#f (attach) pid PTRACE-DETACH %& (detach) pid 'F8Bo ##i$Ra . . : EPERM (h init) %fE$m%@E%&6%&80 EsRCH pid fi%$%l~@~%~o EIO 8% request ;f;-S&o /* H%SG7%E, E% write 0 3i%PJH#E%%~8BB */ #define attach (pid) ptrace (PTRACE-ATTACH, pid. (char *I 1, 0) #define detach (pid) ptrace (PTRACE-DETACH, pid, (char *} 1. 0) #define trace-sys (pid) ptrace (PTRACE-SYSCALL. pid. (char *I 1, 0) #define traceme ( ) ptrace (PTRACE-TRACEME. 0, (char *I 1, 0) long get-regs (int pidr int reg) long val ; val = ptrace (PTRACE-PEEKUSER, pid. (char *I [4*reg} , 01 ; if (val == -1) perror ( 'lptrace (PTRACE-PEEKUSER. . . .I " ; return val; 1 void print-regs ipid) { struct pt-regs Regs; Regs-orig-eax = get-regs (pid, ORIG-EM) ; Regs,eax = get-regs {pid, EAXI ; Regs .eip = get-regs (gid, EIP~ ; if {Regs.orig-eax != 0x41 return; printf ("ORIG-EAX = Ox%x , EAX = Ox%x. . Regs.orig-eax, Regs.eax) ; printf ["EIP = Ox%x\nw , Regs ,eip) ; 1 int trace-syscall bid1 int value; value = trace-sys (pid ; if (value < 0) perror ("ptrace") ; return value; 1 main (ink argc, char *argv[J) I int pid; int p, status; if (argc < 2) ( printf ("sage: &s program\na, argv[Q] 1 ; return; 1 if ( ! (pid= fork (1) ) { if Itracejne I) @ : void siglongjmp C sigjmp-buf env , int val : @&i#W : siglongimp ( ) gZEi2Gh sigsetjmp ( 1 i%W KI@&W%, EBDbff9-l sigsetjmp ( ) Go %& env sigsetjmp ( 1 BB@lltl# kk4;%, 28% val sigsetjmp i :I lD&H@, Itt;!'fiVJ% 0, 8% O % %+naLJ 1 *RRO iE%% longjmp ( lo mR@B : longjmp, siglongjmp, setjmp ex a # : int sigsetjmp sigjmp-buf env, int savesigs): & : sigsetjrnp I ) 2Z;X.G H 14+34;%, % -+ fi&E I~X~~~~~ siglongjrnp C 1 ~@2B&~liB~~ii%~iZ~i~~~ %G&kS @fig;&+ %t%@lF&4ro BB env %M~l%+Hf5;J%&4;%, -&*$I jt14YZ%go @!& savesigs EYJIF O ~~ZF!IR$?~EI&I?B?%~&~-ER# 8. 3 sigsetjmp 0 %D O W4tXELi$ME9, Z&HIb O 4RQQ siglongjmp ( 8h%HX, i?%ltaH,t%% longjmp ( ). B Q : 0 I~KE~XGWHB9&~% Rfi@W# siglongjmp ( 11, %% D4F 0 EII4tWI siglongjmp (1 f El. W&l#W : setjrnp t > $D sigsetjmp 1 2+@@$B+hB& i~J.$E$~@ElQ I system (&fi shell a+) 1 : fork, execve, waitpid, popen & $J gSgl : int system ( const char * string 1: @&mw : system ( ) SflM fork I ) P*T%$, &?BE%flm Ibidsh -c string % %IrS.%% string Sf3 Pfi4%-K.B(II$. I~&+&%?~;~~$RV&HHNR Ei a#$. &%lM system (1 SIGCHLD ~~~%~Ilf#iJ~, SlGINT @ SIGQUIT EB Rrl&@,&BI$ a B a : fa% systemOblM /bin/sh #%%NU%@ I27,3fi&%@6BAd%a -1. g&& string ?~3?-%%f (NULL), !ll%~~k%#, ~IPR system ( ) fiM@i JhJ!~&~~~~#4~~ shell !H&k%H@&fiq&% system 0 1H /bin/sh %Bfi%HlYJ 127, M#k@kfli&R%B ermo *@I %&!7bk~ho r#@ala : &%3R SUlDiSGID @PBMER#iS@@H system ( j, system { 9% *%%%B* a2%%qBqbE2gEg%s*m(dgv #include main 0 L system ("1s -a1 /etc/passwd /etc/shadowm ) ; 1 -rw-r--r-- 1 roat root 705 Sep 3 13: 52 letc/passwd -r- -- - -- - - 1 root root 572 Sep 2 15: 34 letclshadow msfiB : waitpid, fork aai#g : pid-t wait int *status): #include #include #include #include main 0 i. pid-t pid; int status, i; if ( fork () =- O 1 I printf ("This is the child process, pid = %d\nl', getpid () ) ; exit (5) ; 1 else { sleep 11) ; printf ["This is the parent process. wait fox child . . . \nu) ; pid = wait (&status) ; i = WEXITSTATUS (status) ; printf ["child's pid - %d, exit status = %d\nM, pid, i) ; 1 1 This is the child process, pid = 1501 This is the parent process, wait for child ... child's pid = 1501, exit status = 5 : wait, fork a&*# : #include csydtypes.h> #include : pid-t waitpid ( pid-t pid, int *status, int options); @i!ja#W : waitpid ) Id %%$21%&j7T l%~fi~??%%~$?%f9%&o ~u%$E $JM wait 0 W?i&%Et+%%, wait (1 &Tflfl%DTBE%%R& Eio ~~MIFIE%%&B~~~SB status ZE17 mT%@mS@iEEY&& $2 -&if@l0 !lfl%$&&%@k&@i7 Bl]&@ status qU%Jik NULLo @& pid %H%RRT%E~,D,RIJ#~ $Uh#ffi8Xfr~-f;: pid i -1 %@Sf$@i,!!%]R% pid %~@~!3~~~~~0 pid = -1 $$e#fiT%@, $P,f- wait Oo pid = 0 %#8~@i%jfi95 a 8i!H!WW~ff {V?f?@o pid >O %#f H~-B@~P,~~IIW% pid I%?%@. @#.k options qU% 0 %Tm8fi OR $He: WNOHANG $Jl%%?fff flE$3E$HTBBRlJ3k&D7 F?%e0 WUNTRACED @ I!%$%#&$ TB20 TSE@%%%&&BGGF status, 6TGA+%Til$~RI]%%fSE: WlFEXITED(status) ?l~%?%@iE$!$%!i!~%~bO~~ WEJUTSTATUS (status> @fFfB@h exit ) BBm5$%4t@, -&2 %ffl WIFEXITED %811@2EiE%%%&iE1$M l4z 0 WIFSIGNALED Cstatus) @%?%@@~%fi?~%%~l/k%~%~o WTEWSIG (status) k, -&%% H WIFSIGNALED %$l] @%;rf@fflk%* WIFSTOPPED (status) @%?%@&~%@&fi~~~!k~@%&o - #!J,%@H WUNTRACED Fbf&%k##?Ro WSTOPSIG I status 1 &@~li!i?%#E%@@~~~R#~ -&+%M WIFSTOPPED 3k#1&E8@33kZQ a H .I$ : ~u%fifia~~a~g~~~~izg;I]a~ cprm, BR*%~KB+N~J~~-~., x~ L$H;Tfif+ ermo CP. t-3 igB3$ wait ( >, : printf, fscanf* vfprintf %&&# : #include &X a& : int fprintf (FILE *stream, const char *format, ... ); aB%M : fprintf () 2EBB& fomlat ?7$*%9fiR#&st:kB@, KG%%%%& 3JBB stream I~ZII~X~PP, Bgfl WW+@%% {'IO') %kQ a # : #F@B format %R#rn&3iSg3 printf &@jJ!fl&lEl%bi$&3!(&? R@* %%R~JaH -1, t~~RB@T em0 +o /* fprintf (stdout, ...... I X$j$@#$ig& (stdoutl fi$$j$ *I #include %SMfJ9 %-'P!&?&%?.h #,6i!, h~B?E44&,6G, fi@%%N%-+ U~~W~!I e %S%o %E -5 %e #85mB, %-KRIJE~E@@%%UI~S~ E #S%%o %g bgdobule @E@H$Q $l%BU %f 3 46e tlCJ%3*flf-P9 #&%%ER E@nm$ClBBBH$BmR%@%*R$a G a cg nna w, %-ER~~GU%#%&~~EPF~~&#%E #PIX. + +R&PRb: c E$&m@%&#!i@& unsigned char zfl@lbo %S %~~B*PJ$#~S@BF%~, B31%@ NULL 7fi%ko %p ~u%B$@E 'void *' @EWfllJ@N+h%%!&SE%Q prec ~JL#laB: 1. iE%&HBfJhi$Bo 2. Z@&8&P4t%~I~BEB 0 3. i7 %g @34t%*HnBmsABQ 4* a %s &&4t%?R*H@AKEo 5. 2% * R9N~Ji%ST+SB@%~AEEo width %@Bb!JS,d~EE, SLkF#qF#!@, %E * ?$?+ PJZ%UT- +B&%f@$@KBo + flags fi7;HLff#N: - ~R;fz~%-%~I;i]z~~~ t -&$E~~EPRBI% printr (1 2~aW-+W$, +Xtc9iJ+Daff fitPo lk%#Z&@@&~T€PZ@3~-+Z5 (40 # ~&$~2~~#G%%~B~+~~R~~~Xo %E%%% 0 2% OR %#oh @ll~?z~E~~~~%!&~~$~--CiC 00 ma%@% x 2 a OR %#x) @12$lTEPt7?-iB%J&@B&EP 'OX', @%%WJ x !!IJ W%fl@ ox'. &!I!&% e, E, f, g @G .t$f~I]453i33&lfiE~/Jh , BRZ% g B G 2R~81M@f%l~J\&dGW/l~BfiB%EM 0 %Kf~%B@Fr? Z%+rn@%%?bll Ob %%B%rnkB+%? EfrH -&&tT~Lt!trB%Ra #include main (1 I int i = 150; int j = -100; double k = 3.14159; print€ ('%d %f %x\nA, j printf ("%2d %*d\nW, i, 2, i) ; /* +& 2 $$RA%$ * p, %2d */ a& ': int scanf I const char *format, ...... ); . . U~B7B&RmB&%B##B%, ih % -5 type RflE&GZft% * ~%~%NEFJ@&BR,B@~~%~O size Afti?SBMhmBBEBo 1 %A b%@#$%@I kl long int 2 double h $@hm!!$#$!$!@u short int @#@, &7;fitfl type #LH%ji: %d ~A#J&%H&%&-%B9@+%$!&% cintlo %i MAm&B&@%B-*RTmtB$UB?, %i$A%#!2 "0~'~ 3 "oxBr ~~R~Cf#?+~~$ll@~~ Z U "Or' P%R~l%RI~3%!&% #4h1t2R4t2? t%$4 0 %o #AB@@$%$%#&-%P+3&){8%II%% (unsigned int ) %u %h@%~~#9f#$t@-~f'6~b'!JE@& ( unsigned int l. %x #$A&%k~%Z~-%&t~%i$1]&~, $$@%l&'?T unsigned int %$go %X Iq %KO %f $itiAN#B%R@9H@,&@&+ EfiEFF float 22Eo %e O %fo %E H %fQ % El %fo %s %ABB%!22&FR%%k~F@@o %C hih&@?~@-?T% [I i%@lBBi!3Rfii?B$F'i~?Bo PR [a-zle fA] BR%BWFfciF+%?m A R+GBcJPBttlJR, b iA@91. % H B : &JhN%EIB%%H9 %!?iRlJ2iEI-l, %iEBHGT ern0 q0 #include main (1 i int i; unsigned int j ; char sI51; scanf (*%d %x %5[a-zl %*s %fr, &is &j, S* S) ; printf ("%d %d $sinh, i, j, s) ; 1 10 Oxlb aaaaaaaa bbbbbbb 10 27 aaaaa / * Oxlb %%%a 27, b sR$*/ : printf, sprintf %&*# : #include @a : int snprintf (char *str, size-t size, const char *format, ... ); @&aB : snprintf () &R@%'Ctfi fmt ST+$l%E###3R%B9 fiG#%gB $lZB@ str %tB@~7+@Btl, B$fl $tX~R@~%( 70'1%&2!lBZk size %emA/J~%ko KT&& format SE$ IB%ZiBB% printf ( 1, #include main () I char a[] = "This is string A!"; char buf [801; snprintf (bufr sizeof (buf) , ">>> %s <<<\nu, a) ; printf I"%s", buf) ; >>> This is string A! <<< mx : printf, snprintf &x @a : int sprintf (char *str, const char *format, ... ); i@&#W : sprinlf () +MLD& format SfiO%fftJe#&$k&B* tGBGJiCB $llHB& str RBmF@@@a, BWtm~R@%Jk (7fl) %Lo #TB@ format ?-t$@rn&$iR@3$ printf (1, B H B : &~h!!~~~HB!!i str ?R$EE, %Y!WJ&H-I, %iEL$iHfiT ermo q0 #include >r %s <<<\nV, a) ; printf (*%sq, bufl ; >>> This is string A! &x a# : int sscanf (const char *str, const char *format, ... 1; : sscanf 14%BL str &SR$$B$t format SR@ft%%#&$&& &$EB%Z%B% scanf lo %#EH%%GTHEH@Bfie 8 a a : a~hR~l%a%B% El, %%RiJ&D-l+ EBBrn8F em0 +o /* T%%% scanf 0. . 2EEfi$$ibdii9&&%WZ?fie)r input [ 1 */ #include main {I I int i; unsigned int j; char input[] = "10 Oxlb aaaaaaaa bbbbbbb"; char s[51; sscanf (input, "%d %x %5[a-z] %*s %fn. &i, &j, s, EL) ; printf ("Bd %d %s\nw , i, j, s) ; 10 27 aaaaa /* Oxlh %$.)$el$, 27, gB-8 b ?73sf / @%&& : printf, fscanf, fprintf, &x gSgt : int vfprintf (PILE *stream, const char "format, va-list ap 1 ; &#k#M : vfprintf () &#B%$II format %?*%%%##3R#%I %G%&Sa% ~3S$& stream ESm*1+$, BFWRTR$%% (70') %k0 37.8 %!i format +R* tY1&3i.@B% printf ( ), va-list M&i%@%fl43 C 8 vprintf ( 1 $i$!I. B : R~ht18118851!htlBSR RR@IJ%EI-I, PWAIB'f emo $. if@$% fprintf ( 1 B vprintf I 1. @sj&a : scanf, sscanf, fscanf el a& : int vfscanf ( FILE *stream, const char *format, va-list ap 1: a&#&J : vfscanf r > 2 h@& stream M*d+;ijfi$2R?R@, BMESB format F 7+$~%&#%S!kB%o &%%@%&igB% scad H#%IYI%% 6 va-list MEi3Sf C 3 vprintf ( ) 5ZAJ.. a M & : E?.hJ!MiElB&BH, %@J!J&B-l, %iZR!ZI?7+ errno +- tg$% fscanf ( > W vprintf ! ), : printf, vfprintf, vsprintf s%g # : #include #include &#&a : int vprintf (const char *format, va-list ap): ii&a*&j : vprintf () #H%D printf 0 mu, B@ format &8&BM0 va-list A+Z+Brn@%FU, rn~&Efidi8S%rntZ c, a Bl : &J~FY&E~~L%~~IYJ~~~~, %&N~j&H-l, +B8EiH@T emo q0 #include #include int myjrint f (const char *format, . . . . . .3 I va-list ap; int retval ; va-sta~tl Iap, format) ; printf ("myjrintf (1 : ''1 ; retval = vprintf (format, ap) ; va-end {ap) ; return rekval ; main 11 i int i = 150, j = -100; double k = 3.14159; myjrintf ("%d %f %x\n", j. k, il ; mygxintf ("%2d %*d\nH, i, 2, i) ; 1 : vsscanf. vfscanf . . . . . . . . a**-# : #illclude wdio.h> a&am : int vscanf (const char *format, va-list ap j: vscanf 1 +%%ABh@ERRS% format %T$@At%#%#&S#&Eo &SR@ESiS$+ scanf )a %%G~!J%Z;~SFHEKJ@& 13 va-list H %S@%H$f C % vprinif ) %#!I %l. % : &Jh!!l~&~&!k#~, %%flq%H-l, @i!?Rfl??? enno is$$% scanf ( > 2 vprintf lo : vprintf, printf, sprintf : #include cstdio.h> &x : int vsnprintf ( char *str, size-t size, const char %xma~, va-list ap): i&&aM : vsnprintf () ~#!@iBR format FFf*%R###&3,~%B9 %Ei%i%%! B%49JS& str fiEW?R@%Q, BBJ~JR?R@%% (7Qj %%ZUB# size %$&k~Jhj!~k~ XF@% format 5fl$~#i&i7!2%~ prind ( ), va-list 83?&i8@%MER C 3 vprintf ( ) & : bkxhyd&D%!$k Sb ?fl$EE, %&@]&@-lr %%Em$f? em0 $b if$&!$ snprintf (1 22 vprintf ( )o .)flk@& : vnsprintf, vprintf, snprintf %A*$# : #include tstdio.h> &a : int vsprintf (char "str, const char *format, va-list ap ): @#i#lJJj : vsprintf C 1 %@i$?ik format F@$lAt@l%#&3RWB~ #%%%RR $W!J$@ str J%%N?R$&$H, B3MR%7-*~% {'\o') hko %k format TR*KJ&3,iE23% printf ( )* vklist Ffj?&i?iS3$M;f$ C $, vprintf i 1 %#I. ifjB% vprintf ( ) vsprintf ), % @ : vscanf, vfscan f SX a& : int vsscanf I const char *str , const char *format, va-list ap 1; i i@-8 ..... A. i: vs6cad 1 HWB sn &%149@1B# format SR@%K##P&fi&@. &$%R%ZiS@% xanf 10 %&G&%%GTN@&S%& 0 va-list HEi@$+fl4$t C 3 vprintf { 1 %PJ %@5$ sscanf ( ) W vprintf ) 0 : m%@& : stat, open, chmod, chown, setuid, setgid &x a & : int access (const char "pathname, int mode ): 8&8M : access ( ) e&&zB$u@/%?&-&@&ffJfr4q0 @@ mode g,%@f@R gfl$, R-OK, W-OK, X-OK @ F-OK, R-OK, W-OK 5 X-OK @ A F-OK RllEaj%9J&%Z# BBBB- @T access 0 Rfi~R&&S, #+B%Ej+E&S*i4h%I Qdk, $II%-HZ~$?%% "VqA (~rihbleI'', -SZqhli%ijidjt+&i~i% f 4% B E0 VIPn9 .t%&EW ms tt!! 3ri+BR% 'Flhjj Cexautable)" @lEP @a] execve 0 #I,~~WR'~&%N~~ @ B QP : 8B%'tikBC~.WIF#%2T@BFISBHQ@, %%,&?h, P,Zt-@I(R#X ELL: (denied) !!!&a -1 #B.fea : EACCESS &!I pathname %@Z@TiB;f;A%FsiS$a!3H&Ro EROFS @@l]%%h&~~@~.fY@%?Ri%%!~%%h EFAULT B@l pathname %!#a 8 RJgR h' 6P ElNVAL %%!? mode qiEl%j0 ENAMETOOLONG Z%@ pathhname hK. ENOTDIR SB pathname %-El%. ENOMEM &d$J6$,zo ELOOP ,@ pathname fij$$@%%fgafi0 BIO vo fiR$EEo #$!@PI : CH access ( ) (@RIP iliiiE3l ~#~~~~XI]~J~~L~f MilB~rtE access ( ) h3@ I& open () BCldjCW~in~~~%$i%+k~I.lEO #include Lname) ; +. +gnome . gnomejrivate ErrorLog WebLog bin boot dev dosc dosd etc home lib lost+found mi sc mn t opt proc root sbin tmP usr var total - 24 a : int chdir ( const char *path : : chdir ( ) M%%F%lfil@1!$ H~&~l;kk&!& path fii&&l a 3. #include main ( 1 { chdir {"/tmpH] ; printf {ucuxrent working directory : %s\nn, getcwd (NULL, NULL) ) ; 1 current working directory ; /tmp : fchmod, stat, open. chown &xaa : int chmod (const char *path, mode-t mode); -- #!1]8 : chmod C) mode @B%!%kz path %$ft#m@Ko mode gTFr]&~a.$;: S-ISUD 04000 *#b!l (setuser-idonexecution)e S-ISGID 02W *{$m (setgraupidonexecutim)~ SJSVTX 01000 *#H sticky fl S-IRUSR S-IREAD 1 00400 *~+fi%%Rg@&&R S-IWUSR ( S-]WRITE) 00200 ft#fi%%s7iS% S-IXUSR S-IEXEC 1 OOlo(l *l'qfifi%Rq&fi&m S-IRG RP 00040 Iv%liRiili$&&B S-IWGRP 00020 HME@J3A$XIR S-IXGRP OOolO ~~f~&~~??&f~ S-IROTH oDlO4 3E.4bHPm%4l&P€4 S-IWOTH 00002 #fl!?APgq%A@PB S-IXOTH ooOOl #4tMP~rrl&fi@~~ fl%%*#b!6683 (owner) E&fi?&fdP%#lj@ (effective UID> 0, $ q!2@&%2fk&IXo 2%7%%%&* @W%%%.@%hh~fi3r:l+v fiC5 fijSfti$&?% S-ISUID 5% S-ISGID !!l]~~/i'fl~#@%d @I%-W Zfig SlSVTX 42@Bl3, @$$EJ& H%TF?R?%*#b%?R8$, root FJ UMlR%Zi+ (9 /tmp lo iki H : l?X~B&%@~Y~%H 0, %%El -1, %i%EIA?Z? ern00 @sfi@ : EPERM B@~R%MP~RB~R%%@&R~NZf~R%-%T H, rnH&+8 mt @Ro EACCESS 23% path Wi#8%Plffi?%%~&0 EROFS @%A@~~*f~@8?~~*f~%%ha EFAULT 4% path f#tk8a%'?@h@qlqo EROFS &5A&E~X~~GETR@fc:!T%%fi EINVAL 8% mode ;f;iE.6Wio ENAMETOOLONG 9% path jd5, ENOENT #kem24+5#&. ENOTDIR f & pa& %$#4k-- I3 2, ENOMEM &)w!j ?mL ELOOP 8% path ~m~$s%rda. EIO Yo &m%% / * % /etclpasswd %#$!Riga S-TRUSRl S-IWSRI S-IRGRP I S-TROTH "/ #include #include main [ ) sx a : int chown ( const char *path, uid-t owner, gid-t gmup 1 ; @!il&r%l : chown ( ) %%@%k path %~ft##Jfifi$iQE%%& owner l%%LhHP, ~~%%A~+IKI~.%&?IB& gmup ille !IU%.@!$~ owner St group % -1, *ErnFhG~StA+&8fi&%o root %z4+%fi%gq&3*{+tB 9 (Hfi fi*&@&@%! group $lb!lafl 3 root M chown ( 1 &$EfC{?fi6%% &W, %Z#SR S-[SUID 3 S-ISGID @PI$, !rI%%RJk&!I%!~* k9blfr~ %87% S-ISGID &m@$$% S-IXGRP E. Wl]$X#%%r]$?ha, @32@@4 @H.fe@ : EPERM 8@@KBfflPiRR[IR5@@&REmAf?mfi%T fliWil&+R rout @Pat, 8E8B owner/grwp $Z@* EACCESS %& path fi)&el?!l*#%%6@! EROFS %3AIBZ~+GG+Ri32i+%%h o EFAULT path %#~~~~@!h?5~rR7d ENAMETWLONG path ENOENT t%z~%f~~fi?kQ ENOTDlR $% pat. $Zi@#~k- W3. ENOMEM &~Cbfil?$zo ELOOP $@ path b?&l;fT?@l'@h El0 YO /* # /etc/passwd HPfi*$$Pg@g% root */ #include main (1 C chown (' /etc/passwdh , 0, 0) ; 1 ; chdir ex : int chrmt ( const chappath ): g@fi@ : EPEIiM EFAULT ENAMETOOLONG ENOTDIR ENOMEM EACCESS ELOOP El0 @R$h!+ %g&g@H30 8% path fE%tB*TilB%fil%Ql'4 B% path kEo %$2+mHs*BfB@4FaEmH30 @~L~I'i!fi+,5L GRa3H@fi%h path d~$??%g#%#Bb vo GrntfiEo main () I chroot ( m/tmpm ) ; /* a%#?HZi2a% /tmp. &T-fitJl#Wj / S%LgW3#HIJ /tmp */ chdir {"/") ; 1 : #include #include ex a g991: : int clmedir I DIR *dir); @&$&!!d : closedir #b@S!& dir fl%g0 & El # : %HbkJhfll2iEl 0, k!?k&B -1, ~#~%BGF errno @H.fea : EBADF 8% dir a & : int fchdir (int fd): IBMW : fchdir 0 Iff#@%bfiI#Hik&%&U%#fd %%m%4$fi%d4 ilS Dl @ : %4?b$%hJ!d5iEl 0, %%%El -1. errno X%S4t@n #include #include #include ifcntl.h> #include main 0 i int id; fd = open ("ltmg", 0-RDONLY} ; fchdir [fd) ; print f ["current working directory I %s\nM , getcwd NULL^ NULL) 1 ; close Ifdl ; 3 current woxking directory : ltmp mg~& : chmob, stat, open, chown zx@ : int fchmod I int fildes, mode-t mode); S&ww : fchmod (1 &#%% mode #X!k!3k!@&%@ fildes fifkft~~k%X~o @3!1 fildes % CFlH2#lB21Y6%flO 8% mode i*$$$ chmod 0, : EBADF g% fildes %~?%~%#-%~flo EPERM %@&%%kHP i~$~1@-~%#&~~&*~~fi%%~~~, $B &$R root @PRO EROFS 8~~A$~~~*~~&~T~i%~~~%%h a EIO I/O @atgizo /* /etc/passwd S-IRUSR I S-IWUSR I S-IRGRP I S-IROTH &J.fXm * / #include #include main (1 \ I int fd; fd = open (M/etc/passwdu. 0-RDONLY) : f chmod f d , S-IRUSR 1 S-IWUSR 1 S-IRGRP I S-IROTH ) ; close (fd) ; &%a& : chown, Ichown, chd $a ; int fchown ( int fd, uid-t owner, gid-t group): Si$iBYl : fchown (1 2%@@ fd tEBftf+Nfi%%9E%B& owner ~~BKIBIP, m%GR!+8tll%&%B% group a. ?ID%%& owner 3 group ;b -1. ~~@?I19%~r$8iII;T~&GPfi&~0 SB fd %i3~TRK1X~~~i&fl~ 3 root fchown 1 &%R!$J$j%$%gll$, i&$d$gR S-ISUID & S-ISGED & PFt J!l12ifiR&$XBF4Z0 H @j : JZJ~F~MPI 0, %%El -1, tWRHj'?T ern00 $&aR@ : EBADF BB fd Z~$%Sd%%B&S82i$E%Ho EPERM %@~%%fl~~~%~%%&#&@~~*~~~fifi%$~~ fiH&$R rmt %&is!& ownerlgroup $E6ho EROFS ~3AM24+G&+~&*i+%%b ., ENOENT ~&~~~~$$~&o EIO uo mQ+iiz* /* $j /etc/passwd ~fi~~$~fl%i~% root */ #include #include #include ~ fstat I> S stat 0 R83%4?#HH1 +Elfi8;f.f%-hHBB 3 EfiRR2l+@Sfi %i%S% stat I >, : &E~BJ~J!IJ~JEI 0, 4iRkZlEJ -1, +ii24t,RG+ ennoa /* Ef6 /etc/passwd P#AIJ! */ #include #include #include sx : int ftw (const char *dir, int ( *fnl (const char *file, const struct stat *sb, int flag), int depth): @r#%!4 i~ ftw () %M@B dir %?Zff!J HSK%, E7;-EE&$Y3%%fiTH3e @iIfA-+@likt @&,QH@& *fn ZX@@%%&B0 ftw (1 2BZ? @%!% fn ( >, %-+$!!$ *file %~+$&Z *sb, ;911 stat %&!f$+f ~&&ZXi%%% stat (OO, %3%&%@kl G -FmhHfleiifi: FTW_F --&*4+ FTW-D I%% FTW-DNR 6m2@bWftd ~~%%lN$$#%4fia FTW-SL fl92EE FTW-NS %@3X@ stat mG&#, @~%~&~l"~o @g-+@!& depth ftw ( @B$Yafi fl~lf$~~@fifi&~#%l0 f -%- fin%%fiWHET depth B%38.5~B#B H, P/i'%h%B~%~XX1Y@Rfr:.4+##fi~B 0 ~u%ZSG& ftw b9%fi9 ft~ (j RS&H--4F%@WFJ+ &EHIFl&% 2 ftw 0 lt9&EIBe BRIl ftw 0 &X%Z%fi%hH%k, RGZH O0 a H a : 8fi+b!fl2rn fn 0 @@&BBE? *BBh%F'J%fl 00 E%%iXE *PIJ%EI -10 #include #include int in (const char *file, const struct stat *sb, int flag) I if (flag == FTW-D) printf ("%s -- directoryinw, file) ; return 0; 1 main 0 I ftw Ih/etcl~L1", fn. 500) ; 1 /etc/X11 -- directory /etc/Xll/wmconfig -- directory /etc/Xll/twm -- directory /etc/Xll/xdm -- directory /etc/Xll/xdrn/authdir -- directory letc/Xll/xsm -- directory /etc/Xll/fs -- directory /etc/Xll/applnk -- directory /etc/Xll/applnk/System -- directory /etc/Xll/applnk/Graphics -- directory /etc/Xll/applnk/Internet -- directory /etc/Xll/gdm -- directory letc/Xll/gdmlSessions -- directory /etc/Xll/gdm/Init -- directory /etc/Xll/guichooser -- directory /etc/Xll/xinit -- directory @*a& : getcwd, getwd, chdir a& : char *get-currentdir-name i void): a@Ng : Lk@%!e&H--%R@BK &m€l5mI~~H3%N~43?8*0 % #i : &8%~hFlj&~$~~~~%~$%#0 NULL, %@fifi$FF emo #include main 0 I char *ptr; ptr = get-current-dir-name I) ; printf Incurrent working directory : %$\nu, ptr) ; I current working directory : /tmp a & : get-currentdir-name, getwd , chdir ex@#& : char *getcwd Cchar *buf, size-t size); : getcwd ( ) +ammu$ a JitaHntcsmrzym b~f ~t~m AAQEJ. @@ size k buf IB~~~IA~J~, &flaj&&%N, buf %BKII~@~P~!EE &Aq ZIfi H 3%N%f+ffi?B*E@B2BB size A~JL~ FllJBB NULL, ermo mffiR~J$~ ERANGE, #$$&@ buf %I NULL getcwd { 1 & E@& size KIA!]\ $ %?JBZS fi G ( @A malloc l) ), $URS$k size &% 0, rll getcwd ( ) w ft%X.f%42BCJ%R$K@%&ZRiRi~hGk 2H%J!A$.@H%k?7$$G$!lH free ) %B&kQSflV iQi El 9 : &G&J~R~%%XB%I]$IJB@ buf RPKJIAJGP~~~~ 3%5Hf188123#~ %%&a NULL, f88jt5q@? emo. #include main (1 i char buf 180 1 ; getcwd {buf, sizeof Ibuf) ) ; printf ("current working directory : %s\nv, buf) ; 1 current working directory : ltmp : get-current-dicname, getwd, chdir @#cikm : getcwd ( ) g%%%b!lI# W W%%k8@$3fll$l~@ buf %%@J h@$fN, %G%&%49?R*1B#5Bo NULL, %%i main {) I link ("/etc/passwd*, 'pass") ; 1 : stat, fstat, chmod, &own, readlink, utime s5C; gfi : #include #include tunistd. h> ex fi : int lstat const char *file-name, struct stat *buf 1: a&am : lstat i > 5 stat (1 #air32#iFJ, #BER#%& file-name fiKib!!Z#% &, Xf %@'f, 321+%4$-44@ (symbolic link) N. Istat 1 4% Bi.% link $4 m8&e i$Q fi %i-BB?$ stat ( >* jg a B.: %17&~RU&H 0, %%&El -17 RiE-w$*T em00 igs* stat ! >o .: int nftw (const char *dirt in1 (*fn) (const char *file? const struct stat *sb, int flag, struct FXWl *s 1, depth, int flags ): &&a.&l : nftw () -4 ftw () ?Rf@, @EM%# dir %5?stlH3Rk, i$T;-EEN BYI$BET @%A-+H3, @&NMBB *fn 5i'XH@@Hl BO nftw 0 %4$lEl+BB% fn 0, %--+@& *file E@~~~+%Z€IBEI 3@42+ g~+@$!&@ *sb, 31 stat (%f41&9.i3@?$ stat { %z+BB%lE~t fiETAHTi1fikB: FRF .-&*i+ 0 rn-D a20 FTwFTWDNR $%@RMH3a kHSUT@5#%iho m-sL R+M&o Fl'?v-Ns X%%R stat %!i&@#, RGEERK/'4Ev FTW-DP BZt, fiH#THS#e%ShaTo WSLN B%$$$* @BB$&ZIYIX#~ ~II 0 $tJ%a+BBfE FTW %Nt ZXPRT: st ruc t FTW I int base; int level; 1 level If3i@fi%tlE/m%%Zt nftw (1 %=?@% depth nftw 0 f€B 4T-SfiHSF/NHl@f17f~%d~&a ftw 1 8%EH%-E HZW$%!E -+~+itt~i.7, ea%%rnamT dep~l ~H~~RB~BH, P+-$E~ B6%~#*!Y@RZ!+#*fi8@%Nv nftw () 16--.++& flags HiRta*W%fiBSK%bik i7W-USUV!J&&ff %H OR Be: FTW-CHDIR $kg13235kH chdir () @$l]lkugo FIW_DEPTH hji%bbk9f;@% ( depth-first search j. &%hlk 1 3 rn%%%%T H3%!5%0 FTWMOUNT %fiW+B@BEUS#21tF:%%o FTW-PHYS +9i@fiR3g%&aS4 RS2~h~WSBBS0 Pn%ZL% nftw O #$lfj. fn 0 A%&H-~~oEW~. &QMYC+ B nftw ( > bWZH#io BQI] nftw > ~S%afiZR%m EI3, %G%H 0, % H Q ; i!iIRj+&filEH fn 0 i%LBtli!iB~X, +1%fiEN411$Idov QdOBE *J!qi!ZEl -10 Ir#MNM : is@% ftw ( 1, ig@% ftw 0, mg $j # : open, readdir, closedir , rewinddir, seekdir, telldir, scandir : #include #include #J& : DIR 'opendir ( const char *name 1; $&am : opendir ( 1 Mft11R8B name %BKI B 3, #&m DIR * %&m B%%. open ( 1 WIU. $7;%RlEl B~i31f~BR%~leBJk6H@d i65. : F;k@Rq&H DIR * !2!8mH3&+ fiR%RkRll%m NULL, #i#fiR : EACCESS &~~~~, EMFILE Z%Zl]B#!?WH FI#TTFIYJ*#&kR~ ENFILE i3%~fl%%~~@$~%~*~~%k~o ENOTDIR @% name ~~$%.iE~~5ko ENOENT @& name fkS?@ El it+%$€, $YS%& name ,%-$?@$a ENOMEM @d,bh&$E0 : open, opendir, closedir, rewinddir, seekdir, telldir, scandir : struct dirent *readdir (DIR *dir ): $#i#w : readdir (1 i6$il$% dir H $%B(17;? fl%%h,&. %$kl dirent $x@T: struct dirent { ino-t d-inn ; f f-t d-of f; signed short int Lreclen; unsigned char d-type; har d-nameI2561; 1; d-ino kW $%hfib!I inode, d-off d-reclen - name mE&, NULL ?fro d-t~pe d-name & @J 2 f%% go d-name *Pi% .9 $ El 4B : &Dldf DT+ El ikSA,$,o 61B%&%i$?BPlH t24BE!'@Ji5iEl NULL. r#@i#W : EBADF 4% dir 3%. #include csys/tyges.hr #include #include d-name) ; I closedir (dir] ; 1 d-name : . d-name : . . d-name , init .d d-name : rc 0 . d d-name : rc1.d d-name , rc2.d d-name : rc3 .d d-name : rc4 .d d-name : rc5.d d-name : rc 6 . d d-name . rc d-name . rc.1ocal d-name : rc.sysinit ms@# : stat, Istat, symlink ax@@& : int readlink (const char *path, char %uf, sixt bufsiz ): @l&ikm : madlink ! ) #$%# path b%!f$i$3% h %&9 buf &#ErS Fl I BDrnh@+@U NULL fi+?+*%Rv iH&%$R*@?RB&HP i5 B% bufsiz /J\Tfi%$@mfi?Fi5Bl i$fi@fi%~@@l8ro 8 El iE : %f7.aJhN~f+n9sBfitBm*f+%t3~R+? *fii!ZEl -19 ffiGitRG : EACCESS EINVAL EIO ELOOP ENAMETOOLONG ENOENT ENOMEM ENOTDIR R2i+W@#E%F @R+&o %& bufsiz %a%!, 0 GR%Go R#Tfi~*~+~~~~~~~~d~* %& path M@W~&kt+.o 8% path %%gm*i?$fii!Ev &JL~I@+Z@ ~t pllh g~q~tl ~~~~~~isz~fitl B%. Msii&# : readlink, getcwd &xj@& : char *realpath ( const char *path, char *resolved-path) : &&i#m 1 realpath ( 1 fl%!$@$!7 path fi%H@~tif%jg%&&%N%fP%@T%& resolved-path #i%W%fi * &%* @ @i : $uRB&R?$N~JBHE resolved-path &&#ko XP!kEH NULL, @i%!f, #in:lude main 0 I char resalvedgath[80]; realgath (" /usr/~ll~6/lib/modules/, . / . . /include/. . In, resolved~ath) ; print f ( "resolvedgath : %s\nm . resolvedjath) ; 1 resolved-path : /usr/XllR6 ms : link, rename, unlink # 2 #include a# : iot remove (const char *pathname 1; S#ki#iB : remove 1 %%%Pa pathname %iiB!I%f+. $%&% pathname k-* $jjflR unlink 0 &IQ, g%!& pathname %J--H~. Wljfifa rmdir 0 $$&@, i%@$ unlink 1 rmdir ( 1 ., S Et #i : E?hQlJi!ZEI 0, %RtB -1, %SliS!3lgT G~QO #$sR@ : EROFS %S A&*~WSTPhST.CT@%& .30 EFAULT S3-k pathname #i#B!di%?Rh@"Plq. ENAMETOOLONG && pathname kJko ENOMEM #J~FJ&$L ELOOP %& pathnme l?i$$$f?gfglal@o EIO 0 &@%% mga& : link, unlink, symlink : #include &x a# : int rename (const char *oldpath, const char *newpath 1: $I#i#M : rename ( ) 6%@& oldpath HtiiilBP142%&%$& newpath fi%l%p /* iftit-+ DDOS T#J rename (ren) a+: rename \BXfi$$ %ft#Z *I #include void main (int argc, char *argv[l) I if {argc<3) I printf ("sage; %s olLname new-namein". argvf01) ; return; I printf (*%s => %s ". argv[ll, argvl2lj ; if (rename (argv[ll, argv[2]) < 0) printf (" lerror!] \nw) ; else printf ("[ok!1\nM) ; : open, openhr, closedir, telldir, seekdir, readdir, sc-dndir & a : void rewinddir ( DIR *dir 1 ; @j#Bg ; rewinddi ( i M*8f dir B 2% ~~l?!J~~~~~%%%~%~~~fi~O @B.fe@ : EBADF dir %%%#l!JH 3%. #include rdirent.h> #include main 0 { DIR *dir; struct dirent *per; dir = opendir (*/etc/rc.dP) ; while ( (ptr = readdir (dir) ) != NULL) I printf ("d-name : %sinn. ptr->d-name) ; 1 rewinddir (dir) : /* @I%, JA%%K */ printf ("Readair again!\nW) ; while ( (ptr = readdir (dir) ) != NULL) I printf ("d-name : %s\nn. ptr->&name) ; d-name : . d-name : . . d-name : ini t . d d-name : rc0 ,d d-name : rcl .d d-naine : rc2, d d-name : rc3.d d-name : rc4.d d-mrne : rc5.d d-mrne : rc6 .d d-name . rc d-name : rc. local d-name : rc.sysinit Readdir again! d-name : . &name . . . d-name : init .d d-name : rc0.d d-name : rcl .d d-name rc2 . d d-name : rc3 . d d-name : rc4 . d d-name : rc5.d d-name rc6.d d-name ; rc d-name : rc. local d-name : rc. sysinit : opendir, readdir, alphasort ex &# : int scandir (const char *dir, struct dirent ***namelist, nt ( *select 1 const struct dirent * jj, nt *cornpar) ( const struct dirent **, const struct dirent** H ): @&sg : scandir (1 23il%i@!@ dir I$*#, %h8@ select %$mB3#!% ~bi%B3EtOE~& namelist #i..H$, @GRi,JlH@& compar Ki$K~iifi %%#@ namelist &%M!I I3 3@G0 @?Ah El 32~++3JB-'P H 3% $QG@%kk%&~i$%$& select fitkl19iBB1, select @&Z$@Z%k H 3 B%l24 namelist &BR&H 0, 2 select 31 5?%HN4ifS&Bfi&KI B 3% #g scandir ( ) 2qaj qsort i ) %HI@&#, @@ compar @!I] h qsart ( ) &P!&, &~%#?fl~ S%W?eF!I]q@8] alphasort ( ). %& direot readdir ), a a : &,?~IF!~J&IBIB$~~~ namelist &kH$Il9&E!%#ii% H, %%%EW!Y&H -1 #include int select (const struct dirent *air) { if (strlen {dir->d-name} > 5) return 1; else rerurn 0; 3 main 0 { struct dirent **namelist; int i, total; total = scandir ("in, &namelist, select, 0) ; if (total < 0) perror I * scandir' 1 ; else { far (i = 0; i < total, i++j printf {"%s\,nW. namelist [il ->inname) ; printf ("total = %djnx. total) ; 1 1 . gncme .gnome_priva~e WebLog Error Log total = 5 seekdir (~~~I%?fiAk~ikh!!fi@$) m& & : open, opendir, closedir, rewinddir, telldir, readdir, scandir a : void seekdir ( DIR *dir , off-t offset 1 ; I%$&&M : seekdir H%i!?R@& dir H%?h Hi$t!Ji%rnEB9 rddir bj@hdk%@ER%i3B0 S& offset R%fFiS El 3*4$%%~fi8Bo $#%fiR : EBADF B%! dir ~$%%~€l~%, #include #include cdirent . h> #include main I) I DIR *air; struct dirent *ptr; int offset, offset-5, i = 0; dir = opendir ("/etc/rc.dV) ; while [ptr = readdir (dir) != NULL) I offset = telldir (dir) ; if (++i -- 5) offset-5 = offset: /* im% 5 ?%%@ */ printf ("d-name : %s offset : %d \nu, ptr->d-name, offset) ; 1 seekdir (dir. off set-5) ; printf "Readdir again! ',nWj : while ( (ptr = readdir rdirj 1 != NULL) { offset = telldir Idir) ; printf ("d-name , %s offset : %d \nV, ptr->d-name, offset) ; d-name . . offset : 12 d-name . . . offset : 24 d-name : init.d offset : 40 d-name , rc0.d offset - 56 d-name , rcl .d offset ; 72 d-name, rc2.d offset 88 d-name - rc3.d offset . 104 d-name : rc4 .d offset : 120 d-name . rc5.d offset : 136 d-name . rc6.d offset ! 152 d-name : rc offset - 164 d-name : rc .local offset : 180 d-name : rc.sysinit offset : 4096 Readdir again! /* M% 5 /I'i$gfi&gi% * / d-name : rc2.d offset : 88 d-name : rc3.d offset : 104 d-name : rc4.d offset , 120 d-name : rc5.d offset : 136 d-name . rc6.d offset : 152 d-name : rc of £set ; 164 d-name . rc.local offset : 180 d-name : rc .sysinit offset ; 4096 : fstat, Mat, chmod, chown, readlink, utime & : int stat ( const char *file-name, struct stat *buf j: a&&U?l : stat 0 al%#Bgifi file-name Wi##M*f?#&, B%llF'l@& buf %@I&% &+ (struct stat ), 7;mB smct stat fi&S&(j9$LM: struct Stat { dev-t st-dev; P device */ ino-t stjno; I* iinode */ mode-t st-mode; I* pmtection */ nlink-t s t-nlink; I* number of hard links *I uid-t st-uid; * user ID of owner '"I $id-t stjid: I* group ID of owner *I dev-t st-rdev; I* device type ( if inodedevice ) */ oKt s t-size; I* total size, in bytes *I unsigned Iong xtblksize; I* blocksize for filesystem VO*J unsigned long st-blocks; /* numkr of blocks alloca~ed*/ time-t st-atime; P time of last access */ time-t st-mtime; /* time of last modification */ time-t st-ctime; /* time of last change */ 1: st-dev 2#ff!li!?&%-% st-i~io *#&i-node, st-mde 2i+~%GB@RmRRo s~nlink %3Y%*{$&J@f!j&% (hard link) !&I , RI]&~$!!*!~@% 1 st-uid *~$%fi8@~~##j6~~userID~a stjid B14fi#t~IWRl@(~oupID). st-rdev ~k*#%%i!!W&*f?, Rfl%~~&$~ + st-size T{TAfJh7 U?-7fiZtSTo st-blksize %#%%m WO #$@Ekfjho st-blocks &Hz#@gm'P& I @-@&A)]\% 512 +?$ st-atime %f?&~-?k@~@%~@&47~B;flds7 ..-&Ea\ GH mknod. utime, read, write, 3 mncate fl$&go stmtime x#-8% -&#@&mu/fl+ -&!?%am rnknod, utime @ write Ut$&a%o st-ctime i-node SE-?A#~$NJ~;~I'sJ~ kB%HE*4+Fih~2$. a. & R@E&SrERo %ii%@%BtJ stmode NIliZX 7 T~IJB~IJH!R: S-IFMT 0170r>oo T#%2m@%s SIFSOCK 0140000 socket S-IFLNK 0 1 20000 #jB 2 l$ ( symbolic link S-IFREG 01 OOOOO -@*# S-IFBLK oU6UWO g&%z (block device) S-IFDIR 0040000 83 S-IFCHR 0020000 9 $f$$z (character device S-FIFO OOlOOOO %%%a tr(fifo1 S-ISUID OOOJOOO Z# l!l$ set user-id on execution ) .f$ S-ISGID 0002000 *{+ @ C set group-id on execution j fi SISVTX ~1000 sticky !$ S-IRW XU 00700 *#-fig%m%ga (l3w%&B@) S-IRUSR 00400 *M%8Rfl&@&m S-IWUSR 00200 wq%8%Rq%A&B S-IXUSR 00100 ~~tfififi%gq;bh,@@W S-IRWXG 00070 F~WH~I~&%EL (~P~~@@@> S-IRGRP 00040 R ~fL&J8-@&R? S-IWGRP 00020 F~W~~WWI~BPF! S-IXGRP 00010 Bi]P&RFHM7Rl@ S-IRWXO 00007 #~MPb%@WE UY%fi@bs@) S-IROTH 00004 mbwmJ@%&lJk S-IWDTH 00002 #4bHp%q%A@R S-IXOTH 00001 $w%b3@J%fi@E M~ZI+~G POSIX +sx7~~~5wmzzx: S-ISLNK ( st-mode 1 $dt&BhR%$l& (symbloic link) ? S-ISREG st-mode 1 5% & -.--E*#? S-ISDIR C st-mode 1 ~EhEhk? SSCHR st-mode 1 mm?R%g%!?? S-ISBLK ( s3e ) 75%%i&%$ (fife) ? S-ISSOCK C st-mode 1 BB % sucket? g-. 3ER sticky fi I S-ISVTX), WdB%&k El %Fb!l*#3%&%fr: iFlj7$j'%f.s kB%%@8$-, ma (delete) Irename), $$B.fe@ : ENoENT B& file-name ~~~ *f$$&& ENOTDIR EB?~$MH%&BB~F&ERIH~~ ELOOP i%fifi&*f%k&%%%?l'~% kl&b 16 fi?s#9 EFAULT && buf %%B%H+ %h%&~&b!lh??~l% EACCESS @&*PHFl@@%O ENOMEM &diIwf$L ENAMETOOLONG 25% file-name lFM@% %A# /* 9% /etc/passwd %@$,ij\ *I #icclude #include $I]@!$ oldpath figzm ZA&*ff. $% oldpath ~BI%X.~YT~---Q~~~ &, @%B& newpath ~S~%%~J-E~$M~~~~R~I~~&~~~E~ ig (g'i : &JhWIJiGH 0, *l!kH -1, a%J$HG'f em00 @@Ra : EPERM $%? oldpath 5 newpath fiBBtlft#%%$bMR 9SEO EROFS H~IS~%A@RBtJ31f+@.fE?Ri$2.I~%%h0 EFAULT 8% oldpath newpath t##&! Lb AR fi 7?2 l'4 t70 ENAMETOOLONG 8% oldpath 8 newpath kk ENOMEM @~c*h@$Eo EEXIST @& newpath fiEtftFtffg E!&%. EMLINK ,% oldpath f%iB(lfti+?&@k%@% 8 ELOOP )& pathname ~j$$?$~#%l'.7a0 ENOSPC ~~~%%~%~%~l'd$~o EIO UO mX%%o /* 4% /etc/passwd fi-flT%@ pass */ #include main 0 { symlink In/etc/passwdu, "pass") ; 1 : open, opendir, closedir, rewinddir, seekdir, readdir, scandir : off-t telMir I DIR *dir 1: &#@,m : telldir ( 1 &B%& dir Hf ifiHiiilh!l$@.$iP. &&m@it%fE& B%!* +t-RA.tBc3%@Eo f#$i$$Rfi : EBADF B# dir %%%I%B$%. #include #include #include . main (1 { DIR *dir; struct dirent *ptr; int offset; air = opendir IR/etc/rc.dml ; while ( (ptr = readdir Idirl ) ! = NULL) I offset - telldir Idir) ; printf ('d-name : 8s offset : %d \nu 1 closedir Idir) ; 1 ptr->&name, offset) ; d-name . offset : 12 d-name : . . offset , 24 d-name : init .d offset : 40 d-name rc0.d offset 56 d-name rcl+d offset 72 d-name rc2,d offset 88 d-name rc3,d offset 104 d-name rc4.d offset 120 d-name rc5.d offset 136 d-name rc6 .d off set 152 d-name rc offset 164 d-name rc . local offset 180 d-name rc-sysinit offset 4C96 mke# : open, ftruncate : int truncate ( const char *path, off-t length ) : aBibE&l : truncate C ) $?$$@!& path f8ZKt%f+k/l~!kStlZ?-& length fEab!!kjbo $U %Ef HA.14%d\L-L length k, $lJlLltV16%6@MJ h. a M @ : &~~JZ?~!R~$~IGI 0, -1, +~BL$il3l@3= emon %aR@ : EACCESS $@ path ffr$k$@J*!%ke@@o EROFS ~5A&W$G&TR@X+I%%h g EFAWLT %t path tk~tRWGNh&WJo ElNVAL @# path l3%T~%q~o ENAMETOQLONG %& path ;kko ENOTDIR B%f! path af?#dk- 1 EISDIR @& path %h-Wk ETXTBtrSY 8% path fiEKlZ#%#~Il?. EIIZ@&fT$. ELOOP %B path %U$?$?i%@l'd%. EIO I/o GR%i%e &I%@# : Great, open $$& 3 # : #include #include & & : rnodct umask i mode-t mask ) : @&#,rJliJ : urnask 0 &&$% umask ~i&&S& mask&0777 Gm@t7 %%#%a I% umask @tgH. S@M open ( ) @3ipJift#Ftt %@& mode #jk8 iE&2T1+H&R1 fiE (mode &- umask) H&BEBo P14Ht E@SZZl$ Ff#z%#@!%% 0666, urnask #i.%%h 022, !!'h%~%~J%iE&~ R1] % 0666 & -022 = W, rw-r--r--, # : &~~~~?T%i2@&lE/o BB@%R%%%b!l umask fro #k : link, rename, remove sP;*f+ : #include &# : int unlink ( const char *pathname 1: iih&%!!d : unlink ( ) &!i!l%@% pathame tPilB(JX#. fmRi?tff#%%&@!8%,6,, @EX4d&@5JRTItt;21+, !!llBR~#Tbk*Pt-~~f~B%fl B%I;fJEi$ 21%. $DL+# pathname %-@k&% (symbolic link), Ilk%%%#!$ 8i'lRP #HRa : EROFS *4$7?SFR@*#-%%h 0 EFAULT pathname g%tfidi%%@l8i%gI'% ENAMETOOLoNG 8% pathname kt? ENOMEM &~~\h%$za ELOOP %!& pathname fia%?$4s&i.11@!. EIO mX%#o #sGB : utimes, stat %&*# : #include #include cut ime. h> sxa# : int utime (const char *filename, smct utirnbuf *buf); : utime 1 B] dt4@&$& filename ff{qfiRm inode $@#ia7. &!&I utimbuf 5ZX4U-F: s:ruct utimbuf I time-t act ime; /* @~&l~~ */ time-t modtime, /* EaEl;fjg */ I : a%&% buf %q@q (NULL), !Ij%Z#m$%Nrflag&NI'87*%& iE% H%Wr% % H 68. : Af7&?hRU&B 0, %!!ki!Zn -I, gia.rtR@r errno. eE#a : EACCE-SS fiR*~~~hJ@@%~ RR$.Eo ENOENT Ef%b9*~~m?%o : utime, stat : int utimes (char *filename, strud timeval *tvp): : utimes (> H%@&B%! filename *{TffrRffl inode ~~~l'~%Ll~~&~~~O %& timeval ZXPa7;: struct timeval { long tv-sec; I* @ *I long tv-uaec; /* @@ */ 1; 23% ~vp $:1;;1fi-/I' timeval %R@M1 $U utime i > 12H Kj utimbuf %mIk E, tvp[O].tv-sec utirnbuf.actime, tvp[L].tv-sec %I utirnbuf.modtime. a IRJ : Yi,47&Jh!llJBB 0, %@&El -1, ~~iZ~t~9GF emo~ #$gRa : EACCESS GJ$l*#m&?E%, @I@$@O ENOENT %8#J*~~;f;GSo $H%i#i& : signal, sleep : a : unsigned int alarm ( unsigned int seconds 1: @#i#W : alarm (1 A%8gE? SIGALRM &@2$& seconds %Bl%Jf!!%64$% % 1 $$k!Ji&@o !#I%&& seconds 0, !~~i?~~~&l%lf$$?#@%3 %# %JT;rnHTe7&HO /* Em#t?? 5 @l%El]& hello ?R@*I #include #include void handler ( 1 { printf I "hello\nU) : I main (1 { int i; signal [SIEALRM, handler) ; alarm 15) ; for li=l; i<7;i++) ( printf ("sleep %d . . .\nu, i) ; sleep (1) ; 1 sleep 1 . . . sleep 2 . . . sleep 3 &. . sleep 4 . . . sleep 5 . + . hello sleep 6 . . . : raise, signal $x j@& : int kill ( pid-t pid , inr sig ): : kill () ~~~%%@& sig B@&P?%$#k pid #$#I%#$. @%! pid a AH%R: pid > 0 %e%$%%@$?%flfiq% pid rn8#5! pid = 0 %fi?i$%%Q I3 ia%jg@m%@&mfifi%@o pid = -1 %E%i@r#&i$g%%%h %m!Ji$l#?+ pid < 0 %6i?6%8@%%!#d@% pid %m'%m fi6 l?f!$. S% sig i?%~E3%ESJB% -443 Wgfia: EINVAL $!& sig ;I;*#. ESRCH %%? pid fi$& bfJ%E%%@fB$%S EPERM @~$%%'&6%f$~?%%~h!l%@~ #include #include #include main 0 C pid-t pid; int status; if (! {pid = fork (1 ) ) I printf ("Hi I am child process!\nU) ; sleep (10) ; re turn; 1 else { printf ("send signal to child process (%dl inC, pid) ; sleep (1) ; kill (pid, SIGABRT) ; wait (&status) ; if (WIFSIGNALED (status) ) printf {"child process receive signal %d\nn. WTERMSIG (status) ) ; 1 1 send signal to child process (3170) IIi I am child pxocess! child process receive signal 6 & : kiII+ signal, sleep gx$&@ : int pause (void 1: 5;$BR@ : EINTR fiig?W.?!i+&T1Et:8%k0 ex @ & : void psignal i int sig, const char *s ); : psignal 1 2 ~4*%2S sys-siglist[] F!J$?cP$fXR$-@& sig ME?%! %, R%$f$& s Sk~~?~$@rbnkfi~%$%%~~h$%Sf~ cstderr), &H@:X /* 3l]$$j 9 +ge#$i!$ */ #include main 0 1 int sig; char s 1401 ; for (sig = 1: sig < 10; sig++) { sprintf (s, "signal %dmr sig) ; psignal (sig, sj ; 1 1 signal 1 - Hangup signal 2: Interrupt signal 3, Quit signal 4: Illegal instruction signal 5. ~race/breakpoint trap signal 6 Aborted signal 7, Bus error signal 8, ~loating point exception signal 9: Killed : kill, signal : #include zx@# : int raise (int sig): fieam : raise 0 ffl%@g% sig $k%b?liS3f!?3%H$Jh!lj$@g &l3f kill Cgetpid #include #include void handler (1 I printf I "hello\nn I ; 1 main 0 i signa 1 (SIGUSR~ , handler) ; raise (STGUSRl] ; . ,. ,. - *&-* $fi. .. . . .. . . hello @zj$$ @ : signal, sigpwmask, sigpending , sigsuspend %&*# : #include csigna1,hr 2.x && : int sigaction (int signum, const struct sigaction 'act, struct sigaction *oldact 1; @#&lj,Fj : sigaction ( 1 fiZ%P!& signum fEZ8tlE-9%%*BZi.3E?H&B@%0 %@I signum q L&'$ SIGKlLL $O SIGSTOP Ei9bW%%f240 POL@% act $E NULL i/#t, !j!~~~%$?~%~!~~!&@~~o !$# sigaction ZXaT: struct sigaction I void (*sa-handler) (int 1 ; sigset-t sa-mask; int sa-f lags; void (*sa-restorer) (void) ; 3 sa-handler &@&a signal ( ) handler &IMP if,%%@lfi$!& BE@%, J$#&Xi.@@+ signal (1. sa-mask H%82??E&@z%%?&Fh% sa-mask #$??b!lfi?#d E!o s a-res torer lkS&BGf$M o sa-flag s H%ESE%*Emgi&4'H##fF9 TPdHBfiFlH OR 5!$ (11 a*; A-NOCLDSTOP: f %P% signum SIGCHLD, 3 3 3@Wi+W%+&B%2&%0 SA-ONESHOT/SA,REsETHAND: %$!R %HE% BE% %rn* %&E~~EZ3&%%3%%rn3$0 SA-RESTART: ~~~~cPBt3im~~mm~nfiz~~~~ SA-NUMASK/~~N~EFER: E@E1Y;~E9%%~%+ @%kE?rntCJ&%IJ*O h%$%k oldact $2 NULL fk% RI~H%~!J%?!&@~%Z&&~& sigaction 5 El : EINVAL 8% signum $-%, gj& SIGKILUSIGSTOP 9, EFAULT @a act, oldact #$#hhhk%?&6@le EINTR &~83~~&~ #include cunistd. h> #include ~signal.h> void show-handler (struct sigaction *act) I switch (act->sa-flags1 I case SIC-DFL: printf (''~efault action\nh] : break; case SIC-1C;N: printf { "~gnore the signal\na} : break; default, printf ("Ox%x\nU, act-rsa-handler) ; I main () I int i; struct sigaction act, oldacL; act.sa-handler = show-handler; act.sa-flags = $A-ONESHOTISLNOMASK; sigaction (SIGUSRl, &act. &oldact) ; for (i=5; i<15; it+) I printf ("sa-handler of signal %2d = ", i) ; sigact ion (i, NULL, &oldact) ; show-handler (&oldact) 1 sa-handler of signal 5 = Default action sa-handler of signal 6 = Default action sa-handler of signal 7 = Default action sa-handler of signal 8 = Default action sa-handler of sig~al 9 - Default action sa-handler of signal 10 = 0x8048400 $a-handler of signal 11 = Default action sa-handler of signal 12 = Default action sa-handler of signal 13 = Default action sa-handler of signal 14 = Default action : sigemptyset , sigfillset , sigdelset, sigismember &x,a a,- : int sigaddset ( sigsett *set, int signum) : @#BM : sigaddset ( ) H5%%g% signum ?Clkffl~%h~hZ@#$ set E%%g ig @ : ~~aJ?l~~l~Pl 09 ~%%~flEFqzm -10 #HR@ : EFAULT @% set jE@&ikjE&j'?Ro EINVAL Z!S& signum ]kg&2;8(1{$$# 3, #% : sigemptyset , sigfillset , sigaddset , sigismember a**# : #include & : int sigdelset ( sigset-t *set, int signum j; @#am : sigdelset ( 1 MWf%# signum ifffmE?M#3# set EBft;FMR. a lEq @ : +Y~~$~J~RIJ&H 0, @%fi+E%NlJ&B -10 f#$$RRrq : EFAULT %%? set 4FWk~K%BR0 EINVAL @% signum dF$&&!$-&f$% : sigaddset , sigfillset. sigdelset , sigismember %%** : ##include . . . . . . . ex : int sigemptyset ( sigse~t *set): BtEl : sigemptyset (1 a]%%+% set E$%$J%%jf%$. % @ P : $k,fi%IALIIH OF fi~tB%fRl)f @ -1 0 #@fiii!j : EFAULT && set fk#id~~~&%~~ : sigemptyset , sigaddset , sigdelset , sigismember sx : int siefillset I sigset-t *set): EiMk~ri8 : sigfillset 0 WJtE.%S@ set E%W%%ik, MERR~~~E~DUAPJ~E ?%Zo 2s El #i : t'4~%%F!J*@ Ot fI[I%G%sglJzH -10 #H-fe# : EFAULT @% set f~~b!lfk%$k~@e m*a ; sigemplysct, sigfillset, sigaddset, sigdelset zx a& : int sigismember (const sigset-t *set, int signum ): : sigismembert ( 1 Bj$tfllS+& signum fi%mE$2FCbflhE%g set g %%go ~~~9%~E~i%~~~~~&~ 19 Bp!lJ&H 00 #$@Ra : EFAULT $!& set Ek@lfkZ%7?$ab EINVAL $$ !& signum db Q%k b!J fi 9 % 9. @a' : sigaction, loll, raise ex @ # : void ( *signal ( int signum, void C *handler 1 ( int ) 1 ( int 1: a&*M : signal (1 2ES& signum $ZSmE9%9*8Z%1Z9m&B@Bv 3 @Fi%J~?F~i&Bf%~@k%E~k&?#l handler ~$~@&.fkf70 PIlR$%! handler $6!i%i&#%t9 !LbZt%fl&F$l]fi+%&~?-: SIG-IGN Bs$@%! signum fi$mE?v SIG-DFL $I%!& signum $k$~~?~$%&~~~%~&~~~fiE8~~ #.flE%ttQ#%mi%Y %%% cIY;f% D>* M : Bld%KFJ~9,4bBiQ%!tB%t 9 ~u%~#ZR~J&H SIG-ERR (-1 jo S&$?$i : ~6~%~8~%~~!~~~ handler ~@GBhfiGv %%%WlW&Ea B.IWDE$3@&BBm&BZEt:p @%!E sigaction ( 1. t-3 igs* alarm ( > $ raise (), sigpause (QRB9JB5B!jff> s% &# : #include ex : int sigpause ( int sigmask): fi&Bw : sigpause ( ) W $$fiE$g.Y$!7H&&@%f! sigmask %#% &!lE%l!%g, Em #%lF"r@&f7 0 lk $& E & sigsuspend ( ) @if,, a Q #j : +%-#L~T~J?~%*%#ZB -1, ~~EJ~NU em0 EINTRO a g)g[ : signal sigactbn , sigpmmask, sigsuspend sx a #& : int sigpending I sigset-t *set); : sigpending 0 %%@#J~~~~?%-fi-@%& set $k%%ao a El B : %47am!MEl 0, fiUE%%@Nfl%H -10 #BR@$ : EFAULT @& set %&hhkE%%%& ElNTR kfiH#qNa #&% & : signal, sigac tion, sigpending , sigsuspend %%*#- : #include tsignal.h> ex a& : int sigpmcmask int how, const sigset-t *set, sigset-t *oldset 1: @&R% : sigprocmask ( 1 TiJ HM%&% ?$b!lE%E%l ##@%%% how %RZ: SIG-BLOCK %~JW~.!!W~~I ~~WI!~%%~@$% set Fi%b%'E 3B%i+%%o SIG-UNBLOCK @ EMt%E?%%&R#$$%!& set %iHt9E%%pQ $1~-SETMASK % Hiii~!53%%@&@!@ ~t EZmEqBqe $a$B& oldset &!$ NULL &k ~$I&f~%%%%hhk%#%~b IEI @ : %4T&JhRl4&B 0, $~E$j%i$!fllJi!ZH -Io #gfi# : EFAULT %% set. oldset #BS+&ikz$&@@o EINTR ktfiH@q&d 9C] % a : signal, sigaction , sigprocmask, sigpending @ : #include &a : int sigsuspend ( const sigset_t *mask ): : sigsuspend ( B%BIlBiS$%BI##e@@& mask %%Wb!J@$%% #%~8B@@BPI]RE?B~*.$%&BH*mE9%~* #%@@%efP & B # ; +ikhi?B?h%9d?kBi5iH -1. tM%!i&J!q emo +$t@ Emo MX@& : signal, alarm %$*# : #include &a@&& : char *strsignd (int sig); @&i#jlj : strsignal 4hf E3ES sys-siglisti] Y4K+BfifiB%%& sig NE$% 9, %E4$&E%mES&?8*#F8%H4 #j : &n%8is3m?f+*tk#o L-3 /* 9M3 9 'PE+%& */ #include main (I t int signum; for {signum = 1: signum < 10; signum++) printf ("%-2d ; %s\nM, signum, strsignal (signum) ) ; 1 . Hangup 2 : Interrupt 3 : Quit 4 ; Illegal instruction 5 : Trace/breakpoint trap 6 Aborted 7 : BUS error 8 : Floating point exception 9 : Killed %&&@ : hnclude atdi0.b .' &a a : int ferror (FILE *stream ) ; a#&m : ferror O FB%@&BB stream %%8&X!+%B~E%TtKi%1R8 5mfi +B%E*Xfl%H4F 0 Bo @ : ~IJ%X~+%~~~EE%JY&H~F 0 ; #include : void perror (const char *s ); : perror i H~%k--+~BWYCE$~RB~$3I]G%%i2 (stdet-r). %a s fi%H~E@g%flEp$, Gi@Rbnk%8&RBl?7$% tkfESEE%lR %2E%S ermo FJB*&@B%&~$.R%. ~H#I:% #include main (1 { FILE *fp; fp = fopen (n/tmp/noexist", "r+"} ; if I fp == NULL] perror [ "fopen* 1 ; 1 $ . Iperror fopcn, No such file or directory @J & : char *strenor C int emurn); @##1: strenor ( 1 MftKS& emurn #%iB.ItW4tf i$3f%iBRBm@%?R++ RGRSTR @tBtt&rn 0 ig H 11 : &HB~~~~Emm~E@E4tO /* $iT;tGiZ{%% o 2 9 fi%iZRE#A& */ #include main 0 I int i; for (i=O;i #include isys/stat.h~ #include #include #inzlude #define FIFO "/trnp/fifon main (1 { char buffer[8Cll; int fd; unlihk (FIFO] ; i* fi]R FIFO X!+ */ rnkfifo(~IFO.0666); /*@2FIFOp#*/ if (fork 0 > 01 { ,* X&E */ char s[] = nHello!\nl'; fd open {FIFO, 0-WRONLY) : /* tJ'FF FIFO */ write Ifd, s, sizeof (s) 1 ; /* ;I$$?f$qA%%W */ close (fd) ; 1 else I /* 33E *, fd = open {FIFO, 0-RDOMLY) ; /* ~JR FIFO ft# */ read (id. buffer. 80) : /* ,!k FIFO ?#g&S??@ */ printf ("%sU, buffer) ; close (fd) : 1 Hello ! &x a& : int pclose (FILE *stream): : pclose ( 1 H%%I'fl& popn (1 ~~%ZIR$~BXWP+~. @% stream jtl %m&t popen ( j %ZBHfti+fk+t. H @$ : BHF%#EWti%%&o $uZW%F~BB -1, %iZRH@T emno q0 @H.ft@ : ECHILD plose < 1 %?&~#?8#~~%$#&g #%%& : mkfifo, popen, read, write, fork as*# : #include WYL @a : int pipe ( in[ filedes[2] 1; @!l#fNm : pipe ( } g@$E%t #%*fF#%fl&$# filedes !@%%Do filedes[O] %%.ELHSR%, filedes[l] J!jljtlgj$mSAM. 23 : E&JhNug@%, ~R4im -1, ~aizEa&T em0 +o Bsfia : EMFILE 3% CH%*~~#~~~~.A~o ENFILE %%cx%!q#%3qA 0 EFAULT @#I filedes @@&hk$e&k0 /* 2?&e#@g%?B$ 'Hello ! \n1I f$&T%E%B?j? */ #include main 0 { int filedes [2 1 ; char bufferI801; pipe (filedes) ; if {fork 0 > 0) ( /* 23E */ char s[] = oHello!\nc; write {f iledeslll , s, sizeof ($1 ) ; /* %?R;%Tx$s *: else { /* 724% */ read {filedes[O], buffer, 801 ; I* M@%*R?fl$ */ printf ("%sv, buffer) ; I 3 Hello! ; pipe, rnkfifo, pclose, fork, system, fopen : #include ex : FILE *popen (const char 'command, const char *type ): B#'&I : popen 0 bfiffl fork 0 P!k?%$i!- #~&?ii!i&!~iQJR /bin/sh -c 3k tkiT%& command Il!J%+. +& type RJ@R r , "w" IfRB A. Rltk type B+ pop () $@3gSifi$UT8@M#Z%#$8&%#,Jr %%A@&, fiG%B-+2f+$k#t0 ~G8E@q$lffl&*!?%R*@R TBfFmMm 9&3E3A~~TB@rnt%%rnAB&+o k9k* R6EH2 I+%# (FILE*) ~fi~%tJ$&th$pTiJU~$N+ RT fclose 0 H9C.e lIIJ # : %IEJ~~~]ED~.IYBR, ?@l]%B NULL, i#$@B@F emo Fa %$!&fie : EINVAL @!& type ;f;%kd #include main I) I FILE *fp: char bufferf801; */ fp = popen ("cat /etc/passwdn , "rm) ; I* Rfi cat /&tc/passwd nptt fget;s {buffer, sizeof {buffer) , fp) ; /* gR--fi cat m#l~ *I print5 ("%so, buffer) ; pclose ( fpl : 1 /* 5% /etc/passwd s-fi */ root. x: 0 0: root. /root: /bin!bash I 4 Socket I I --... ,--., --,..-- accept -----.- (gs socket gg) I : socket, bind, listen, connect k : #include #include &X : int accept ( int s, struct sockaddr *addr, int 'addrlen : : accept ( ) Ht#f +% s B(J socket 8% s & socket db%5kB bind ( ). listen ! 1 G@fiBi$, accept ( ) $%B--'P%m socket !& B!if% 4.%6m%@.l+S%i3%%EtZh%m socket fi@*B%! s FJ socket hk%%@FB accept 0 %~3?%~%%~$0 8%&Jhs;fr @& addr fi%m%&%8%%%A3E&&m~jha@7 8% addrlen % sockaddr m%NEl&o #?%#$I scickaddr @J$2?+@$@i bind ( lo El #j : &J~Y~U&B~~ socket &Eit'264, WL%B -I. +#@E88'f em $. BgRfi : EBADF 8% s socket &!9!l!it6% EFAULT &!$! addr $&%#B6l xE$?%!m h a ENOTSOCK ,$% s %-%#fi%d1 qF socketo EOPNOTSUPP $E FZ b!l socket 8 4 k SOCK-STREAM, EPERM A& (firewall) fi%tk%%o ENOBUFS %%~%@h~~~o ENOMEM @J~~~&~~~ '** la 3 listen ( 1, #s~a : socket, accept, connect, listen 2 a : in! bind (int sockfd, struct smkaddr *my-addr, int addrlen 1: @&i#IJI : bind ( 1 ~%%P%d& smkfd @ socket -+$5$$b ~8$%&@8 my-addr $8a- swkaddr %G, socket domain &K7-.-433aj&9%#3 %N: st ruc t sockaddr { unsigned short int sa-family; char $a-data [I41 : ); sa-family h%lH socket ) €Fjm domain $@, Abxxx @ sa-data s$@m 14 +??$%ao tk sockaddr %$fis[Bi%H$Hk$ socket domain mfiTH%#Fi?X, $l]@ $k AF-INET domain, #% sockaddr $ x@ % : struct sockstaddr-in { unsigned short int sin-family; uintl6-t sinqort; struct in-addr sin-addr; unsigned char sin_zero[8]; 1; struct in-addr { uint32-t s-addr; 1; si n-fami 1 y SP % sa-famil yo sin-prt %%MBcl port $3. sin-adbr.s-addr % IP &kto sin-mo *@H P @& addrlen 3 sockaddr BIIi$#0EE9 %@.fee : EBADF $#k swkfd $&% socket !%J!Eit6go EACCESS &N$gQ ENOTSOCK @&7 sockfd 3 -*49#6%g, qk socket, %$3% listen lo mxaa : socket, bind, listen #include # : int connect (int sockfd, stnrct wkaddr *sen-addr, int addrlen ): @#uI$1 : connect (> M%%B& sockfd #J socket gg$38 serv-addr %$mfl% ahk %&I sockaddr bind ( @& addrlen 3 sockaddr k!l GmkB H B : &rnRlJ%B 09 %fi%H -1, %zxaGT em0 #g.fe@ : EBADF EFAULT ENOTSOCK EISCONN ECONNREFUSED ETIMEDOUT ENETUNREACH EAFNOSUPPORT EALREADY $# swkfd qEe% socket &Ff~f$$o %%k sen-ad& %##$~%%%%~h@~~87~ %& sockfd h-*{qfii$iZl, 41 socket, B& sockfd FJ socket EE2!%%&o Z%S3@ server &m~%m~i$a~E2~r~B*emEo X%4*E@R@3EZrn%R+ sockaddr &$#%I b!J sa-family socket %$~MI&B%~~IBS%@#&*Z~Q /* +dg~ socket TCP client &@!%%% TCP server #q?&&#f?n@jSR$m& server, TCP server zfl]is@% listen (} . * / #include #include #include cnetinetlin-h> #include #include < sy s / socket. h> main (int argc, char 'argv { 1 ) I struct hostent *host; if (arqc < 21 return; host z gethostbyaddr (argv [I] . sizeof (argv [l] 1 . AF-INETJ ; if (host: == (struct hostent *) WLL) herror I *gethostbyaddrU ) ; else { printf (''name : %s\nw, host->h-name) ; print f I "type : %din", host ->h-addrtype) ; printf ("addr : %sin", host-~h-addr-list[O]) ; 1 1 #% fi a : gethostbyaddr , sethostent gx @j & : shct hostent *gethostbyname C const char "name ); @j#am : gethostbyname ( ) $&@--? hostent %fi, $3% name qu;fi~ -+&#l,g #$$ IPv4/ih6 IP ithike Gfl hostent $x@-F: struct hostent I char *h-name; char **h,aliases; int h-addrtype; int h-length; char **h-addr-list; 1; &B&%.X4iIT: h-name iEzm$&g%- h-aliases ~kfi&&i3%~#4@1~&~ h-addrty pe &&m@&, AF-INET, h-length fiM$mk@ o h-addr-I is t Ni%%!k%%R@%&tAm%gH#u 8 : hfiQyd&k!l hostent %##% $$@$&%!ij$iB NULL tg8t. #ijiL%B!i!lJ @? h-ermo %e @BR@ : HOSTNOT-FOUND $k$BJ$kiZ IYJ &#I, 0 NO-ADDRESS %$$~fi%%f;~% IP ihkt:, NO-RECOVERY %21B%%%%~E$0 TRY-AGAIN i%&fllfj--Ro /* gethostbyname 0 mf@@ET!Z IP *I #include #include aetdb.h> #include #include int main lint argc, char *argv [ 1 1 { struct hostent *hp; struct in-addr in; struct sockaddr-in local-addr; if (argc < 2) return; if ( ! (hp=gethostbyname (argvC1 I ) ) ) { fprint f (stderr. "Can't resolve host. \nn : exit (1) ; 1 memcpy (&local_addr.sin-addr.s-addr, hp->h-addr, 4) ; in+s-addr = local-addr.sin-addr.s_addr; print£ ("Domain Name: %sin", argvll] ; printf ("IP address : %s\n\ iinet-ntoa (in) } ; 1 $ ./nslookup linux.ee.tku.sdu+tw Domain Name, 1inux.ee.tku.edu.t~ IF address . 163.13,132,68 @ ft a & : getprotoby number , getprotoent , setpmtoen t , endprotoent & : struct protoent *getprotobyname ( const char *name) : : getprotobyname ( 1 %&D--'P protoent %&I 8% name %@BafitlM% w~iKb%. &l%$h letdprotocols $f fiW%%!q&&#!,#&%& protoent &H. $84 protoent %Xi%$% gecprotoent (). 23 Q t$ : @~h%lJi!%CCl protoent %W%H, %t%B%a;l;flB8m@%l1jgH NULL tE$t, / * RB i cmp bi$!,&B * / #include main (1 I struct protoent *protocol; protocol = getprotobyname I'icmpm) ; pr intf ( ''protocol name ; %s\nu , protocol-rp-name) ; printf (vprotocol number : %dinn, protocol->pjroto} ; printf (''protocol alias : %sin", protocol->p-aliases[OI) ; 1 [ getprotobynumber ( ~&~~~~lf@#~t7(&#) - # : getprotobyname, getprotoent . setprotoefit, endptoent &x &a : struct protoent *getprotobynumber ( int proto); /* R#BiX%S 0 Z 4 t%ithW%E */ #include cnetdb.h> main I) { in t number ; srruct protoent *protocol; for (number = 0 : number < 5; number++) { protocol = getprotobynumber (number] ; if {protocol == Istruct protoent * ) NULL) continue; print£ ("B2d : %-lo$ : 8-10s\nn. protocol-rpgroto, protocol-zp-name, protocol->p-aliases [Ol 1 ; 0 : ip : IF 1 : icmp . ICMP 2 . igmp ; IGMP 3 : g'JP : GGP 4 . ipencap ! IF-ENCAP @e : getprotobyname, getprotobynumber, setprotent, endprotoen1 *%%@ : #Include PY & : strvcl protoent 'getprotoent ( void 1; @jBi$&EQ : getprotoent ( 1 ~~T7f/etc/protocols, $?G~9-.tj%%G&%t4 protoent BEI. ~~~nmwa~aa~~i~roc7;-~a~~9 pgj~a~~anwa;tm NULL $%to 46 fi protoent Z ,XBU 7;: struct protoent { char *p-name, /* official protocol name */ char **p-aliases; /* alias list */ int pgroto; I* protocol number */ 1; /"8R%!JM& */ #include main () i struct prot0er.t *p: while (p = getprotoent () ) printf IF%s %s %din*, p->p-name, p-zpgliases [OI , p-zp_proto) ; endprotoeat 0 ; lcrnp ICMP i gmp IGMP ggP GG P 3 ipencap IP-ENCAP 4 st ST 5 tcP TC P 6 WP EGP 8 PUP PIJP 12 u d~ UDP 17 &l% && : getservent , getservbyport, setservent , endservent ex : struct servent *getservbyname (const char *name, const char *prom 1: @#-i..m : getservbyname ( 1 2gD- .+ servent EW, &% name irJu,?g. .-/i-~%!!'i %!F.J%#~:~ S% proto RIJj3 %l6t%Wii$H @fiIX0 &@I @2M. letdservices 'P-B&~+-ft&#H@E#&%$fd servent BHO $k#$.t servent HiZXi$@?$ getservent )o tj a : !k4?ifll]%kl sservent %bJifs$tl X$$f#@!l@m NULL E$fo #include rnetdb.h> mairi 0 { strbct servent *s; s = getservbyname ("telnet" 'tcpm) ; printf Im%s %dl%s\nm. s-zs-name. ntohs {s-;.s_port} , s->s9roto) : 1 telnet 23itcp Ms fia : getservent, getservbyname , setservent , endservent a a $J & ; stmct servent *getsewby port ( int port, const char *proto 1 : @&#a : gctservbypon (1 $%H--/i' servent %m, g%! port flu%--+ port -% $9, O% proto ~Uxl%BB%%fiMHtbil~ &Cl!i@kM /etc/services q1Bji T$fi%.I+N&B, #hG# sewent BDO %!iN servent #~kZXii$@% getservent >, tif$@i#g : 6% port @%%h htons () %#to 5 @ : K?,ljNIJZH servent i$&$k~, 8%$E@@lJi!iB NULL #%kto #include cneLdb.hr main 0 I struct servent *s; s = getservbyport (htons (23) , *tcp') ; grintf ("%s %dl%s\nU, s->s-name, ntohs (s->sgort) , s->sgroto) ; telnet 231tcp 8% a : getservbyname, getservbyport, setservent, endservent .: struct servent *getservent ( void); aaaw : getservent i ) &Tfl ktclservices, $!%i@$X-$?&%!& servent El* 2GBfiMk@BF4&%%BmT-Jfi%Bj %4kE~U*i+EW%El NULL fE%to %tQ servent ZXh7;: struct servent I char s-name: /* ESHJ#g%$$ */ char **s-aliases: /"ll8iPq%*/ int s-port, I* jfi@m@J port */ char *sjroto; I* @R&Wi28& */ 1; & # : &~h!!I]&a servenl %tg%H, gR@%%$%9( 7 *{*Eb!g%B NULL /* 3~~nmrng1~g~ag v #include main (1 { struct Servent *s; while ( (s = getservent (1 ) 1 printf IU%s %d/%s\nm, s->s-name, ntohs (s->sjortl , s->s_proto) ; endservent ( ) ; 1 tcpmux lltcp echo 7ltcp echo 7/udp discard 9itcp getsockopt (@% soeket 8&) ME&& : setsockopt &a & ; int getsockopt (int s. int level, int optname, void *optval, socklen-t *opllen ): ea$&m : getsockopt ( 1 Q%$% s %%X85 socket #&!iH. @@ optname I%%%% @!Vl%JR%&, mj @ 8 optval RIltEi Fl %@Kt6 R Et fi GMkt SB optlea NJ j'g i&Pla7 FJ~~JL %& level, optname if$$%!$ setsockopt ( ). @@.fee : EBADF S#k s #W-%FJ s~ket ~Bl'W* ENOTSOCK 8% S %-2##%&fl, qb socket, EFAULT 8% optval %MEn~&~Rbt1pfi6%1'~~ #include #include main 0 I int i; for {i=O;i<6;i++) printf {"%d : %s\nm. i, hstrerror (i) 1 : 0: Resolver Error 0 (no error) 1. Unknown hose 2, H,xt name lookup failure 3, Unknown server error 4, Nn address associated with name 5, Unknown resolver error &% @& : htons, ntohl, ntohs sP;&w : #include V mEG# : htonl: ntohl, ntuhs # : #include cnetinet./in.h> &x a# : unsigned short int htons ( unsigned shun int hostshort 1; #q&$&w ,: htons ( > 83%%%&fi$E~ 16 iil hostshort ~~&W@tTfiR#~O ig Ig f.g : &DNErnrn%?Rrn@o f 4% getservbypon ( 1 $ connect ). sx # : unsigned long int inet-addr ( const char *cp ) : @@-i#W : inet-Adr ( M%%@#k cp fi%~~%&~?8*%&~H%fi@81@1% %!H@To FI%.&~k?R;Pt~ b!@?Bfi%fiFlTR@ I @MU: " 163.13.132.68", (j$ : %m!~~@~Emm%=8%~m#?, %%&El -10 j$f 19 $f Socket &$ &# 327 &x a : int inet-aton ( const char %p, struct in-addr *inp ) : iWl#@I : inet-aton I > 83%%8@ cp RtkmN%~k4%%@%&BM@%1fMm~ %Xilb9tP, %6@lf+t inp M%FJ in-a& %@+o 8 &I in-addr 2 X $1 T; ; struct in-addr I unsigned long int s-addr; 1; &?$j& : char *inet-ntoa (struct in-addr in): ii!R?j&M : inet-ntoa > FB%%B% in %E@Fl%~8%ll@B?R#&W%~kt:+ 8 E%%rnkN%~fk+@*rnEWBBo %& in-addr $&bT: struct in-addr I unsigned long int s-addr; 1; is@% connect r > , : socket, bind, accept, connect ex a#& : int listen (int s, int backlog 1; @#aM listen c) 8J%%R&% s I% sock- b& backlog $k$2Hi%%kbEBC)f. %S@E$, Pn%SEB H %a+ RBd client ~~~3J ECONNREFUSED connect ( > 1. listen ( 1 #?k3&&$%%. RgBf socket A listen @d, &iEBf client I%i&fIE acceptO.ZBR listed )%b mket 0, bind 0 26fl8j+ %&$@I accept Oo Mmi#w : listen ( ) !?%!iH SOCK-STREAM 8 SOCK-SEQPACKET 8(1 socket %so scoket % AKINET @]%& backlog @%BSiT$g 12a0 #$i%fiH : EBADF 4% sockfd 4E.$& mket &lfi@$+ EACCESS &E$zo EOPNOTSUPP EiFZFJ socket g%t;rfi@$ listen #&o /* +flfl socket TCP server &EB%B@& TCP client I$%H?R@#BF. TCP cl ient zfiji&$t-% connect ( . * 7' #include #include #include #define PORT I234 /& Efflm port ?@ */ #define WSOCKFD 10 /* Tm@flE%&lsk&%@/2/ */ main ( ) I int sockfd. newsockfd, is-connectedEMAXSOCKFD1, fd; struct sockaddr-in addr; int addr-len = sizeof (struct sockaddr-in) ; fd-set readfds; char buffer (2561 ; char msg[l = "Welcome to server!"; if ( {sockfd = socket (AF-INBT, SOCK-STREAM, 0) 1 < 0) { perror ("socket") ; exit 11) ; 1 /* 3% sockaddr-in %#I */ bzero {&addr, sizeof (addr) 1 : addx,sin-family = AF-INET; addr +sin_port: = htons (PORT) ; addr.sin_addr.s-addr = htonl ~INADDR-ANY) ; if {bind (sockfd. &addr, sizeof Iaddr) 1 < 0) { perror [ "connect ' 1 ; exit (1) ; 1 if (listen (sockfd. 3) 4) i perror {"listen"] : exit (1) ; } /"gRis$lA%@* *, for (fd = 0; fd < WSDCKFD; fd ++) is-connectedlfdl = 0; while (1) ( FD-ZERO {&readfds) ; FD-SET (sockfd. &readfds) ; for Ifd = 0: fd i MAXSOCKFD, fd +4) if {is-connected[fdll FD-SET (fa. &readfds) ; if (!select MAXSOCKFD, &readfds, NULL, NULL, NULL) ) continue; /* 9j%E5G%%%$%E8%3 */ for ( fd = 0; id < MAXSOCKFD: fd++) if (FD-ISSET (fd, kreadfds) { if (sockfd == fd) ( /* &@%Z% */ if I (newsockfd = accept {sockfd, &addr, &addr-len) 1 < 0) perror I accept " ; /* j++&iQ8R$+3&!$ client % */ write (newsockfd, rnsg, sizeof (msg) ) : ia-connecttd[newaockfd] = 1; print f ("Connect from %s\n* , inetstoa Iaddr. sin-addr) 1 ; 1 else { /* @rtfr%6* *, bzero (buffer. sizeof (buffer) ) ; if (read lfd, buffer. sizeof (buffer) <= 0) { /* Z%E4%, %BE%#&@& */ printf {'Connection closed. /n" ) ; is-connected[fd] = 0; close (fd) ; ) else printf ("Bs", buffer) ; 1 1 1 1 (i#~filEt tcp server ZfiU)E@&fi tcp client) $ ./listen Connect from 127.0,0,1 /* B@!$j*B 127.0.0.1 mg% */ hi I am client! /* @$I client %f$%b??fl$l */ connection closed. /* i5%*@ */ : htonl, htons, ntohs &x &B : unsigned long int ntohl (unsigned long int netlong 1: : ntohl ( 1 M%#$3BfiZ&i 32 $Z netlong %%A& #'?XIIRR. gi IBl #$ : %H%EM&a%BmGo t-3 : htonl! htons, ntohl %&-&# : #include & fie : unsigned shod int ntohs ( unsigned short int netshort 1: $&#&R : ntohs () ma@4@QldF.8j 16 6 netshon ff@df fi%??-IlE%+ H a : aanjaaamnnB. Mga& : recvfmm, recvmsg, send, sendto, socket a# : int recv ( int s, void *buf, int len, unsigned int flags); iW&i#&l : r.cv i 81%%&3%%fl43fE%&l socket i$$l%&R1 %#!%#f&$d~&I ?%k buf ~IkI~~~~iK!, g& len %qg$k@#m@Ai5@o B& flags -&8 0, #&&B$X$UT: MSG-OOB E~ d out-of-band h'!J@% MSG-PEEK %m%m%#%$+B%%%, fi~%Raa mv{ ) *&HfimmBBnBQ MSG-WAITALL %i&@g$u len 9~44f~&B68i!iLi!ZD, Bqbj%%$!% IE9Pko MSG-NOSIGNAL kk#!$$E@ SIGPIPE iG$q 8 Ba.fe@$ : EBADF EFAULT ENOTSOCK EINTR EAG AIN ENOBUFS ENOMEM EINVAL %& s jk~&l8l socket &13!!ff10 B@+R-~E~~~B~XEG%~~@~I'~~ 8B s %-.2#%%4, dk socket, @IS7%Cfi@O k3fi%%%f?m&, @@% s socket %~q@l!!fi ~%rn@#&G+go BdL+m+Z* f$%%%fl~b!j$%TE@~~ g 19 $ Socket &g &# 333 iE@?$ listen )o I recvfrom (& socket B&&4l[ts) '1 &z@B : recv, recvmsg, send, sendto, socket ex f&j& : int recvfrom ( int s, void *buf, int len, unsigned int flags struct soclcaddr "from, int *hmlen ); GBaaJtf : recv (1 Bil%~&5$~M,9~E2b9 socket #JRIt!jBB, #EBCBG$ll& B& buf %I~HI~~PI'~, ZSB len %Til&4k%#fHBAK@0 P% flags --MB 0, #dbBfi$X#&!$ recv ( >, B@ from AI%@ZRfii3FIm gkkjk, $& sockaddr iE@% bind ( ), $& fromlen % sockaddr ffi EBo @ : &J~R~J%HE&R!&FR&, %@%El -1, %SBUe.f em Cfi. $#RRa : EBADF EFAULT ENOTSOCK EINrn EAG AIN ENOBUFS ENOMEM EINVAL @& s 4E-$i&% socket &@I%@$, B&+%-RttmnzeG@mfl*2f~o B& s k-*#%ldi$/, 41 socket, @Eqfi*%o ikrifi#&+$EPHli%, s & socket %$SiJPHbo ~%H%RhG$Ev @1C~hB+EO 4$%%%aBSm@B;f;iEfio /* fflA socket UDP client Itk$%%&% unp server. #;1$@&UA~S?T$.fS%?$ server. UDP server Z@1%&% sendto 0 , * / #include csys/stat.h> #include #include #include #include #include cnetinet/in.h> #include #define PORT 2345 /* fiMH port */ #define SERVELIP X127.0.0.1n /* server IP *I main I) i int s, len; struct sockaddr-in addr; int adelen sizeof (struct sockaddr-in) ; char buffer [256] ; /* socket */ if (S = socket (AF-INET. SOCLDGRAM. 0) ) < 0) I perror (*socketw) ; exit (11 ; 1 /* %!$' sockaddr-in %& */ bzero (&addr, sizeof (addr) ) ; addr.si~family = AF-INET; addr.singort = htons (PORT) ; addr.sin-addr.s-addr = inet-addr (SERVER-IP) ; while (1) { bzero (buffer, sizeof (buffer) ) ; /* M%@WA;s;$rRR?R@ */ len = read (STDIN-FILENO. buffer, sizeof (buffer} ) ; /* 4$9R%#i3% server # */ sendto (s. buffer, len, 0, &ad&, addr-len) ; /* E@ server %BiEIm9fi@ */ 1en = recvfrom (6, buffer, sizeof (buffer) , 0, staddr, &addr-len) : printf ("receive : %sU, buffer) ; 1 # 19 $ Socket d & 335 (%he udp server Wfifiltk udp client) he1 lo /* Ah%&aA?fl$ */ receive . he1 lo /* server $j&B%@iTfl& */ I recvmsg (83 socket #$&&#) I ; recv, recvf~orn, send, sendto, sendmsg, socket ax @J a : int recvmsg ( int s, struct msghdr *msg, unsigned int flags 1: &#S#M : recvrnsg () #I$3&@i%@&$JL$$~i5H socket ~$?kl%&@o s %e Ba$fS%H socket, Pa%$JH UDp Dt.!ClJT~%t22&%Bfi0 8% msg @kWi%%JiBclBSr@L&fi@, %% flags -&8 0, iT%l#%ii$Bf send ( 1, %-F$$& msghdr B(I$aif$&!$ sendmsg ( 1. gsRJ3q : EBADF EFAULT ENOTSOCK EINTR EAGAIN ENOBUFS ENOMEM EINVAL @& s qFi$Em socket &Efl@, B&+a~--Etttkfi ;il%?Z-RBtShG~I'~l %%k s %-*#&.$@I, qk socketo BETR+WO &EiT%~iE@PH@iJ IH@% s @J swket %;T.qf!lk%, %%rn&Hrn@+Eb &~LibGqizQ !$%%%flMHlfJ&$iEMo I send (& socket &%RE) I ; sendto, sendmsg, recv, recvfrom, socket & 2 # : #include .: int send ( in1 s, const void *msg, int len, unsigned int flags ): : send 0 H3k%?!k@Eb%$?& socket {$%r(if7?&fio @& s %Et$$@% %l% socket, @!!& msg %fi%@$k)%#$~$@+ @% len R'J%&#El$, 8% nags -&E 0, &~BBZX@7;: MSG-OOB d+%m%f@ out-of-band 8 a MSG-DONTROUTE J$K~ I& 8 (routing-table $%. MSG-DONTWAIT iE!?%6~~~@~~$o MSG-NOSIGNAL lkd#$%Y!k SIGPIPE 6?q%o : EBADF EFAULT ENOTSOCK ETNTR EAGAIN ENOBUFS ENOMEM EINVAL i3$% connect ( ), && s jk&'&l% socket !&3!l!dt@o ~~Pcp-~~~ml;i3~@~m~~~iFso Z%& s %-*#6%d, dF socket, #i23fi+@* &%b#&+%Epfl&@B& s b!l mket h$gB&i0 %%rn%#h*;f;Zo &~~i9'@+2Q ~%%%flHM$@+iE%o mg&# : send, sendto, recv, recvfrom, recvmsg, socket &X @J& : int sendmsg iint s, const struct msghdr "sg, unsigned int flags 1; aa%& : sendmsg 0 ffl%l&#f&fPBH socket R%Xt54#Ig t@ s %1?&2 BS%FJ socket, fUl!$!$um UDP Ii@iX!ll~$%&j$%&%fiO 8% msg % $J@~%m%E%@J h @, 8% flags -h!ikct*% 0, #@#6&i%$i?$ send ( ) $&&I msghdr 2Xa-F: struzt msghdr E void *msg-name, I* Address to send t~/receive from. */ socklen-t msg-namelen, /* Length of address data. */ struct iovec *msg-iov, /* Vector of data to sendlreceive into. */ size-t msg-iovlen, /* Number of elements in the vector. *! void "msg-control: /* Ancillary data *I sizet msg-controllen, /* Ancillary data bsffer length. */ int msg-flags, / * Fl.ags on received message. */ 1; #@fi@ : EBADF EFAULT ENOTSOCK ElNTR EAGAlN ENOBUFS ENOMEM EINVAL $%! s lp&&b!J socket $!bEfifi, ~%PG-#~H%~%%@@rnh&~fdo %%$! s %-2#-#%fl? socketd %E+fi+Ro bkkk#2+8@!1%? @%# s b!t socket %%rn%#fi@$Z? EJL~~~~~O e~mammsa+E*o I sendto (9 socket fig&#) I &zfi# : send, sendmsg , recv, rcvfrom, socket x@B : int sendto (int s, const void "rnsg, int len, unsigned int flags, const struct sockaddr *[to iin tolen); WBmM : sendto (1 l%k$!l@&b%$h!l socket i$K$.l$3&&Lb g& 5 $jE%fZkf socket, fi~%$dBJ UDP thi$%J$%%dj?@&fiO ?%B msg %6l i$ti!E%K~!&%fiB, 8% len ~~~~~K~~ &B flags -&ti! 0, iMI#i i$ig@% send (), && to H?l!tfi~#kf$%@~&f#!hkt %vl sockaddr ii$ @% bind i 1, && tolen 3~ sockaddr m%&i@$ E@fi# : EBADF EFAULT ENOTSOCK EINTR EAGAIN ENOBUFS ENOMEM EIN VAL %% s qF.$;-?f;m socket ~3!l!~f.b~, $%$fi -#@%hZ&GRWhfiQl'~o 8% s i%'-*#%&d, jb socket, @iZ,7-fi+&o ~dl#~~~~~PH%, s b!l socket 5!.tl.l;qPH@i0 %%H%)ffihG;r;Zo EJLW~ZO i*%%%~Mrn~~$iE~o / * $llB socket UDP server. 91;EF%@@€Q UDp client &%Hf fi$RfiW-ff'ilEl client. TCP client ?6fl%$% recvfrom 0 . * / #include #include #include #include #define PORT 2345 /*fifl&port4$@*/ main I) I int sockfd, len; struct sockaddr-in addr; int addr-len = sizeof Istruct sockaddr-in) ; char bufferE2561; /+ &h socket */ if ( (sockfd = socket (AF-INET. SOCK-DGRAM. 0) ) < 0) I perror ("socket "1 ; exit (1) ; 1 I* #% sockaddr-in %@I */ bzero (&addr, sizeof (addr) ) ; addr-sin-family = AF-INET; addr . sin-part = htons (PORT) ; addr . sin-addr . s-addr = htonl I INADDRJNY 1 ; , if (bind (sockfd, &addr, sizeof laddr) ) < 0) I perror ( "connect ) ; exit (1) ; 1 while (1) { /* client i#$+&PR$ *I bzero (buffer, sizeof (buffer) ) ; len = recvfrom Isockfd, buffer, sizeaf [buffer) , 0, &addr. -&addr,len) ; /* 8% client %@W&&i. (IP) */ printf ("receive from %s\nn, ine~-ntoa (addr-sin-addr) ; /* #Tlm$i&Q% client i$ */ sendto (sockfd, buffer. len, 0, &addr, addr-led ; 1 getprotobyname, getprotoby number, endprotoent : #include $ & : void setprotoent ( int stayopen ) ; @I&~M : setprotoent ( ) R33EjTjf Ietc/prot~ols, @%@@ stayopen @I% 1, R1JB-F getprotobyname ( 1 d getprotobynumber ( ) #$$9 $ %?I# kfl#X#. @ & : getservent, getservby name + getservbypon , endservent %A*# : #include : void setservent (int stayopen): P#Pm : setservent ( ) H%$TK /etc/services, imJB+#? stayopen @%I 1. IJ@7;4F @I getservbyname ( ) % getservbyport 1 %Tb fi rj)lftffJflW*ff I,- set~ckopt,,,,(~,B socket #&) 7 &x : int setsockopt( int s , int level, int optname. const void *optval . socklen-t optlen ) ; @&BM : setsockopt 0 aj]3ki?Z@@ s f!h$k#m socket #&. 8% level it3!ti$,kg gl%fl%EF -h!?Ei;k SOL-SOCKET ul%m socket E, 3%% optname fi 2%@Brn&@, GTFdh@@B: SqDESUC mfg%M#%##& SO-REUSEADDR fci?& bind () ~$$~$&M~~~~@~o SO-TY PE &m socket 3% ( !Ul: SOCK-STREAM ) a SO-ERROR &m socket ~Z@k&@%b%d~ SO-DON'TROUTE ~~~##@~~$~~%&~&~fi~o SOBROAK AST @BS~#~S~%O SU-SNDBUF i!i~%~~~@~%~j~p SO-RCVSUF t!E!3%&~~~~~/~~~ SO-KEEPALIVE ~#@$g@gsz%ko SO-OOBINLINE %@@@] OOB (out-of-band ) %@ilI$244%5 t%%#Aift&o SO-LINGER a#g#%$HqfFb9Eg$ &. fi 8% optval ItKRiQBBrlQ. .& optlen lbl% optval BCJXE. m&I#,,W : EBADF BB s #jk.$;&H mket !&BRbq, ENOTSOCK B@l s %-*{$@%fl, socketp ENcIPR~T~~ET SB optname ~BtKi&Jfi;l;iEl EFAULT %& optval @RE k %%k@lm h 6? C87 shutdown (&k socket aEa# : socket, connect &jya& : int shutdown iinc s, in( how); Ei#i#M : shutdown C) aj%%kB!& s R~ZPJ socket B& s f&S%$$rl swket &@4ft3, Bg how ??FPUL@%R: how =O %ki&@&#a how=1 *-. %kf$%&if0 how = 2 %k@%R~$~f#~~ a @ : &JhFY%B 0, 9$!k&H -1, %SEB@T emoo BBR@ : EBADF @& s $E-8%m socket figfiao ENOTSOCK s %-*##&%fl, qb socket, ENOTCONN %& s %zm mket #-7k%%0 socket (@&-.k socket @B) m%@!j& : accept, bind, connect, listen a&*# : #include #include sx : int socket ( int domaini in1 typei int protocol ): @#aw : socket 0 ajfffgz--?%b!l slxlket, &f&2m%$f$k!lf, %%llS%BZ- aE% Po %& domain %$!@~#T#$~%kl:%@., 5%%H %x7!E /usr/include /bits/socket.h h ;J, ETi%!i!,bWM : PF-UNIWF-LOCAWAF- UNIX i!f#%%flt%Ei.. UNIWAF-LOCAL PF_INET/AF-INET IPv4 W % fi 2 PFA1NET4/AF-INET6 IPv6 F!%b$! PF-IPWA F-IPX IPX - Novel1 Hi$! PF-NETLEN WAF-NETLINK &JL%HF~%IJ%Z!? PF-X251 AF-X25 ITW-T X.25 / 130-8208 WR # 19 $f Socket &$ 343 PF_AX25/AF_AX2$ AX.25 tihi1 PF-ATMPVC/AF-ATMPVC %a@@ ATM PVCs PF-APPLETAL WAF-APPLETALK Appletalk (DDP ) biJ! PF-PACKET/AF-PACKET +~%~a&n @@ type RT?UA$tt'%@: SOCK-STREAM ~&R161~%~~~#~~@i%t TCPo 2@ OOB (out-of-band) &@I, ~fifi#@fi~$&~ @EM connect 1 sffgGS&lA&o SOCK-DGMM l'Bw%&$?il~~%~%@~%f3&~ SOCK-SEQPACKET 5-wB!EJf~%~&~@%~~ SOCK-RAW @~~~b~%fi$Gi%~ SOCK-RDM 8%kq!$fim@@&g&o SOCK-PACKET ~&@fl%%aEIfB%%gO %@% pmtocd ffl3kfkz socket fif!t~t!JWfiiit&i.%% j&%h&%$q@? e, E,% o emQ # : &TbNfl%B swket @E!f63, %@%Ej -1 @@.fee : EPROTONOSUPPORT 8% domain $!$%m%%$b%s% type E!?, protocol stJBi$4 ENFILE &)bfil'?&& %?k@2%ft!l socket $&I EMFrLE 8%*f%%%&9 %?k~~~%b!l socket* EACCESS @R&E, it%!&s@& type 8 protocol BHt!J BiX ENOBUFSENOMEM h j?$,6! ElNVAL B% domain/type/protocol $.f;kk. ia"@% connect ( >, mXa& : msgget, semget, shmget : #include #include : key-[ ftok (char *pathname, char proj ): : ftok () H$$f@%7 pathname #%is(lft#mit$I] (project ID), #@% System V 1PC @%H%@R] key. b@ pathname #$@!l*##iffffi58 aqHG4xo 8 B #$ : E-&J,,fllJEH key-t @, GRlJ&B -1, %GRBkT emno q0 #RR@ : ENOENT 23% pathname B$WCfWi%Ee ENOTDIR BWPm El 3l%UH@4FBErn Hfto ELOOP &?Tfil%l*!%U%E%%%/d%, kl$!% 16 +fl %SEO EACCESS GB*fqH%F%* ENOMEM @JL$~B$E~ ENAMETOOLONG B3;8 pathname m@t%$ b %At;. L-1 key-t key = ftok (n/tmp/sysvipcu, 1) ; . . . - . , - - . . . . . . . . . . . . . . i j&X , @a- .. . : int msgctl (int msqid, int cmd. struct -id-ds *buf): $#a18 : msgctl ( 1 @KT LHB$4€B$Ji~,$IM~1]~5fi O& msgid %ifR%EBcJ !i%8fl$!bR%jft6% %a cmd %%@%flm#iT9 fiF?uh#&ffi: IPC-STAT tfi4g.BA?d msqid-ds %&%!#$ b#B]@% buf. IPC-SET %%& buf msqid-ds msggerm.uid, msgperm.gid, ms~perm.mode .#l msgqby tes $% gi%( 34 E48RFI@I msqid-ds %@no IPC-WID Itr)JR~~d~~PtJiF~~&B%&e msqid-ds m%#&X.bT;: struct msqid-ds { struct ipcjerm msgzerm; st ruct msg *msg-f irst ; struct msg *msg_last; time-t msg-stime; time-t msg-rtime; time-t msg-ctime; struct wait-queue *wwait; struct wait-queue *rwait; unsigned short int msg-cbytes; unsigned short int msg-qnum; unsigned short inC msg-qbytes; ipcsid-t msg-lspid; ipcgid-t msg-lrpid; I; msgfirst msglast msg-stirne rnskrtime rnsgctime ms~cbytes msg-qnum ms~qby tes msglspid SG-Wll msgsnd ( 1 BhQ,B@~i&BW5;1164. rnsglrpid j%E-'M msgrcv ( ) @~~~,~~~#~i~%~~. "'WLP~~ %@W m%# ipc-perm WX4aT: st ruc t ipc-perm I key-t key; unsigned short int uid; unsigned short int gid; unsigned short int cu id; unsigned short int cgid; unsigned short int mode; unsigned short int seq; ); + key: &{$,i?.B(1 IPC key. + uid: kLfi,&MPlWiEBclHP ~EZJJR. + gid: am wgdlJmaetliEs;rla, + cud: ~Q~2,BlRP!lBCJHPIRRdF). + cgid: I~.I~d~M~d~~W9J6~o + mode: k~'i,~RFd~i$T&B. + seq: @%. #@.fe@ : EACCESS %& crnd 3 IPCSTAT, 8%@fk!@J$7%@I@,ISh~~0 EFAULT @& buf %l%~~h@f&kto ElDRM key figb!lfid86h?l E%%lR. EINVAL %*m@!& cmd 3 msqid. EPERM ~~cmd%IPC_SET~IKPCRMID,@~~&~@F$L&f~o &%@# : ftok, rnsgctl, msgsnd, msgrcv sx ~a : int msgget key-t key, int msgflg ): @t#Mj : msgget ( ) HdtRR%rT[ key BXR@i~.~MY4iE%IJit:% $%$B key % IPC-PRIVATE J!h?$3$a%@l%,@PhTI], @% key $3 IPC-PRIVATE, & $ZEgz@ [PC key, 3B/I,$%RI]2?i!B& msgflg IPC-CREAT ( msgflg & IPC-CREAT %I 1 3k & 2 & 5 tPC key % key ft!i fz .$. Bh ~uA@% msgflg $2 IPC-CREAT IPC-EXCL e, fi %$k@i@% key dF@ZE,BBhPd, Fqt%l'i,&l%Fd E&& k$b7 $@ msgflg dIfl3k BBE,@~MFU~JB~WWR, #@83;f open O FJ+B mode Hit. (MI7 4?7f;@Fa >o % H Q : B&J~R~~&HE,B,R~~WBJ~~%, BRJ6El -1. liBRB'.mBT errno rg~a : :ACCESS key ~~B~E,RP~YI~E. igaeroreaw. EEXIST key fi%%B%,&l!A?d, f!!%%?g ~~~@7k EIDRM 9% key Phtif89EBIA~l tgGBI$kO ENoENT 3% key I3 tPMiE,E. WSd WE, %& msgflg tk % iS IPC-CREAT ENOMEM &JL\~ @$z ENOSPC d3j3Ti1@~8tJEI@PA9UB%& H 0 #include #include #include #define KEY 1234 #define TEXT-SIZE 4 8 struct msgbuffer I long mtype; char rntext[TEXT-SIZE]; lmsw; main I) { int msqid; msqid = msgget (KEY, IPC-CREAT 1 0600) ; if (fork (1 == 0) ( msgp.mtype = 1; strcpy (msgp.mtext, "Hi! 1 am child process!\nm) ; rnsgsnd {msqid. &msgpr TEXT-SIZE, 0) ; /* {4%E,~,9PA34 */ return; ) else { sleep (3) ; /* %3i&@hq?EF */ msgrcv (msqid, &msgp, TEXT-SIZE, 0, 0) ; /* #ltft~$I]k!I~,$? print f ("parent receive mtext: %s" , msgp .mtext 1 ; msgctl {msqid, IPC-RMID, NULL) ; /* PdWRE8Wr] */ 1 parent receive mtext. Hi! I am child process! : msgget, msgctl, msgsnd . .- . . a%*'# : #include <~ys/type~.h> #include csyslipc.h> #include &a : int msgrcv ( int msqid, struct rnsgbuf *msgp, ini lnsgsz, long msgtyp, int msgflg ): BjBirLI : msgcv () n*k%@ mqid idT2B(JEBI$ABdi3REdGI&$, %88TD $!I msgp %tPWf%&&lb. msgbuf #$X$DT: strucL msgbuf long mtype; /* E,%,H@% */ char mtext [I] : /* fi,gg@ */ 1 ; Z%& msgsz %E,@bBBmE&, EP mtext &&stJ-tiEO B& msgtyp EH4tEZwi%$@BtlEJR;WI%: msgtyp = 0 &BMYUfi %-- JAi$;.@-. msgtyp > O &HR?1Al4lE msgtyp -4 mtype @H;le5E.B, msgtyp < o GDBhFflh%-Jfi mtype l\T$%3 msgtyp %jmQ~E.R. ,!$! msgflg Ffk&M PC-NOWAIT, %BE$%WPU&@RE,BNi$I 11GB%lif9 8@p %fl ENOMSG. @ t rnsgflg iffa MSG-NOERROR. Elk~l~lB%& mgsz H+-l$@@. Q : ErJjBldf ~$P~~&&ZIJ~E~~~~BEE* RH -1 C$ial$,BB'f errno +, #gRfl : E2BIG iEt&B#XEIfR&%! mgsz L'lftiBf MSG-NUERROR. EACCESS %gk!$k8X%f$,&W?do EFAULT %&msgpf~~%!&~~@&~kd ElDRM &@ msqid f!fiaft!&B Pi?fl E%#h% EINTR %f+@%NR/h HEJB~BRT@~E?~Ha ENOMSG @%msgflgijl@ili-NOWAlT, ihR;"IJh$iAi%,l%J$o ig$+% msgget ( 1, #j$j$jB : msgget, msgctl, msgrcv ex$# : int msgsnd (int msqid, struct msgbuf *msgp. int msgsz, int msgflg); @i#MW : msgsnd ( ) M3k%@& msgp g$3?%,$.%2@@ msqid b%,BlSh?dh, %# msgp % msgbuf %%I, #?i?ff$l!F: struct msgbuf C long mtype; /* E%t!#lGk, 2i%JjAT 0 */ char mtext [l] . /* G,e.@# */ 1; S% msgsz jtll,RBBB(lEE, BP mtext D%BCIKBo msgflg bTuiQ& IPC-NOWAIT, &Q~$UJBfi~~i2$$Z$~#f&fA~ %%qk.zhE,Bl RqzE[r%a EAGAlN- @gRli!$: EAGAIN $%!? msgflg iff IPC-NOWAIT, fipAPIE%o EACCESS X@.I%!%A%~E,RL&Fd* EFAULT @& msgp f%ilE%H fi&hhhk. EIDRM B$ msqid %@mPBPhll B@Pl%o EINTR bh?~~%~&?%@~~%~#fi?$&Q EINVAL %aBc]$& msqid . msgsz E&$% mtype d\f' 0. SX a : int semctl ( int semid, int semnum, int cmd, union semun atg 1: : semctl ( @KThf13&*B%llE%fl?flmg.fF0 $@ senlid h@kkEb!l 8% cmd %~'&@%j~#fi, %T?ljA#@@: IPCSTAT $%$flBgh!l sernid-ds %&&#B$I~%$ arg.buf, IPC-SET %$% arg.buf f~fr#$@j semid-ds !$$fi+m sem-~rm-uid, sem-perm.gid 50 sem-perm.mode # $4 (lft; b 7% h!l semid-ds $?Qh 0 IPCRMID W)I~~~?PhF'fl@~&%~@Jo GETALL %E 9 P~t~~I%~%~~ semval I&E%13~JP% arg.array o GETPID &~{~'$~A?~~sempidffio GETVAL #Ef$?Ph?l]qB% semnum jEsge8tl semval @$iBa GETZCNT il%@E+l%?l]b!l semzcnr @, SETALL RE% fi ~~W~G!E&IYI semval &$i%&@=B arg.array. SETVAL #I! ,E ?i Ph Fd CP 8 @!i semnum $E 2 % -k KI semval @i -ift arg.val. union semun IYJZJL$IT : union semun { irlL val; struct semid-ds *buf; unsigned short int *array; struct seminfo *buf: 1; val SETVAL ffl @I sernval @, bu f )i lPC-STAT $!& PC-SET m b!l sernid-ds %#I Array GETALL 8 SETAL mb%!l%o semid-ds b!l%&J%x@T: srruct semid-ds ( struct ipcjerm seuerm: time-t sem-otime; time-t sem-ctime; struct sern *sembase; stsuct sem-queue *sem_pending; struct sem-queue *sem_pending-last: struct sem-undo *undo; unsigned short int semnsems; 1; $em-nsems E?%+mB El o shm-cti me k-&$!ffl semctl ( ) ~~/~o sem-otime k-~flH semop ( ) ~~ls7. sem-perm ipc_perm &$&$iX%&% msgctl Oo sembase @&l%fgm sem zx@T: struct sern i int semual ; int: s ernpid ; 1; semval ~a n 7 semval sempid k-?~~~h&@%#~%~~~%~~~~~ : EACCESS 8% cmd 31 IPC-STAT, @E@IJE!~@iC%f$YPAFIIO EFAULT 4& arg.buf d srg.array %m%%t)eJm%%!lYo EIDRM 9% semid RBBtlEBR?lJ EBM%. ElNVAL cmd i% semido EPERM $%? cmd % TFT-SET 8 IPC-MID, hp%W&k%ljl(R%% ERANGE @%k cmd h SETALL 8 SETVAL, @@k$?~b!l sernval 0 SA'F SEMVMX (32767h : int semget ( key-t key. int nsems, int sernflg 1 ; fiaay : semget ( 1 H %@#Z8@ key ffr%%B(1E'?~iXXlJ{fY $1%%& key 3 IPC-PRIVATE Rl1$$~2%ffl16?Ph$'~+ && nsems %d$%%~!?b!!% 1 . !% key $%I IPC-PRIVATE, &TG$G$bBCld9RPI IPC key. 4a%& semflg gg$f IPC-CREAT f$ (semflg & IPC-CREAT %& 1 % &z IPC key %@% key ft!Jfi%fi?h Bn % $3 !@ semflg $3 IPC-CREAT @ IPC-EXCL 43, i$~ fik @ & key ~&2.1Z4WBd9 RI%Z%!~$W?4C@$E0 #PIv Pt semflg &W %l4t?ZE3MFfl~@X#1R7 WBtHJ'f open 0 W@& mode H& (M t712+i*H~* aHK@ : EACCES key ~$~~%Ph~fl~@&~P%@!k~@~o EEXIST AB' key fij%6W{Z4 WT1. I!3E!,$31T?k0 EIDRM $% key %%K~~?l%?g i3~fiH%o ENOENT key Fr%FJtlb4PA~d;l;Bb, $% semflg &*6! IPC-CREAT fi, ENOMEM &~ttF+jj%FhL ENOSPC ~~~%%fi~$~&~~~~?~?~@%@o : semctl, semget #A*# : #include #include #include # : int semop ( int semid , struct sembuf *sops, unsigned nsops 1 : @B~g : %& semid jtl@fif@lig?WIT~i2X~J~~R, %?& sops %fi%@ sembuf, #%RI2X@PR: srruct sembuf { short int sem-num: short int sem-op; short int sem-flg: 1; semnum ~fit!f#~E?tI$R~ 0 it%%-+iS9 sem-op : 1. @~%kiEiAtF 0: rtt;B2h~!Z semvalo 2. %&{14@;f 0: semop ( ) &% $4 semval % 0, @-lE sem-flg % &!$TE% IPC-NOWAIT, 3. Pal&&ICF 0: Z semval AT33T sem-op FJ%RE~ 811 semval @~fi2&!i& sern-op IXJ#!X$~~ i$ semval kt: sem-op B(1%$t fi/lhH sern-flg % 8 IPC-NOWAIT, !j!Y semop ( ) &aBq~n%%e 8% nsops .It%&& sops B(J%%IL H . jg H B.: gaaFlJ&a 0, GN'l&lFl -1t @%%BGT em0 +o #$B{%@ : EZElG EACCESS EAGAIN EFAULT EFBlG EIDRM EINVAL EINTR EINVAL @% nsops kT%%fi (SEMOPM>. ZiYNGR#Z~G'3!%~o H + sem-flg Bg6tlQT IPC-NOWATI'. @& sops ~3T;ilX59rnhG~kEO sem-num @i&/S\T O %AT%TfE3%kKWH %% semid %%mE%PhFflE%fffij%o %$$#J@% cmd 5k =mid6 ~arn~e~~~wr~~~9m~. 9% semid %%@J!$q;f;G?&bb? 0, t!#ITiT%E nsops % @i&* shmat (attach W$&@) : shmget, shmctl, shmdt &x @j@ : void "hrnat (int shrnid, const void "shmaddr, int shmflg ); &&i#l : shmat ( ) B]3k~~~ shrnid %#$8(l%f hfifi~@d%$%~ (attach). B%? shmid %%i?i@&%$h@@8fit@, shmaddr fiT$l]hHfi$ ?X: .f. shrnaddr )tl 0, @~D$~~$f-'?&hhO shmaddr $% 0, 8% shmflg SHM-RND Rlfk Rl/U@@ shmaddr % g% %kt .f. shmaddr $% 0, @s$& shmflg @P7 SHM-RND E&, R481 shmaddr $$ad$@@% SHMLBA ft!J!k%kih.b shmflg %Pl k& SHM-RDONLY #$#$, fi%h%###,R SH3k& @%A$hGo Q : brji~hlll&l@lBZ%HMaM9 BAlH@I -I* %iBRHB'F em *o MBiBM : 1. @i2a fork i) 6 FBE%#@Cig@~#S~~&kEO 2. &f$2 exec > 6, E%@b!J#$&$FakE$ $%bB$$ (detach), 3. &%%%@%, ~~#$J#!$F%'?~kt&%?t~$!i (detach), #Efi@ : EACCESS Z&RUB$ZSS@%~fi#Fa EINVAL z%fl!J @$!I shrnid % shmaddr; 8% shmaddr Nhk#%%gaR it;f%&%i%?i!f SHM-RND #%go ENOMEM &~~fi$f$z~ #define KEY 1234 #defineSIZE 1024 /*%J$3m#$P]6k!!*I main (1 i int shnid; char "hmaddr; struct shmid-ds buf: shmid = shrnget (KEY. SIZE, IPC-CREAT 1 0600) : /* $h#$&g I/ if (fork () == 0) { shmaddr = (char *) shat Ishid. WLL, 01 : strcgy [shmaddr. "Hi! I am child process!\nm) ; shmdt Ishmaddr) ; return; ) else I sleep (3) /* %+%Ejhf7%$ */ shmctl (shmid, IPC-STAT. &buf) : /* @%#f h%%& **/ printf IHshrrc_segsz = %d bytes\nm, buf.shmsegsz) ; printf ( " shcpid = %d\nu , buf . shm-cpid) : print f ( " shm-lpid = %d\nM , buf . shm-lpid) ; shmaddr = (char * ) shrnar. (shmid, NULL, U ) ; printf ("8s" . shmaddr) ; /* 5m%%fi6fi8 */ shmdt { shmaddr ) : shmctl (shmid, IPC-MID. NULL) : /* ~t%#SI%% */ 1 1 sh~segsz = 1024 bytes shm-cpid = 1176 shm-lpid = 1177 Hi! I am child process! : shrnget, shmat, shmdt f#jS : int shmctl (int shmid, int cmd, struct shmid-ds *bbuf 1: @@#w : shmctl ( @#Th##fj&7k#'%I]%$h8~#fiO $a shmid %&!&@&I &?fi&%!di%% &@ cmd %%@%!b!l$E6v gTFUl,,%g@: IK-STAT fi$k$fi@@J shmid-ds %@&#@$I~FI]~I& buf, IPC-SET %$a buf Wig@!! shmid-ds %ft 8(1 shrn-perrn.uid, shm-perm.gid ;#l shm-perm.mode B $34 f l# f h a shmid-ds %&I , IPC-RM ID @~@%~h6@#~@%~o SHM-LOCK $it##f hff!iS%ElI swap. SHM-UNLOCK Air&#$ hffB@l swap, SHM-LOCK @ SHUNLOCK Linux %I% Hq%!&BMP (root) ki+fEfflO shmid-ds I% % @I T : st ruct shmid-ds i. s truc t ipcjerm shrrcperm; int shm-segsz ; time-t shmatime; time-t shm-dtime; time-t shm-ctime; unsigned short slm-cpid; unsigned short shxlpid; short shm-nattch; unsigned short shm-npages; unsigned long "shmgages; stxuct smdesc *attaches; 1; shm-segsz #$ABB(JR/J\ (bytes). shm-atime @GAR attach k$!~h@b!l~l'~p shm-dtime &%---I%, detach ~~~~~~I~I'S~~ shm-ctime @G--&Z3stt;%$b miwt!l~l'~ o shm-cpid @ak%$b@~%@i~#~fi0 s hm-lpid @G-+@i$k%$ ~SJGH~@~RRIJ~-~~ shm-perm F!@M&%+Q ipc-perm ZXiE%% msgctl ( )+ f& : E&$~!i!~li!ilE! 0, ,BF~J&H -1, !iEiZRBIGT errno #$@Re : EACCESS 3% cmd IPC-STAT, @%&!E!#@%%$mfig EFAULT @$k buf %l;i]%$&mfi @h!lkt: EIDRM &!$!shrnidfiMt!~%~b%~%fl~+ EINVAL %#!I@@@ cmd $, shrnid, EPERM $3& cmd % IPCSET Zk IPC-RMID, @%~&h!l#~%L570 i$B% shmat ( 1, shmdt (detach %$ h@) : shmget, shmctl, shmat : #include #include sx a & : int shmdt (const void *shmaddr 1; &#NM : shrndt ( ) H%$$%maj shmat 1 ) g% (attach) Ul?!J#?&@!$% (detach 1 Hmb!18$!i!o @& shmaddr %5km shmat (1 &HF~2qhfifihbko M # : ~&lhRll&D 0, STRlJ&H -I+ %%RUG7 emo qP #$#$RE$ : EINVAL %%l!l!l@!$! shmaddr: 3% shmaddr h!lhk#~k#$h@h!4bkQ i$f@% shmat ( 1 #indude @a : int shmget (key-t key, int size, int shmflg j: @aBW : shrnget ) aj%@@g& key fi%R%b!f#$hgi.8I]it:% #.llXg# key % PC-PRIVATE YlkP8%@1#$kG~ #hj]\h$a size &a. a# key $% IPC-PRIVATE, &;l;EZ%i~b!~#$h& IPC key, BP&%%F!;firl%@ $@ shmflg IPC-CREAT I? (shmflg Lk IPC-CREAT ) IPC key 3 %a key &#$ h a. $%g& shmflg $ $7 IPC-CREAT @ [PC-EXCL i8, fi%?&%&!% key 3k@2#5?F?li% Bl%%i&%%%'? Egg. Bt;9b, P#lr shmfIg &Msft.&%#f AB858NJ!Rg ffQ&33 open 0 WISB mode ME (&~~+.f2H)o @@Re : EINVAL B& size SHMMIN SHMMAX, EEXIST #kar$P key fi%&8(1#f ha7 @L?&%E. EiDRM P%k key fi#%t!fRqfi l?E~~!WkkO ENOENT B#? key %%Ef%$b7'%$@%1 2B% shdg &%8 [PC-CREAT no ENOMEM @&lh @$h! . ENOSPC Ej&!k!%%,fii?@@~fij&$ b%@%E (SHMALLI t%B% shmat ( ##a& : openlog, syslog 5&*&# : #include &a : void closelog (void ) ; @##lJQ : closelog ( j FRlt%%EB openlog ( 1 flflBSE,BE!30 'BH@i:% syslog ( 10 #gE@# : syslog, closelog $&A# : #include &x : void openlog (char "dent, int option, int facility 1; : dheam : openlag (1 H%!@ syslog 0 %!M%b!J#%Q ident 87+$ffl%)kBB4& iF33b?#ZFF%%, -&%H argv[O]* B& option ~LiB7;311hH%~fi%-k: LOG-CONS @%%%%E,I?kjl:E syslogd WIJB@%$ $11@%4& (console 1, LOG-NDELAY g@flR@ syslogd @Js%, -fi$$&%H%-&fiE ,~~Z3@$%~%0 LOG-PERROR $hi ~ %&?#$$I dj $?& stdem j LOG-PID #%,B?fi*b~k?%E,e~@8@iE#l]@~ BllP facility tBEBW RBTPd he: LOG-AUTH ikiiE~%@~d~o LOG-CRON @ cron 3 at #%@~%ltWZ,@+o LOG-DAEMON %% daemon P&@l%,8. LOG-KERN R4y?k &E>ao LOG-LPR ?m~~fi~~~%~fi,80 LOG-MAIL & mail m#~~f"*~F~fi,@~ LOG-NEWS h news #~R#!?@F~FJ~,@~ LOG-USER A P #%P& ~!J.I$~& c @@#I > LOG-UUCP d3 uucp #w%&$P*mi%,&o LOG-LOCAL0 3 LOG-LOCAL7 %I% fd I'BN E,R@%&E,K /etclsyslog,co~~f PIE %Stf;l&% kt! , @J$n : mail. * /var/log/maillog RII%%E 3 W-WdL Bfii$,B??-7+ @ %%$$I i4 ~var~o8,~og Z!+ . : voidsyslog (intpriority, char*format, ... ): @&i#W : syslog i %iH printf (1 82, ,91$31+0;(21$$DEB?R$M%S%% El ZfclW 0 BB priority aTUH~t8~~S,&~Ipp%~%%o B@ format KIS~$@&% IS5 printf (), +T$S&8&T %m WH19Ff&+EWt errno ~~5% i26%?7$* 0 $a priority ~1i$,BRWJ83 openlog ( 1. B& pot BSiS,B%la%-FFUhff: LOG-EMERG ~&i~h4i&~~#%%mi~,~o LOG-ALERT @BY !m~%8~,@o LOG-CRLT FIEWBG!E,B* LOGERR -&m%%E,B4 LOG-WARNING gBg,B LOG-NOTICE BE@BmE,B+ LOG-INFO %%#I% FJ-E,E>* LOG-DEBUG @@#!*I%~$,&Q #include isyslog.h> main Iint argc, char *argvl] ) i char *str = Otest"; openlog (argv[O], LOG-PID. LOG-USER) ; syslog (LOG-INFO. "%s\nU, str) ; closelog I) ; 1 .. . , Jan 14 16: 44: 22 localhost ./openlog[898] : test ; putenvy setenv, unsetenv @ : char *getenv (const char *name): i?&&iRM : getenv t ) H?#%RBB name ~;%%EI#BS~ %& name %9r&!$Eftj %% fiuR~9&GE!~J%&~t~~~~~~~Ett 9;E%EmB&,% name = value, ig H #i : ~t~~~h~~J~~~~~~~ %rn*Fiff, ~4~$~Remq&~Eg%Rq~~ NULL, /* l$!i%T$#%# USER h$ */ #include main 0 I char *p; if { Ip = getenv ("USER" 1 1 pxintf { "USER=%s\n", PI ; mJba& : getenv, setenv, unsetenv : #include gg : int putenv (const char *string): @@&&la : putenv (1 H%B2f#laQllaG%2@mhB* bB sfiring B(IP&% name=vaiue, BUSB$W@.B%R%&!E, R4%t&184RP% string &$+ ~NIJ~S%~~B~E%%M~%~EO /* i$!Pv$$S# USER test / #include main () { char *p; if ( (p = getenv ("USER") ) ) printf ( "USER=8s\nU, p} ; putenv {"~S~~=test") ; printf ("USER=%s\nm, getenv IUus~~* 1 : 1 : getenv, putenv, unsetenv : #include ex : int setenv Isonst char *name, const char *value, int overwrite ); @&aM : setenv () ~~%&%$~~uV%~GH~B~ ~34% name %@%%%%%S ?$$, B% value !i!ll%*Efi%, @!!& overwrite H*&ZEGZ&%CG IQYJV%,~B~ jk!/? overwrite 73.j 0, ~Z~~~BE~~~%, F4Bh 82@&9j&& value W~%~~J~EIJ-JB; overwrite $1 0, HI%^+%$ BRE6h?FI RllB33 value %%,Z!@I~ & Ig #j : #k~T&~h!~JiFm 0, ?wME%wan -I* /* islS putenv 0 SeP!flR$% */ #include main () I char *p; if ( (p = getenv (WUSER") ) ) printf { "USER=%s\nm , p) ; setenv ("USER", "testu, 11 ; /* i!$f USER %&$#m8% test */ printf (nUSER=%s\nm, getenv InUSERn) } ; unsetenv I 'USER" ) ; I* $@% USER f;r.i@&f */ printf ("USER=%S\~~, gerenv Inu$~~") ) ; 1 USER=root USER= test USER= (null) /USER fl;#$f d;j$EiSR, fiM%!2$R$ Inull) */ M%&& : getenv, putenv, setenv sx@# : void unsetenv (const char *name); &&a&j : unsetenv H%lf&%%EB(J4;R2Smh@0 23% name %%%RB(J4.tji %E%WR$O SHB:Z : #include #include sx fi a : int regcomp I regex-t *preg, const char *regex, int cflags ): &&i#M : 25% preg %--EH, pattern buffer, 81~~~%~~~~%.$& regex Fi hZFlJ%%zlti18~?R*, fiD% cflags Wlj2B#Z 23% cflags 67;8d,JL@ti$R, 'RNLJa OR (1) 33% REG-EXTENDED @ffl POSIX {q #%%i%% ;;?$. !!lJ@ffl POSIX B$i6&6 REG-ICASE m!$%+~tt!J 35% REG-NOSUB ,$?@@?!$ nmatch pmarch, REG-NEWLINE %%%%.%$2g@fi??g!$k@, %%%R A J!q %%?t? $ F~~fti!? &fT?Rrn3-+EBo a 9 : @f &~h&El 0. %%2B%~R~J&8QZI1. 88% regenor 0. r#aa~ : iEIJKk33 (regularexpression : @W RE) RH~R$dFS%X~~J!I~ %-=$*%%, UEi$#%2Mn #i$-&?Rrn%%??+rn%: .--R+'R: 4b .V+~$I\[H) 0 0 67+9Lh88. %%?R: + R%-+iZ!q?fi, "abc." !fZ!? "abc" fi[.kEiq-'P%?$ - ? $f&ij~---i~$~@IX~~@, l'abc?" i'@ "akl' 5t "abc ha ki3FJ -- +??4 "~%fi~~b%3Nfl!l??fg fill "abc*" I%!?EB~N "abc" b!l%'?@ 13 3 "abc" ., + .f?,rREBkbfJLE$ -?A&?@, 4a "abct" {t%Efi!!i%k?~j,% ''abcll @J? R;&+ "abc", I fit%% (OR) b!l%,l, $ "sbclABCm fib "abc $ "ABC", \I R%7;'-?@4E%%g:R, frEl "\." RS %-l$;tFfio A 4f%@@%rn?R@ &3Rat3gm $ ff%%@%W?R*&~3~&%TEP [ itsFT+%e%%* 1 m?fl%emfi %R%W [%it%%&* J B!U4t3%%* aa?R%*hmR%*T7dh $+%a: A ~%E?$f%e& la 0 - m%%$%@? [a-21 $ 10-91. &TEh+#%%HTR%e%%$Q&x; [: alnum : j %!234%89, @if% [a-zj. IA-Z] O [O-9). [: alpha: 1 %2?@-~ Rfl!f% [st-z]\ [A-Z], [: blank: ] $&@ (space) Ek%4$.3% (tab), [: cntrl: 1 ASCII R@@$3?%9 [: digit: 3 8d~1fi%3ft%I Eo-91, [: graph: 1 qflW?% @$@ae#?@ (t'h [: lower: 1 fJG%ft8-@j BP [a-21. [: print; 1 srlTrsrJN7Rt aB%&Tfi ('90 I: punct: ] kfiR5%R%R?f flP4PWL 4k&?NP%k%@. 1: space: j dt32# (' 'h 5?# (%'I. CR (V), (b~'). @j3@ & (lvt) EtBB (V) mmao [: upper: ] kS%*?.f$l ap [A-21. [: xdigit: ] ~h~f~~%%, ap [0-91, [a-fl [A*% sx : size-[ regerror ( int errcode, const regex-t *preg. char *errbuf, size-t errbuf- size 1: W : regemor ( 1 M %R.l%'i! regcomp ( 1 iji regexec ( ) HU EEel, 9 B encode %i% regcomp (1 % regexec (1 $HmESffW, m&& p~g &--E ffI Ekl pattern buffer, @& errbuf Efi@$fB&ffqf$@fi%#g1 @ % errbuf-size 811 ?$! $$# EAjJl #include #include main I) regey_t preg ; char *regex = "[abld*; char errbuf [256 1 ; int errcode; errcode = regcornp I&preg, regex, 0) ; if (errcode ! = 0) { regerror (errcode, &peg, ersbuf, sizeof (errbuf) 1 ; printf (wregerror : %sinm, errbuf) ; I regerror : Unmatched [ or [A : regcomp, regerror, regfree &Xi&& : int regexec (const regex_t "reg, const char *string, size-t nrnatch, regmatch-t prnatch[], int eflags 1; EiaBW : @$fr preg %-.@%tt, %6J pattern buffer. &3~5kBB regcomp 0 PRG @%%. 98 string %I%%~~b!I%'$bh!dh %a nmatch {t)ia pmatch &#f%kb, @& pmatch - regexec ( ) !AkkwJ%tl&Do %!i @ regmatch-t Z ,X !&I 7; : typedef struct ( regoff-t rm-so: regoff-t rLeo; j regmatch-t; rm-soR%Re%~$m~&~B 0 m-eo itsEe%!+m@%Ezb rm-so BelE3i -1 , lW,31tl;%@#*ik regexec ( 1 45RJ $s& eflags %WH7EQ. q@H OR (1) I%: REG-NOTBOL ik%%?fi m$ffl0 REG-NOTEOL iw%??f $ ~~~~~ #include #include #define nmatch 5 char *string = "Tabs axe 8 characters, and thus indentations are also 8 characters+\nm; main 0 I regex-t preg; char *regex = "charfa-zl+": regmatch-t prnatch[nmatchl; unsigned int i, len; /* pattern buffer */ bzero (&preg, sizeof (regex-t} 1 ; if (regcomp {tipreg, regex. REG-EXTENDED) == 0) { if (regexec (&preg, string, nmatch, pmatch, 0) == 0) I for (i = 0; i < nmatch, it+) t if [pmatch[i] .rLso == -1) continue; len - (pmatch [ i] . rm-eo - pmatch Eil . rm-so) ; printf ("%.*snap len. (string + pmatchiil .m-so) ) ; } 1 reg f ree I &pxeg I ; I characters I : regcomp , regexec, regerror, regfree i %& # : #include #include uegex. h> & # ; void regfree ( regex-t *preg > ; : $m@flftf+tdr%$ regcomp 0 40 regexec 0 pattern buffer. f Sfe preg h ~k pattern buffer 85%# am#:% 73 if$$% regexec ! 1, : dlopcn, dlsym, dlenor &a jePI : intdlclase (void *handle ) ; a&r#M : dlclose ( ) H%R&B& handle @2b$iifi.f%&2W-%!$id-&, $%%;it& 31 o ~~~~~#~%~%~~@*~~o Z?I&N%HB%~+@~IF~SB~R dlopen ( ) i3 ,&fi#B~ mmaw : $@i$Wi%%~nA -Id1 @&@a : dlopen, ctlclose, dlsym : #include 1 : const char *dlerror i void ): i?qJ&rir/l : dlemr 0 B]f@B%m dlopen Oh dlclose 0 % dlsym 0 Wfit5k&l %$I?$@~ dle~or 1 fi&i%--'?%i@ih %%%i%%k@%i$!Rme@ 7%k#!?g, q,@ dlerror C) &P#E.Jk~&%~%%%%(I Blk, dlerror () ,0, 2%H-&#% #l!$?@%'R?$#lf#S, dlerror !ll]%$$%&B NULLQ $JNZq%#, dlerror 0 B%~&iElffl% dlsym 0 G+~lflW. WEN 6 dlsym (1 ft$~BfiR%QEQ. m@ay : %i+B;f%j~A -Id1 BBo H @ : @R@B3&@&EW%@SE&Fd~H-+B%3%@@HH?R%B Ro GRl]&a NULL, /* TrnE%flrn#BER */ char *error; /* &F dlerror (1 R&R--~#&~Kl, m&!k$f&%g */ dlerror I) ; /* %%@%E$@ */ function = dleyrn (handle, symbol) ; if ( (error = dlerror != NULL) { /* fgg*f'g */ 1 : dlsym, dlclose, dlerror a : void *dlopen ( cons t char *filename, int flag : : dlopen ( 1 813k%&& filename fk%@h&Z~@&fC{$%hTl]h@+ %%& filname NULLl !l~J2;fJR3@*9mfi1+0 WW!& filename #HFH / ???Tf-%~%Xf%@, dlopen ) @~fifTF~h!l&@@%%i~f$if~: I. fi@%%B LD-LIBRARY-PATH Eftgo 2. # /etcAd.so.cache Rf* h @+ i$ ffl@@ 3. M lusrflib TZ/Zfio 4. fi nib B& flag WLPbSiJk: RTLD-LAZY W@&% %%%gNB&~R%b FMZD-NOW wJ&@, %~%#$fi~%&~~fi~o RTLD-GLOB AL fiWWfl?* dlopen ( ) &lf Iji?,h6$ifddfiikT%fI!J~$~&f~@, kit@&! %i$ dlsym I) 2 dlclose (1 i%%bfJ@8!. #include main () { int (*myjrintf) (const char *, ...I ; void *handle = dlopcn (*/lib/libc-2.1.2 .sow, RTLD-LAZY) ; mygrintf = dlsym (handle, "printf ") ; /* f 8 printf */ /* @jM mysrintf WPfiM libc-2.1.2. so F@J printf 0 */ mysrintf ("printf (1 's address : Ox%x\nn, mygrintf) ; dlclose (handle) ; 1 printf 0 's address . Ox40067f4c a# @ a : dlopen, dlclose, dlerror @J & : void *dlsym i void *handle, char *symbol 1; : dlsym ( ) B]3kM#$X$&$#&a&F6&0 @@ handle %flBj dlopen () Fr%HAtj#$HPR6-3i 234% symbol J!IlS5BB3R9$tJg%?R% dlsyrn 0 $%@$??!f%l?!J&hh, @%&B NULL 10) $--@@a%fi# %%!k, T%J$k%~$bb$ Bp% 0 ~l&~%%k%%fi~ dlemor { 1 *#/wrBBBrnE*%8Q % M @ : &f?&Jb~%sfi#T%~%$%%j%RC $I%%@ NULL %%% dlerrori ). I $a : int gelopt ( int arge, char * const argvfl, cons! char *optstring : B J.: getopt O a#€+ @i$+GFiPB. $& argc ugv %ELI main (1 E%WJ %&'Pi&@ PIC. PL optstring RW!?R!&EtYJ%Ifi3146. &EIL~P El$ argv PT-+Btl%N?~~ kL99-$NBtt optstring +HS@o $ R%Ja?R*Em?QGg%s3 I9 Rfl%%~fi@xm~&~ *WB optarg &Pel k&W9bPt4 getopt ( ) t%$?fl@8B(lS&R4+EP &f$i2@,B9 #@f @i2f optopt $3~ '? +'A, @%$%B getopt( )@ $%WE,!%, !l!RZ%f @XB opterr 8% 0 @Pa #include #include int main Iint argc. char *argvCI 1 i int ch; opterr = 0: Ik ;I;#TW&%%GBI * / while ( (ch = getopt (argc, argv, "a: bcde') ) != -1) switch Ich) t case ' a' : prinef I 'option a : l%s'\nn, optarg) : break; case 'b': printf ('option b . b \nn) ; break; default, printf ("other option, %c\nu, ch) : 1 printf {"optopt = %c\nU. optopt) ; $ .lmjetopt -b option b : b $ . Igetopt -c other option: c $ ./getopt -a other option: ? $ .;getopt -a12345 option a : '12345 ' : ttyname : int isatty (int desc): atal : $ tB% desc WiifZH*{+l%ia7% --%%#~J!II%EI~ + BR&B O. i8@% ttyname C 1 : int select ( int n. fd-set *readfds, fd-set 'writefds, fd-set *exceptfds, struct timeval *timeou t > ; @gPPtaM : select (> HWf+*~$#%d#%N&%. 8% n .ft%SAm2i+G%i4 $ 1, ,,@ readfds, writefds .#l exceptfds %% #%flffl, k~?k~~i%# Bid@@, F%@J91tt9#Ru @T&%g#T~BSZ@%&i4tH@ZZ: FD-CLR (int fd, fd-set *set): R~%%ik!i!%id% set qhl% fd PJ~T~ FD-ISSET ( int fd, fdset *set); #E #!l] 8g % iq set $ # % fd b!l fi 25$%I%o FD-SET (int fd, fd-set *=set ); Ja$5!$f$%$i4tR set $El% fd &I@, FD-ZERO Ifd-set *set): id% set ~~~~?o @% ttimeout ,YJ%~O timeval, RJ%%S select ( ) tB%R8;fl'fJ, Y&$&J% X@T: struct timeval I t ime-t tv-sec ; /* */ time-t tv-usec. /* %@& */ 1: a a : timeout 8% NULL (01, !I]$% select () i5Si'g tirneouto gg.fe@$ : %47~JhRlI~~ft~TB&~%SCB~H+B~ ?IUR~H 0 R3?&fi2!i3 4k&&&iE&CL timmut Nl4, %Gt#iE2%@!lf&El -1, f#GRB% 'f ermo, dk8lf@%? readfds, writefds, exceptfds @ timeout b!l@L%h&$W @@I 0 EBADF *PHi%%dh%@~%%~f%~%l'flo EINTR ~d,@6?%$%0 EINVAL HI! n %%Eta Trn3*Nrn@B5rn%B: fs-set readset; PD-ZERO (&readset) ; FD-SET Ifd. &readset) ; select {fd+l, &readset, NULL. NULL, NULL) ; if (FD-ISSET (id, readset C . . . 1 mxa# : isatty &x : char *ttyname ( int dex ); #include cunistd.h> #include #include ~ -Wparentheses fiU%@?i&%@lf l@@%@~~&~@~~&Elrf~~%'F%~fi81 @I@: if (a> if Ib) foo c ); else bar (1; 1 BW9 else EWSHEH if (bj, U&BN@@o -Wunusd ;tm%FY@$#gB@EP#*@H, @J%f !!zEE,Ro 411%%@B.f4!$ i33&&fB%kGZ@R fliJ!!3Mfl42H -w -Wunused i&J@, -Wsign-compare 38% $3 8%?# e8 % signed @ unsigned kt$,? Bf, signed f& %&@ unsigned B;fFJflk&iSfi-%%%* k&%&%f.%E. -W BQ flfik&G? @R$@N -w W%,@@&%E+ qig1 -w - W no-sign-compare . d@%fiR%$E>RO ~fl*z%am%%6,Ro @I@&THa&* 3&aa$EflfiH%mB WRI]P&%E: fcm (a) { if (a>O) return a; 1 -wall HB4t@+WikE&lbl~HS+P%E$~BEEd@~b - ff ast-mat h Jk%J$!%+ GCC %mqb ANSI IEEE &@3kh~fi@fi%$f~@I] M, GCCeEB#% sqrt 0 IFI&&%%~E@~ E&@ '-ol;f;2% lk%J%UR 0 -fornit-frame-pointer 98fitFt? lWfkf%E@q ( frame-pointer), #%J@29 -E!4 6 Z &HB@$lF@o LX Linux %% ~%&-~~%%ILIAE,T%~~: push %ebp ~@m%&&@H 1 mov %esp, %ebp (8g@&@tBH) BRii3%YriZl (ret> leave #+ t%E@##!&%H) ,a% inline #%%, hk%Jfi#@%!fi8 inline EI&@R~ inline i!i!.~&~%&fTRfik&xgW$%G%~ %tfl!@ inline ~&09r%KliR1A%#Md!k!20 GCC G$eWB %%@@@E~"F~~FBEFJ#~B+~ inline E~&qUB&flal@% m@9bfitHo ikh%$&@, GCC $2#!{£fClfk6hb!!%fir fi&zh!l@B@@fik FiCfll%. IrRSb@RBf eB register XQq, BRJ GCC &;l;&f $JikSfi%E#2S@H 6 iB$7fiik#%i$, i.k GCC %8~$E~k~J~R&f7Wl'fl ~&Ja2 ?f -fthread-jumps 5 -fdefer-pop %af #$hl%#%ld&rh!l/ RYk&Zo BfiF~~~~&~, GCC 2P%&f?2EE!km@Ba0 2flR%~~BRi!k~t %7@9;EB~ inline @BR%b9#%% JQ 0 %J -02 #H&H, REHTRT inline 1%@@7fb!J?%J%~ #@%@A+f kLG2RK6A -02 +;F~%~uBBRIB fik&R!+ B$EP*B~J~rn~B@O $%a~@~B@ffl%~%o ENfi?A@Wfi%o ~%~~~B~Efl~~~@~o k%JR&&%i~#&Z*%~E Gffi@%fiRJfrn#i+O ~%G%%B~R*rnRik#@9 %%%#%i%-+%@H* @T #a: for (i-0; i<3, i+t) { a+=b[i 1 ; 1 #def ine DEBUG / * 22 DEBUG , lk DEBUG Tfi$fEfi#k@ * / #define MAX 200 /* EX MAX Bjtl 200 */ const int max = 50; 9aRA gdb HW, const %fi13%%FsB~-B;t~E#~~~ #E@HWi1 huk- jj: const int max = 50; %b%kqii&% movl $0~32, Oxmc ( %ebp ). ii'ijN #define 8 8 xH*B~P%azm%B* fin push $0~~8 /* Oxc8 = 200 = MAX */ #define IS-RANGE (x) (xi1271 && (x>O) /* (1) */ #define each (i) for (i=O;i 51 printd (dl ; else printf ("a < 5!\nmJ ; if (a r 5) if (d > 0) printf ("%dm. d) ; else pxintf ("a < 5!/nn) ; #define printd (d) if (d r 0) { printf (*%dm. 8) : ) IV5) */ #define printd (d) \ do I if Id z 0) printf I"BdH, d] ; 1 while (01 /* (61 k/ printf ("dm, d) ; 1; else printf [*a < 5!\nm) ; B&$ll, I&? else Ki$T/i. ": " , 2+~%%3i+2~J else MEK! if, ii5eFB It FI * EbZit0%%5if~klf~Gi%+r.1@~ %g%!&W?kQ@CH% (61 /r1138i@HB do{ ...... )while (0) %Gi. %@ffE%Bm%: if {a > 5) do C if Id > 01 printf ("%dm, dl t ) while (0) ; else printf ("a < 5!\nn1 ; $!ZSlffLYgWaf;f;W+f %%Ej13, &f7B++fil.]IT0 &!2Hl+fir5@11%% do {......) while (0) EsRstfiffJ ; 8, fiHd3f while h% 0, gcc @%$B1'2$31 do-while, H (Mefme ZXB(IBR.Z BiF@ibT~&B(lftP@@%.ffX%~!i~ &kPb td@W4\ m$getl@ti,MM@3t8Rt, i%iLE?dIR;f for-Imp #&#RT;g unroll-loop M% l'sJ&awr~m*Bo QHltRTqt%flk%%%BZREt &RG@&D#H%RB9 PnREgmw RBZI&~~~ZHMYRWJ@M gcc E%BS inline #IS%QX1%. 8188% Ctt N T inline &$I@%, a: inline int printd (ink d) if (d r 0) printf IC%d", d} ; return d; 3% @fflNkj&rfi%%@, k inline function %3CiELICJI%J4H9 R$ihPi2Kk& Jfi~*&ETFQ typedef char *va-list; #define va-start (ap , p j {ap = Ichar *I (& (g) +I1 ) #define va-arg (ap, type) ! (type *) {ap += sizeof (type) ) 1 [-I1 #define va-end (ap) #include void fun Ichar *s , . . . , . ,) /* . . . . . . %pMTg?$lJI$% */ { va-list ap; int t; /* 5 main 0 $m a, b, c mW@i%ZB#l */ va-start (ap. s) ; printf {"%s@, ss) ; while ( (t = va-arg (ap, int) ) printf (*%d ", t) ; va-end lap) r 1 main () { int a = 1, b = 2. c = 3: fun {"test: ". a. b, c, NULL) : /* 1;1 NULL i$i$* */ printf ("\nM) ; 1 test, 1 2 3 I 2 3 4 5 6 7 8 9 10 I I I2 13 14 15 16 17 17 18 19 20 2 1 22 %?@ SIGHUP SIGINT SIGQUIT SIG~LL SIGTRAP SIGABRT SlGBUS SEGFPE SIGKILL SIGUSR I SlGSEGV SIGUSR2 SIGPIPE SIGALRM SIGTERM SIGSTKFLT SlGCHLD SIGCLD SIGCONT SIGSTOP SIGTSTP SIGTTIN SIGTTOU %I* A A A A CG C AG C AEF A C A A A A AG B DEF D D D B*i#M ~%%~%%~~%k%f%~fi~@%~$%~~ (8 9 '!I~@&~RH?~NH~~N, Pn cTRL+€, @ileP &&E9 3~Q@&$?*Ek&fiEt, $1 CTRLt\, !lll$z? +. Ikj-S 9 mm~14r T --++em CPU #B+ ~~~%~~#~~fi~~@fi?:fkkf$ ?%?% fi#5 flH abort 0 B;f2?%kfgT Bus ~%$igNe?*&f$q 3%7%%SBfkIE4TI9J9b~;W8i7 BRW 0 $k$$2h!J($'yq jk{$?$#$#r;j&$,g@ EMF' l!l~~~~ 8tW&$%hi?b'W&&kk {KAP !a ml4% t8@€t!tEZ; @~AX~ER%KIBSH@P~FI~~: is ? ${H alarm > %$Zmwrd87E(F5%P ETXTBUSY EZBIG ~hf~&*f~~#~#~@~fl~~~~%~%A8*~%~ @&&mA

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 6 金币 [ 分享pdf获得金币 ] 0 人已下载

下载pdf

pdf贡献者

xuezd

贡献于2013-12-26

下载需要 6 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!