jobs.c (345559) | jobs.c (345561) |
---|---|
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: stable/11/bin/sh/jobs.c 345559 2019-03-26 21:30:26Z jilles $"); | 39__FBSDID("$FreeBSD: stable/11/bin/sh/jobs.c 345561 2019-03-26 22:34:07Z jilles $"); |
40 41#include <sys/ioctl.h> 42#include <sys/param.h> 43#include <sys/resource.h> 44#include <sys/time.h> 45#include <sys/wait.h> 46#include <errno.h> 47#include <fcntl.h> --- 51 unchanged lines hidden (view full) --- 99 struct procstat *ps; /* status or processes when more than one */ 100 short nprocs; /* number of processes */ 101 pid_t pgrp; /* process group of this job */ 102 char state; /* true if job is finished */ 103 char used; /* true if this entry is in used */ 104 char changed; /* true if status has changed */ 105 char foreground; /* true if running in the foreground */ 106 char remembered; /* true if $! referenced */ | 40 41#include <sys/ioctl.h> 42#include <sys/param.h> 43#include <sys/resource.h> 44#include <sys/time.h> 45#include <sys/wait.h> 46#include <errno.h> 47#include <fcntl.h> --- 51 unchanged lines hidden (view full) --- 99 struct procstat *ps; /* status or processes when more than one */ 100 short nprocs; /* number of processes */ 101 pid_t pgrp; /* process group of this job */ 102 char state; /* true if job is finished */ 103 char used; /* true if this entry is in used */ 104 char changed; /* true if status has changed */ 105 char foreground; /* true if running in the foreground */ 106 char remembered; /* true if $! referenced */ |
107 char pipefail; /* pass any non-zero status */ |
|
107#if JOBS 108 char jobctl; /* job running under job control */ 109 struct job *next; /* job used after this one */ 110#endif 111}; 112 113 114static struct job *jobtab; /* array of jobs */ --- 23 unchanged lines hidden (view full) --- 138static void checkzombies(void); 139static void cmdtxt(union node *); 140static void cmdputs(const char *); 141#if JOBS 142static void setcurjob(struct job *); 143static void deljob(struct job *); 144static struct job *getcurjob(struct job *); 145#endif | 108#if JOBS 109 char jobctl; /* job running under job control */ 110 struct job *next; /* job used after this one */ 111#endif 112}; 113 114 115static struct job *jobtab; /* array of jobs */ --- 23 unchanged lines hidden (view full) --- 139static void checkzombies(void); 140static void cmdtxt(union node *); 141static void cmdputs(const char *); 142#if JOBS 143static void setcurjob(struct job *); 144static void deljob(struct job *); 145static struct job *getcurjob(struct job *); 146#endif |
147static int getjobstatus(const struct job *); |
|
146static void printjobcmd(struct job *); 147static void showjob(struct job *, int); 148 149 150/* 151 * Turn job control on and off. 152 */ 153 --- 181 unchanged lines hidden (view full) --- 335 showjobs(0, mode); 336 else 337 while ((id = *argptr++) != NULL) 338 showjob(getjob(id), mode); 339 340 return (0); 341} 342 | 148static void printjobcmd(struct job *); 149static void showjob(struct job *, int); 150 151 152/* 153 * Turn job control on and off. 154 */ 155 --- 181 unchanged lines hidden (view full) --- 337 showjobs(0, mode); 338 else 339 while ((id = *argptr++) != NULL) 340 showjob(getjob(id), mode); 341 342 return (0); 343} 344 |
345static int getjobstatus(const struct job *jp) 346{ 347 int i, status; 348 349 if (!jp->pipefail) 350 return (jp->ps[jp->nprocs - 1].status); 351 for (i = jp->nprocs - 1; i >= 0; i--) { 352 status = jp->ps[i].status; 353 if (status != 0) 354 return (status); 355 } 356 return (0); 357} 358 |
|
343static void 344printjobcmd(struct job *jp) 345{ 346 struct procstat *ps; 347 int i; 348 349 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 350 out1str(ps->cmd); --- 20 unchanged lines hidden (view full) --- 371#if JOBS 372 if ((j = getcurjob(NULL)) != NULL) { 373 curr = j - jobtab + 1; 374 if ((j = getcurjob(j)) != NULL) 375 prev = j - jobtab + 1; 376 } 377#endif 378 coredump = ""; | 359static void 360printjobcmd(struct job *jp) 361{ 362 struct procstat *ps; 363 int i; 364 365 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 366 out1str(ps->cmd); --- 20 unchanged lines hidden (view full) --- 387#if JOBS 388 if ((j = getcurjob(NULL)) != NULL) { 389 curr = j - jobtab + 1; 390 if ((j = getcurjob(j)) != NULL) 391 prev = j - jobtab + 1; 392 } 393#endif 394 coredump = ""; |
379 status = jp->ps[jp->nprocs - 1].status; | 395 status = getjobstatus(jp); |
380 if (jp->state == 0) { 381 statestr = "Running"; 382#if JOBS 383 } else if (jp->state == JOBSTOPPED) { 384 ps = jp->ps + jp->nprocs - 1; 385 while (!WIFSTOPPED(ps->status) && ps > jp->ps) 386 ps--; 387 if (WIFSTOPPED(ps->status)) --- 162 unchanged lines hidden (view full) --- 550 /* 551 * Loop until a process is terminated or stopped, or a SIGINT is 552 * received. 553 */ 554 555 do { 556 if (job != NULL) { 557 if (job->state == JOBDONE) { | 396 if (jp->state == 0) { 397 statestr = "Running"; 398#if JOBS 399 } else if (jp->state == JOBSTOPPED) { 400 ps = jp->ps + jp->nprocs - 1; 401 while (!WIFSTOPPED(ps->status) && ps > jp->ps) 402 ps--; 403 if (WIFSTOPPED(ps->status)) --- 162 unchanged lines hidden (view full) --- 566 /* 567 * Loop until a process is terminated or stopped, or a SIGINT is 568 * received. 569 */ 570 571 do { 572 if (job != NULL) { 573 if (job->state == JOBDONE) { |
558 status = job->ps[job->nprocs - 1].status; | 574 status = getjobstatus(job); |
559 if (WIFEXITED(status)) 560 retval = WEXITSTATUS(status); 561 else 562 retval = WTERMSIG(status) + 128; 563 if (! iflag || ! job->changed) 564 freejob(job); 565 else { 566 job->remembered = 0; --- 208 unchanged lines hidden (view full) --- 775 } 776 INTOFF; 777 jp->state = 0; 778 jp->used = 1; 779 jp->changed = 0; 780 jp->nprocs = 0; 781 jp->foreground = 0; 782 jp->remembered = 0; | 575 if (WIFEXITED(status)) 576 retval = WEXITSTATUS(status); 577 else 578 retval = WTERMSIG(status) + 128; 579 if (! iflag || ! job->changed) 580 freejob(job); 581 else { 582 job->remembered = 0; --- 208 unchanged lines hidden (view full) --- 791 } 792 INTOFF; 793 jp->state = 0; 794 jp->used = 1; 795 jp->changed = 0; 796 jp->nprocs = 0; 797 jp->foreground = 0; 798 jp->remembered = 0; |
799 jp->pipefail = pipefailflag; |
|
783#if JOBS 784 jp->jobctl = jobctl; 785 jp->next = NULL; 786#endif 787 if (nprocs > 1) { 788 jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 789 } else { 790 jp->ps = &jp->ps0; --- 279 unchanged lines hidden (view full) --- 1070#if JOBS 1071 if (jp->jobctl) { 1072 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0) 1073 error("tcsetpgrp failed, errno=%d\n", errno); 1074 } 1075 if (jp->state == JOBSTOPPED) 1076 setcurjob(jp); 1077#endif | 800#if JOBS 801 jp->jobctl = jobctl; 802 jp->next = NULL; 803#endif 804 if (nprocs > 1) { 805 jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 806 } else { 807 jp->ps = &jp->ps0; --- 279 unchanged lines hidden (view full) --- 1087#if JOBS 1088 if (jp->jobctl) { 1089 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0) 1090 error("tcsetpgrp failed, errno=%d\n", errno); 1091 } 1092 if (jp->state == JOBSTOPPED) 1093 setcurjob(jp); 1094#endif |
1078 status = jp->ps[jp->nprocs - 1].status; | 1095 status = getjobstatus(jp); |
1079 if (origstatus != NULL) 1080 *origstatus = status; 1081 /* convert to 8 bits */ 1082 if (WIFEXITED(status)) 1083 st = WEXITSTATUS(status); 1084#if JOBS 1085 else if (WIFSTOPPED(status)) 1086 st = WSTOPSIG(status) + 128; --- 466 unchanged lines hidden --- | 1096 if (origstatus != NULL) 1097 *origstatus = status; 1098 /* convert to 8 bits */ 1099 if (WIFEXITED(status)) 1100 st = WEXITSTATUS(status); 1101#if JOBS 1102 else if (WIFSTOPPED(status)) 1103 st = WSTOPSIG(status) + 128; --- 466 unchanged lines hidden --- |