Deleted Added
full compact
jobs.c (155301) jobs.c (163085)
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/jobs.c 155301 2006-02-04 14:37:50Z schweikh $");
39__FBSDID("$FreeBSD: head/bin/sh/jobs.c 163085 2006-10-07 16:51:16Z stefanf $");
40
41#include <fcntl.h>
42#include <signal.h>
43#include <errno.h>
44#include <paths.h>
45#include <unistd.h>
46#include <stdlib.h>
47#include <sys/param.h>

--- 45 unchanged lines hidden (view full) ---

93STATIC pid_t waitproc(int, int *);
94STATIC void cmdtxt(union node *);
95STATIC void cmdputs(char *);
96#if JOBS
97STATIC void setcurjob(struct job *);
98STATIC void deljob(struct job *);
99STATIC struct job *getcurjob(struct job *);
100#endif
40
41#include <fcntl.h>
42#include <signal.h>
43#include <errno.h>
44#include <paths.h>
45#include <unistd.h>
46#include <stdlib.h>
47#include <sys/param.h>

--- 45 unchanged lines hidden (view full) ---

93STATIC pid_t waitproc(int, int *);
94STATIC void cmdtxt(union node *);
95STATIC void cmdputs(char *);
96#if JOBS
97STATIC void setcurjob(struct job *);
98STATIC void deljob(struct job *);
99STATIC struct job *getcurjob(struct job *);
100#endif
101STATIC void showjob(struct job *, pid_t, int, int);
101STATIC void showjob(struct job *, pid_t, int);
102
103
104/*
105 * Turn job control on and off.
106 */
107
108MKINIT int jobctl;
109

--- 150 unchanged lines hidden (view full) ---

260}
261#endif
262
263
264int
265jobscmd(int argc, char *argv[])
266{
267 char *id;
102
103
104/*
105 * Turn job control on and off.
106 */
107
108MKINIT int jobctl;
109

--- 150 unchanged lines hidden (view full) ---

260}
261#endif
262
263
264int
265jobscmd(int argc, char *argv[])
266{
267 char *id;
268 int ch, sformat, lformat;
268 int ch, mode;
269
270 optind = optreset = 1;
271 opterr = 0;
269
270 optind = optreset = 1;
271 opterr = 0;
272 sformat = lformat = 0;
273 while ((ch = getopt(argc, argv, "ls")) != -1) {
272 mode = SHOWJOBS_DEFAULT;
273 while ((ch = getopt(argc, argv, "lps")) != -1) {
274 switch (ch) {
275 case 'l':
274 switch (ch) {
275 case 'l':
276 lformat = 1;
276 mode = SHOWJOBS_VERBOSE;
277 break;
277 break;
278 case 'p':
279 mode = SHOWJOBS_PGIDS;
280 break;
278 case 's':
281 case 's':
279 sformat = 1;
282 mode = SHOWJOBS_PIDS;
280 break;
281 case '?':
282 default:
283 error("unknown option: -%c", optopt);
284 }
285 }
286 argc -= optind;
287 argv += optind;
288
289 if (argc == 0)
283 break;
284 case '?':
285 default:
286 error("unknown option: -%c", optopt);
287 }
288 }
289 argc -= optind;
290 argv += optind;
291
292 if (argc == 0)
290 showjobs(0, sformat, lformat);
293 showjobs(0, mode);
291 else
292 while ((id = *argv++) != NULL)
294 else
295 while ((id = *argv++) != NULL)
293 showjob(getjob(id), 0, sformat, lformat);
296 showjob(getjob(id), 0, mode);
294
295 return (0);
296}
297
298STATIC void
297
298 return (0);
299}
300
301STATIC void
299showjob(struct job *jp, pid_t pid, int sformat, int lformat)
302showjob(struct job *jp, pid_t pid, int mode)
300{
301 char s[64];
302 struct procstat *ps;
303 struct job *j;
304 int col, curr, i, jobno, prev, procno;
303{
304 char s[64];
305 struct procstat *ps;
306 struct job *j;
307 int col, curr, i, jobno, prev, procno;
308 pid_t ppid;
305 char c;
306
309 char c;
310
307 procno = jp->nprocs;
311 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
308 jobno = jp - jobtab + 1;
309 curr = prev = 0;
310#if JOBS
311 if ((j = getcurjob(NULL)) != NULL) {
312 curr = j - jobtab + 1;
313 if ((j = getcurjob(j)) != NULL)
314 prev = j - jobtab + 1;
315 }
316#endif
317 for (ps = jp->ps ; ; ps++) { /* for each process */
312 jobno = jp - jobtab + 1;
313 curr = prev = 0;
314#if JOBS
315 if ((j = getcurjob(NULL)) != NULL) {
316 curr = j - jobtab + 1;
317 if ((j = getcurjob(j)) != NULL)
318 prev = j - jobtab + 1;
319 }
320#endif
321 for (ps = jp->ps ; ; ps++) { /* for each process */
318 if (sformat) {
319 out1fmt("%d\n", (int)ps->pid);
322 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
323 ppid = (mode == SHOWJOBS_PIDS) ? ps->pid :
324 getpgid(ps->pid);
325 out1fmt("%d\n", (int)ppid);
320 goto skip;
321 }
326 goto skip;
327 }
322 if (!lformat && ps != jp->ps && pid == 0)
328 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps && pid == 0)
323 goto skip;
324 if (pid != 0 && pid != ps->pid)
325 goto skip;
326 if (jobno == curr && ps == jp->ps)
327 c = '+';
328 else if (jobno == prev && ps == jp->ps)
329 c = '-';
330 else
331 c = ' ';
332 if (ps == jp->ps)
333 fmtstr(s, 64, "[%d] %c ", jobno, c);
334 else
335 fmtstr(s, 64, " %c ", c);
336 out1str(s);
337 col = strlen(s);
329 goto skip;
330 if (pid != 0 && pid != ps->pid)
331 goto skip;
332 if (jobno == curr && ps == jp->ps)
333 c = '+';
334 else if (jobno == prev && ps == jp->ps)
335 c = '-';
336 else
337 c = ' ';
338 if (ps == jp->ps)
339 fmtstr(s, 64, "[%d] %c ", jobno, c);
340 else
341 fmtstr(s, 64, " %c ", c);
342 out1str(s);
343 col = strlen(s);
338 if (lformat) {
344 if (mode == SHOWJOBS_VERBOSE) {
339 fmtstr(s, 64, "%d ", (int)ps->pid);
340 out1str(s);
341 col += strlen(s);
342 }
343 s[0] = '\0';
344 if (ps != jp->ps) {
345 *s = '\0';
346 } else if (ps->status == -1) {

--- 36 unchanged lines hidden (view full) ---

383 * statuses have changed since the last call to showjobs.
384 *
385 * If the shell is interrupted in the process of creating a job, the
386 * result may be a job structure containing zero processes. Such structures
387 * will be freed here.
388 */
389
390void
345 fmtstr(s, 64, "%d ", (int)ps->pid);
346 out1str(s);
347 col += strlen(s);
348 }
349 s[0] = '\0';
350 if (ps != jp->ps) {
351 *s = '\0';
352 } else if (ps->status == -1) {

--- 36 unchanged lines hidden (view full) ---

389 * statuses have changed since the last call to showjobs.
390 *
391 * If the shell is interrupted in the process of creating a job, the
392 * result may be a job structure containing zero processes. Such structures
393 * will be freed here.
394 */
395
396void
391showjobs(int change, int sformat, int lformat)
397showjobs(int change, int mode)
392{
393 int jobno;
394 struct job *jp;
395
396 TRACE(("showjobs(%d) called\n", change));
397 while (dowait(0, (struct job *)NULL) > 0);
398 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
399 if (! jp->used)
400 continue;
401 if (jp->nprocs == 0) {
402 freejob(jp);
403 continue;
404 }
405 if (change && ! jp->changed)
406 continue;
398{
399 int jobno;
400 struct job *jp;
401
402 TRACE(("showjobs(%d) called\n", change));
403 while (dowait(0, (struct job *)NULL) > 0);
404 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
405 if (! jp->used)
406 continue;
407 if (jp->nprocs == 0) {
408 freejob(jp);
409 continue;
410 }
411 if (change && ! jp->changed)
412 continue;
407 showjob(jp, 0, sformat, lformat);
413 showjob(jp, 0, mode);
408 jp->changed = 0;
409 if (jp->state == JOBDONE) {
410 freejob(jp);
411 }
412 }
413}
414
415

--- 571 unchanged lines hidden (view full) ---

987 if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F])
988 out1str(sys_siglist[i & 0x7F]);
989 else
990 out1fmt("Signal %d", i & 0x7F);
991 if (WCOREDUMP(status))
992 out1str(" (core dumped)");
993 out1c('\n');
994 } else
414 jp->changed = 0;
415 if (jp->state == JOBDONE) {
416 freejob(jp);
417 }
418 }
419}
420
421

--- 571 unchanged lines hidden (view full) ---

993 if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F])
994 out1str(sys_siglist[i & 0x7F]);
995 else
996 out1fmt("Signal %d", i & 0x7F);
997 if (WCOREDUMP(status))
998 out1str(" (core dumped)");
999 out1c('\n');
1000 } else
995 showjob(thisjob, pid, 0, 0);
1001 showjob(thisjob, pid, SHOWJOBS_DEFAULT);
996 }
997 } else {
998 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
999 if (thisjob)
1000 thisjob->changed = 1;
1001 }
1002 return pid;
1003}

--- 240 unchanged lines hidden ---
1002 }
1003 } else {
1004 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1005 if (thisjob)
1006 thisjob->changed = 1;
1007 }
1008 return pid;
1009}

--- 240 unchanged lines hidden ---