csh and tcsh work

Sources of csh/sh.wait.c

csh と tcsh のソース

Bill Joy

First published: October, 1978.

Copyright © 1979 Regents of the University of California.

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

To the top of this page