Commit | Line | Data |
---|---|---|
f6227721 | 1 | #ifndef lint |
f39a7cf3 | 2 | static char sccsid[] = "@(#)io.c 4.3 %G%"; |
f6227721 | 3 | #endif |
ce24f27d KM |
4 | |
5 | # | |
6 | /* | |
7 | * UNIX shell | |
8 | * | |
9 | * S. R. Bourne | |
10 | * Bell Telephone Laboratories | |
11 | * | |
12 | */ | |
13 | ||
14 | #include "defs.h" | |
ce24f27d KM |
15 | |
16 | ||
17 | /* ======== input output and file copying ======== */ | |
18 | ||
19 | initf(fd) | |
20 | UFD fd; | |
21 | { | |
22 | REG FILE f=standin; | |
23 | ||
24 | f->fdes=fd; f->fsiz=((flags&(oneflg|ttyflg))==0 ? BUFSIZ : 1); | |
25 | f->fnxt=f->fend=f->fbuf; f->feval=0; f->flin=1; | |
26 | f->feof=FALSE; | |
27 | } | |
28 | ||
29 | estabf(s) | |
30 | REG STRING s; | |
31 | { | |
32 | REG FILE f; | |
33 | ||
34 | (f=standin)->fdes = -1; | |
35 | f->fend=length(s)+(f->fnxt=s); | |
36 | f->flin=1; | |
37 | return(f->feof=(s==0)); | |
38 | } | |
39 | ||
40 | push(af) | |
41 | FILE af; | |
42 | { | |
43 | REG FILE f; | |
44 | ||
45 | (f=af)->fstak=standin; | |
46 | f->feof=0; f->feval=0; | |
47 | standin=f; | |
48 | } | |
49 | ||
50 | pop() | |
51 | { | |
52 | REG FILE f; | |
53 | ||
54 | IF (f=standin)->fstak | |
55 | THEN IF f->fdes>=0 THEN close(f->fdes) FI | |
56 | standin=f->fstak; | |
57 | return(TRUE); | |
58 | ELSE return(FALSE); | |
59 | FI | |
60 | } | |
61 | ||
62 | chkpipe(pv) | |
63 | INT *pv; | |
64 | { | |
65 | IF pipe(pv)<0 ORF pv[INPIPE]<0 ORF pv[OTPIPE]<0 | |
66 | THEN error(piperr); | |
67 | FI | |
68 | } | |
69 | ||
70 | chkopen(idf) | |
71 | STRING idf; | |
72 | { | |
73 | REG INT rc; | |
74 | ||
75 | IF (rc=open(idf,0))<0 | |
76 | THEN failed(idf,badopen); | |
77 | ELSE return(rc); | |
78 | FI | |
79 | } | |
80 | ||
81 | rename(f1,f2) | |
82 | REG INT f1, f2; | |
83 | { | |
84 | IF f1!=f2 | |
f39a7cf3 | 85 | THEN dup2(f1, f2); |
ce24f27d KM |
86 | close(f1); |
87 | IF f2==0 THEN ioset|=1 FI | |
88 | FI | |
89 | } | |
90 | ||
91 | create(s) | |
92 | STRING s; | |
93 | { | |
94 | REG INT rc; | |
95 | ||
96 | IF (rc=creat(s,0666))<0 | |
97 | THEN failed(s,badcreate); | |
98 | ELSE return(rc); | |
99 | FI | |
100 | } | |
101 | ||
102 | tmpfil() | |
103 | { | |
104 | itos(serial++); movstr(numbuf,tmpnam); | |
105 | return(create(tmpout)); | |
106 | } | |
107 | ||
108 | /* set by trim */ | |
109 | BOOL nosubst; | |
110 | ||
111 | copy(ioparg) | |
112 | IOPTR ioparg; | |
113 | { | |
114 | CHAR c, *ends; | |
115 | REG CHAR *cline, *clinep; | |
116 | INT fd; | |
117 | REG IOPTR iop; | |
118 | ||
119 | IF iop=ioparg | |
120 | THEN copy(iop->iolst); | |
121 | ends=mactrim(iop->ioname); IF nosubst THEN iop->iofile &= ~IODOC FI | |
122 | fd=tmpfil(); | |
123 | iop->ioname=cpystak(tmpout); | |
124 | iop->iolst=iotemp; iotemp=iop; | |
125 | cline=locstak(); | |
126 | ||
127 | LOOP clinep=cline; chkpr(NL); | |
128 | WHILE (c = (nosubst ? readc() : nextc(*ends)), !eolchar(c)) DO *clinep++ = c OD | |
129 | *clinep=0; | |
130 | IF eof ORF eq(cline,ends) THEN break FI | |
131 | *clinep++=NL; | |
132 | write(fd,cline,clinep-cline); | |
133 | POOL | |
134 | close(fd); | |
135 | FI | |
136 | } |