int bgcmd(int argc, char **argv) { struct job *jp; do { jp = getjob(*++argv); if (jp->jobctl == 0) error("job not created under job control"); if (jp->state == JOBDONE) continue; restartjob(jp); jp->foreground = 0; out1fmt("[%td] ", jp - jobtab + 1); printjobcmd(jp); } while (--argc > 1); return 0; }
int fgcmd(int argc __unused, char **argv) { struct job *jp; pid_t pgrp; int status; jp = getjob(argv[1]); if (jp->jobctl == 0) error("job not created under job control"); printjobcmd(jp); flushout(&output); pgrp = jp->ps[0].pid; tcsetpgrp(ttyfd, pgrp); restartjob(jp); jp->foreground = 1; INTOFF; status = waitforjob(jp, (int *)NULL); INTON; return status; }
static void showjob(struct job *jp, int mode) { char s[64]; char statestr[64]; struct procstat *ps; struct job *j; int col, curr, i, jobno, prev, procno; char c; procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs; jobno = jp - jobtab + 1; curr = prev = 0; #if JOBS if ((j = getcurjob(NULL)) != NULL) { curr = j - jobtab + 1; if ((j = getcurjob(j)) != NULL) prev = j - jobtab + 1; } #endif ps = jp->ps + jp->nprocs - 1; if (jp->state == 0) { strcpy(statestr, "Running"); #if JOBS } else if (jp->state == JOBSTOPPED) { while (!WIFSTOPPED(ps->status) && ps > jp->ps) ps--; if (WIFSTOPPED(ps->status)) i = WSTOPSIG(ps->status); else i = -1; if (i > 0 && i < sys_nsig && sys_siglist[i]) strcpy(statestr, sys_siglist[i]); else strcpy(statestr, "Suspended"); #endif } else if (WIFEXITED(ps->status)) { if (WEXITSTATUS(ps->status) == 0) strcpy(statestr, "Done"); else fmtstr(statestr, 64, "Done(%d)", WEXITSTATUS(ps->status)); } else { i = WTERMSIG(ps->status); if (i > 0 && i < sys_nsig && sys_siglist[i]) strcpy(statestr, sys_siglist[i]); else fmtstr(statestr, 64, "Signal %d", i); if (WCOREDUMP(ps->status)) strcat(statestr, " (core dumped)"); } for (ps = jp->ps ; ; ps++) { /* for each process */ if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) { out1fmt("%d\n", (int)ps->pid); goto skip; } if (mode != SHOWJOBS_VERBOSE && ps != jp->ps) goto skip; if (jobno == curr && ps == jp->ps) c = '+'; else if (jobno == prev && ps == jp->ps) c = '-'; else c = ' '; if (ps == jp->ps) fmtstr(s, 64, "[%d] %c ", jobno, c); else fmtstr(s, 64, " %c ", c); out1str(s); col = strlen(s); if (mode == SHOWJOBS_VERBOSE) { fmtstr(s, 64, "%d ", (int)ps->pid); out1str(s); col += strlen(s); } if (ps == jp->ps) { out1str(statestr); col += strlen(statestr); } do { out1c(' '); col++; } while (col < 30); if (mode == SHOWJOBS_VERBOSE) { out1str(ps->cmd); out1c('\n'); } else printjobcmd(jp); skip: if (--procno <= 0) break; } }