. , - . - ; "" . - , ( ? - ?), , , . , - , , . ( - READ (5,...) WRITE (6,...) ). - ; - . - . ("SHELL") , , , , 0, 1 2 - . , 0 1 2, , . , SHELL "<" ">" : PROG <INFILE>OUTFILE SHELL - 0 1 . 2 - , - . , . , SHELL, . , 0 1 2 , - , . 8.2. / - READ WRITE / UNIX - - , - - ; - . : READ WRITE. - . , . - . : N_READ=READ(FD,BUF,N); N_WRITTEN=WRITE(FD,BUF,N); , - . - , . - , "-1" - . - ; , , . , , . "1", (.. ), "512", . , - . , , - , 1. UNIX , . #DEFINE BUFSIZE 512 /*BEST SIZE FOR PDP-11 UNIX*/ MAIN() /*COPY INPUT TO OUTPUT*/ \( CHAR BUF[BUFSIZE]; INT N; WHILE((N=READ(0,BUF,BUFSIZE))>0) WRITE(1,BUF,N); \) BUFSIZE, READ , - WRITE; - READ . , READ WRITE , GETCHAR, PUTCHAR .. , , GETCHAR, - . #DEFINE CMASK 0377 /*FOR MAKING CHAR'S > 0*/ GETCHAR() /*UNBUFFERED SINGLE CHARACTER INPUT*/ \( CHAR C; RETURN((READ(0,&C,1)>0 7 & CMASK : EOF); \) "C" CHAR, - READ . - 0377 - ; - . ( 0377 PDP-11, ). GETCHAR - , . #DEFINE CMASK 0377 /*FOR MAKING CHAR'S>0*/ #DEFINE BUFSIZE 512 GETCHAR() /*BUFFERED VERSION*/ \( STATIC CHAR BUF[BUFSIZE]; STATIC CHAR *BUFP = BUF; STATIC INT N = 0; IF (N==0) \( /*BUFFER IS EMPTY*/ N=READ(0,BUF,BUFSIZE); BUFP = BUF; \) RETURN((--N>=0) ? *BUFP++ & CMASK : EOF); \) 8.3. , , (UNLINK) , , , - , . - : OPEN CREAT. OPEN FOPEN, - 7, , , - INT. INT FD; FD=OPEN(NAME,RWMODE); FOPEN, NAME , . - , , : RWMODE : 0 - , 1 - , 2 - . - , OPEN "-1"; . , , . CREAT - . - FD=CREAT(NAME,PMODE); , - NAME, "-1" . , CREAT - ; , , - . , CREAT , - PMODE. UNIX - , , , . , . , 0755 - , - . , - CP UNIX. ( , - ). #DEFINE NULL 0 #DEFINE BUFSIZE 512 #DEFINE PMODE 0644/*RW FOR OWNER,R FOR GROUP,OTHERS*/ MAIN(ARGC,ARGV) /*CP: COPY F1 TO F2*/ INT ARGC; CHAR *ARGV[]; \( INT F1, F2, N; CHAR BUF[BUFSIZE]; IF (ARGC ! = 3) ERROR("USAGE:CP FROM TO", NULL); IF ((F1=OPEN(ARGV[1],0))== -1) ERROR("CP:CAN'T OPEN %S", ARGV[1]); IF ((F2=CREAT(ARGV[2],PMODE))== -1) ERROR("CP: CAN'T CREATE %S", ARGV[2]); WHILE ((N=READ(F1,BUF,BUFSIZE))>0) IF (WRITE(F2,BUF,N) !=N) ERROR("CP: WRITE ERROR", NULL); EXIT(0); \) ERROR(S1,S2) /*PRINT ERROR MESSAGE AND DIE*/ CHAR *S1, S2; \( PRINTF(S1,S2); PRINTF("\N"); EXIT(1); \) ( 15 - 25) , - . , - , - . CLOSE . EXIT . UNLINK (FILENAME) FILENAME ( - . , , - .).  8-1 -------------- CAT 7, READ, WRITE, OPEN CLOSE - . - . 8.4. - SEEK LSEEK - : READ WRITE , - . - . LSEEK - , - . LSEEK(FD,OFFSET,ORIGIN); FD OFFSET (), , ORIGIN ( ). . OFFSET LONG; FD ORIGIN INT. ORIGIN 0,1 2, , OFFSET - , - . , , : LSEEK(FD,0L,2); (" "), - : LSEEK(FD,0L,0); 0L; (LONG) 0. LSEEK , , - . , , , . GET(FD,POS,BUF,N) /*READ N BYTES FROM POSITION POS*/ INT FD, N; LONG POS; CHAR *BUF; \( LSEEK(FD,POS,0); /*GET TO POS*/ RETURN(READ(FD,BUF,N)); \) , 7 UNIX, - SEEK. SEEK LSEEK, , OFFSET INT, LONG. , PDP-11 16 , - OFFSET, SEEK, 65535; ORIGIN 3, 4, 5, SEEK OFFSET 512 ( ) ORIGIN, 0, 1 2 . , - , SEEK: , , , ORIGIN 1 - .  8-2 --------------- , SEEK LSEEK . . 8.5. - FOPEN GETC FOPEN GETC , . , , . , : , ; , ; ; , - ..; . STDIO.H, ( #INCLUDE) , - . - . STDIO.H , - , , - . DEFINE _BUFSIZE 512 DEFINE _NFILE 20 /*FILES THAT CAN BE HANDLED*/ TYPEDEF STRUCT _IOBUF \( CHAR *_PTR; /*NEXT CHARACTER POSITION*/ INT _CNT; /*NUMBER OF CHARACTERS LEFT*/ CHAR *_BASE; /*LOCATION OF BUFFER*/ INT _FLAG; /*MODE OF FILE ACCESS*/ INT _FD; /*FILE DESCRIPTOR*/ ) FILE; XTERN FILE _IOB[_NFILE]; DEFINE STDIN (&_IOB[0]) DEFINE STDOUT (&_IOB[1]) DEFINE STDERR (&_IOB[2]) DEFINE _READ 01 /* FILE OPEN FOR READING */ DEFINE _WRITE 02 /* FILE OPEN FOR WRITING */ DEFINE _UNBUF 04 /* FILE IS UNBUFFERED */ DEFINE _BIGBUF 010 /* BIG BUFFER ALLOCATED */ DEFINE _EOF 020 /* EOF HAS OCCURRED ON THIS FILE */ DEFINE _ERR 040 /* ERROR HAS OCCURRED ON THIS FILE */ DEFINE NULL 0 DEFINE EOF (-1) DEFINE GETC(P) (--(P)->_CNT >= 0 \ ? *(P)->_PTR++ & 0377 : _FILEBUF(P)) DEFINE GETCHAR() GETC(STDIN) DEFINE PUTC(X,P) (--(P)->_CNT >= 0 \ ? *(P)->_PTR++ = (X) : _FLUSHBUF((X),P)) DEFINE PUTCHAR(X) PUTC(X,STDOUT) GETC , . ( #DEFINE , ). - , GETC _FILEBUF, , . - : GETC 0377, , PDP-11, . - , PUTC, - , , GETC, _FLUSHBUF. FOPEN. FOPEN - , , - . FOPEN - ; _FILEBUF . #INCLUDE <STDIO.H> #DEFINE PMODE 0644 /*R/W FOR OWNER;R FOR OTHERS*/ FILE *FOPEN(NAME,MODE) /*OPEN FILE,RETURN FILE PTR*/ REGISTER CHAR *NAME, *MODE; \( REGISTER INT FD; REGISTER FILE *FP; IF(*MODE !='R'&&*MODE !='W'&&*MODE !='A') \( FPRINTF(STDERR,"ILLEGAL MODE %S OPENING %S\N", MODE,NAME); EXIT(1); \) FOR (FP=_IOB;FP<_IOB+_NFILE;FP++) IF((FP->_FLAG & (_READ \! _WRITE))==0) BREAK; /*FOUND FREE SLOT*/ IF(FP>=_IOB+_NFILE) /*NO FREE SLOTS*/ RETURN(NULL); IF(*MODE=='W') /*ACCESS FILE*/ FD=CREAT(NAME,PMODE); ELSE IF(*MODE=='A') \( IF((FD=OPEN(NAME,1))==-1) FD=CREAT(NAME,PMODE); LSEEK(FD,OL,2); \) ELSE FD=OPEN(NAME,0); IF(FD==-1) /*COULDN'T ACCESS NAME*/ RETURN(NULL); FP->_FD=FD; FP->_CNT=0; FP->_BASE=NULL; FP->_FLAG &=(_READ \! _WRITE); FP->_FLAG \!=(*MODE=='R') ? _READ : _WRITE; RETURN(FP); \) _FILEBUF . - , _FILEBUF , - . - - CALLOC, ; , _FILEBUF / , - , . #INCLUDE <STDIO.H> _FILLBUF(FP) /*ALLOCATE AND FILL INPUT BUFFER*/ REGISTER FILE *FP; ( STATIC CHAR SMALLBUF(NFILE);/*FOR UNBUFFERED 1/0*/ CHAR *CALLOC(); IF((FR->_FLAG&_READ)==0\!\!(FP->_FLAG&(EOF\!_ERR))\!=0 RETURN(EOF); WHILE(FP->_BASE==NULL) /*FIND BUFFER SPACE*/ IF(FP->_FLAG & _UNBUF) /*UNBUFFERED*/ FP->_BASE=&SMALLBUF[FP->_FD]; ELSE IF((FP->_BASE=CALLOC(_BUFSIZE,1))==NULL) FP->_FLAG \!=_UNBUF; /*CAN'T GET BIG BUF*/ ELSE FP->_FLAG \!=_BIGBUF; /*GOT BIG ONE*/ FP->_PTR=FP->_BASE; FP->_CNT=READ(FP->_FD, FP->_PTR, FP->_FLAG & _UNBUF ? 1 : _BUFSIZE); FF(--FP->_CNT<0) \( IF(FP->_CNT== -1) FP->_FLAG \! = _EOF; ELSE FP->_FLAG \! = _ ERR; FP->_CNT = 0; RETURN(EOF); \) RETURN(*FP->_PTR++ & 0377); /*MAKE CHAR POSITIVE*/ ) GETC , _FILEBUF. _FILEBUF , - , EOF. , , . _FLAG . , _FILEBUF READ , . , . _IOB STDIN, STDOUT STDERR: FILE _IOB[NFILE] = \( (NULL,0,_READ,0), /*STDIN*/ (NULL,0,NULL,1), /*STDOUT*/ (NULL,0,NULL,_WRITE \! _UNBUF,2) /*STDERR*/ ); _FLAG , STDIN , STDOUT - STDERR - .  8-3 -------------- FOPEN _FILEBUF, .  8-4 --------------- _FLUSHBUF FCLOSE.  8-5 --------------- FSEEK(FP, OFFSET, ORIGIN) LSEEK, , FP - , . FSEEK. , FSEEK - , . 8.6. -  - , , . LS (" ") UNIX. , , - , , .. , , UNIX , , LS - ; . - , LS , . FSIZE. FSIZE LS, , . , FSIZE . - , . . - , , . , "I - ". I- - , , . - : I- . - SYS/DIR.H, #DEFINE DIRSIZ 14 /*MAX LENGTH OF FILE NAME*/ STRUCT DIRECT /*STRUCTURE OF DIRECTORY ENTRY*/ \( INO_T&_INO; /*INODE NUMBER*/ CHAR &_NAME[DIRSIZ]; /*FILE NAME*/ \); "" INO_T - TYPEDEF , I- . PDP-11 UNIX UNSIGNED, - , : . - TYPEDEF. "" SYS/TUPES.H. STAT - I- ( -1, - ). , STRUCT STAT STBUF; CHAR *NAME; STAT(NAME,&STBUF); STBUF I- NAME. , STAT , SYS/STAT.H : STRUCT STAT /*STRUCTURE RETURNED BY STAT*/ \( DEV_T ST_DEV; /* DEVICE OF INODE */ INO_T ST_INO; /* INODE NUMBER */ SHORT ST_MODE /* MODE BITS */ SHORT ST_NLINK; / *NUMBER OF LINKS TO FILE */ SHORT ST_UID; /* OWNER'S USER ID */ SHORT ST_GID; /* OWNER'S GROUP ID */ DEV_T ST_RDEV; /* FOR SPECIAL FILES */ OFF_T ST_SIZE; /* FILE SIZE IN CHARACTERS */ TIME_T ST_ATIME; /* TIME LAST ACCESSED */ TIME_T ST_MTIME; /* TIME LAST MODIFIED */ TIME_T ST_CTIME; /* TIME ORIGINALLY CREATED */ \) . ST.MODE , ; SYS/STAT.H. #DEFINE S_IFMT 0160000 /* TYPE OF FILE */ #DEFINE S_IFDIR 0040000 /* DIRECTORY */ #DEFINE S_IFCHR 0020000 /* CHARACTER SPECIAL */ #DEFINE S_IFBLK 0060000 /* BLOCK SPECIAL */ #DEFINE S_IFREG 0100000 /* REGULAR */ #DEFINE S_ISUID 04000 /* SET USER ID ON EXECUTION */ #DEFINE S_ISGID 02000 /* SET GROUP ID ON EXECUTION */ #DEFINE S_ISVTX 01000 /*SAVE SWAPPED TEXT AFTER USE*/ #DEFINE S_IREAD 0400 /* READ PERMISSION */ #DEFINE S_IWRITE 0200 /* WRITE PERMISSION */ #DEFINE S_IEXEC 0100 /* EXECUTE PERMISSION */ FSIZE. - STAT , - , . - , ; - , - . , ; FSIZE . #INCLUDE <STDIO.H.> #INCLUDE <SYS/TYPES.H> /*TYPEDEFS*/ #INCLUDE <SYS/DIR.H> /*DIRECTORY ENTRY STRUCTURE*/ #INCLUDE <SYS/STAT.H> /*STRUCTURE RETURNED BY STAT*/ #DEFINE BUFSIZE 256 MAIN(ARGC,ARGV) /*FSIZE:PRINT FILE SIZES*/ CHAR *ARGV[]; \( CHAR BUF[BUFSIZE]; IF(ARGC==1) \( /*DEFAULT:CURRENT DIRECTORY*/ ATRCPY(BUF,"."); FSIZE(BUF); \) ELSE WHILE(--ARGC>0) \( STRCPY(BUF,*++ARGV); FSIZE(BUF); \) \) FSIZE . , FSIZE DIRECTORY . - S_IFMT _IFDIR STAT.H. FSIZE(NAME) /*PRINT SIZE FOR NAME*/ CHAR *NAME; \( STRUCT STAT STBUF; IF(STAT(NAME,&STBUF)== -1) \( FPRINTF(STDERR,"FSIZE:CAN'T FIND %S\N",NAME); RETURN; \) IF((STBUF.ST_MODE & S_IFMT)==S_IFDIR) DIRECTORY(NAME); PRINTF("%8LD %S\N",STBUF.ST_SIZE,NAME); \) DIRECTORY . - , . DIRECTORY(NAME) /*FSIZE FOR ALL FILES IN NAME*/ CHAR *NAME; ( STRUCT DIRECT DIRBUF; CHAR *NBP, *NEP; INT I, FD; NBP=NAME+STRLEN(NAME); *NBP++='/'; /*ADD SLASH TO DIRECTORY NAME*/ IF(NBP+DIRSIZ+2>=NAME+BUFSIZE) /*NAME TOO LONG*/ RETURN; IF((FD=OPEN(NAME,0))== -1) RETURN; WHILE(READ(FD,(CHAR *)&DIRBUF,SIZEOF(DIRBUF))>0) \( IF(DIRBUF.D_INO==0) /*SLOT NOT IN USE*/ CONTINUE; IF(STRCMP (DIRBUF.D_NAME,".")==0 \!\! STRCMP(DIRBUF.D_NAME,"..")==0 CONTINUE; /*SKIP SELF AND PARENT*/ FOR (I=0,NEP=NBP;I<DIRSIZ;I++) *NEP++=DIRBUF.D_NAME[I]; *NEP++='\0'; FSIZE(NAME); \) CLOSE(FD); *--NBP='\0'; /*RESTORE NAME*/ ) ( ), - I- , . , - ".", , ".."; , , , . FSIZE , . -, - " "; - , - . -, , " ", STAT.H DIR.H, , . 8.7. -  5 ALLOC. , , - : ALLOC FREE - ; , ALLOC - . , , - , - - -- , , TYPEDEF. , , ALLOC . - , , ALLOC, . - . , . , ( ) - , - . - , . , . , , - , - . , , ; - . - - . - , , . , . , 5, - , ALLOC , . - , , - , - , . , IBM 360/370,HONEYWELL 6000 - , - DOUBLE; PDP-11 - INT. - , ; . , . , - : TYPEDEF INT ALIGN; /*FORCES ALIGNMENT ON PDP-11*/ UNION HEADER \( /*FREE BLOCK HEADER*/ STRUCT \( UNION HEADER *PTR; /*NEXT FREE BLOCK*/ UNSIGNED SIZE; /*SIZE OF THIS FREE BLOCK*/ \) S; ALIGN X; /*FORCE ALIGNMENT OF BLOCKS*/ \); TYPEDEF UNION HEADER HEADER; ALLOC ; , , , , , SIZE . , - ALLOC, - , . STATIC HEADER BASE; /*EMPTY LIST TO GET STARTED*/ STATIC HEADER *ALLOCP=NULL; /*LAST ALLOCATED BLOCK*/ CHAR *ALLOC(NBYTES)/*GENERAL-PURPOSE STORAGE ALLOCATOR*/ UNSIGNED NBYTES; \( HEADER *MORECORE(); REGISTER HEADER *P, *G; REGISTER INT NUNITS; NUNITS=1+(NBYTES+SIZEOF(HEADER)-1)/SIZEOF(HEADER); IF ((G=ALLOCP)==NULL) \( /*NO FREE LIST YET*/ BASE.S PTR=ALLOCP=G=&BASE; BASE.S.SIZE=0; \) FOR (P=G>S.PTR; ; G=P, P=P->S.PTR) \( IF (P->S.SIZE>=NUNITS) \( /*BIG ENOUGH*/ IF (P->S.SIZE==NUNITS) /*EXACTLY*/ G->S.PTR=P->S.PTR; ELSE \( /*ALLOCATE TAIL END*/ P->S.SIZE-=NUNITS; P+=P->S.SIZE; P->S.SIZE=NUNITS; \) ALLOCP=G; RETURN((CHAR *)(P+1)); \) IF(P==ALLOCP) /*WRAPPED AROUND FREE LIST*/ IF((P=MORECORE(NUNITS))==NULL) RETURN(NULL); /*NONE LEFT*/ \) \) BASE . ALLOCP NULL, ALLOC, : . . (ALLOCP), ; - . , ; , . - - , . - , ALLOC "P" . MORECORE - . , , , , . UNIX SBRK(N) "N" - .( - ). , ALLOC. MORECORE ; , . , . #DEFINE NALLOC 128 /*#UNITS TO ALLOCATE AT ONCE*/ STATIC HEADER *MORECORE(NU) /*ASK SYSTEM FOR MEMORY*/ UNSIGNED NU; \( CHAR *SBRK(); REGISTER CHAR *CP; REGISTER HEADER *UP; REGISTER INT RNU; RNU=NALLOC*((NU+NALLOC-1)/NALLOC); CP=SBRK(RNU*SIZEOF(HEADER)); IF ((INT)CP==-1) /*NO SPACE AT ALL*/ RETURN(NULL); UP=(HEADER *)CP; UP->S.SIZE=RNU; FREE((CHAR *)(UP+1)); RETURN(ALLOCP); \) , - SBRK "-1", NULL . "-1" INT. () , - - . - FREE. ALLOCP, . , . , , . - , , , - . FREE(AP) /*PUT BLOCKE AP IN FREE LIST*/ CHAR *AP; \( REGISTER HEADER *P, *G; P=(HEADER*)AP-1; /*POINT TO HEADER*/ FOR (G=ALLOCP; !(P>G && P>G->S.PTR);G=G->S.PTR) IF (G>=G->S.PTR && (P>G \!\! P<G->S.PTR)) BREAK; /*AT ONE END OR OTHER*/ IF (P+P->S.SIZE==G->S.PTR)\(/*JOIN TO UPPER NBR*/ P->S.SIZE += G->S.PTR->S.SIZE; P->S.PTR = G->S.PTR->S.PTR; \) ELSE P->S.PTR = G->S.PTR; IF (G+G->S.SIZE==P) \( /*JOIN TO LOWER NBR*/ G->S.SIZE+=P->S.SIZE; G->S.PTR=P->S.PTR; \) ELSE G->S.PTR=P; ALLOCP = G; \) - , , . TYPEDEF UNION ( , - SBRK ). . - , .  8-6 -------------- CALLOC(N,SIZE) - "N" SIZE, - . CALLOC, ALLOC , , .  8-7 --------------- ALLOC , - ; FREE , , , . , .  8-8 --------------- BFREE(P,N), - "P" "N" , - ALLOC FREE. BFREE - .  * 9. : 'C' *  9.1.  '' DEC PDP-11, HONEYWELL 6000, IBM /370 INTERDATA 8/32. , , PDP-11, , . , - ; . 10.  : , , , , . , , (, " "), , , - , - . - , , - . , - , . 10.1.  /* /*. . 10.2. () - ; - . _ . . - , , . , - , : DEC PDP-11 7 , 2 HONEYWELL 6000 6 , 1 IBM 360/370 7 , 1 INTERDATA 8/32 8 , 2 10.3.  - : INT EXTERN ELSE CHAR REGISTER FOR FLOAT TYPEDEF DO DOUBLE STATIC WHILE STRUCT GOTO SWITCH UNION RETURN CASE LONG SIZEOF DEFAULT SHORT BREAK ENTRY UNSIGNED CONTINUE *AUTO IF ENTRY - - ; . FORTRAN ASM 10.4.  , . 10.6 - , . 10.4.1.  , , , 0 ( ), . 8 9 10 11 . - , 0 (, -- ) 0 ( -), - . () () F () F () 10 15. , , - ; , , . 10.4.2.  , , L (-) L (-), . , . 10.4.3.  - , - , , , 'X'. -