standards/103331: [ patch ] add '-p' support for jobs sh(1) builtin

Roman Bogorodskiy novel at FreeBSD.org
Sun Sep 17 10:10:27 PDT 2006


>Number:         103331
>Category:       standards
>Synopsis:       [ patch ] add '-p' support for jobs sh(1) builtin
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Sep 17 17:10:25 GMT 2006
>Closed-Date:
>Last-Modified:
>Originator:     Roman Bogorodskiy
>Release:        FreeBSD 6.0-STABLE i386
>Organization:
>Environment:
>Description:
	Add '-p' support for sh(1)'s job builtin.

	References:
	  http://www.opengroup.org/onlinepubs/000095399/utilities/jobs.html

	PS Maybe it would make sense to rewrite it in a way that it used
	only one varible for all formats (i.e. introduce format codes) or
	use one bit-varible instead of one varible per format like it is done
	now.
>How-To-Repeat:
>Fix:

--- src_bin_sh_jobs_p.diff begins here ---
Index: jobs.c
===================================================================
RCS file: /home/ncvs/src/bin/sh/jobs.c,v
retrieving revision 1.71
diff -u -r1.71 jobs.c
--- jobs.c	4 Feb 2006 14:37:50 -0000	1.71
+++ jobs.c	17 Sep 2006 16:39:29 -0000
@@ -98,7 +98,7 @@
 STATIC void deljob(struct job *);
 STATIC struct job *getcurjob(struct job *);
 #endif
-STATIC void showjob(struct job *, pid_t, int, int);
+STATIC void showjob(struct job *, pid_t, int, int, int);
 
 
 /*
@@ -265,16 +265,19 @@
 jobscmd(int argc, char *argv[])
 {
 	char *id;
-	int ch, sformat, lformat;
+	int ch, sformat, lformat, pformat;
 
 	optind = optreset = 1;
 	opterr = 0;
 	sformat = lformat = 0;
-	while ((ch = getopt(argc, argv, "ls")) != -1) {
+	while ((ch = getopt(argc, argv, "lps")) != -1) {
 		switch (ch) {
 		case 'l':
 			lformat = 1;
 			break;
+		case 'p':
+			pformat = 1;
+			break;
 		case 's':
 			sformat = 1;
 			break;
@@ -287,16 +290,16 @@
 	argv += optind;
 
 	if (argc == 0)
-		showjobs(0, sformat, lformat);
+		showjobs(0, sformat, lformat, pformat);
 	else
 		while ((id = *argv++) != NULL)
-			showjob(getjob(id), 0, sformat, lformat);
+			showjob(getjob(id), 0, sformat, lformat, pformat);
 
 	return (0);
 }
 
 STATIC void
-showjob(struct job *jp, pid_t pid, int sformat, int lformat)
+showjob(struct job *jp, pid_t pid, int sformat, int lformat, int pformat)
 {
 	char s[64];
 	struct procstat *ps;
@@ -319,6 +322,10 @@
 			out1fmt("%d\n", (int)ps->pid);
 			goto skip;
 		}
+		if (pformat) {
+			out1fmt("%d\n", (int)getpgid(ps->pid));
+			goto skip;
+		}
 		if (!lformat && ps != jp->ps && pid == 0)
 			goto skip;
 		if (pid != 0 && pid != ps->pid)
@@ -388,7 +395,7 @@
  */
 
 void
-showjobs(int change, int sformat, int lformat)
+showjobs(int change, int sformat, int lformat, int pformat)
 {
 	int jobno;
 	struct job *jp;
@@ -404,7 +411,7 @@
 		}
 		if (change && ! jp->changed)
 			continue;
-		showjob(jp, 0, sformat, lformat);
+		showjob(jp, 0, sformat, lformat, pformat);
 		jp->changed = 0;
 		if (jp->state == JOBDONE) {
 			freejob(jp);
@@ -992,7 +999,7 @@
 					out1str(" (core dumped)");
 				out1c('\n');
 			} else
-				showjob(thisjob, pid, 0, 0);
+				showjob(thisjob, pid, 0, 0, 0);
 		}
 	} else {
 		TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
Index: jobs.h
===================================================================
RCS file: /home/ncvs/src/bin/sh/jobs.h,v
retrieving revision 1.18
diff -u -r1.18 jobs.h
--- jobs.h	6 Apr 2004 20:06:51 -0000	1.18
+++ jobs.h	17 Sep 2006 16:39:29 -0000
@@ -84,7 +84,7 @@
 int fgcmd(int, char **);
 int bgcmd(int, char **);
 int jobscmd(int, char **);
-void showjobs(int, int, int);
+void showjobs(int, int, int, int);
 int waitcmd(int, char **);
 int jobidcmd(int, char **);
 struct job *makejob(union node *, int);
Index: main.c
===================================================================
RCS file: /home/ncvs/src/bin/sh/main.c,v
retrieving revision 1.28
diff -u -r1.28 main.c
--- main.c	2 Apr 2006 18:51:32 -0000	1.28
+++ main.c	17 Sep 2006 16:39:29 -0000
@@ -211,7 +211,7 @@
 		inter = 0;
 		if (iflag && top) {
 			inter++;
-			showjobs(1, 0, 0);
+			showjobs(1, 0, 0, 0);
 			chkmail(0);
 			flushout(&output);
 		}
Index: sh.1
===================================================================
RCS file: /home/ncvs/src/bin/sh/sh.1,v
retrieving revision 1.122
diff -u -r1.122 sh.1
--- sh.1	29 Jul 2006 09:56:29 -0000	1.122
+++ sh.1	17 Sep 2006 16:39:46 -0000
@@ -1777,7 +1777,7 @@
 If the
 .Ar job
 argument is omitted, use the current job.
-.It Ic jobs Oo Fl ls Oc Op Ar job ...
+.It Ic jobs Oo Fl lps Oc Op Ar job ...
 Print information about the specified jobs, or all jobs if no
 .Ar job
 argument is given.
@@ -1787,6 +1787,10 @@
 .Fl l
 option is specified, the PID of each job is also printed.
 If the
+.Fl p
+option is specified, only the process IDs for the process group leaders
+are printed, one per line.
+If the
 .Fl s
 option is specified, only the PIDs of the jobs are printed, one per line.
 .It Ic local Oo Ar variable ... Oc Op Fl
--- src_bin_sh_jobs_p.diff ends here ---


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-standards mailing list