- /. - , /, fork - execl. , / - fork(), - -72- "", . 15.2.4.  - , , , , . - shell cshell, : ls | pr popen pclose, , - , , , , (. popen(3)). , - . pipe: int fd[2]; ... stat = pipe(fd); if(stat == -1) /* */... fd - , - - fd[1] , fd[0] . . , , , ; , . , , "0" - . , pipe, fork. , - . execl - - . , - , dup . , pr , -73- pr : int fd[2]; #define R 0 #define W 1 pipe(fd); if(fork() == 0) { close(fd[W]); close(0); dup(fd[R]); close(fd[R]); execl("/bin/pr","pr",NULL); exit(1); /* execl */ } close(fd[R]); close(1); dup(fd[W]); close(fd[W]); .... , , .... . close(1); . dup(fd)", fd, . - , 0 dup 0 dup . dup fd[0] fd[1] . 15.3.  "". (, , "" - interupt), - . , , signal . , , , "" " ". signal.h: #include <signal.h> signal (,)  - , SIGINT, SIGKILL, ... ( . signal(2)).  - , - , : -74- SIG_IGN - , SIG_DFL - . signal . , : #include <signal.h> main() { int onintr(); /* */ if(signal(SIGINT,SIG_IGN) != SIG_IGN) { signal(SIGINT, onintr); } ... exit(0); } onintr() { unlink(tempfile); exit(1); } (if(signal...) , SIGINT , . , shell - SIGINT, , . - SIGINT onintr shell . . - , . , , - , , read . - , , , read, , - . - , , , : -75- signal(SIGINT, onintr); ... if(fork() == 0) { signal(SIGINT, SIG_DFL); execl(...) ... } signal(SIGINT, SIG_IGN); wait(&status); signal(SIGINT, onintr); , , .  * 16.  . 16.1.  : : _ * & - ! ~ ++ l_ -- l_ l_ ++ l_ -- sizeof ( ) _ ? : l_ _ , -76- _: ^ () _ ( ) _ [] l_ . -> l_: _ [] l_ . _ -> * (l_) () [] . -> . * & - ! ~ ++ -- sizeof( ) , - , , - . . - , - : : * / % + - >> << < > <= >= == != & ~ | && || ?: - : = += -= *= ?= %= >>= <<= &= ~= |= -77- . 16.2.  : _ _- _; _: _ _ __ - _ __: auto static extern register typedef _: char short int long unsigned float double ___ __ _ __: _ _,- __ _ _ : () * () -78- [ ] ___: struct __ struct {_- _} struct union {__} union {_- _} union __: _ _ _- _ : _ _- _ __ _ _,_- _ _: : :_ : = = {_} = {_} : _,_- {_} _: _ - _ -79- _: {_} * _ _ () _ [- _] __: _: enum _ enum _ enum _: _, : = 16.3.  _: {_ _} _: _ _: _ -80- : ; if () if () else while () do while (); for(1;2;3) switch () case _ : default: break; continue; return; return ; goto ; : ; 16.4.  : _ _ _: _ _ _: _ _- _ _: (_) _: , _ _: __ _ _: { _} -81- : extern _ - ; static ; 16.5.  #define _ #define (- ,...,) _ #undef #include "_" #include <_> #if _ #ifdef #ifndef #else #endif #line "_"  * 17.  1: fgets fputs (. "- /. / "). -82- #include <stdio.h> char *fgets(s,n,iop) /*<=n */ char *s; /* iop */ int n; register FILE *iop; { register int c; register char *cs; cs = s; while(--n>0&&(c=getc(iop)) !=EOF) if ((*cs++ = c)=='\n') break; *cs = '\0'; return((c==EOF && cs==s) ? NULL : s); } fputs(s,iop) /* s */ register char *s; /* iop */ register FILE *iop; { register int c; while (c = *s++) putc(c,iop); } 2. , .sh 1 ... -83- #include <stdio.h> #define NEWH ".sh 1" /* */ /* : cc -o ds ds.c : ds : ds a pref suff a pref00.suff, pref01.suff, ... */ main (ac,av) char **av; { int nfile=0; /* */ char str[512]; /* */ if(ac != 4) { fprintf(stderr, " 0); exit(1); } /* freopen fopen, , . stdin */ if(!freopen(av[1],"r",stdin)) { fprintf(stderr, " :%s0,av[1]); exit(2); } /* . */ of(av[2],nfile,av[3]); while( gets(str)) { /* strncmp(s1,s2,l) 0, l */ if(strncmp(str,NEWH,strlen(NEWH))== 0) { fclose(fp); nfile++; /* */ fprintf(stderr, " %d0,nfile); fp = of(av[2],nfile,av[3]); } puts(str); if(ferror(stdout)) { -84- fprintf(stderr, " %.2d0,nfile); exit(4); } } exit (0); } /* */ of(s1,n,s2) char *s1,*s2; { register FILE *f; char buf[100]; /* sprintf */ if(( f = freopen( sprintf(buf,"%s%02d.%s",s1,n,s2) ,"w",stdout))== NULL) { fprintf(stderr, " :%s0,buf); exit(4); } return; } -85- '''' ................... 2 1. .......................................... 1 2. ............................ 3 2.1. .................................. 3 2.2. ....................................... 4 2.2.1. ............................... 4 2.2.2. (long) ...................... 4 2.2.3. .......................... 4 2.2.4. ........................ 5 2.3. .......................................... 5 2.4. ............... 6 3. ' .................................. 6 3.1. ................... 6 3.2. l_ ............................ 8 3.3. .................................. 8 3.3.1. ............................... 8 3.3.2. float double ........................... 9 3.3.3. ......... 9 3.3.4. ............................. 9 3.3.5. ............................... 9 3.3.6. ................. 10 4. ......................................... 10 4.1. ............................. 11 4.2. ................................ 13 4.3. ...................... 14 4.4. ............................. 15 4.5. ................................. 16 4.6. .............................. 16 4.7. .............................. 17 4.8. '' .......................... 17 4.9. '' ........... 17 4.10. '' ............ 17 4.11. '' ......................... 18 4.12. '' ...................... 18 4.13. ............................... 18 4.14. ........................... 19 4.15. .......................... 20 4.16. '' .............................. 20 4.17. . ............... 20 5. .......................................... 22 5.1. ..................... 22 5.2. .............................. 23 5.3. ....................................... 24 5.4. ................................ 24 -86- 5.5. ................. 26 5.6. ................................ 29 5.7. ................................... 30 5.8. ..................................... 32 5.9. typedef ............................... 33 6. ......................................... 34 6.1. ........................... 34 6.2. ( ) ................... 34 6.3. .............................. 35 6.4. while .................................. 35 6.5. do ..................................... 35 6.6. for .................................... 35 6.7. switch ................................. 36 6.8. break .................................. 37 6.9. continue ............................... 37 6.10. ............................... 38 6.11. goto ................................... 38 6.12. ............................. 38 6.13. ................................. 38 7. ............................... 39 7.1. ..................... 39 7.2. ...................... 40 8. .................. 40 8.1. .................... 41 8.2. ........ 42 8.3. ................................ 42 9. '' ........................... 43 9.1. ................................... 43 9.2. ................................ 44 9.3. ............................. 45 9.4. #line ................................... 45 10. ................. 46 10.1. ......................... 46 10.2. ......................................... 47 10.3. , ................. 47 10.4. ................. 48 11. ............................. 49 12. ....................... 49 12.1. ..................................... 51 13. ............. 51 13.1. .............. 52 13.2. ........................ 52 13.3. - printf ................ 53 13.4. - scanf .................. 55 13.5. ............... 58 -87- 13.6. ................................. 59 13.7. - stderr exit .............. 61 13.8. .............................. 62 13.9. ungetc .................................. 62 13.10. ...................... 62 13.10.1. ............................ 62 13.10.2. .................. 63 14. ............ 63 14.1. ............ 64 14.2. ..................... 64 15. ........................... 66 15.1. / ...................................... 66 15.1.1. ............................ 66 15.1.2. /. .................... 67 15.1.3. , , ....... 68 15.1.4. - lseek ................... 69 15.2. ........................... 70 15.2.1. system ................................ 71 15.2.2. - execl ...... 71 15.2.3. - fork ............. 71 15.2.4. ..................... 73 15.3. ............................ 74 16. ...................... 76 16.1. ....................................... 76 16.2. ........................................ 78 16.3. ....................................... 80 16.4. ............................. 81 16.5. .................................... 82 17. ............................ 82 -88-