Commit | Line | Data |
---|---|---|
0a63e3e8 BJ |
1 | /* Copyright (c) 1979 Regents of the University of California */ |
2 | #include "sh.h" | |
3 | ||
4 | /* | |
5 | * C Shell | |
6 | */ | |
7 | ||
8 | static struct tbuffer { | |
9 | #ifdef V6 | |
10 | int put, pst; | |
11 | #else | |
12 | time_t put, pst; | |
13 | #endif | |
14 | time_t cut, cst; | |
15 | } times0; | |
16 | time_t time0; | |
17 | ||
18 | long | |
19 | secs(bef, aft) | |
20 | struct tbuffer *bef, *aft; | |
21 | { | |
22 | ||
23 | return ((aft->cut - bef->cut + aft->cst - bef->cst)); | |
24 | } | |
25 | ||
26 | settimes() | |
27 | { | |
28 | ||
29 | time(&time0); | |
30 | times(×0); | |
31 | } | |
32 | ||
33 | dotime(v, kp) | |
34 | register char **v; | |
35 | struct command *kp; | |
36 | { | |
37 | struct tbuffer timeszer, timesdol, *tzp; | |
38 | time_t timezer, timedol; | |
39 | ||
40 | if (v[1] != 0) { | |
41 | time(&timezer), times(×zer); | |
42 | lshift(v, 1); | |
43 | if (func(kp) == 0) { | |
44 | timflg = 1; | |
45 | return (0); | |
46 | } | |
47 | tzp = ×zer; | |
48 | } else | |
49 | timezer = time0, tzp = ×0; | |
50 | time(&timedol); | |
51 | times(×dol); | |
52 | ptimes(timedol - timezer, tzp, ×dol); | |
53 | return (1); | |
54 | } | |
55 | ||
56 | donice(v) | |
57 | register char **v; | |
58 | { | |
59 | register char *cp; | |
60 | ||
61 | v++, cp = *v++; | |
62 | if (cp == 0) { | |
63 | #ifndef V6 | |
64 | nice(20); | |
65 | nice(-10); | |
66 | #endif | |
67 | nice(4); | |
68 | return (1); | |
69 | } | |
70 | if (*v == 0 && any(cp[0], "+-")) { | |
71 | #ifndef V6 | |
72 | nice(20); | |
73 | nice(-10); | |
74 | #endif | |
75 | nice(getn(cp)); | |
76 | return (1); | |
77 | } | |
78 | return (0); | |
79 | } | |
80 | ||
81 | struct tbuffer bef, aft; | |
82 | time_t btim, atim; | |
83 | ||
84 | pwait(i) | |
85 | register int i; | |
86 | { | |
87 | register int p, e; | |
88 | char *name; | |
89 | int s; | |
90 | ||
91 | if (i == 0) | |
92 | return; | |
93 | time(&btim); | |
94 | do { | |
95 | times(&bef); | |
96 | p = wait(&s); | |
97 | if (p == -1) | |
98 | return; | |
99 | times(&aft); | |
100 | if (p == getn(value("child"))) | |
101 | unsetv("child"); | |
102 | time(&atim); | |
103 | e = s & TRIM; | |
104 | if (e > 0 && (e > 15 || mesg[e])) { | |
105 | if (p != i) | |
106 | printf("%d: ", p); | |
107 | if (name = cname(p)) | |
108 | printf("%s: ", name); | |
109 | if (e <= 15) | |
110 | printf(mesg[e]); | |
111 | else | |
112 | printf("Sig %d", e); | |
113 | if (s & 0200) | |
114 | printf(" -- Core dumped"); | |
115 | printf("\n"); | |
116 | } | |
117 | if (e != 0 && i == p) { | |
118 | cdone(p); | |
119 | if (e == SIGINT && setintr && (!gointr || !eq(gointr, "-"))) | |
120 | pintr(); | |
121 | error(0); | |
122 | } | |
123 | if (i == p) { | |
124 | set("status", putn(e ? e | QUOTE : (s >> 8) & 0377)); | |
125 | if (exiterr && !eq(value("status"), "0")) { | |
126 | if (e == 0) { | |
127 | if (name = cname(p)) | |
128 | printf("%s: ", name); | |
129 | printf("Exit status %s\n", value("status")); | |
130 | } | |
131 | exitstat(); | |
132 | } | |
133 | cdone(p); | |
134 | break; | |
135 | } | |
136 | cdone(p); | |
137 | } while (i != p); | |
138 | if (timflg || (!child && adrof("time") && secs(&bef, &aft) / 60 >= getn(value("time")))) { | |
139 | timflg = 0; | |
140 | ptimes(atim - btim, &bef, &aft); | |
141 | } | |
142 | } | |
143 | ||
144 | ptimes(sec, bef, aft) | |
145 | time_t sec; | |
146 | register struct tbuffer *bef, *aft; | |
147 | { | |
148 | ||
149 | p60ths(aft->cut - bef->cut); | |
150 | printf("u "); | |
151 | p60ths(aft->cst - bef->cst); | |
152 | printf("s "); | |
153 | psecs(sec); | |
154 | printf(" %d%%\n", (int) ((100 * secs(bef, aft)) / (60 * (sec ? sec : 1)))); | |
155 | } | |
156 | ||
157 | endwait() | |
158 | { | |
159 | ||
160 | signal(SIGINT, SIG_IGN); | |
161 | cleft(); | |
162 | bferr("Interrupted"); | |
163 | } | |
164 | ||
165 | await() | |
166 | { | |
167 | if (setintr) | |
168 | signal(SIGINT, endwait); | |
169 | pwait(-1); | |
170 | if (setintr) | |
171 | signal(SIGINT, SIG_IGN); | |
172 | } | |
173 | ||
174 | struct achild { | |
175 | int pid; | |
176 | char *cname; | |
177 | struct achild *cnext; | |
178 | } children; | |
179 | ||
180 | char * | |
181 | cname(pid) | |
182 | int pid; | |
183 | { | |
184 | register struct achild *cp; | |
185 | ||
186 | for (cp = children.cnext; cp; cp = cp->cnext) | |
187 | if (cp->pid == pid) | |
188 | return (cp->cname); | |
189 | return (NOSTR); | |
190 | } | |
191 | ||
192 | cadd(pid, cname) | |
193 | int pid; | |
194 | char *cname; | |
195 | { | |
196 | register struct achild *cp = (struct achild *) calloc(1, sizeof (struct achild)); | |
197 | ||
198 | cp->pid = pid; | |
199 | cp->cname = savestr(cname); | |
200 | cp->cnext = children.cnext; | |
201 | children.cnext = cp; | |
202 | } | |
203 | ||
204 | cdone(pid) | |
205 | int pid; | |
206 | { | |
207 | register struct achild *cpp, *cp; | |
208 | ||
209 | cpp = &children; | |
210 | for (cp = cpp->cnext; cp; cp = cp->cnext) { | |
211 | if (cp->pid == pid) { | |
212 | xfree(cp->cname); | |
213 | cpp->cnext = cp->cnext; | |
214 | xfree(cp); | |
215 | return; | |
216 | } | |
217 | cpp = cp; | |
218 | } | |
219 | } | |
220 | ||
221 | cleft() | |
222 | { | |
223 | ||
224 | register struct achild *cp; | |
225 | ||
226 | for (cp = children.cnext; cp; cp = cp->cnext) | |
227 | printf("%6d %s\n", cp->pid, cp->cname); | |
228 | } |