diff options
author | April Chin <April.Chin@Sun.COM> | 2008-12-27 14:59:38 -0800 |
---|---|---|
committer | April Chin <April.Chin@Sun.COM> | 2008-12-27 14:59:38 -0800 |
commit | 7c2fbfb345896881c631598ee3852ce9ce33fb07 (patch) | |
tree | 4b173b5657508562dfc0aa05f7d056d1e9add505 /usr/src/lib/libcmd/common/pids.c | |
parent | 6071ac1de68fed78e1e10052045bbb5f1732a263 (diff) | |
download | illumos-joyent-7c2fbfb345896881c631598ee3852ce9ce33fb07.tar.gz |
PSARC/2008/094 ksh93 Update 1
PSARC/2008/344 ksh93 Integration Update 1 Amendments 1
PSARC/2008/589 Remove /usr/bin/printf from PSARC case 2008 094
6619428 *ksh93* RFE: Update ksh93 in Solaris to ast-ksh.2008-11-04
6788659 RFE: Update libpp in Solaris to ast-open.2008-07-25
6561901 RFE: Add "shcomp" (shell script compiler) + kernel module to exec binary sh code
6599668 RFE: Move consumers of alias.sh over to ksh93
6595183 *ksh93* RFE: Update ksh93-integration demo code
6775901 *ksh93* no C message catalogs are generated for ksh93
6451262 *sleep* RFE: /usr/bin/sleep should support floating-point values
6687139 *ksh93* command substitution, exec, and stdout redirection cause allocation loop
6703761 *ksh93* crashes in script containing uncommon output redirections
6715496 *ksh93* SEGVs on array reinitialization
6713682 *ksh93* Creating a compound variable in a subshell "bleeds through" to the calling subshell
6672350 *ksh93* causes parent shell to die when child shell is suspended
6745015 *ksh93* VARIABLE=`command substitution` assignment is not reliable on OpenSolaris
6710205 *ksh93* problem with command substitution (within back quotes) containing \$'
6737600 *ksh93* exits debugger when user presses ctrl-c
6748645 *ksh93* fc -l -e - is mis-parsed, outputs wrong error message "-e - requires single argument"
6754020 *ksh93* does weird '[' expansion
6753538 *ksh93* umask modification leaks out of a ksh93 subshell
6766246 *ksh93* bug in pattern matching
6763594 *ksh93* executes command after "command" builtin twice on failure
6762665 *ksh93* Difficult-to-reproduce SIGSEGV in ksh93
Diffstat (limited to 'usr/src/lib/libcmd/common/pids.c')
-rw-r--r-- | usr/src/lib/libcmd/common/pids.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/usr/src/lib/libcmd/common/pids.c b/usr/src/lib/libcmd/common/pids.c new file mode 100644 index 0000000000..ca419850c1 --- /dev/null +++ b/usr/src/lib/libcmd/common/pids.c @@ -0,0 +1,120 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2008 AT&T Intellectual Property * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.opensource.org/licenses/cpl1.0.txt * +* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#define FORMAT "PID=%(pid)d PPID=%(ppid)d PGID=%(pgid)d TID=%(tid)d SID=%(sid)d" + +static const char usage[] = +"[-?\n@(#)$Id: pids (AT&T Research) 2008-04-01 $\n]" +USAGE_LICENSE +"[+NAME?pids - list calling shell process ids]" +"[+DESCRIPTION?When invoked as a shell builtin, \bpids\b lists one or " + "more of the calling process ids determined by \bgetpid\b(2), " + "\bgetppid\b(2), \bgetpgrp\b(2), \btcgetpgrp\b(2) and \bgetsid\b(2). " + "Unknown or invalid ids have the value \b-1\b.]" +"[f:format?List the ids specified by \aformat\a. \aformat\a follows " + "\bprintf\b(3) conventions, except that \bsfio\b(3) inline ids are used " + "instead of arguments: " + "%[-+]][\awidth\a[.\aprecis\a[.\abase\a]]]]]](\aid\a)\achar\a. The " + "supported \aid\as are:]:[format:=" FORMAT "]" + "{" + "[+pid?The process id.]" + "[+pgid?The process group id.]" + "[+ppid?The parent process id.]" + "[+tid|tty?The controlling terminal id.]" + "[+sid?The session id.]" + "}" +"[+SEE ALSO?\bgetpid\b(2), \bgetppid\b(2), \bgetpgrp\b(2), " + "\btcgetpgrp\b(2), \bgetsid\b(2)]" +; + +#include <cmd.h> +#include <ast_tty.h> +#include <sfdisc.h> + +/* + * sfkeyprintf() lookup + * handle==0 for heading + */ + +static int +key(void* handle, Sffmt_t* fp, const char* arg, char** ps, Sflong_t* pn) +{ + register char* s; + int fd; + long tid; + + if (!(s = fp->t_str) || streq(s, "pid")) + *pn = getpid(); + else if (streq(s, "pgid")) + *pn = getpgid(0); + else if (streq(s, "ppid")) + *pn = getppid(); + else if (streq(s, "tid") || streq(s, "tty")) + { + for (fd = 0; fd < 3; fd++) + if ((tid = tcgetpgrp(fd)) >= 0) + break; + *pn = tid; + } + else if (streq(s, "sid")) + *pn = getsid(0); + else if (streq(s, "format")) + *ps = (char*)handle; + else + { + error(2, "%s: unknown format identifier", s); + return 0; + } + return 1; +} + +int +b_pids(int argc, char** argv, void* context) +{ + char* format = 0; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + for (;;) + { + switch (optget(argv, usage)) + { + case 'f': + format = opt_info.arg; + continue; + case '?': + error(ERROR_USAGE|4, "%s", opt_info.arg); + continue; + case ':': + error(2, "%s", opt_info.arg); + continue; + } + break; + } + argv += opt_info.index; + if (error_info.errors || *argv) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + if (!format) + format = FORMAT; + sfkeyprintf(sfstdout, format, format, key, NiL); + sfprintf(sfstdout, "\n"); + return 0; +} |