Add -s (output PID's only) and -l (show PID's) options to the jobs(1)

builtin. Modify the output format to match what SUSv3 requires.
This commit is contained in:
Tim J. Robbins 2002-05-31 14:04:23 +00:00
parent dc1024256c
commit ad8a075902
4 changed files with 86 additions and 19 deletions

View File

@ -109,7 +109,7 @@ STATIC void setcurjob(struct job *);
STATIC void deljob(struct job *);
STATIC struct job *getcurjob(struct job *);
#endif
STATIC void showjob(struct job *);
STATIC void showjob(struct job *, int, int);
/*
@ -271,31 +271,85 @@ restartjob(struct job *jp)
int
jobscmd(int argc __unused, char **argv __unused)
jobscmd(int argc, char *argv[])
{
showjobs(0);
return 0;
struct job *jp;
char *id;
int ch, sformat, lformat;
optind = optreset = 1;
sformat = lformat = 0;
while ((ch = getopt(argc, argv, "ls")) != -1) {
switch (ch) {
case 'l':
lformat = 1;
break;
case 's':
sformat = 1;
break;
case '?':
default:
error("unknown option: -%c", optopt);
}
}
argc -= optind;
argv += optind;
if (argc == 0)
showjobs(0, sformat, lformat);
else
while ((id = *argv++) != NULL)
showjob(getjob(id), sformat, lformat);
return (0);
}
STATIC void
showjob(struct job *jp)
showjob(struct job *jp, int sformat, int lformat)
{
char s[64];
struct procstat *ps;
int col, i, jobno, procno;
struct job *j;
int col, curr, i, jobno, prev, procno;
char c;
procno = 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
for (ps = jp->ps ; ; ps++) { /* for each process */
if (ps == jp->ps)
fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
if (sformat) {
out1fmt("%d\n", ps->pid);
goto skip;
}
if (!lformat && ps != jp->ps)
goto skip;
if (jobno == curr)
c = '+';
else if (jobno == prev)
c = '-';
else
fmtstr(s, 64, " %d ", ps->pid);
c = ' ';
if (ps == jp->ps)
fmtstr(s, 64, "[%d] %c ", jobno, c);
else
fmtstr(s, 64, " %c ", c);
out1str(s);
col = strlen(s);
if (lformat) {
fmtstr(s, 64, "%d ", ps->pid);
out1str(s);
col += strlen(s);
}
s[0] = '\0';
if (ps->status == -1) {
/* don't print anything */
strcpy(s, "Running");
} else if (WIFEXITED(ps->status)) {
fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
} else {
@ -320,7 +374,7 @@ showjob(struct job *jp)
} while (col < 30);
out1str(ps->cmd);
out1c('\n');
if (--procno <= 0)
skip: if (--procno <= 0)
break;
}
}
@ -335,7 +389,7 @@ showjob(struct job *jp)
*/
void
showjobs(int change)
showjobs(int change, int sformat, int lformat)
{
int jobno;
struct job *jp;
@ -351,7 +405,7 @@ showjobs(int change)
}
if (change && ! jp->changed)
continue;
showjob(jp);
showjob(jp, sformat, lformat);
jp->changed = 0;
if (jp->state == JOBDONE) {
freejob(jp);
@ -909,7 +963,7 @@ dowait(int block, struct job *job)
sig = WTERMSIG(status);
}
if (sig != 0 && sig != SIGINT && sig != SIGPIPE)
showjob(thisjob);
showjob(thisjob, 0, 0);
} else {
TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
if (thisjob)

View File

@ -87,7 +87,7 @@ void setjobctl(int);
int fgcmd(int, char **);
int bgcmd(int, char **);
int jobscmd(int, char **);
void showjobs(int);
void showjobs(int, int, int);
int waitcmd(int, char **);
int jobidcmd(int, char **);
struct job *makejob(union node *, int);

View File

@ -229,7 +229,7 @@ cmdloop(int top)
inter = 0;
if (iflag && top) {
inter++;
showjobs(1);
showjobs(1, 0, 0);
chkmail(0);
flushout(&output);
}

View File

@ -1570,9 +1570,22 @@ Print the process id's of the processes in the specified
If the
.Ar job
argument is omitted, use the current job.
.It Ic jobs
This command lists out all the background processes
which are children of the current shell process.
.It Xo
.Ic jobs
.Op Fl ls
.Op Ar job ...
.Xc
Print information about the specified jobs, or all jobs if no
.Ar job
argument is given.
The information printed includes job ID, status and command name.
.Pp
If the
.Fl l
option is specified, the PID of each job is also printed.
If the
.Fl s
option is specified, only the PID's of the jobs are printed, one per line.
.It Ic pwd Op Fl LP
Print the path of the current directory. The builtin command may
differ from the program of the same name because the