diff options
Diffstat (limited to 'usr/src/lib/libcmd/common')
54 files changed, 13379 insertions, 86 deletions
diff --git a/usr/src/lib/libcmd/common/RELEASE b/usr/src/lib/libcmd/common/RELEASE new file mode 100644 index 0000000000..7e343a5607 --- /dev/null +++ b/usr/src/lib/libcmd/common/RELEASE @@ -0,0 +1,217 @@ +07-03-28 date.c: add --unelepsed=scale, -U: fmtelapsed() => strelapsed() +07-03-25 wclib.h: iswspace() requires <wctype.h>! +07-03-11 tty.c: add sysV --line-number, -l +07-02-26 Makefile: sumlib.o: direct extract from +lsum (vcodex someday) +07-02-24 Makefile: tweak cmdext.h action for --mam bootstrap +07-02-09 Makefile: { cmdext.h cmdlist.h } depend on *.c list! +07-02-09 Makefile: +lsum to bring in static -lsum (no dynamic right now) +07-02-07 cksum.c: move from src/cmd/std with ftwalk => fts +07-02-07 getconf.c: handle /bin == /usr/bin in defer logic +07-01-26 chmod.c: don't FTS_FOLLOW if !FTS_PHYSICAL +07-01-23 cut.c: Cut_t variable dimension list[] must be last member +07-01-22 uname.c: fix -h typo that clobbered astconf() state -- ouch +07-01-02 fmt.c: fix buffer splice off by one bug -- what else +06-11-23 cmd.h: because of proto cmdinit cannot be a function like macro +06-11-21 cp.c: fix 06-10-31 const dot[] readonly assignment +06-11-15 cp.c: fix 06-10-31 ln -s enoent bug +06-11-11 getconf.c: let astconf() handle "undefined" vs. "" +06-11-11 getconf.c: fix deferred getconf path search +06-11-11 fmt.c: handle two char { \t \n } in --usage ouput +06-10-31 global edit to eliminate most non-const static data0 +06-10-31 use <cmd.h> for all b_*() implementations; drop <cmdlib.h> +06-10-31 cmd.h: add CMD_ prefix to { BUILTIN DYNAMIC STANDALONE } +06-10-31 join.c: tone down /tmp usage vi SFSK_DISCARD +06-10-31 cp.c,rm.c: update to <fts.h> to accomodate non-static data +06-10-29 date.c: "...%H%..." => "...%H" "%..." to avoid SCCS conflict +06-10-26 fds.c: handle sctp +06-10-18 tail.c: fix invalid suffix infinite loop +06-10-11 chgrp.c,cp.c: add sfstruse() error checks +06-10-10 tee.c: add --linebuffer, -l +06-10-06 getconf.c: preserve native getconf(1) known variable behavior +06-10-04 sync.c: add (thanks to Roland Mainz) +06-10-04 getconf.c: add -v specification => run native getconf(1) +06-09-28 stty.c: static setmode() => set() for darwin.i386 +06-09-27 head.c: handle -1c => -c1 +06-09-19 pathchk.c: pathconf() => astconf() +06-09-11 tail.c: handle compatibility corner cases +06-09-08 date.c: add output write error diagnostic +06-09-04 tail.c: fix initial position for -n0, no args => no -f +06-08-28 uniq.c: add -D,--all-repeated +06-08-25 wc.c,wclib.c: add -L,--longest-line,WC_LONGEST +06-08-24 wc.c,wclib.c: implement -m and WC_MBYTE +06-08-24 rmdir.c: -sp applies to every message, add gnu -e +06-08-23 rmdir.c: add solaris --suppress, -s +06-08-23 mkdir.c: don't add 0300 to -p final dir mode +06-07-17 cut.c: handle last line with no newline +06-07-17 cut.c: --output-delimiter == --line-delimiter +06-06-25 chmod.c: mask -c output with S_IPERM +06-05-09 uname.c: add -o; change -a to match linux +06-05-03 date.c: add --last -L to list last of multiple time args +06-02-14 tail.c: fix -f bug that lost fast stream data +06-02-11 getconf.c: exit 1 if name invalid -- duh +06-01-28 cp.c,rm.c: fix astquery() 'q' to return and not exit() +05-08-11 fmt.c: fix -o to handle raw --usage strings +05-05-17 cat.c,head.c: disable EPIPE error messages +05-04-14 chgrp.c: -f means all non-syntax error messages +05-04-11 fds.c: add from old internal open(1) +05-04-09 cmdext.h,cmdlist.h: generate from source -- about time +05-03-24 features/symlink: verify { lchmod lchown } implementations +05-03-07 date.c: add --listzones to list the time zone table +05-02-14 chmod.c: add --reference=file +05-01-11 cat.c: restore output stream to binary mode on exit +04-12-15 cp.c: add --preserve high resolution time support +04-12-08 date.c: add high resolution time support +04-12-01 cmp.c: fix %6I*ld => %6I*d -- doh + fmt.c: handle "\n\n operands \n\n" + head.c: handle -cN -nN, N > 4Gb +04-11-22 cmp.c: handle >2G chars/lines +04-11-18 fold.c: add --prepend=text, --append=text +04-10-31 tail.c: use SF_LOCKR macro +04-10-28 tail.c: use strtol() for old stype [+-]number[suffix] -- doh +04-10-22 cp.c: check rename() errno==ENOENT to retain destination +04-10-11 fmt.c: fix -o,--optget sublist bugs + tail.c: use strton() for number conversion +04-10-08 pathchk.c: add empty path and -p - first component char +04-10-01 fmt.c: add -o,--optget concatenated usage string format + stty.c: context is ERROR_INTERCATIVE + rm.c: restore 3d before exit +04-09-24 pathchk.c: fix docs +04-09-14 date.c: add %| and %& --parse docs +04-08-27 cp.c: add FTW_DC check -- duh +04-08-01 fmt.c: handle last char != '\n' +04-07-22 date.c,uname.c: access() => eaccess() +04-07-01 fmt.c: handle large input lines -- ouch +04-06-11 id.c: fix -r to output something! +04-05-27 expr.c: fix `:' op subexpression output +04-04-15 chmod.c: follow symlink for relative mode +04-04-12 Makefile: add STDCHMOD (for osf.alpha) +04-03-19 tail.c: handle -f sfreserve() large chunk failure +04-02-29 cp.c: decouple -f and -i for standard CONFORMANCE + cp.c: mv now attempts rename() before remove()+rename() + date.c: -f format or +format disables system clock set +04-02-14 cp.c: add -F --fsync to call fsync(2) for each copied file +04-01-05 head.c: -s now uses opt_info.number for >2Gb skip +03-09-18 tail.c: add --log +03-09-11 rm.c: add --unconditional +03-08-11 fold.c: add --delimiter=c to break at c +03-07-28 features/time: change settimeofday() test to 2nd arg of (void*)0 + expr.c: add {match,substr,index,length,quote} +03-07-15 fmt.c: fix trailing space bug +03-06-20 uname.c: fix -p constant string overwrite +03-06-04 stty.c: add undef to control assignment docs +03-05-31 uname.c: add -f and sysinfo()/confstr() compatibility via astconf() +03-05-27 rm.c: fix inappropriate "/.." append path overflow + cut.c: snarf from dgk +03-05-18 rm.c: check st_nlink to verify progress w.r.t. ftwalk/fts +03-05-15 join.c: fix stealth -v2 bug (thanks ahs) +03-05-04 wc.c: drop trailing space for `wc -l < file' +03-03-21 date.c: add %Q/recent/distant/ docs +03-02-19 date.c: fix %+|!flag docs +02-11-14 update for cmdinit() 4th arg and ERROR_NOTIFY for interrupt cleanup +02-10-02 date.c: tmform() => tmfmt() +02-09-30 date.c,uname.c: change execv() calls to procrun(): exec|exit => bad +02-09-06 wclib.c: fix 1 char-at-a-time miscount bug +02-08-19 chgrp.c: convert to use <cdt.h> +02-07-23 join.c: fix comm snarf typo +02-04-05 date.c: add %u +02-01-24 stty.c: ifdef a few more macros for uts (yes, its still running) +01-12-14 date.c: clarify %z doc +01-10-31 mkdir.c: mkdir() on existing dir could fail with errno!=EEXIST + uname.c: add execve() loop check for unknown options +01-10-29 tail.c: SF_SHARE on only if not reading through EOF +01-10-11 getconf.c: fix usage typos +01-09-11 cp.c,cmd.h: handle . in argv[0] + cp.c: add O_BINARY to all open() calls +01-09-06 tail: input streams must be SF_SHARE -- duh +01-07-16 stty: fix cntl() macro for CC_NATIVE!=CC_ASCII +01-05-31 date: fix /bin/date fallback logic + stty: fix a few mismatched flags, -a and -g option logic + stty: tone down sane to modify current settings rather than from zero +01-05-01 uname: -i => -h, add sol.sun4 -i, add sgi -R, punt to /usr/bin/uname +01-04-17 date,rm: add +01-03-07 cp: fix readonly string mod on "." +01-01-23 cp: `cp foo' => `cp foo .' only for CONFORMANCE!=standard +00-12-01 cut: multibyte support +00-10-31 mkdir: handle races by checking EEXIST +00-09-20 cp: copy argv to stack before modifying in place +00-05-18 add setlocale(LC_ALL,"") +00-04-30 join: drop wierd opt_info.argv reference +00-03-17 expr: add == operator -- duh + cp,ln,mv: delay pathcanon() on destination to verify `cp a b/.' + getconf: use astgetconf for proper message control + ERROR_translate: dictionary update +00-03-08 tail: handle multiple -f files +00-03-07 fmt: add +00-03-07 dirname: handle PATH_LEADING_SLASHES as documented + tail: accept + options +00-02-14 chmod: --ignore-umask to ignore umask(2) in symbolic expressions + chmod,chgrp,cp: use FTS_NOSEEDOTDIR for correct path construction + cat: fix -n (was ignored, wow) +00-01-27 getconf: add "-a" and "-v spec" for sol7.* compatibility +99-09-09 join: fix -j1 vs. -j 1, add --ignorecase +99-06-22 paste: defualt delim in writable string +99-06-16 cat: fix --dos-ouput typo +99-06-11 cp: tighten chown() diagnostics +99-06-08 expr: nothing for NULL string bug fix +99-05-21 paste: fix missing newline columnize bug +99-05-20 mv: do not check for `mv foo foo' since rename() handles it +99-05-01 cmp,comm,cp/mv/ln,expr,fold,getconf,head: long options + join,logname,paste,pathchk,tail,tee: long options +99-04-10 uname: long options, stdize -a + chmod,head,tail,rev: long options + cut: long options, pass regression test 02 +99-04-07 cat: long options, fix O_TEXT modes +99-01-11 tail: fix +n + join: another ggs/psm bug + join: all 1 and/or 2 to be unseekable +99-01-01 cp: fix -p + chmod: drop -l option because of clash with l (lock) mode +98-12-25 cat: add -T to sfopen(,,"rt") +98-11-11 chgrp,chmod: cannot open file stream => not found + join: fix another ggs/psm bug; thanks guys +98-10-20 cp: fix cp -rp to update dir times too +98-09-22 join: fix ggs null field bug +98-08-11 join: fix last regression test bug +98-05-29 join: add jp->common to handle boundary conditions +98-03-11 cat,cp,rev,tee: fix sfmove() error checks +98-03-01 join: fix bug that emitted records more than once after eof + cp: fix sfmove() error check +98-02-14 cp: -R physical, -[HLP], -r getconf(PATH_RESOLVE) +98-01-11 cp: check sfclose() return value +98-01-07 chown,chgrp,chmod: use fts for -R + mkdir: fix -p default mode +97-12-07 mkdir: fix umask() reset +97-11-11 chown,chgrp: proper interpretation of -h,-l for lchown() + chown,chgrp: only chown() if uid or gid change +97-10-31 mkdir: do umask right +97-08-11 cmdinit: clear opt_info.index to allow multiple calls + cp,ln,mv: add +97-07-17 join: fix a few more -a bugs +97-05-31 expr: optget() only if CONFORMANCE==standard +97-04-01 join: fix a few bugs that make it work! +96-12-25 head: sfset(sfstdin,SF_SHARE,1) + Makefile: add -last to cmd lib list + drop function __IMPORT__ +96-08-11 tail: check for truncated file and rewind for -f +96-04-08 update <cmd.h> +96-02-29 uname: -a like std, -l for everything + id: add -a (default) +96-02-14 wc: speed up inner loop newline breakout +96-01-30 unused var cleanup +96-01-01 AT&T Research now + pathchk: handle getcwd(0,0) error + expr: switch to <regex.h> +95-11-11 add expr.c + fix cut exit code and -s optimization +95-10-11 add extern b_* to cmd.h + add void* context 3rd arg to b_main() +95-05-09 add getconf + cat -u avoids mmap + add chown|chgrp -m uid|gid map file + add chown|chgrp -P for systems with lchown(2) + chown|chgrp -P => lstat() too! + chmod|chown|chgrp -HLP +95-04-01 version 1.2 + add rmdir +95-02-14 fix mkdir -p symlink bug + fix mkdir '/' skip bug that went one too far diff --git a/usr/src/lib/libcmd/common/basename.c b/usr/src/lib/libcmd/common/basename.c new file mode 100644 index 0000000000..f715091c32 --- /dev/null +++ b/usr/src/lib/libcmd/common/basename.c @@ -0,0 +1,116 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * namebase pathname [suffix] + * + * print the namebase of a pathname + */ + +static const char usage[] = +"[-?\n@(#)$Id: basename (AT&T Research) 1999-04-10 $\n]" +USAGE_LICENSE +"[+NAME?basename - strip directory and suffix from filenames]" +"[+DESCRIPTION?\bbasename\b removes all leading directory components " + "from the file name defined by \astring\a. If the file name " + "defined by \astring\a has a suffix that ends in \asuffix\a, " + "it is removed as well.]" +"[+?If \astring\a consists solely of \b/\b characters the output will " + "be a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by " + "\bgetconf\b(1) is \b1\b and \astring\a consists of multiple " + "\b/\b characters in which case \b//\b will be output. " + "Otherwise, trailing \b/\b characters are removed, and if " + "there are any remaining \b/\b characters in \astring\a, " + "all characters up to and including the last \b/\b are removed. " + "Finally, if \asuffix\a is specified, and is identical the end " + "of \astring\a, these characters are removed. The characters " + "not removed from \astring\a will be written to standard output.]" +"\n" +"\n string [suffix]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?Successful Completion.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bdirname\b(1), \bgetconf\b(1), \bbasename\b(3)]" +; + + +#include <cmd.h> + +static void namebase(Sfio_t *outfile, register char *pathname, char *suffix) +{ + register char *first, *last; + register int n=0; + for(first=last=pathname; *last; last++); + /* back over trailing '/' */ + if(last>first) + while(*--last=='/' && last > first); + if(last==first && *last=='/') + { + /* all '/' or "" */ + if(*first=='/') + if(*++last=='/') /* keep leading // */ + last++; + } + else + { + for(first=last++;first>pathname && *first!='/';first--); + if(*first=='/') + first++; + /* check for trailing suffix */ + if(suffix && (n=strlen(suffix)) && n<(last-first)) + { + if(memcmp(last-n,suffix,n)==0) + last -=n; + } + } + if(last>first) + sfwrite(outfile,first,last-first); + sfputc(outfile,'\n'); +} + +int +b_basename(int argc,register char *argv[], void* context) +{ + register int n; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + argc -= opt_info.index; + if(error_info.errors || argc < 1 || argc > 2) + error(ERROR_usage(2), "%s", optusage(NiL)); + namebase(sfstdout,argv[0],argv[1]); + return(0); +} + diff --git a/usr/src/lib/libcmd/common/cat.c b/usr/src/lib/libcmd/common/cat.c new file mode 100644 index 0000000000..884726e572 --- /dev/null +++ b/usr/src/lib/libcmd/common/cat.c @@ -0,0 +1,381 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Bell Laboratories + * + * cat + */ + +#include <cmd.h> +#include <fcntl.h> + +static const char usage[] = +"[-?\n@(#)$Id: cat (AT&T Research) 2006-05-17 $\n]" +USAGE_LICENSE +"[+NAME?cat - concatenate files]" +"[+DESCRIPTION?\bcat\b copies each \afile\a in sequence to the standard" +" output. If no \afile\a is given, or if the \afile\a is \b-\b," +" \bcat\b copies from standard input starting at the current location.]" + +"[b:number-nonblank?Number lines as with \b-n\b but omit line numbers from" +" blank lines.]" +"[d:dos-input?Input files are opened in \atext\amode which removes carriage" +" returns in front of new-lines on some systems.]" +"[e?Equivalent to \b-vE\b.]" +"[n:number?Causes a line number to be inserted at the beginning of each line.]" +"[s?Equivalent to \b-S\b for \aatt\a universe and \b-B\b otherwise.]" +"[t?Equivalent to \b-vT\b.]" +"[u:unbuffer?The output is not delayed by buffering.]" +"[v:show-nonprinting?Causes non-printing characters (whith the exception of" +" tabs, new-lines, and form-feeds) to be output as printable charater" +" sequences. ASCII control characters are printed as \b^\b\an\a," +" where \an\a is the corresponding ASCII character in the range" +" octal 100-137. The DEL character (octal 0177) is copied" +" as \b^?\b. Other non-printable characters are copied as \bM-\b\ax\a" +" where \ax\a is the ASCII character specified by the low-order seven" +" bits. Multibyte characters in the current locale are treated as" +" printable characters.]" +"[A:show-all?Equivalent to \b-vET\b.]" +"[B:squeeze-blank?Multiple adjacent new-line characters are replace by one" +" new-line.]" +"[D:dos-output?Output files are opened in \atext\amode which inserts carriage" +" returns in front of new-lines on some systems.]" +"[E:show-ends?Causes a \b$\b to be inserted before each new-line.]" +"[S:silent?\bcat\b is silent about non-existent files.]" +"[T:show-blank?Causes tabs to be copied as \b^I\b and formfeeds as \b^L\b.]" + +"\n" +"\n[file ...]\n" +"\n" + +"[+SEE ALSO?\bcp\b(1), \bgetconf\b(1), \bpr\b(1)]" +; + +#define RUBOUT 0177 + +/* control flags */ +#define B_FLAG (1<<0) +#define E_FLAG (1<<1) +#define F_FLAG (1<<2) +#define N_FLAG (1<<3) +#define S_FLAG (1<<4) +#define T_FLAG (1<<5) +#define U_FLAG (1<<6) +#define V_FLAG (1<<7) +#define D_FLAG (1<<8) +#define d_FLAG (1<<9) + +/* character types */ +#define T_ENDBUF 1 +#define T_CONTROL 2 +#define T_NEWLINE 3 +#define T_EIGHTBIT 4 +#define T_CNTL8BIT 5 + +#define printof(c) ((c)^0100) + +/* + * called for any special output processing + */ + +static int +vcat(register char* states, Sfio_t *fdin, Sfio_t *fdout, int flags) +{ + register unsigned char* cp; + register unsigned char* cpold; + register int n; + register int m; + register int line = 1; + register unsigned char* endbuff; + unsigned char* inbuff; + int printdefer = (flags&(B_FLAG|N_FLAG)); + int lastchar; + + unsigned char meta[4]; + + meta[0] = 'M'; + meta[1] = '-'; + for (;;) + { + /* read in a buffer full */ + if (!(inbuff = (unsigned char*)sfreserve(fdin, SF_UNBOUND, 0))) + return sfvalue(fdin) ? -1 : 0; + if ((n = sfvalue(fdin)) <= 0) + return n; + cp = inbuff; + lastchar = *(endbuff = cp + --n); + *endbuff = 0; + if (printdefer) + { + if (states[*cp]!=T_NEWLINE || !(flags&B_FLAG)) + sfprintf(fdout,"%6d\t",line); + printdefer = 0; + } + while (endbuff) + { + cpold = cp; + /* skip over printable characters */ + if (mbwide()) + while ((n = (m = mbsize(cp)) < 2 ? states[*cp++] : (cp += m, states['a'])) == 0); + else + while ((n = states[*cp++]) == 0); + if (n==T_ENDBUF) + { + if (cp>endbuff) + { + if (!(n = states[lastchar])) + { + *endbuff = lastchar; + cp++; + } + else + { + if (--cp > cpold) + sfwrite(fdout,(char*)cpold,cp-cpold); + if (endbuff==inbuff) + *++endbuff = 0; + cp = cpold = endbuff; + cp[-1] = lastchar; + if (n==T_ENDBUF) + n = T_CONTROL; + + } + endbuff = 0; + } + else n = T_CONTROL; + } + if (--cp>cpold) + sfwrite(fdout,(char*)cpold,cp-cpold); + switch(n) + { + case T_CNTL8BIT: + meta[2] = '^'; + do + { + n = (*cp++)&~0200; + meta[3] = printof(n); + sfwrite(fdout,(char*)meta,4); + } + while ((n=states[*cp])==T_CNTL8BIT); + break; + case T_EIGHTBIT: + do + { + meta[2] = (*cp++)&~0200; + sfwrite(fdout,(char*)meta,3); + } + while ((n=states[*cp])==T_EIGHTBIT); + break; + case T_CONTROL: + do + { + n = *cp++; + sfputc(fdout,'^'); + sfputc(fdout,printof(n)); + } + while ((n=states[*cp])==T_CONTROL); + break; + case T_NEWLINE: + if (flags&S_FLAG) + { + while (states[*++cp]==T_NEWLINE) + line++; + cp--; + } + do + { + cp++; + if (flags&E_FLAG) + sfputc(fdout,'$'); + sfputc(fdout,'\n'); + if (!(flags&(N_FLAG|B_FLAG))) + continue; + line++; + if (cp < endbuff) + sfprintf(fdout,"%6d\t",line); + else printdefer = 1; + } + while (states[*cp]==T_NEWLINE); + break; + } + } + } +} + +int +b_cat(int argc, char** argv, void* context) +{ + register int n; + register int flags = 0; + register char* cp; + register Sfio_t* fp; + char* mode; + int att; + int dovcat=0; + char states[UCHAR_MAX+1]; + + NoP(argc); + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + att = !strcmp(astconf("UNIVERSE", NiL, NiL), "att"); + mode = "r"; + for (;;) + { + switch (optget(argv, usage)) + { + case 'A': + flags |= T_FLAG|E_FLAG|V_FLAG; + continue; + case 'B': + flags |= S_FLAG; + continue; + case 'b': + flags |= B_FLAG; + continue; + case 'E': + flags |= E_FLAG; + continue; + case 'e': + flags |= E_FLAG|V_FLAG; + continue; + case 'n': + flags |= N_FLAG; + continue; + case 's': + flags |= att ? F_FLAG : S_FLAG; + continue; + case 'S': + flags |= F_FLAG; + continue; + case 'T': + flags |= T_FLAG; + continue; + case 't': + flags |= T_FLAG|V_FLAG; + continue; + case 'u': + flags |= U_FLAG; + continue; + case 'v': + flags |= V_FLAG; + continue; + case 'd': + mode = "rt"; + continue; + case 'D': + flags |= d_FLAG; + continue; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + memset(states, 0, sizeof(states)); + if (flags&V_FLAG) + { + memset(states, T_CONTROL, ' '); + states[RUBOUT] = T_CONTROL; + memset(states+0200, T_EIGHTBIT, 0200); + memset(states+0200, T_CNTL8BIT, ' '); + states[RUBOUT|0200] = T_CNTL8BIT; + states['\n'] = 0; + } + if (flags&T_FLAG) + states['\t'] = T_CONTROL; + states[0] = T_ENDBUF; + if (att) + { + if (flags&V_FLAG) + { + states['\n'|0200] = T_EIGHTBIT; + if (!(flags&T_FLAG)) + { + states['\t'] = states['\f'] = 0; + states['\t'|0200] = states['\f'|0200] = T_EIGHTBIT; + } + } + } + else if (flags) + { + if (!(flags&T_FLAG)) + states['\t'] = 0; + } + if (flags&(V_FLAG|T_FLAG|N_FLAG|E_FLAG|B_FLAG)) + { + states['\n'] = T_NEWLINE; + dovcat = 1; + } + if (flags&B_FLAG) + flags |= S_FLAG; + if (flags&d_FLAG) + sfopen(sfstdout, NiL, "wt"); + if (cp = *argv) + argv++; + do + { + if (!cp || streq(cp,"-")) + { + fp = sfstdin; + if (flags&D_FLAG) + sfopen(fp, NiL, mode); + } + else if (!(fp = sfopen(NiL, cp, mode))) + { + if (!(flags&F_FLAG)) + error(ERROR_system(0), "%s: cannot open", cp); + error_info.errors = 1; + continue; + } + if (flags&U_FLAG) + sfsetbuf(fp, (void*)fp, -1); + if (dovcat) + n = vcat(states, fp, sfstdout, flags); + else if (sfmove(fp, sfstdout, SF_UNBOUND, -1) >= 0 && sfeof(fp)) + n = 0; + else + n = -1; + if (fp != sfstdin) + sfclose(fp); + if (n < 0 && errno != EPIPE) + { + if (cp) + error(ERROR_system(0), "%s: read error", cp); + else + error(ERROR_system(0), "read error"); + } + if (sferror(sfstdout)) + break; + } while (cp = *argv++); + if (sfsync(sfstdout)) + error(ERROR_system(0), "write error"); + if (flags&d_FLAG) + sfopen(sfstdout, NiL, "w"); + return error_info.errors; +} diff --git a/usr/src/lib/libcmd/common/chgrp.c b/usr/src/lib/libcmd/common/chgrp.c new file mode 100644 index 0000000000..5538f5e310 --- /dev/null +++ b/usr/src/lib/libcmd/common/chgrp.c @@ -0,0 +1,454 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Research + * + * chgrp+chown + */ + +static const char usage_1[] = +"[-?@(#)$Id: chgrp (AT&T Research) 2006-10-11 $\n]" +USAGE_LICENSE +; + +static const char usage_grp_1[] = +"[+NAME?chgrp - change the group ownership of files]" +"[+DESCRIPTION?\bchgrp\b changes the group ownership of each file" +" to \agroup\a, which can be either a group name or a numeric" +" group id. The user ownership of each file may also be changed to" +" \auser\a by prepending \auser\a\b:\b to the group name.]" +; + +static const char usage_own_1[] = +"[+NAME?chown - change the ownership of files]" +"[+DESCRIPTION?\bchown\b changes the ownership of each file" +" to \auser\a, which can be either a user name or a numeric" +" user id. The group ownership of each file may also be changed to" +" \auser\a by appending \b:\b\agroup\a to the user name.]" +; + +static const char usage_2[] = +"[c:changes?Describe only files whose ownership actually changes.]" +"[f:quiet|silent?Do not report files whose ownership fails to change.]" +"[l|h:symlink?Change the ownership of the symbolic links on systems that" +" support this.]" +"[m:map?The first operand is interpreted as a file that contains a map" +" of \afrom_uid:from_gid to_uid:to_gid\a pairs. Ownership of files" +" matching the \afrom\a part of any pair is changed to the corresponding" +" \ato\a part of the pair. The process stops at the first match for" +" each file. Unmatched files are silently ignored.]" +"[n:show?Show actions but don't execute.]" +"[r:reference?Omit the explicit ownership operand and use the ownership of" +" \afile\a instead.]:[file]" +"[v:verbose?Describe changed permissions of all files.]" +"[H:metaphysical?Follow symbolic links for command arguments; otherwise don't" +" follow symbolic links when traversing directories.]" +"[L:logical|follow?Follow symbolic links when traversing directories.]" +"[P:physical|nofollow?Don't follow symbolic links when traversing directories.]" +"[R:recursive?Recursively change ownership of directories and their contents.]" +"[X:test?Canonicalize output for testing.]" + +"\n" +"\n" +; + +static const char usage_3[] = +" file ...\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files changed successfully.]" + "[+>0?Unable to change ownership of one or more files.]" +"}" +"[+SEE ALSO?\bchmod\b(1), \btw\b(1), \bgetconf\b(1), \bls\b(1)]" +; + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide lchown +#else +#define lchown ______lchown +#endif + +#include <cmd.h> +#include <cdt.h> +#include <ls.h> +#include <ctype.h> +#include <fts.h> + +#include "FEATURE/symlink" + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide lchown +#else +#undef lchown +#endif + +typedef struct /* uid/gid map */ +{ + Dtlink_t link; /* dictionary link */ + int id; /* id */ + int uid; /* id maps to this uid */ + int gid; /* id maps to this gid */ +} Map_t; + +#define NOID (-1) + +#define OPT_CHOWN (1<<0) /* chown */ +#define OPT_FORCE (1<<1) /* ignore errors */ +#define OPT_GID (1<<2) /* have gid */ +#define OPT_LCHOWN (1<<3) /* lchown */ +#define OPT_SHOW (1<<4) /* show but don't do */ +#define OPT_TEST (1<<5) /* canonicalize output */ +#define OPT_UID (1<<6) /* have uid */ +#define OPT_VERBOSE (1<<7) /* have uid */ + +extern int lchown(const char*, uid_t, gid_t); + +#if !_lib_lchown + +#ifndef ENOSYS +#define ENOSYS EINVAL +#endif + +int +lchown(const char* path, uid_t uid, gid_t gid) +{ + return ENOSYS; +} + +#endif /* _lib_chown */ + +/* + * parse uid and gid from s + */ + +static void +getids(register char* s, char** e, int* uid, int* gid, int options) +{ + register char* t; + register int n; + char* z; + char buf[64]; + + *uid = *gid = NOID; + while (isspace(*s)) + s++; + for (t = s; (n = *t) && n != ':' && n != '.' && !isspace(n); t++); + if (n) + { + options |= OPT_CHOWN; + if ((n = t++ - s) >= sizeof(buf)) + n = sizeof(buf) - 1; + *((s = (char*)memcpy(buf, s, n)) + n) = 0; + while (isspace(*t)) + t++; + } + if (options & OPT_CHOWN) + { + if (*s) + { + if ((n = struid(s)) == NOID) + { + n = (int)strtol(s, &z, 0); + if (*z) + error(ERROR_exit(1), "%s: unknown user", s); + } + *uid = n; + } + for (s = t; (n = *t) && !isspace(n); t++); + if (n) + { + if ((n = t++ - s) >= sizeof(buf)) + n = sizeof(buf) - 1; + *((s = (char*)memcpy(buf, s, n)) + n) = 0; + } + } + if (*s) + { + if ((n = strgid(s)) == NOID) + { + n = (int)strtol(s, &z, 0); + if (*z) + error(ERROR_exit(1), "%s: unknown group", s); + } + *gid = n; + } + if (e) + *e = t; +} + +int +b_chgrp(int argc, char** argv, void* context) +{ + register int options = 0; + register char* s; + register Map_t* m; + register FTS* fts; + register FTSENT*ent; + Dt_t* map = 0; + int flags; + int uid; + int gid; + char* op; + char* usage; + Sfio_t* sp; + Dtdisc_t mapdisc; + struct stat st; + int (*chownf)(const char*, uid_t, gid_t); + + cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); + flags = fts_flags() | FTS_TOP | FTS_NOPOSTORDER | FTS_NOSEEDOTDIR; + if (!(sp = sfstropen())) + error(ERROR_SYSTEM|3, "out of space"); + sfputr(sp, usage_1, -1); + if (error_info.id[2] == 'g') + sfputr(sp, usage_grp_1, -1); + else + { + sfputr(sp, usage_own_1, -1); + options |= OPT_CHOWN; + } + sfputr(sp, usage_2, -1); + if (options & OPT_CHOWN) + sfputr(sp, ERROR_translate(0, 0, 0, "[owner[:group]]"), -1); + else + sfputr(sp, ERROR_translate(0, 0, 0, "[[owner:]group]"), -1); + sfputr(sp, usage_3, -1); + if (!(usage = sfstruse(sp))) + error(ERROR_SYSTEM|3, "out of space"); + for (;;) + { + switch (optget(argv, usage)) + { + case 'c': + case 'v': + options |= OPT_VERBOSE; + continue; + case 'f': + options |= OPT_FORCE; + continue; + case 'l': + options |= OPT_LCHOWN; + continue; + case 'm': + memset(&mapdisc, 0, sizeof(mapdisc)); + mapdisc.key = offsetof(Map_t, id); + mapdisc.size = sizeof(int); + if (!(map = dtopen(&mapdisc, Dthash))) + error(ERROR_exit(1), "out of space [id map]"); + continue; + case 'n': + options |= OPT_SHOW; + continue; + case 'r': + if (stat(opt_info.arg, &st)) + error(ERROR_exit(1), "%s: cannot stat", opt_info.arg); + uid = st.st_uid; + gid = st.st_gid; + options |= OPT_UID|OPT_GID; + continue; + case 'H': + flags |= FTS_META|FTS_PHYSICAL; + continue; + case 'L': + flags &= ~(FTS_META|FTS_PHYSICAL); + continue; + case 'P': + flags &= ~FTS_META; + flags |= FTS_PHYSICAL; + continue; + case 'R': + flags &= ~FTS_TOP; + continue; + case 'X': + options |= OPT_TEST; + continue; + case ':': + error(2, "%s", opt_info.arg); + continue; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + argc -= opt_info.index; + if (error_info.errors || argc < 2) + error(ERROR_usage(2), "%s", optusage(NiL)); + s = *argv; + if (map) + { + char* t; + int nuid; + int ngid; + + if (streq(s, "-")) + sp = sfstdin; + else if (!(sp = sfopen(NiL, s, "r"))) + error(ERROR_exit(1), "%s: cannot read", s); + while (s = sfgetr(sp, '\n', 1)) + { + getids(s, &t, &uid, &gid, options); + getids(t, NiL, &nuid, &ngid, options); + if (uid != NOID) + { + if (m = (Map_t*)dtmatch(map, &uid)) + { + m->uid = nuid; + if (m->gid == NOID) + m->gid = ngid; + } + else if (m = (Map_t*)stakalloc(sizeof(Map_t))) + { + m->id = uid; + m->uid = nuid; + m->gid = ngid; + dtinsert(map, m); + } + else + error(ERROR_exit(1), "out of space [id dictionary]"); + } + if (gid != NOID) + { + if (gid == uid || (m = (Map_t*)dtmatch(map, &gid))) + m->gid = ngid; + else if (m = (Map_t*)stakalloc(sizeof(Map_t))) + { + m->id = gid; + m->uid = NOID; + m->gid = ngid; + dtinsert(map, m); + } + else + error(ERROR_exit(1), "out of space [id dictionary]"); + } + } + if (sp != sfstdin) + sfclose(sp); + } + else if (!(options & (OPT_UID|OPT_GID))) + { + getids(s, NiL, &uid, &gid, options); + if (uid != NOID) + options |= OPT_UID; + if (gid != NOID) + options |= OPT_GID; + } + switch (options & (OPT_UID|OPT_GID)) + { + case OPT_UID: + s = ERROR_translate(0, 0, 0, " owner"); + break; + case OPT_GID: + s = ERROR_translate(0, 0, 0, " group"); + break; + case OPT_UID|OPT_GID: + s = ERROR_translate(0, 0, 0, " owner and group"); + break; + default: + s = ""; + break; + } + if (!(fts = fts_open(argv + 1, flags, NiL))) + error(ERROR_system(1), "%s: not found", argv[1]); + while (!cmdquit() && (ent = fts_read(fts))) + switch (ent->fts_info) + { + case FTS_F: + case FTS_D: + case FTS_SL: + case FTS_SLNONE: + anyway: + if (map) + { + options &= ~(OPT_UID|OPT_GID); + uid = ent->fts_statp->st_uid; + gid = ent->fts_statp->st_gid; + if ((m = (Map_t*)dtmatch(map, &uid)) && m->uid != NOID) + { + uid = m->uid; + options |= OPT_UID; + } + if (gid != uid) + m = (Map_t*)dtmatch(map, &gid); + if (m && m->gid != NOID) + { + gid = m->gid; + options |= OPT_GID; + } + } + else + { + if (!(options & OPT_UID)) + uid = ent->fts_statp->st_uid; + if (!(options & OPT_GID)) + gid = ent->fts_statp->st_gid; + } + if (uid != ent->fts_statp->st_uid || gid != ent->fts_statp->st_gid) + { + if ((ent->fts_info & FTS_SL) && (flags & FTS_PHYSICAL) && (options & OPT_LCHOWN)) + { + op = "lchown"; + chownf = lchown; + } + else + { + op = "chown"; + chownf = chown; + } + if (options & (OPT_SHOW|OPT_VERBOSE)) + { + if (options & OPT_TEST) + { + ent->fts_statp->st_uid = 0; + ent->fts_statp->st_gid = 0; + } + sfprintf(sfstdout, "%s uid:%05d->%05d gid:%05d->%05d %s\n", op, ent->fts_statp->st_uid, uid, ent->fts_statp->st_gid, gid, ent->fts_accpath); + } + if (!(options & OPT_SHOW) && (*chownf)(ent->fts_accpath, uid, gid) && !(options & OPT_FORCE)) + error(ERROR_system(0), "%s: cannot change%s", ent->fts_accpath, s); + } + break; + case FTS_DC: + if (!(options & OPT_FORCE)) + error(ERROR_warn(0), "%s: directory causes cycle", ent->fts_accpath); + break; + case FTS_DNR: + if (!(options & OPT_FORCE)) + error(ERROR_system(0), "%s: cannot read directory", ent->fts_accpath); + goto anyway; + case FTS_DNX: + if (!(options & OPT_FORCE)) + error(ERROR_system(0), "%s: cannot search directory", ent->fts_accpath); + goto anyway; + case FTS_NS: + if (!(options & OPT_FORCE)) + error(ERROR_system(0), "%s: not found", ent->fts_accpath); + break; + } + fts_close(fts); + if (map) + dtclose(map); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/chmod.c b/usr/src/lib/libcmd/common/chmod.c new file mode 100644 index 0000000000..b1745585a0 --- /dev/null +++ b/usr/src/lib/libcmd/common/chmod.c @@ -0,0 +1,296 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Research + * + * chmod + */ + +static const char usage[] = +"[-?\n@(#)$Id: chmod (AT&T Research) 2007-07-26 $\n]" +USAGE_LICENSE +"[+NAME?chmod - change the access permissions of files]" +"[+DESCRIPTION?\bchmod\b changes the permission of each file " + "according to mode, which can be either a symbolic representation " + "of changes to make, or an octal number representing the bit " + "pattern for the new permissions.]" +"[+?Symbolic mode strings consist of one or more comma separated list " + "of operations that can be perfomed on the mode. Each operation is of " + "the form \auser\a \aop\a \aperm\a where \auser\a is zero or more of " + "the following letters:]{" + "[+u?User permission bits.]" + "[+g?Group permission bits.]" + "[+o?Other permission bits.]" + "[+a?All permission bits. This is the default if none are specified.]" + "}" +"[+?The \aperm\a portion consists of zero or more of the following letters:]{" + "[+r?Read permission.]" + "[+s?Setuid when \bu\b is selected for \awho\a and setgid when \bg\b " + "is selected for \awho\a.]" + "[+w?Write permission.]" + "[+x?Execute permission for files, search permission for directories.]" + "[+X?Same as \bx\b except that it is ignored for files that do not " + "already have at least one \bx\b bit set.]" + "[+l?Exclusive lock bit on systems that support it. Group execute " + "must be off.]" + "[+t?Sticky bit on systems that support it.]" + "}" +"[+?The \aop\a portion consists of one or more of the following characters:]{" + "[++?Cause the permission selected to be added to the existing " + "permissions. | is equivalent to +.]" + "[+-?Cause the permission selected to be removed to the existing " + "permissions.]" + "[+=?Cause the permission to be set to the given permissions.]" + "[+&?Cause the permission selected to be \aand\aed with the existing " + "permissions.]" + "[+^?Cause the permission selected to be propagated to more " + "restrictive groups.]" + "}" +"[+?Symbolic modes with the \auser\a portion omitted are subject to " + "\bumask\b(2) settings unless the \b=\b \aop\a or the " + "\b--ignore-umask\b option is specified.]" +"[+?A numeric mode is from one to four octal digits (0-7), " + "derived by adding up the bits with values 4, 2, and 1. " + "Any omitted digits are assumed to be leading zeros. The " + "first digit selects the set user ID (4) and set group ID " + "(2) and save text image (1) attributes. The second digit " + "selects permissions for the user who owns the file: read " + "(4), write (2), and execute (1); the third selects permissions" + "for other users in the file's group, with the same values; " + "and the fourth for other users not in the file's group, with " + "the same values.]" + +"[+?For symbolic links, by default, \bchmod\b changes the mode on the file " + "referenced by the symbolic link, not on the symbolic link itself. " + "The \b-h\b options can be specified to change the mode of the link. " + "When traversing directories with \b-R\b, \bchmod\b either follows " + "symbolic links or does not follow symbolic links, based on the " + "options \b-H\b, \b-L\b, and \b-P\b. The configuration parameter " + "\bPATH_RESOLVE\b determines the default behavior if none of these " + "options is specified.]" + +"[+?When the \b-c\b or \b-v\b options are specified, change notifications " + "are written to standard output using the format, " + "\bmode of %s changed to %0.4o (%s)\b, with arguments of the " + "pathname, the numeric mode, and the resulting permission bits as " + "would be displayed by the \bls\b command.]" + +"[+?For backwards compatibility, if an invalid option is given that is a valid " + "symbolic mode specification, \bchmod\b treats this as a mode " + "specification rather than as an option specification.]" + +"[H:metaphysical?Follow symbolic links for command arguments; otherwise don't " + "follow symbolic links when traversing directories.]" +"[L:logical|follow?Follow symbolic links when traversing directories.]" +"[P:physical|nofollow?Don't follow symbolic links when traversing directories.]" +"[R:recursive?Change the mode for files in subdirectories recursively.]" +"[c:changes?Describe only files whose permission actually change.]" +"[f:quiet|silent?Do not report files whose permissioins fail to change.]" +"[h:symlink?Change the mode of the symbolic links on systems that " + "support this.]" +"[i:ignore-umask?Ignore the \bumask\b(2) value in symbolic mode " + "expressions. This is probably how you expect \bchmod\b to work.]" +"[F:reference?Omit the \amode\a operand and use the mode of \afile\a " + "instead.]:[file]" +"[v:verbose?Describe changed permissions of all files.]" +"\n" +"\nmode file ...\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files changed successfully.]" + "[+>0?Unable to change mode of one or more files.]" +"}" +"[+SEE ALSO?\bchgrp\b(1), \bchown\b(1), \btw\b(1), \bgetconf\b(1), \bls\b(1), " + "\bumask\b(2)]" +; + + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide lchmod +#else +#define lchmod ______lchmod +#endif + +#include <cmd.h> +#include <ls.h> +#include <fts.h> + +#include "FEATURE/symlink" + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide lchmod +#else +#undef lchmod +#endif + +extern int lchmod(const char*, mode_t); + +int +b_chmod(int argc, char** argv, void* context) +{ + register int mode; + register int force = 0; + register int flags; + register char* amode = 0; + register FTS* fts; + register FTSENT*ent; + char* last; + int (*chmodf)(const char*, mode_t); + int notify = 0; + int ignore = 0; +#if _lib_lchmod + int chlink = 0; +#endif + struct stat st; + + cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); + flags = fts_flags() | FTS_TOP | FTS_NOPOSTORDER | FTS_NOSEEDOTDIR; + + /* + * NOTE: we diverge from the normal optget boilerplate + * to allow `chmod -x etc' to fall through + */ + + for (;;) + { + switch (optget(argv, usage)) + { + case 'c': + notify = 1; + continue; + case 'f': + force = 1; + continue; + case 'h': +#if _lib_lchmod + chlink = 1; +#endif + continue; + case 'i': + ignore = 1; + continue; + case 'v': + notify = 2; + continue; + case 'F': + if (stat(opt_info.arg, &st)) + error(ERROR_exit(1), "%s: cannot stat", opt_info.arg); + mode = st.st_mode; + amode = ""; + continue; + case 'H': + flags |= FTS_META|FTS_PHYSICAL; + continue; + case 'L': + flags &= ~(FTS_META|FTS_PHYSICAL); + continue; + case 'P': + flags &= ~FTS_META; + flags |= FTS_PHYSICAL; + continue; + case 'R': + flags &= ~FTS_TOP; + continue; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (error_info.errors || !*argv || !amode && !*(argv + 1)) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (ignore) + ignore = umask(0); + if (amode) + amode = 0; + else + { + amode = *argv++; + mode = strperm(amode, &last, 0); + if (*last) + { + if (ignore) + umask(ignore); + error(ERROR_exit(1), "%s: invalid mode", amode); + } + } + chmodf = +#if _lib_lchmod + chlink ? lchmod : +#endif + chmod; + if (!(fts = fts_open(argv, flags, NiL))) + { + if (ignore) + umask(ignore); + error(ERROR_system(1), "%s: not found", *argv); + } + while (!cmdquit() && (ent = fts_read(fts))) + switch (ent->fts_info) + { + case FTS_SL: + if (chmodf == chmod) + { + if (!(flags & FTS_PHYSICAL) || (flags & FTS_META) && ent->fts_level == 1) + fts_set(NiL, ent, FTS_FOLLOW); + break; + } + /*FALLTHROUGH*/ + case FTS_F: + case FTS_D: + case FTS_SLNONE: + anyway: + if (amode) + mode = strperm(amode, &last, ent->fts_statp->st_mode); + if ((*chmodf)(ent->fts_accpath, mode) >= 0) + { + if (notify == 2 || notify == 1 && (mode&S_IPERM) != (ent->fts_statp->st_mode&S_IPERM)) + sfprintf(sfstdout, "%s: mode changed to %0.4o (%s)\n", ent->fts_path, mode, fmtmode(mode, 1)+1); + } + else if (!force) + error(ERROR_system(0), "%s: cannot change mode", ent->fts_accpath); + break; + case FTS_DC: + if (!force) + error(ERROR_warn(0), "%s: directory causes cycle", ent->fts_accpath); + break; + case FTS_DNR: + if (!force) + error(ERROR_system(0), "%s: cannot read directory", ent->fts_accpath); + goto anyway; + case FTS_DNX: + if (!force) + error(ERROR_system(0), "%s: cannot search directory", ent->fts_accpath); + goto anyway; + case FTS_NS: + if (!force) + error(ERROR_system(0), "%s: not found", ent->fts_accpath); + break; + } + fts_close(fts); + if (ignore) + umask(ignore); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/chown.c b/usr/src/lib/libcmd/common/chown.c new file mode 100644 index 0000000000..f31421df18 --- /dev/null +++ b/usr/src/lib/libcmd/common/chown.c @@ -0,0 +1,39 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Research + * + * chown + */ + +static const char id[] = "\n@(#)$Id: chown (AT&T Research) 1997-11-11 $\0\n"; + +#include <cmd.h> + +int +b_chown(int argc, char** argv, void* context) +{ + NoP(id[0]); + return b_chgrp(argc, argv, context); +} diff --git a/usr/src/lib/libcmd/common/cksum.c b/usr/src/lib/libcmd/common/cksum.c new file mode 100644 index 0000000000..25d9ee0c0f --- /dev/null +++ b/usr/src/lib/libcmd/common/cksum.c @@ -0,0 +1,599 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * sum -- list file checksum and size + */ + +static const char usage[] = +"[-?\n@(#)$Id: sum (AT&T Research) 2007-02-07 $\n]" +USAGE_LICENSE +"[+NAME?cksum,md5sum,sum - print file checksum and block count]" +"[+DESCRIPTION?\bsum\b lists the checksum, and for most methods the block" +" count, for each file argument. The standard input is read if there are" +" no \afile\a arguments. \bgetconf UNIVERSE\b determines the default" +" \bsum\b method: \batt\b for the \batt\b universe, \bbsd\b otherwise." +" The default for the other commands is the command name itself. The" +" \batt\b method is a true sum, all others are order dependent.]" +"[+?Method names consist of a leading identifier and 0 or more options" +" separated by -.]" +"[+?\bgetconf PATH_RESOLVE\b determines how symbolic links are handled. This" +" can be explicitly overridden by the \b--logical\b, \b--metaphysical\b," +" and \b--physical\b options below. \bPATH_RESOLVE\b can be one of:]{" +" [+logical?Follow all symbolic links.]" +" [+metaphysical?Follow command argument symbolic links," +" otherwise don't follow.]" +" [+physical?Don't follow symbolic links.]" +"}" + +"[a:all?List the checksum for all files. Use with \b--total\b to list both" +" individual and total checksums and block counts.]" +"[b:binary?Read files in binary mode. This is the default.]" +"[c:check?Each \afile\a is interpreted as the output from a previous \bsum\b." +" If \b--header\b or \b--permissions\b was specified in the previous" +" \bsum\b then the checksum method is automatically determined," +" otherwise \b--method\b must be specified. The listed checksum is" +" compared with the current value and a warning is issued for each file" +" that does not match. If \afile\a was generated by \b--permissions\b" +" then the file mode, user and group are also checked. Empty lines," +" lines starting with \b#<space>\b, or the line \b#\b are ignored. Lines" +" containing no blanks are interpreted as [no]]\aname\a[=\avalue\a]]" +" options:]{" +" [+method=name?Checksum method to apply to subsequent lines.]" +" [+permissions?Subsequent lines were generated with" +" \b--permissions\b.]" +"}" +"[h:header?Print the checksum method as the first output line. Used with" +" \b--check\b and \b--permissions\b.]" +"[l:list?Each \afile\a is interpreted as a list of files, one per line," +" that is checksummed.]" +"[p:permissions?If \b--check\b is not specified then list the file" +" mode, user and group between the checksum and path. User and group" +" matching the caller are output as \b-\b. If \b--check\b is" +" specified then the mode, user and group for each path in \afile\a" +" are updated if necessary to match those in \afile\a. A warning is" +" printed on the standard error for each changed file.]" +"[r:recursive?Recursively checksum the contents of directories.]" +"[s:silent|status?No output for \b--check\b; 0 exit status means all sums" +" matched, non-0 means at least one sum failed to match. Ignored for" +" \b--permissions\b.]" +"[t:total?List only the total checksum and block count of all files." +" \b--all\b \b--total\b lists each checksum and the total. The" +" total checksum and block count may be different from the checksum" +" and block count of the catenation of all files due to partial" +" blocks that may occur when the files are treated separately.]" +"[T:text?Read files in text mode (i.e., treat \b\\r\\n\b as \b\\n\b).]" +"[w!:warn?Warn about invalid \b--check\b lines.]" +"[x:method|algorithm?Specifies the checksum \amethod\a to" +" apply. Parenthesized method options are readonly implementation" +" details.]:[method]{\fmethods\f}" +"[L:logical|follow?Follow symbolic links when traversing directories. The" +" default is determined by \bgetconf PATH_RESOLVE\b.]" +"[H:metaphysical?Follow command argument symbolic links, otherwise don't" +" follow symbolic links when traversing directories. The default is" +" determined by \bgetconf PATH_RESOLVE\b.]" +"[P:physical?Don't follow symbolic links when traversing directories. The" +" default is determined by \bgetconf PATH_RESOLVE\b.]" + +"\n" +"\n[ file ... ]\n" +"\n" + +"[+SEE ALSO?\bgetconf\b(1), \btw\b(1), \buuencode\b(1)]" +; + +#include <cmd.h> +#include <sum.h> +#include <ls.h> +#include <fts.h> +#include <error.h> + +typedef struct State_s /* program state */ +{ + int all; /* list all items */ + Sfio_t* check; /* check previous output */ + gid_t gid; /* caller gid */ + int header; /* list method on output */ + int list; /* list file name too */ + Sum_t* oldsum; /* previous sum method */ + int permissions; /* include mode,uer,group */ + int haveperm; /* permissions in the input */ + int recursive; /* recursively descend dirs */ + unsigned long size; /* combined size of all files */ + int silent; /* silent check, 0 exit if ok */ + int (*sort)(FTSENT* const*, FTSENT* const*); + Sum_t* sum; /* sum method */ + int text; /* \r\n == \n */ + int total; /* list totals only */ + uid_t uid; /* caller uid */ + int warn; /* invalid check line warnings */ +} State_t; + +static void verify(State_t*, char*, char*, Sfio_t*); + +/* + * open path for read mode + */ + +static Sfio_t* +openfile(const char* path, const char* mode) +{ + Sfio_t* sp; + + if (!path || streq(path, "-") || streq(path, "/dev/stdin") || streq(path, "/dev/fd/0")) + { + sp = sfstdin; + sfopen(sp, NiL, mode); + } + else if (!(sp = sfopen(NiL, path, mode))) + error(ERROR_SYSTEM|2, "%s: cannot read", path); + return sp; +} + +/* + * close an openfile() stream + */ + +static int +closefile(Sfio_t* sp) +{ + return sp == sfstdin ? 0 : sfclose(sp); +} + +/* + * compute and print sum on an open file + */ + +static void +pr(State_t* state, Sfio_t* op, Sfio_t* ip, char* file, int perm, struct stat* st, Sfio_t* check) +{ + register char* p; + register char* r; + register char* e; + register int peek; + struct stat ss; + + if (check) + { + state->oldsum = state->sum; + while (p = sfgetr(ip, '\n', 1)) + verify(state, p, file, check); + state->sum = state->oldsum; + if (state->warn && !sfeof(ip)) + error(2, "%s: last line incomplete", file); + return; + } + suminit(state->sum); + if (state->text) + { + peek = 0; + while (p = sfreserve(ip, SF_UNBOUND, 0)) + { + e = p + sfvalue(ip); + if (peek) + { + peek = 0; + if (*p != '\n') + sumblock(state->sum, "\r", 1); + } + while (r = memchr(p, '\r', e - p)) + { + if (++r >= e) + { + e--; + peek = 1; + break; + } + sumblock(state->sum, p, r - p - (*r == '\n')); + p = r; + } + sumblock(state->sum, p, e - p); + } + if (peek) + sumblock(state->sum, "\r", 1); + } + else + while (p = sfreserve(ip, SF_UNBOUND, 0)) + sumblock(state->sum, p, sfvalue(ip)); + if (sfvalue(ip)) + error(ERROR_SYSTEM|2, "%s: read error", file); + sumdone(state->sum); + if (!state->total || state->all) + { + sumprint(state->sum, op, SUM_SIZE|SUM_SCALE); + if (perm >= 0) + { + if (perm) + { + if (!st && fstat(sffileno(ip), st = &ss)) + error(ERROR_SYSTEM|2, "%s: cannot stat", file); + else + sfprintf(sfstdout, " %04o %s %s", + modex(st->st_mode & S_IPERM), + (st->st_uid != state->uid && ((st->st_mode & S_ISUID) || (st->st_mode & S_IRUSR) && !(st->st_mode & (S_IRGRP|S_IROTH)) || (st->st_mode & S_IXUSR) && !(st->st_mode & (S_IXGRP|S_IXOTH)))) ? fmtuid(st->st_uid) : "-", + (st->st_gid != state->gid && ((st->st_mode & S_ISGID) || (st->st_mode & S_IRGRP) && !(st->st_mode & S_IROTH) || (st->st_mode & S_IXGRP) && !(st->st_mode & S_IXOTH))) ? fmtgid(st->st_gid) : "-"); + } + if (ip != sfstdin) + sfprintf(op, " %s", file); + sfputc(op, '\n'); + } + } +} + +/* + * verify previous sum output + */ + +static void +verify(State_t* state, register char* s, char* check, Sfio_t* rp) +{ + register char* t; + char* e; + char* file; + int attr; + int mode; + int uid; + int gid; + Sfio_t* sp; + struct stat st; + + if (!*s || *s == '#' && (!*(s + 1) || *(s + 1) == ' ' || *(s + 1) == '\t')) + return; + if (t = strchr(s, ' ')) + { + if ((t - s) > 10 || !(file = strchr(t + 1, ' '))) + file = t; + *file++ = 0; + attr = 0; + if ((mode = strtol(file, &e, 8)) && *e == ' ' && (e - file) == 4) + { + mode = modei(mode); + if (t = strchr(++e, ' ')) + { + if (*e == '-' && (t - e) == 1) + uid = -1; + else + { + *t = 0; + uid = struid(e); + *t = ' '; + } + if (e = strchr(++t, ' ')) + { + if (*t == '-' && (e - t) == 1) + gid = -1; + else + { + *e = 0; + gid = struid(t); + *e = ' '; + } + file = e + 1; + attr = 1; + } + } + } + if (sp = openfile(file, "rb")) + { + pr(state, rp, sp, file, -1, NiL, NiL); + if (!(t = sfstruse(rp))) + error(ERROR_SYSTEM|3, "out of space"); + if (!streq(s, t)) + { + if (state->silent) + error_info.errors++; + else + error(2, "%s: checksum changed", file); + } + else if (attr) + { + if (fstat(sffileno(sp), &st)) + { + if (state->silent) + error_info.errors++; + else + error(ERROR_SYSTEM|2, "%s: cannot stat", file); + } + else + { + if (uid < 0 || uid == st.st_uid) + uid = -1; + else if (!state->permissions) + { + if (state->silent) + error_info.errors++; + else + error(2, "%s: uid should be %s", file, fmtuid(uid)); + } + if (gid < 0 || gid == st.st_gid) + gid = -1; + else if (!state->permissions) + { + if (state->silent) + error_info.errors++; + else + error(2, "%s: gid should be %s", file, fmtgid(gid)); + } + if (state->permissions && (uid >= 0 || gid >= 0)) + { + if (chown(file, uid, gid) < 0) + { + if (uid < 0) + error(ERROR_SYSTEM|2, "%s: cannot change group to %s", file, fmtgid(gid)); + else if (gid < 0) + error(ERROR_SYSTEM|2, "%s: cannot change user to %s", file, fmtuid(uid)); + else + error(ERROR_SYSTEM|2, "%s: cannot change user to %s and group to %s", file, fmtuid(uid), fmtgid(gid)); + } + else + { + if (uid < 0) + error(1, "%s: changed group to %s", file, fmtgid(gid)); + else if (gid < 0) + error(1, "%s: changed user to %s", file, fmtuid(uid)); + else + error(1, "%s: changed user to %s and group to %s", file, fmtuid(uid), fmtgid(gid)); + } + } + if ((st.st_mode & S_IPERM) ^ mode) + { + if (state->permissions) + { + if (chmod(file, mode) < 0) + error(ERROR_SYSTEM|2, "%s: cannot change mode to %s", file, fmtmode(mode, 0)); + else + error(ERROR_SYSTEM|1, "%s: changed mode to %s", file, fmtmode(mode, 0)); + } + else if (state->silent) + error_info.errors++; + else + error(2, "%s: mode should be %s", file, fmtmode(mode, 0)); + } + } + } + closefile(sp); + } + } + else if (strneq(s, "method=", 7)) + { + s += 7; + if (state->sum != state->oldsum) + sumclose(state->sum); + if (!(state->sum = sumopen(s))) + error(3, "%s: %s: unknown checksum method", check, s); + } + else if (streq(s, "permissions")) + state->haveperm = 1; + else + error(1, "%s: %s: unknown option", check, s); +} + +/* + * sum the list of files in lp + */ + +static void +list(State_t* state, register Sfio_t* lp) +{ + register char* file; + register Sfio_t* sp; + + while (file = sfgetr(lp, '\n', 1)) + if (sp = openfile(file, state->check ? "rt" : "rb")) + { + pr(state, sfstdout, sp, file, state->permissions, NiL, state->check); + closefile(sp); + } +} + +/* + * order child entries + */ + +static int +order(FTSENT* const* f1, FTSENT* const* f2) +{ + return strcoll((*f1)->fts_name, (*f2)->fts_name); +} + +/* + * optget() info discipline function + */ + +static int +optinfo(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp) +{ + if (streq(s, "methods")) + return sumusage(sp); + return 0; +} + +int +b_cksum(int argc, register char** argv, void* context) +{ + register int flags; + register char* s; + char* file; + Sfio_t* sp; + FTS* fts; + FTSENT* ent; + Optdisc_t optdisc; + State_t state; + + cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); + memset(&state, 0, sizeof(state)); + setlocale(LC_ALL, ""); + flags = fts_flags() | FTS_TOP | FTS_NOPOSTORDER | FTS_NOSEEDOTDIR; + state.warn = 1; + optinit(&optdisc, optinfo); + for (;;) + { + switch (optget(argv, usage)) + { + case 'a': + state.all = 1; + continue; + case 'b': + state.text = 0; + continue; + case 'c': + if (!(state.check = sfstropen())) + error(3, "out of space [check]"); + continue; + case 'h': + state.header = 1; + continue; + case 'l': + state.list = 1; + continue; + case 'p': + state.permissions = 1; + continue; + case 'r': + flags &= ~FTS_TOP; + state.recursive = 1; + state.sort = order; + continue; + case 's': + state.silent = opt_info.num; + continue; + case 't': + state.total = 1; + continue; + case 'w': + state.warn = opt_info.num; + continue; + case 'x': + if (!(state.sum = sumopen(opt_info.arg))) + error(3, "%s: unknown checksum method", opt_info.arg); + continue; + case 'H': + flags |= FTS_META|FTS_PHYSICAL; + continue; + case 'L': + flags &= ~(FTS_META|FTS_PHYSICAL); + continue; + case 'P': + flags &= ~FTS_META; + flags |= FTS_PHYSICAL; + continue; + case 'T': + state.text = 1; + continue; + case '?': + error(ERROR_USAGE|4, "%s", opt_info.arg); + break; + case ':': + error(2, "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (error_info.errors) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + + /* + * check the method + */ + + if (!state.sum && !(state.sum = sumopen(error_info.id)) && !(state.sum = sumopen(astconf("UNIVERSE", NiL, NiL)))) + state.sum = sumopen(NiL); + + /* + * do it + */ + + if (state.permissions) + { + state.uid = geteuid(); + state.gid = getegid(); + state.silent = 0; + } + if (!state.check && (state.header || state.permissions)) + { + sfprintf(sfstdout, "method=%s\n", state.sum->name); + if (state.permissions) + sfprintf(sfstdout, "permissions\n"); + } + if (state.list) + { + if (*argv) + { + while (file = *argv++) + if (sp = openfile(file, "rt")) + { + list(&state, sp); + closefile(sp); + } + } + else if (sp = openfile(NiL, "rt")) + { + list(&state, sp); + closefile(sp); + } + } + else if (!*argv && !state.recursive) + pr(&state, sfstdout, sfstdin, "/dev/stdin", state.permissions, NiL, state.check); + else if (!(fts = fts_open(argv, flags, state.sort))) + error(ERROR_system(1), "%s: not found", *argv); + else + { + while (!cmdquit() && (ent = fts_read(fts))) + switch (ent->fts_info) + { + case FTS_SL: + if (!(flags & FTS_PHYSICAL) || (flags & FTS_META) && ent->fts_level == 1) + fts_set(NiL, ent, FTS_FOLLOW); + break; + case FTS_F: + if (sp = openfile(ent->fts_accpath, "rb")) + { + pr(&state, sfstdout, sp, ent->fts_path, state.permissions, ent->fts_statp, state.check); + closefile(sp); + } + break; + case FTS_DC: + error(ERROR_warn(0), "%s: directory causes cycle", ent->fts_accpath); + break; + case FTS_DNR: + error(ERROR_system(0), "%s: cannot read directory", ent->fts_accpath); + break; + case FTS_DNX: + error(ERROR_system(0), "%s: cannot search directory", ent->fts_accpath); + break; + case FTS_NS: + error(ERROR_system(0), "%s: not found", ent->fts_accpath); + break; + } + fts_close(fts); + } + if (state.total) + { + sumprint(state.sum, sfstdout, SUM_TOTAL|SUM_SIZE|SUM_SCALE); + sfputc(sfstdout, '\n'); + } + sumclose(state.sum); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/cmd.h b/usr/src/lib/libcmd/common/cmd.h new file mode 100644 index 0000000000..50092c2018 --- /dev/null +++ b/usr/src/lib/libcmd/common/cmd.h @@ -0,0 +1,173 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * AT&T Research + * + * builtin cmd definitions + */ + +#ifndef _CMD_H +#define _CMD_H + +#include <ast.h> +#include <error.h> +#include <stak.h> + +#define cmdinit _cmd_init +#define cmdquit() 0 + +#if _BLD_cmd && defined(__EXPORT__) +#define extern __EXPORT__ +#endif + +#include <cmdext.h> + +#undef extern + +#if defined(CMD_BUILTIN) && !defined(CMD_STANDALONE) +#define CMD_STANDALONE CMD_BUILTIN +#endif + +#ifdef CMD_STANDALONE + +#if CMD_DYNAMIC + +#include <dlldefs.h> + +typedef int (*Builtin_f)(int, char**, void*); + +#else + +extern int CMD_STANDALONE(int, char**, void*); + +#endif + +#ifndef CMD_BUILTIN + +/* + * command initialization + */ + +static int +cmdinit(int argc, register char** argv, void* context, const char* catalog, int flags) +{ + register char* cp; + register char* pp; + + if (cp = strrchr(argv[0], '/')) + cp++; + else + cp = argv[0]; + if (pp = strrchr(cp, '_')) + cp = pp + 1; + error_info.id = cp; + if (!error_info.catalog) + error_info.catalog = (char*)catalog; + opt_info.index = 0; + if (context) + error_info.flags |= flags; + return 0; +} + +#endif + +int +main(int argc, char** argv) +{ +#if CMD_DYNAMIC + register char* s; + register char* t; + void* dll; + Builtin_f fun; + char buf[64]; + + if (s = strrchr(argv[0], '/')) + s++; + else if (!(s = argv[0])) + return 127; + if ((t = strrchr(s, '_')) && *++t) + s = t; + buf[0] = '_'; + buf[1] = 'b'; + buf[2] = '_'; + strncpy(buf + 3, s, sizeof(buf) - 4); + buf[sizeof(buf) - 1] = 0; + if (t = strchr(buf, '.')) + *t = 0; + for (;;) + { + if (dll = dlopen(NiL, RTLD_LAZY)) + { + if (fun = (Builtin_f)dlsym(dll, buf + 1)) + break; + if (fun = (Builtin_f)dlsym(dll, buf)) + break; + } + if (dll = dllfind("cmd", NiL, RTLD_LAZY)) + { + if (fun = (Builtin_f)dlsym(dll, buf + 1)) + break; + if (fun = (Builtin_f)dlsym(dll, buf)) + break; + } + return 127; + } + return (*fun)(argc, argv, NiL); +#else + return CMD_STANDALONE(argc, argv, NiL); +#endif +} + +#else + +#undef cmdinit +#define cmdinit(a,b,c,d,e) do{if(_cmd_init(a,b,c,d,e))return -1;}while(0) + +#ifndef CMD_BUILTIN + +#undef cmdquit +#define cmdquit() (_cmd_quit) + +#if _BLD_cmd && defined(__EXPORT__) +#define extern extern __EXPORT__ +#endif +#if !_BLD_cmd && defined(__IMPORT__) +#define extern extern __IMPORT__ +#endif + +extern int _cmd_quit; + +#undef extern + +#endif + +#if _BLD_cmd && defined(__EXPORT__) +#define extern extern __EXPORT__ +#endif + +extern int _cmd_init(int, char**, void*, const char*, int); + +#undef extern + +#endif + +#endif diff --git a/usr/src/lib/libcmd/common/cmdinit.c b/usr/src/lib/libcmd/common/cmdinit.c new file mode 100644 index 0000000000..0a8f4359c8 --- /dev/null +++ b/usr/src/lib/libcmd/common/cmdinit.c @@ -0,0 +1,68 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * command initialization + */ + +#include <cmd.h> + +int _cmd_quit = 0; + +int +_cmd_init(int argc, char** argv, void* context, const char* catalog, int flags) +{ + register char* cp; + + if (argc < 0) + { + _cmd_quit = 1; + return -1; + } + _cmd_quit = 0; + if (cp = strrchr(argv[0], '/')) + cp++; + else + cp = argv[0]; + error_info.id = cp; + if (!error_info.catalog) + error_info.catalog = catalog; + opt_info.index = 0; + if (context) + error_info.flags |= flags; + return 0; +} + +#if __OBSOLETE__ < 20080101 + +#if defined(__EXPORT__) +#define extern __EXPORT__ +#endif + +#undef cmdinit + +extern void +cmdinit(char** argv, void* context, const char* catalog, int flags) +{ + _cmd_init(0, argv, context, catalog, flags); +} + +#endif diff --git a/usr/src/lib/libcmd/common/cmp.c b/usr/src/lib/libcmd/common/cmp.c new file mode 100644 index 0000000000..3baebdd8da --- /dev/null +++ b/usr/src/lib/libcmd/common/cmp.c @@ -0,0 +1,288 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Bell Laboratories + * + * cmp + */ + +static const char usage[] = +"[-?\n@(#)$Id: cmp (AT&T Research) 2004-12-01 $\n]" +USAGE_LICENSE +"[+NAME?cmp - compare two files]" +"[+DESCRIPTION?\bcmp\b compares two files \afile1\a and \afile2\a. " + "\bcmp\b writes no output if the files are the same. By default, " + "if the files differ, the byte and line number at which the " + "first difference occurred are written to standard output. Bytes " + "and lines are numbered beginning with 1.]" +"[+?If \askip1\a or \askip2\a are specified, or the \b-i\b option is " + "specified, initial bytes of the corresponding file are skipped " + "before beginning the compare. The skip values are in bytes or " + "can have a suffix of \bk\b for kilobytes or \bm\b for megabytes.]" +"[+?If either \afile1\a or \afiles2\a is \b-\b, \bcmp\b " + "uses standard input starting at the current location.]" +"[c:print-chars?Writes control characters as a \b^\b followed by a letter of " + "the alphabet and precede characters that have the high bit set with " + "\bM-\b as with \bcat\b(1).]" +"[i:ignore-initial]#[skip:=0?Sets default skip values for the operands " + "\askip1\a and \askip2\a to \askip\a.]" +"[l:verbose?Write the decimal byte number and the differing bytes (in octal) " + "for each difference.]" +"[s:quiet|silent?Write nothing for differing files; return non-zero " + "exit status only.] ]" +"\n" +"\nfile1 file2 [skip1 [skip2]]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?The files or portions compared are identical.]" + "[+1?The files are different.]" + "[+>1?An error occurred.]" +"}" +"[+SEE ALSO?\bcomm\b(1), \bdiff\b(1), \bcat\b(1)]" +; + + +#include <cmd.h> +#include <ls.h> +#include <ctype.h> + +#define CMP_VERBOSE 1 +#define CMP_SILENT 2 +#define CMP_CHARS 4 + +#define cntl(x) (x&037) +#define printchar(c) ((c) ^ ('A'-cntl('A'))) + +static void outchar(Sfio_t *out, register int c, int delim) +{ + if(c&0200) + { + sfputc(out,'M'); + sfputc(out,'-'); + c &= ~0200; + } + else if(!isprint(c)) + { + sfputc(out,'^'); + c = printchar(c); + } + sfputc(out,c); + sfputc(out,delim); +} + +/* + * compare two files + */ + +static int +cmp(const char* file1, Sfio_t* f1, const char* file2, Sfio_t* f2, int flags) +{ + register int c1; + register int c2; + register unsigned char* p1 = 0; + register unsigned char* p2 = 0; + register Sfoff_t lines = 1; + register unsigned char* e1 = 0; + register unsigned char* e2 = 0; + Sfoff_t pos = 0; + int ret = 0; + unsigned char* last; + + for (;;) + { + if ((c1 = e1 - p1) <= 0) + { + if (!(p1 = (unsigned char*)sfreserve(f1, SF_UNBOUND, 0)) || (c1 = sfvalue(f1)) <= 0) + { + if ((e2 - p2) > 0 || sfreserve(f2, SF_UNBOUND, 0) && sfvalue(f2) > 0) + { + ret = 1; + if (!(flags & CMP_SILENT)) + error(ERROR_exit(1), "%s: EOF", file1); + } + return(ret); + } + e1 = p1 + c1; + } + if ((c2 = e2 - p2) <= 0) + { + if (!(p2 = (unsigned char*)sfreserve(f2, SF_UNBOUND, 0)) || (c2 = sfvalue(f2)) <= 0) + { + if (!(flags & CMP_SILENT)) + error(ERROR_exit(1), "%s: EOF", file2); + return(1); + } + e2 = p2 + c2; + } + if (c1 > c2) + c1 = c2; + pos += c1; + if (flags & CMP_SILENT) + { + if (memcmp(p1, p2, c1)) + return(1); + p1 += c1; + p2 += c1; + } + else + { + last = p1 + c1; + while (p1 < last) + { + if ((c1 = *p1++) != *p2++) + { + if (flags) + { + ret = 1; + if(flags&CMP_CHARS) + { + sfprintf(sfstdout, "%6I*d ", sizeof(pos), pos - (last - p1)); + outchar(sfstdout,c1,' '); + outchar(sfstdout,*(p2-1),'\n'); + } + else + sfprintf(sfstdout, "%6I*d %3o %3o\n", sizeof(pos), pos - (last - p1), c1, *(p2 - 1)); + } + else + { + sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u\n", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines); + return(1); + } + } + if (c1 == '\n') + lines++; + } + } + } +} + +int +b_cmp(int argc, register char** argv, void* context) +{ + char* s; + char* e; + Sfio_t* f1 = 0; + Sfio_t* f2 = 0; + char* file1; + char* file2; + int n; + off_t o1 = 0; + off_t o2 = 0; + struct stat s1; + struct stat s2; + + int flags = 0; + + NoP(argc); + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'l': + flags |= CMP_VERBOSE; + break; + case 's': + flags |= CMP_SILENT; + break; + case 'c': + flags |= CMP_CHARS; + break; + case 'i': + o1 = o2 = opt_info.num; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if (error_info.errors || !(file1 = *argv++) || !(file2 = *argv++)) + error(ERROR_usage(2), "%s", optusage(NiL)); + n = 2; + if (streq(file1, "-")) + f1 = sfstdin; + else if (!(f1 = sfopen(NiL, file1, "r"))) + { + if (!(flags & CMP_SILENT)) + error(ERROR_system(0), "%s: cannot open", file1); + goto done; + } + if (streq(file2, "-")) + f2 = sfstdin; + else if (!(f2 = sfopen(NiL, file2, "r"))) + { + if (!(flags & CMP_SILENT)) + error(ERROR_system(0), "%s: cannot open", file2); + goto done; + } + if (s = *argv++) + { + o1 = strtol(s, &e, 0); + if (*e) + { + error(ERROR_exit(0), "%s: %s: invalid skip", file1, s); + goto done; + } + if (s = *argv++) + { + o2 = strtol(s, &e, 0); + if (*e) + { + error(ERROR_exit(0), "%s: %s: invalid skip", file2, s); + goto done; + } + } + if (*argv) + { + error(ERROR_usage(0), "%s", optusage(NiL)); + goto done; + } + } + if (o1 && sfseek(f1, o1, SEEK_SET) != o1) + { + if (!(flags & CMP_SILENT)) + error(ERROR_exit(0), "%s: EOF", file1); + n = 1; + goto done; + } + if (o2 && sfseek(f2, o2, SEEK_SET) != o2) + { + if (!(flags & CMP_SILENT)) + error(ERROR_exit(0), "%s: EOF", file2); + n = 1; + goto done; + } + if (fstat(sffileno(f1), &s1)) + error(ERROR_system(0), "%s: cannot stat", file1); + else if (fstat(sffileno(f2), &s2)) + error(ERROR_system(0), "%s: cannot stat", file1); + else if (s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev && o1 == o2) + n = 0; + else n = ((flags & CMP_SILENT) && S_ISREG(s1.st_mode) && S_ISREG(s2.st_mode) && (s1.st_size - o1) != (s2.st_size - o2)) ? 1 : cmp(file1, f1, file2, f2, flags); + done: + if (f1 && f1 != sfstdin) sfclose(f1); + if (f2 && f2 != sfstdin) sfclose(f2); + return(n); +} diff --git a/usr/src/lib/libcmd/common/comm.c b/usr/src/lib/libcmd/common/comm.c new file mode 100644 index 0000000000..3e28867e7e --- /dev/null +++ b/usr/src/lib/libcmd/common/comm.c @@ -0,0 +1,201 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * comm + */ + +static const char usage[] = +"[-?\n@(#)$Id: comm (AT&T Research) 1999-04-28 $\n]" +USAGE_LICENSE +"[+NAME?comm - select or reject lines common to two files]" +"[+DESCRIPTION?\bcomm\b reads two files \afile1\a and \afile2\a " + "which should be ordered in the collating sequence of the " + "current locale, and produces three text columns as output:]{" + "[+1?Lines only in \afile1\a.]" + "[+2?Lines only in \afile2\a.]" + "[+3?Lines in both files.]" + "}" +"[+?If lines in either file are not ordered according to the collating " + "sequence of the current locale, the results are not specified.]" +"[+?If either \afile1\a or \afile2\a is \b-\b, \bcomm\b " + "uses standard input starting at the current location.]" + +"[1?Suppress the output column of lines unique to \afile1\a.]" +"[2?Suppress the output column of lines unique to \afile2\a.]" +"[3?Suppress the output column of lines duplicate in \afile1\a and \afile2\a.]" +"\n" +"\nfile1 file2\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?Both files processed successfully.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bcmp\b(1), \bdiff\b(1)]" +; + + +#include <cmd.h> + +#define C_FILE1 1 +#define C_FILE2 2 +#define C_COMMON 4 +#define C_ALL (C_FILE1|C_FILE2|C_COMMON) + +static int comm(Sfio_t *in1, Sfio_t *in2, register Sfio_t *out,register int mode) +{ + register char *cp1, *cp2; + register int n1, n2, n, comp; + if(cp1 = sfgetr(in1,'\n',0)) + n1 = sfvalue(in1); + if(cp2 = sfgetr(in2,'\n',0)) + n2 = sfvalue(in2); + while(cp1 && cp2) + { + n=(n1<n2?n1:n2); + if((comp=memcmp(cp1,cp2,n-1))==0 && (comp=n1-n2)==0) + { + if(mode&C_COMMON) + { + if(mode!=C_COMMON) + { + sfputc(out,'\t'); + if(mode==C_ALL) + sfputc(out,'\t'); + } + if(sfwrite(out,cp1,n) < 0) + return(-1); + } + if(cp1 = sfgetr(in1,'\n',0)) + n1 = sfvalue(in1); + if(cp2 = sfgetr(in2,'\n',0)) + n2 = sfvalue(in2); + } + else if(comp > 0) + { + if(mode&C_FILE2) + { + if(mode&C_FILE1) + sfputc(out,'\t'); + if(sfwrite(out,cp2,n2) < 0) + return(-1); + } + if(cp2 = sfgetr(in2,'\n',0)) + n2 = sfvalue(in2); + } + else + { + if((mode&C_FILE1) && sfwrite(out,cp1,n1) < 0) + return(-1); + if(cp1 = sfgetr(in1,'\n',0)) + n1 = sfvalue(in1); + } + } + n = 0; + if(cp2) + { + cp1 = cp2; + in1 = in2; + n1 = n2; + if(mode&C_FILE1) + n = 1; + mode &= C_FILE2; + } + else + mode &= C_FILE1; + if(!mode || !cp1) + { + if(cp1 && in1==sfstdin) + sfseek(in1,(Sfoff_t)0,SEEK_END); + return(0); + } + /* process the remaining stream */ + while(1) + { + if(n) + sfputc(out,'\t'); + if(sfwrite(out,cp1,n1) < 0) + return(-1); + if(!(cp1 = sfgetr(in1,'\n',0))) + return(0); + n1 = sfvalue(in1); + } + /* NOT REACHED */ +} + +int +b_comm(int argc, char *argv[], void* context) +{ + register int n; + register int mode = C_FILE1|C_FILE2|C_COMMON; + register char *cp; + Sfio_t *f1, *f2; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case '1': + mode &= ~C_FILE1; + break; + case '2': + mode &= ~C_FILE2; + break; + case '3': + mode &= ~C_COMMON; + break; + case ':': + error(2, "%s",opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s",opt_info.arg); + break; + } + argv += opt_info.index; + argc -= opt_info.index; + if(error_info.errors || argc!=2) + error(ERROR_usage(2),"%s",optusage(NiL)); + cp = *argv++; + if(streq(cp,"-")) + f1 = sfstdin; + else if(!(f1 = sfopen(NiL, cp,"r"))) + error(ERROR_system(1),"%s: cannot open",cp); + cp = *argv; + if(streq(cp,"-")) + f2 = sfstdin; + else if(!(f2 = sfopen(NiL, cp,"r"))) + error(ERROR_system(1),"%s: cannot open",cp); + if(mode) + { + if(comm(f1,f2,sfstdout,mode) < 0) + error(ERROR_system(1)," write error"); + } + else if(f1==sfstdin || f2==sfstdin) + sfseek(sfstdin,(Sfoff_t)0,SEEK_END); + if(f1!=sfstdin) + sfclose(f1); + if(f2!=sfstdin) + sfclose(f2); + return(error_info.errors); +} + diff --git a/usr/src/lib/libcmd/common/cp.c b/usr/src/lib/libcmd/common/cp.c new file mode 100644 index 0000000000..3aa6b75273 --- /dev/null +++ b/usr/src/lib/libcmd/common/cp.c @@ -0,0 +1,927 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * cp/ln/mv -- copy/link/move files + */ + +static const char usage_head[] = +"[-?@(#)$Id: cp (AT&T Research) 2006-11-21 $\n]" +USAGE_LICENSE +; + +static const char usage_cp[] = +"[+NAME?cp - copy files]" +"[+DESCRIPTION?If the last argument names an existing directory, \bcp\b" +" copies each \afile\a into a file with the same name in that" +" directory. Otherwise, if only two files are given, \bcp\b copies" +" the first onto the second. It is an error if the last argument is" +" not a directory and more than two files are given. By default" +" directories are not copied.]" + +"[a:archive?Preserve as much as possible of the structure and attributes of" +" the original files in the copy. Equivalent to \b--physical\b" +" \b--preserve\b \b--recursive\b.]" +"[p:preserve?Preserve file owner, group, permissions and timestamps.]" +"[h:hierarchy|parents?Form the name of each destination file by appending" +" to the target directory a slash and the specified source file name." +" The last argument must be an existing directory. Missing destination" +" directories are created.]" +"[H:metaphysical?Follow command argument symbolic links, otherwise don't" +" follow.]" +"[l:link?Make hard links to destination files instead of copies.]" +"[L:logical|dereference?Follow symbolic links and copy the files" +" they point to.]" +"[P|d:physical|nodereference?Don't follow symbolic links; copy symbolic" +" rather than the files they point to.]" +; + +static const char usage_ln[] = +"[+NAME?ln - link files]" +"[+DESCRIPTION?If the last argument names an existing directory, \bln\b" +" links each \afile\a into a file with the same name in that" +" directory. Otherwise, if only two files are given, \bln\b links" +" the first onto the second. It is an error if the last argument is" +" not a directory and more than two files are given. By default" +" directories are not linked.]" +; + +static const char usage_mv[] = +"[+NAME?mv - rename files]" +"[+DESCRIPTION?If the last argument names an existing directory, \bmv\b" +" renames each \afile\a into a file with the same name in that" +" directory. Otherwise, if only two files are given, \bmv\b renames" +" the first onto the second. It is an error if the last argument is" +" not a directory and more than two files are given. If a source and" +" destination file reside on different filesystems then \bmv\b copies" +" the file contents to the destination and then deletes the source" +" file.]" +; + +static const char usage_tail[] = +"[f:force?Replace existing destination files.]" +"[i:interactive|prompt?Prompt whether to replace existing destination files." +" An affirmative response (\by\b or \bY\b) replaces the file, a quit" +" response (\bq\b or \bQ\b) exits immediately, and all other" +" responses skip the file.]" +"[r|R:recursive?Operate on the contents of directories recursively.]" +"[s:symlink|symbolic-link?Make symbolic links to destination files.]" +"[u:update?Replace a destination file only if its modification time is older" +" than the corresponding source file modification time.]" +"[v:verbose?Print the name of each file before operating on it.]" +"[b:backup?Make backups of files that are about to be replaced. See" +" \b--suffix\b and \b--version-control\b for more information.]" +"[F:fsync|sync?\bfsync\b(2) each file after it is copied.]" +"[S:backup-suffix|suffix?A backup file is made by renaming the file to the" +" same name with the backup suffix appended. The backup suffix is" +" determined in this order: this option, the \bSIMPLE_BACKUP_SUFFIX\b," +" environment variable, or the default value \b~\b.]:[suffix]" +"[V:backup-type|version-control?The backup type is determined in this order:" +" this option, the \bVERSION_CONTROL\b environment variable, or the" +" default value \bexisting\b. \atype\a may be one of:]:[type]{" +" [+numbered|t?Always make numbered backups. The numbered backup" +" suffix is \b.\aSNS\a, where \aS\a is the" +" \bbackup-suffix\b and \aN\a is the version number," +" starting at 1, incremented with each version.]" +" [+existing|nil?Make numbered backups of files that already" +" have them, otherwise simple backups.]" +" [+simple|never?Always make simple backups.]" +"}" +"[x|X|l:xdev|local|mount|one-file-system?Do not descend into directories in" +" different filesystems than their parents.]" + +"\n" +"\nsource destination\n" +"file ... directory\n" +"\n" + +"[+SEE ALSO?\bpax\b(1), \bfsync\b(2), \brename\b(2), \bunlink\b(2)," +" \bremove\b(3)]" +; + +#include <cmd.h> +#include <ls.h> +#include <times.h> +#include <fts.h> +#include <fs3d.h> +#include <hashkey.h> +#include <stk.h> +#include <tmx.h> + +#define PATH_CHUNK 256 + +#define CP 1 +#define LN 2 +#define MV 3 + +#define BAK_replace 0 /* no backup -- just replace */ +#define BAK_existing 1 /* number if already else simple*/ +#define BAK_number 2 /* append .suffix number suffix */ +#define BAK_simple 3 /* append suffix */ + +typedef struct State_s /* program state */ +{ + int backup; /* BAK_* type */ + int directory; /* destination is directory */ + int flags; /* FTS_* flags */ + int force; /* force approval */ + int fs3d; /* 3d fs enabled */ + int hierarchy; /* preserve hierarchy */ + int interactive; /* prompt for approval */ + int missmode; /* default missing dir mode */ + int official; /* move to next view */ + int op; /* {CP,LN,MV} */ + int pathsiz; /* state.path buffer size */ + int perm; /* permissions to preserve */ + int postsiz; /* state.path post index */ + int presiz; /* state.path pre index */ + int preserve; /* preserve { id mode time } */ + int recursive; /* subtrees too */ + int suflen; /* strlen(state.suffix) */ + int sync; /* fsync() each file after copy */ + int uid; /* caller uid */ + int update; /* replace only if newer */ + int verbose; /* list each file before op */ + + int (*link)(const char*, const char*); /* link */ + int (*stat)(const char*, struct stat*); /* stat */ + + char* path; /* to pathname buffer */ + char* opname; /* state.op message string */ + char* suffix; /* backup suffix */ + + Sfio_t* tmp; /* tmp string stream */ + + char text[PATH_MAX]; /* link text buffer */ +} State_t; + +static const char dot[2] = { '.' }; + +/* + * preserve support + */ + +static void +preserve(State_t* state, const char* path, struct stat* ns, struct stat* os) +{ + int n; + + if (tmxtouch(path, tmxgetatime(os), tmxgetmtime(os), TMX_NOTIME, 0)) + error(ERROR_SYSTEM|2, "%s: cannot reset access and modify times", path); + n = ((ns->st_uid != os->st_uid) << 1) | (ns->st_gid != os->st_gid); + if (n && chown(state->path, os->st_uid, os->st_gid)) + switch (n) + { + case 01: + error(ERROR_SYSTEM|2, "%s: cannot reset group to %s", path, fmtgid(os->st_gid)); + break; + case 02: + error(ERROR_SYSTEM|2, "%s: cannot reset owner to %s", path, fmtuid(os->st_uid)); + break; + case 03: + error(ERROR_SYSTEM|2, "%s: cannot reset owner to %s and group to %s", path, fmtuid(os->st_uid), fmtgid(os->st_gid)); + break; + } +} + +/* + * visit a single file and state.op to the destination + */ + +static int +visit(State_t* state, register FTSENT* ent) +{ + register char* base; + register int n; + register int len; + int rm; + int rfd; + int wfd; + int m; + int v; + char* s; + char* e; + char* protection; + Sfio_t* ip; + Sfio_t* op; + FTS* fts; + FTSENT* sub; + struct stat st; + + if (cmdquit()) + return -1; + if (ent->fts_info == FTS_DC) + { + error(2, "%s: directory causes cycle", ent->fts_path); + fts_set(NiL, ent, FTS_SKIP); + return 0; + } + if (ent->fts_level == 0) + { + base = ent->fts_name; + len = ent->fts_namelen; + if (state->hierarchy) + state->presiz = -1; + else + { + state->presiz = ent->fts_pathlen; + while (*base == '.' && *(base + 1) == '/') + for (base += 2; *base == '/'; base++); + if (*base == '.' && !*(base + 1)) + state->presiz--; + else if (*base) + state->presiz -= base - ent->fts_name; + base = ent->fts_name + len; + while (base > ent->fts_name && *(base - 1) == '/') + base--; + while (base > ent->fts_name && *(base - 1) != '/') + base--; + len -= base - ent->fts_name; + if (state->directory) + state->presiz -= len + 1; + } + } + else + { + base = ent->fts_path + state->presiz + 1; + len = ent->fts_pathlen - state->presiz - 1; + } + len++; + if (state->directory) + { + if ((state->postsiz + len) > state->pathsiz && !(state->path = newof(state->path, char, state->pathsiz = roundof(state->postsiz + len, PATH_CHUNK), 0))) + error(3, "out of space"); + if (state->hierarchy && ent->fts_level == 0 && strchr(base, '/')) + { + s = state->path + state->postsiz; + memcpy(s, base, len); + while (e = strchr(s, '/')) + { + *e = 0; + if (access(state->path, F_OK)) + { + st.st_mode = state->missmode; + if (s = strrchr(s, '/')) + { + *s = 0; + stat(state->path, &st); + *s = '/'; + } + if (mkdir(state->path, st.st_mode & S_IPERM)) + { + error(ERROR_SYSTEM|2, "%s: cannot create directory -- %s ignored", state->path, ent->fts_path); + fts_set(NiL, ent, FTS_SKIP); + return 0; + } + } + *e++ = '/'; + s = e; + } + } + } + switch (ent->fts_info) + { + case FTS_DP: + if (state->preserve && state->op != LN || ent->fts_level > 0 && (ent->fts_statp->st_mode & S_IRWXU) != S_IRWXU) + { + if (len && ent->fts_level > 0) + memcpy(state->path + state->postsiz, base, len); + else + state->path[state->postsiz] = 0; + if (stat(state->path, &st)) + error(ERROR_SYSTEM|2, "%s: cannot stat", state->path); + else + { + if ((ent->fts_statp->st_mode & S_IPERM) != (st.st_mode & S_IPERM) && chmod(state->path, ent->fts_statp->st_mode & S_IPERM)) + error(ERROR_SYSTEM|2, "%s: cannot reset directory mode to %s", state->path, fmtmode(st.st_mode & S_IPERM, 0) + 1); + if (state->preserve) + preserve(state, state->path, &st, ent->fts_statp); + } + } + return 0; + case FTS_DNR: + case FTS_DNX: + case FTS_D: + if (!state->recursive) + { + fts_set(NiL, ent, FTS_SKIP); + if (state->op == CP) + error(1, "%s: directory -- copying as plain file", ent->fts_path); + else if (state->link == link && !state->force) + { + error(2, "%s: cannot link directory", ent->fts_path); + return 0; + } + } + else switch (ent->fts_info) + { + case FTS_DNR: + error(2, "%s: cannot read directory", ent->fts_path); + return 0; + case FTS_DNX: + error(2, "%s: cannot search directory", ent->fts_path); + fts_set(NiL, ent, FTS_SKIP); + + /*FALLTHROUGH*/ + case FTS_D: + if (state->directory) + memcpy(state->path + state->postsiz, base, len); + if (!(*state->stat)(state->path, &st)) + { + if (!S_ISDIR(st.st_mode)) + { + error(2, "%s: not a directory -- %s ignored", state->path, ent->fts_path); + return 0; + } + } + else if (mkdir(state->path, (ent->fts_statp->st_mode & S_IPERM)|(ent->fts_info == FTS_D ? S_IRWXU : 0))) + { + error(ERROR_SYSTEM|2, "%s: cannot create directory -- %s ignored", state->path, ent->fts_path); + fts_set(NiL, ent, FTS_SKIP); + } + if (!state->directory) + { + state->directory = 1; + state->path[state->postsiz++] = '/'; + state->presiz--; + } + return 0; + } + break; + case FTS_ERR: + case FTS_NS: + case FTS_SLNONE: + if (state->link != pathsetlink) + { + error(2, "%s: not found", ent->fts_path); + return 0; + } + break; +#if 0 + case FTS_SL: + if (state->op == CP) + { + error(2, "%s: cannot copy non-terminal symbolic link", ent->fts_path); + return 0; + } + break; +#endif + } + if (state->directory) + memcpy(state->path + state->postsiz, base, len); + if ((*state->stat)(state->path, &st)) + st.st_mode = 0; + else if (state->update && !S_ISDIR(st.st_mode) && (unsigned long)ent->fts_statp->st_mtime < (unsigned long)st.st_mtime) + { + fts_set(NiL, ent, FTS_SKIP); + return 0; + } + else if (!state->fs3d || !iview(&st)) + { + /* + * target is in top 3d view + */ + + if (st.st_dev == ent->fts_statp->st_dev && st.st_ino == ent->fts_statp->st_ino) + { + if (state->op == MV) + { + /* + * let rename() handle it + */ + + if (state->verbose) + sfputr(sfstdout, state->path, '\n'); + goto operate; + } + if (!state->official) + error(2, "%s: identical to %s", state->path, ent->fts_path); + return 0; + } + if (S_ISDIR(st.st_mode)) + { + error(2, "%s: cannot %s existing directory", state->path, state->opname); + return 0; + } + if (state->verbose) + sfputr(sfstdout, state->path, '\n'); + rm = state->op == LN || ent->fts_info == FTS_SL; + if (!rm || !state->force) + { + if ((n = open(state->path, O_RDWR|O_BINARY)) >= 0) + { + close(n); + if (state->force) + /* ok */; + else if (state->interactive) + { + if (astquery(-1, "%s %s? ", state->opname, state->path)) + return 0; + } + else if (state->op == LN) + { + error(2, "%s: cannot %s existing file", state->path, state->opname); + return 0; + } + } + else if (state->force) + rm = 1; + else + { + protection = +#ifdef ETXTBSY + errno == ETXTBSY ? "``running program''" : +#endif + st.st_uid != state->uid ? "``not owner''" : + fmtmode(st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO), 0) + 1; + if (state->interactive) + { + if (astquery(-1, "override protection %s for %s? ", protection, state->path)) + return 0; + rm = 1; + } + else if (!rm) + { + error(2, "%s: cannot %s %s protection", state->path, state->opname, protection); + return 0; + } + } + } + switch (state->backup) + { + case BAK_existing: + case BAK_number: + v = 0; + if (s = strrchr(state->path, '/')) + { + e = state->path; + *s++ = 0; + } + else + { + e = (char*)dot; + s = state->path; + } + n = strlen(s); + if (fts = fts_open((char**)e, FTS_NOCHDIR|FTS_ONEPATH|FTS_PHYSICAL|FTS_NOPOSTORDER|FTS_NOSTAT|FTS_NOSEEDOTDIR, NiL)) + { + while (sub = fts_read(fts)) + { + if (strneq(s, sub->fts_name, n) && sub->fts_name[n] == '.' && strneq(sub->fts_name + n + 1, state->suffix, state->suflen) && (m = strtol(sub->fts_name + n + state->suflen + 1, &e, 10)) && streq(e, state->suffix) && m > v) + v = m; + if (sub->fts_level) + fts_set(NiL, sub, FTS_SKIP); + } + fts_close(fts); + } + if (s != state->path) + *--s = '/'; + if (v || state->backup == BAK_number) + { + sfprintf(state->tmp, "%s.%s%d%s", state->path, state->suffix, v + 1, state->suffix); + goto backup; + } + /*FALLTHROUGH*/ + case BAK_simple: + sfprintf(state->tmp, "%s%s", state->path, state->suffix); + backup: + if (!(s = sfstruse(state->tmp))) + error(ERROR_SYSTEM|3, "%s: out of space", state->path); + if (rename(state->path, s)) + { + error(ERROR_SYSTEM|2, "%s: cannot backup to %s", state->path, s); + return 0; + } + break; + default: + if (rm && remove(state->path)) + { + error(ERROR_SYSTEM|2, "%s: cannot remove", state->path); + return 0; + } + break; + } + } + operate: + switch (state->op) + { + case MV: + for (;;) + { + if (!rename(ent->fts_path, state->path)) + return 0; + if (errno == ENOENT) + rm = 1; + else if (!rm && st.st_mode && !remove(state->path)) + { + rm = 1; + continue; + } + if (errno != EXDEV && (rm || S_ISDIR(ent->fts_statp->st_mode))) + { + error(ERROR_SYSTEM|2, "%s: cannot rename to %s", ent->fts_path, state->path); + return 0; + } + else + break; + } + /*FALLTHROUGH*/ + case CP: + if (S_ISLNK(ent->fts_statp->st_mode)) + { + if ((n = pathgetlink(ent->fts_path, state->text, sizeof(state->text) - 1)) < 0) + { + error(ERROR_SYSTEM|2, "%s: cannot read symbolic link text", ent->fts_path); + return 0; + } + state->text[n] = 0; + if (pathsetlink(state->text, state->path)) + { + error(ERROR_SYSTEM|2, "%s: cannot copy symbolic link to %s", ent->fts_path, state->path); + return 0; + } + } + else if (state->op == CP || S_ISREG(ent->fts_statp->st_mode) || S_ISDIR(ent->fts_statp->st_mode)) + { + if (ent->fts_statp->st_size > 0 && (rfd = open(ent->fts_path, O_RDONLY|O_BINARY)) < 0) + { + error(ERROR_SYSTEM|2, "%s: cannot read", ent->fts_path); + return 0; + } + else if ((wfd = open(state->path, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, ent->fts_statp->st_mode & state->perm)) < 0) + { + error(ERROR_SYSTEM|2, "%s: cannot write", state->path); + if (ent->fts_statp->st_size > 0) + close(rfd); + return 0; + } + else if (ent->fts_statp->st_size > 0) + { + if (!(ip = sfnew(NiL, NiL, SF_UNBOUND, rfd, SF_READ))) + { + error(ERROR_SYSTEM|2, "%s: %s read stream error", ent->fts_path, state->path); + close(rfd); + close(wfd); + } + else + { + n = 0; + if (!(op = sfnew(NiL, NiL, SF_UNBOUND, wfd, SF_WRITE))) + { + error(ERROR_SYSTEM|2, "%s: %s write stream error", ent->fts_path, state->path); + close(wfd); + sfclose(ip); + } + else + { + if (sfmove(ip, op, (Sfoff_t)SF_UNBOUND, -1) < 0) + n |= 3; + if (!sfeof(ip)) + n |= 1; + if (sfsync(op) || state->sync && fsync(wfd) || sfclose(op)) + n |= 2; + if (sfclose(ip)) + n |= 1; + if (n) + error(ERROR_SYSTEM|2, "%s: %s %s error", ent->fts_path, state->path, n == 1 ? ERROR_translate(0, 0, 0, "read") : n == 2 ? ERROR_translate(0, 0, 0, "write") : ERROR_translate(0, 0, 0, "io")); + } + } + } + else + close(wfd); + } + else if (S_ISBLK(ent->fts_statp->st_mode) || S_ISCHR(ent->fts_statp->st_mode) || S_ISFIFO(ent->fts_statp->st_mode)) + { + if (mknod(state->path, ent->fts_statp->st_mode, idevice(ent->fts_statp))) + { + error(ERROR_SYSTEM|2, "%s: cannot copy special file to %s", ent->fts_path, state->path); + return 0; + } + } + else + { + error(2, "%s: cannot copy -- unknown file type 0%o", ent->fts_path, S_ITYPE(ent->fts_statp->st_mode)); + return 0; + } + if (state->preserve) + { + if (ent->fts_info != FTS_SL) + { + if (stat(state->path, &st)) + error(ERROR_SYSTEM|2, "%s: cannot stat", state->path); + else + { + if ((ent->fts_statp->st_mode & state->perm) != (st.st_mode & state->perm) && chmod(state->path, ent->fts_statp->st_mode & state->perm)) + error(ERROR_SYSTEM|2, "%s: cannot reset mode to %s", state->path, fmtmode(st.st_mode & state->perm, 0) + 1); + preserve(state, state->path, &st, ent->fts_statp); + } + } + if (state->op == MV && remove(ent->fts_path)) + error(ERROR_SYSTEM|1, "%s: cannot remove", ent->fts_path); + } + break; + case LN: + if ((*state->link)(ent->fts_path, state->path)) + error(ERROR_SYSTEM|2, "%s: cannot link to %s", ent->fts_path, state->path); + break; + } + return 0; +} + +int +b_cp(int argc, register char** argv, void* context) +{ + register char* file; + register char* s; + char** v; + char* backup_type; + FTS* fts; + FTSENT* ent; + const char* usage; + int path_resolve; + int standard; + struct stat st; + State_t state; + + cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); + memset(&state, 0, sizeof(state)); + state.presiz = -1; + backup_type = 0; + state.flags = FTS_NOCHDIR|FTS_NOSEEDOTDIR; + state.uid = geteuid(); + if (!(state.tmp = sfstropen())) + error(ERROR_SYSTEM|3, "out of space [tmp string]"); + sfputr(state.tmp, usage_head, -1); + standard = !strcmp(astconf("CONFORMANCE", NiL, NiL), "standard"); + switch (error_info.id[0]) + { + case 'c': + case 'C': + sfputr(state.tmp, usage_cp, -1); + state.op = CP; + state.stat = stat; + path_resolve = -1; + break; + case 'l': + case 'L': + sfputr(state.tmp, usage_ln, -1); + state.op = LN; + state.flags |= FTS_PHYSICAL; + state.link = link; + state.stat = lstat; + path_resolve = 1; + break; + case 'm': + case 'M': + sfputr(state.tmp, usage_mv, -1); + state.op = MV; + state.flags |= FTS_PHYSICAL; + state.preserve = 1; + state.stat = lstat; + path_resolve = 1; + break; + default: + error(3, "not implemented"); + break; + } + sfputr(state.tmp, usage_tail, -1); + if (!(usage = sfstruse(state.tmp))) + error(ERROR_SYSTEM|3, "%s: out of space", state.path); + state.opname = state.op == CP ? ERROR_translate(0, 0, 0, "overwrite") : ERROR_translate(0, 0, 0, "replace"); + for (;;) + { + switch (optget(argv, usage)) + { + case 'a': + state.flags |= FTS_PHYSICAL; + state.preserve = 1; + state.recursive = 1; + path_resolve = 1; + continue; + case 'b': + state.backup = 1; + continue; + case 'f': + state.force = 1; + if (state.op != CP || !standard) + state.interactive = 0; + continue; + case 'h': + state.hierarchy = 1; + continue; + case 'i': + state.interactive = 1; + if (state.op != CP || !standard) + state.force = 0; + continue; + case 'l': + state.op = LN; + state.link = link; + state.stat = lstat; + continue; + case 'p': + state.preserve = 1; + continue; + case 'r': + state.recursive = 1; + if (path_resolve < 0) + path_resolve = 0; + continue; + case 's': + state.op = LN; + state.link = pathsetlink; + state.stat = lstat; + continue; + case 'u': + state.update = 1; + continue; + case 'v': + state.verbose = 1; + continue; + case 'x': + state.flags |= FTS_XDEV; + continue; + case 'F': +#if _lib_fsync + state.sync = 1; +#else + error(1, "%s not implemented on this system", opt_info.name); +#endif + continue; + case 'H': + state.flags |= FTS_META|FTS_PHYSICAL; + path_resolve = 1; + continue; + case 'L': + state.flags &= ~FTS_PHYSICAL; + path_resolve = 1; + continue; + case 'P': + state.flags &= ~FTS_META; + state.flags |= FTS_PHYSICAL; + path_resolve = 1; + continue; + case 'R': + state.recursive = 1; + state.flags &= ~FTS_META; + state.flags |= FTS_PHYSICAL; + path_resolve = 1; + continue; + case 'S': + state.suffix = opt_info.arg; + continue; + case 'V': + backup_type = opt_info.arg; + continue; + case '?': + error(ERROR_USAGE|4, "%s", opt_info.arg); + continue; + case ':': + error(2, "%s", opt_info.arg); + continue; + } + break; + } + argc -= opt_info.index + 1; + argv += opt_info.index; + if (*argv && streq(*argv, "-") && !streq(*(argv - 1), "--")) + { + argc--; + argv++; + } + if (!(v = (char**)stkalloc(stkstd, (argc + 2) * sizeof(char*)))) + error(3, "out of space"); + memcpy(v, argv, (argc + 1) * sizeof(char*)); + argv = v; + if (!argc && !standard) + { + argc++; + argv[1] = (char*)dot; + } + if (state.backup) + { + if (!(file = backup_type) && !(backup_type = getenv("VERSION_CONTROL"))) + state.backup = BAK_existing; + else + switch (strkey(backup_type)) + { + case HASHKEY6('e','x','i','s','t','i'): + case HASHKEY5('e','x','i','s','t'): + case HASHKEY4('e','x','i','s'): + case HASHKEY3('e','x','i'): + case HASHKEY2('e','x'): + case HASHKEY1('e'): + case HASHKEY3('n','i','l'): + case HASHKEY2('n','i'): + state.backup = BAK_existing; + break; + case HASHKEY5('n','e','v','e','r'): + case HASHKEY4('n','e','v','e'): + case HASHKEY3('n','e','v'): + case HASHKEY2('n','e'): + case HASHKEY6('s','i','m','p','l','e'): + case HASHKEY5('s','i','m','p','l'): + case HASHKEY4('s','i','m','p'): + case HASHKEY3('s','i','m'): + case HASHKEY2('s','i'): + case HASHKEY1('s'): + state.backup = BAK_simple; + break; + case HASHKEY6('n','u','m','b','e','r'): + case HASHKEY5('n','u','m','b','e'): + case HASHKEY4('n','u','m','b'): + case HASHKEY3('n','u','m'): + case HASHKEY2('n','u'): + case HASHKEY1('t'): + state.backup = BAK_number; + break; + default: + if (file) + error(2, "%s: unknown backup type", backup_type); + break; + } + if (!state.suffix && !(state.suffix = getenv("SIMPLE_BACKUP_SUFFIX"))) + state.suffix = "~"; + state.suflen = strlen(state.suffix); + } + if (argc <= 0 || error_info.errors) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + if (!path_resolve) + state.flags |= fts_flags(); + file = argv[argc]; + argv[argc] = 0; + if (s = strrchr(file, '/')) + { + while (*s == '/') + s++; + if (!(!*s || *s == '.' && (!*++s || *s == '.' && !*++s))) + s = 0; + } + if (file != (char*)dot) + pathcanon(file, 0); + if (!(state.directory = !stat(file, &st) && S_ISDIR(st.st_mode)) && argc > 1) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + if (s && !state.directory) + error(3, "%s: not a directory", file); + if ((state.fs3d = fs3d(FS3D_TEST)) && strmatch(file, "...|*/...|.../*")) + state.official = 1; + state.postsiz = strlen(file); + state.pathsiz = roundof(state.postsiz + 2, PATH_CHUNK); + if (!(state.path = newof(0, char, state.pathsiz, 0))) + error(3, "out of space"); + memcpy(state.path, file, state.postsiz + 1); + if (state.directory && state.path[state.postsiz - 1] != '/') + state.path[state.postsiz++] = '/'; + if (state.hierarchy) + { + if (!state.directory) + error(3, "%s: last argument must be a directory", file); + state.missmode = st.st_mode; + } + state.perm = state.uid ? S_IPERM : (S_IPERM & ~S_ISVTX); + if (!state.recursive) + state.flags |= FTS_TOP; + if (fts = fts_open(argv, state.flags, NiL)) + { + while ((ent = fts_read(fts)) && !visit(&state, ent)); + fts_close(fts); + } + else if (state.link != pathsetlink) + switch (state.op) + { + case CP: + error(ERROR_SYSTEM|2, "%s: cannot copy", argv[0]); + break; + case LN: + error(ERROR_SYSTEM|2, "%s: cannot link", argv[0]); + break; + case MV: + error(ERROR_SYSTEM|2, "%s: cannot move", argv[0]); + break; + } + else if ((*state.link)(*argv, state.path)) + error(ERROR_SYSTEM|2, "%s: cannot link to %s", *argv, state.path); + free(state.path); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/cut.c b/usr/src/lib/libcmd/common/cut.c new file mode 100644 index 0000000000..732d1784da --- /dev/null +++ b/usr/src/lib/libcmd/common/cut.c @@ -0,0 +1,512 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * cut [-sN] [-f flist] [-c clist] [-d delim] [-D delim] [-r reclen] [file] ... + * + * cut fields or columns from fields from a file + */ + +static const char usage[] = +"[-?\n@(#)$Id: cut (AT&T Research) 2007-01-23 $\n]" +USAGE_LICENSE +"[+NAME?cut - cut out selected columns or fields of each line of a file]" +"[+DESCRIPTION?\bcut\b bytes, characters, or character-delimited fields " + "from one or more files, contatenating them on standard output.]" +"[+?The option argument \alist\a is a comma-separated or blank-separated " + "list of positive numbers and ranges. Ranges can be of three " + "forms. The first is two positive integers separated by a hyphen " + "(\alow\a\b-\b\ahigh\a), which represents all fields from \alow\a to " + "\ahigh\a. The second is a positive number preceded by a hyphen " + "(\b-\b\ahigh\a), which represents all fields from field \b1\b to " + "\ahigh\a. The last is a positive number followed by a hyphen " + "(\alow\a\b-\b), which represents all fields from \alow\a to the " + "last field, inclusive. Elements in the \alist\a can be repeated, " + "can overlap, and can appear in any order. The order of the " + "output is that of the input.]" +"[+?One and only one of \b-b\b, \b-c\b, or \b-f\b must be specified.]" +"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bcut\b " + "cuts from standard input. The start of the file is defined " + "as the current offset.]" +"[b:bytes]:[list?\bcut\b based on a list of bytes.]" +"[c:characters]:[list?\bcut\b based on a list of characters.]" +"[d:delimiter]:[delim?The field character for the \b-f\b option is set " + "to \adelim\a. The default is the \btab\b character.]" +"[f:fields]:[list?\bcut\b based on fields separated by the delimiter " + "character specified with the \b-d\b optiion.]" +"[n:nosplit?Do not split characters. Currently ignored.]" +"[R|r:reclen]#[reclen?If \areclen\a > 0, the input will be read as fixed length " + "records of length \areclen\a when used with the \b-b\b or \b-c\b " + "option.]" +"[s:suppress|only-delimited?Suppress lines with no delimiter characters, " + "when used with the \b-f\b option. By default, lines with no " + "delimiters will be passsed in untouched.]" +"[D:line-delimeter|output-delimiter]:[ldelim?The line delimiter character for " + "the \b-f\b option is set to \aldelim\a. The default is the " + "\bnewline\b character.]" +"[N:nonewline?Do not output new-lines at end of each record when used " + "with the \b-b\b or \b-c\b option.]" +"\n" +"\n[file ...]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files processed successfully.]" + "[+>0?One or more files failed to open or could not be read.]" +"}" +"[+SEE ALSO?\bpaste\b(1), \bgrep\b(1)]" +; + +#include <cmd.h> +#include <ctype.h> + +typedef struct Last_s +{ + int seqno; + int seq; + int wdelim; + int ldelim; +} Last_t; + +typedef struct Cut_s +{ + int cflag; + int sflag; + int nlflag; + int wdelim; + int ldelim; + int seqno; + int reclen; + signed char space[UCHAR_MAX]; + Last_t last; + int list[2]; /* NOTE: must be last member */ +} Cut_t; + +#define HUGE (1<<14) +#define BLOCK 8*1024 +#define C_BYTES 1 +#define C_CHARS 2 +#define C_FIELDS 4 +#define C_SUPRESS 8 +#define C_NOCHOP 16 +#define C_NONEWLINE 32 + +/* + * compare the first of an array of integers + */ + +static int mycomp(register const void *a,register const void *b) +{ + return(*((int*)a) - *((int*)b)); +} + +static Cut_t *cutinit(int mode,char *str,int wdelim,int ldelim,size_t reclen) +{ + register int *lp, c, n=0; + register int range = 0; + register char *cp = str; + Cut_t *cuthdr; + if (!(cuthdr = (Cut_t*)stakalloc(sizeof(Cut_t)+strlen(cp)*sizeof(int)))) + error(ERROR_exit(1), "out of space"); + memset(cuthdr->space, 0, sizeof(cuthdr->space)); + cuthdr->last.seqno = 0; + cuthdr->last.seq = 0; + cuthdr->last.wdelim = 0; + cuthdr->last.ldelim = '\n'; + cuthdr->cflag = ((mode&C_CHARS)!=0 && mbwide()); + cuthdr->sflag = ((mode&C_SUPRESS)!=0); + cuthdr->nlflag = ((mode&C_NONEWLINE)!=0); + cuthdr->wdelim = wdelim; + cuthdr->ldelim = ldelim; + cuthdr->reclen = reclen; + cuthdr->seqno = ++cuthdr->last.seqno; + lp = cuthdr->list; + while(1) switch(c= *cp++) + { + case ' ': + case '\t': + while(*cp==' ' || *cp=='\t') + cp++; + case 0: + case ',': + if(range) + { + --range; + if((n = (n==0?HUGE:n-range)) < 0) + error(ERROR_exit(1),"invalid range for c/f option"); + *lp++ = range; + *lp++ = n; + } + else + { + *lp++ = --n; + *lp++ = 1; + } + if(c==0) + { + register int *dp; + *lp = HUGE; + n = 1 + (lp-cuthdr->list)/2; + qsort(lp=cuthdr->list,n,2*sizeof(*lp),mycomp); + /* eliminate overlapping regions */ + for(n=0,range= -2,dp=lp; *lp!=HUGE; lp+=2) + { + if(lp[0] <= range) + { + if(lp[1]==HUGE) + { + dp[-1] = HUGE; + break; + } + if((c = lp[0]+lp[1]-range)>0) + { + range += c; + dp[-1] += c; + } + } + else + { + range = *dp++ = lp[0]; + if(lp[1]==HUGE) + { + *dp++ = HUGE; + break; + } + range += (*dp++ = lp[1]); + } + } + *dp = HUGE; + lp = cuthdr->list; + /* convert ranges into gaps */ + for(n=0; *lp!=HUGE; lp+=2) + { + c = *lp; + *lp -= n; + n = c+lp[1]; + } + return(cuthdr); + } + n = range = 0; + break; + + case '-': + if(range) + error(ERROR_exit(1),"bad list for c/f option"); + range = n?n:1; + n = 0; + break; + + default: + if(!isdigit(c)) + error(ERROR_exit(1),"bad list for c/f option"); + n = 10*n + (c-'0'); + } + /* NOTREACHED */ +} + +/* + * advance <cp> by <n> multi-byte characters + */ +static int advance(const char *str, register int n, register int inlen) +{ + register int size, len=inlen; + register const char *cp=str; + while(len>0 && n-->0) + { + size = mblen(cp, len); + if(size<0) + size = 1; + cp += size; + len -= size; + + } + if(n>0) + return(inlen+1); + return(cp-str); +} + +/* + * cut each line of file <fdin> and put results to <fdout> using list <list> + */ + +static int cutcols(Cut_t *cuthdr,Sfio_t *fdin,Sfio_t *fdout) +{ + register int c, ncol=0,len; + register const int *lp = cuthdr->list; + register char *inp; + register int skip; /* non-zero for don't copy */ + while(1) + { + if(len = cuthdr->reclen) + inp = sfreserve(fdin, len, -1); + else + inp = sfgetr(fdin, '\n', 0); + if(!inp && !(inp = sfgetr(fdin, 0, SF_LASTR))) + break; + len = sfvalue(fdin); + if((ncol = skip = *(lp = cuthdr->list)) == 0) + ncol = *++lp; + while(1) + { + if((c=(cuthdr->cflag?advance(inp,ncol,len):ncol)) > len) + c = len; + else if(c==len && !skip) + ncol++; + ncol -= c; + if(!skip && sfwrite(fdout,(char*)inp,c)<0) + return(-1); + inp += c; + if(ncol) + break; + len -= c; + ncol = *++lp; + skip = !skip; + } + if(!cuthdr->nlflag && (skip || cuthdr->reclen)) + sfputc(fdout,cuthdr->ldelim); + } + return(c); +} + +/* + * cut each line of file <fdin> and put results to <fdout> using list <list> + * stream <fdin> must be line buffered + */ + +#define endline(c) (((signed char)-1)<0?(c)<0:(c)==((char)-1)) + +static int cutfields(Cut_t *cuthdr,Sfio_t *fdin,Sfio_t *fdout) +{ + register unsigned char *cp; + register int c, nfields; + register const int *lp = cuthdr->list; + register unsigned char *copy; + register int nodelim, empty, inword=0; + register unsigned char *endbuff; + unsigned char *inbuff, *first; + int lastchar; + Sfio_t *fdtmp = 0; + long offset = 0; + if(cuthdr->seqno != cuthdr->last.seq) + { + cuthdr->space[cuthdr->last.ldelim] = 0; + cuthdr->space[cuthdr->last.wdelim] = 0; + cuthdr->space[cuthdr->last.wdelim=cuthdr->wdelim] = 1; + cuthdr->space[cuthdr->last.ldelim=cuthdr->ldelim] = -1; + cuthdr->last.seq = cuthdr->seqno; + } + /* process each buffer */ + while ((inbuff = (unsigned char*)sfreserve(fdin, SF_UNBOUND, 0)) && (c = sfvalue(fdin)) > 0) + { + cp = inbuff; + endbuff = cp + --c; + if((lastchar = cp[c]) != cuthdr->ldelim) + *endbuff = cuthdr->ldelim; + /* process each line in the buffer */ + while(cp <= endbuff) + { + first = cp; + if(!inword) + { + nodelim = empty = 1; + copy = cp; + if(nfields = *(lp = cuthdr->list)) + copy = 0; + else + nfields = *++lp; + } + else if(copy) + copy = cp; + inword = 0; + while(!inword) + { + /* skip over non-delimiter characters */ + while(!(c=cuthdr->space[*cp++])); + /* check for end-of-line */ + if(endline(c)) + { + if(cp<=endbuff) + break; + if((c=cuthdr->space[lastchar]),endline(c)) + break; + /* restore cuthdr->last. character */ + if(lastchar != cuthdr->ldelim) + *endbuff = lastchar; + inword++; + if(!c) + break; + } + nodelim = 0; + if(--nfields >0) + continue; + nfields = *++lp; + if(copy) + { + empty = 0; + if((c=(cp-1)-copy)>0 && sfwrite(fdout,(char*)copy,c)< 0) + goto failed; + copy = 0; + } + else + /* set to delimiter unless the first field */ + copy = cp -!empty; + } + if(!inword) + { + if(!copy) + { + if(nodelim) + { + if(!cuthdr->sflag) + { + if(offset) + { + sfseek(fdtmp,(Sfoff_t)0,SEEK_SET); + sfmove(fdtmp,fdout,offset,-1); + } + copy = first; + } + } + else + sfputc(fdout,'\n'); + } + if(offset) + sfseek(fdtmp,offset=0,SEEK_SET); + } + if(copy && (c=cp-copy)>0 && (!nodelim || !cuthdr->sflag) && sfwrite(fdout,(char*)copy,c)< 0) + goto failed; + } + /* see whether to save in tmp file */ + if(nodelim && inword && !cuthdr->sflag && (c=cp-first)>0) + { + /* copy line to tmpfile in case no fields */ + if(!fdtmp) + fdtmp = sftmp(BLOCK); + sfwrite(fdtmp,(char*)first,c); + offset +=c; + } + } +failed: + if(fdtmp) + sfclose(fdtmp); + return(0); +} + +int +b_cut(int argc,char *argv[], void* context) +{ + register char *cp = 0; + register Sfio_t *fp; + int n; + Cut_t *cuthdr; + int mode = 0; + int wdelim = '\t'; + int ldelim = '\n'; + size_t reclen = 0; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'b': + case 'c': + if(mode&C_FIELDS) + { + error(2, "f option already specified"); + break; + } + cp = opt_info.arg; + if(n=='b') + mode |= C_BYTES; + else + mode |= C_CHARS; + break; + case 'D': + ldelim = *(unsigned char*)opt_info.arg; + break; + case 'd': + wdelim = *(unsigned char*)opt_info.arg; + break; + case 'f': + if(mode&(C_CHARS|C_BYTES)) + { + error(2, "c option already specified"); + break; + } + cp = opt_info.arg; + mode |= C_FIELDS; + break; + case 'n': + mode |= C_NOCHOP; + break; + case 'N': + mode |= C_NONEWLINE; + break; + case 'R': + case 'r': + if(opt_info.num>0) + reclen = opt_info.num; + break; + case 's': + mode |= C_SUPRESS; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if (error_info.errors) + error(ERROR_usage(2), "%s",optusage(NiL)); + if(!cp) + { + error(2, "b, c or f option must be specified"); + error(ERROR_usage(2), "%s", optusage(NiL)); + } + if(!*cp) + error(3, "non-empty b, c or f option must be specified"); + if((mode & (C_FIELDS|C_SUPRESS)) == C_SUPRESS) + error(3, "s option requires f option"); + cuthdr = cutinit(mode,cp,wdelim,ldelim,reclen); + if(cp = *argv) + argv++; + do + { + if(!cp || streq(cp,"-")) + fp = sfstdin; + else if(!(fp = sfopen(NiL,cp,"r"))) + { + error(ERROR_system(0),"%s: cannot open",cp); + continue; + } + if(mode&C_FIELDS) + cutfields(cuthdr,fp,sfstdout); + else + cutcols(cuthdr,fp,sfstdout); + if(fp!=sfstdin) + sfclose(fp); + } + while(cp= *argv++); + return(error_info.errors?1:0); +} diff --git a/usr/src/lib/libcmd/common/date.c b/usr/src/lib/libcmd/common/date.c new file mode 100644 index 0000000000..b2ea46d146 --- /dev/null +++ b/usr/src/lib/libcmd/common/date.c @@ -0,0 +1,489 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * date -- set/display date + */ + +static const char usage[] = +"[-?\n@(#)$Id: date (AT&T Research) 2007-03-28 $\n]" +USAGE_LICENSE +"[+NAME?date - set/list/convert dates]" +"[+DESCRIPTION?\bdate\b sets the current date and time (with appropriate" +" privilege), lists the current date or file dates, or converts" +" dates.]" +"[+?Most common \adate\a forms are recognized, including those for" +" \bcrontab\b(1), \bls\b(1), \btouch\b(1), and the default" +" output from \bdate\b itself.]" +"[+?If the \adate\a operand consists of 4, 6, 8, 10 or 12 digits followed" +" by an optional \b.\b and two digits then it is interpreted as:" +" \aHHMM.SS\a, \addHHMM.SS\a, \ammddHHMM.SS\a, \ammddHHMMyy.SS\a or" +" \ayymmddHHMM.SS\a, or \ammddHHMMccyy.SS\a or \accyymmddHHMM.SS\a." +" Conflicting standards and practice allow a leading or trailing" +" 2 or 4 digit year for the 10 and 12 digit forms; the X/Open trailing" +" form is used to disambiguate (\btouch\b(1) uses the leading form.)" +" Avoid the 10 digit form to avoid confusion. The digit fields are:]{" +" [+cc?Century - 1, 19-20.]" +" [+yy?Year in century, 00-99.]" +" [+mm?Month, 01-12.]" +" [+dd?Day of month, 01-31.]" +" [+HH?Hour, 00-23.]" +" [+MM?Minute, 00-59.]" +" [+SS?Seconds, 00-60.]" +"}" +"[+?If more than one \adate\a operand is specified then:]{" +" [+1.?Each operand sets the reference date for the next" +" operand.]" +" [+2.?The date is listed for each operand.]" +" [+3.?The system date is not set.]" +"}" + +"[a:access-time|atime?List file argument access times.]" +"[c:change-time|ctime?List file argument change times.]" +"[d:date?Use \adate\a as the current date and do not set the system" +" clock.]:[date]" +"[e:epoch?Output the date in seconds since the epoch." +" Equivalent to \b--format=%s\b.]" +"[E:elapsed?Interpret pairs of arguments as start and stop dates, sum the" +" differences between all pairs, and list the result as a" +" \bfmtelapsed\b(3) elapsed time on the standard output. If there are" +" an odd number of arguments then the last time argument is differenced" +" with the current time.]" +"[f:format?Output the date according to the \bstrftime\b(3) \aformat\a." +" For backwards compatibility, a first argument of the form" +" \b+\b\aformat\a is equivalent to \b-f\b format." +" \aformat\a is in \bprintf\b(3) style, where %\afield\a names" +" a fixed size field, zero padded if necessary," +" and \\\ac\a and \\\annn\a sequences are as in C. Invalid" +" %\afield\a specifications and all other characters are copied" +" without change. \afield\a may be preceded by \b%-\b to turn off" +" padding or \b%_\b to pad with space, otherwise numeric fields" +" are padded with \b0\b and string fields are padded with space." +" \afield\a may also be preceded by \bE\b for alternate era" +" representation or \bO\b for alternate digit representation (if" +" supported by the current locale.) Finally, an integral \awidth\a" +" preceding \afield\a truncates the field to \awidth\a characters." +" The fields are:]:[format]{" +" [+%?% character]" +" [+a?abbreviated weekday name]" +" [+A?full weekday name]" +" [+b?abbreviated month name]" +" [+c?\bctime\b(3) style date without the trailing newline]" +" [+C?2-digit century]" +" [+d?day of month number]" +" [+D?date as \amm/dd/yy\a]" +" [+e?blank padded day of month number]" +" [+E?unpadded day of month number]" +" [+f?locale default override date format]" +" [+F?locale default date format]" +" [+g?\bls\b(1) \b-l\b recent date with \ahh:mm\a]" +" [+G?\bls\b(1) \b-l\b distant date with \ayyyy\a]" +" [+h?abbreviated month name]" +" [+H?24-hour clock hour]" +" [+i?international \bdate\b(1) date with time zone type name]" +" [+I?12-hour clock hour]" +" [+j?1-offset Julian date]" +" [+J?0-offset Julian date]" +" [+k?\bdate\b(1) style date]" +" [+K?all numeric date; equivalent to \b%Y-%m-%d+%H:%M:%S\b]" +" [+l?\bls\b(1) \b-l\b date; equivalent to \b%Q/%g/%G/\b]" +" [+m?month number]" +" [+M?minutes]" +" [+n?newline character]" +" [+N?nanoseconds 000000000-999999999]" +" [+p?meridian (e.g., \bAM\b or \bPM\b)]" +" [+q?time zone type name (nation code)]" +" [+Q?\a<del>recent<del>distant<del>\a: \a<del>\a is a unique" +" delimter character; \arecent\a format for recent" +" dates, \adistant\a format otherwise]" +" [+r?12-hour time as \ahh:mm:ss meridian\a]" +" [+R?24-hour time as \ahh:mm\a]" +" [+s?number of seconds since the epoch; \a.prec\a preceding" +" \bs\b appends \aprec\a nanosecond digits, \b9\b if" +" \aprec\a is omitted]" +" [+S?seconds 00-60]" +" [+t?tab character]" +" [+T?24-hour time as \ahh:mm:ss\a]" +" [+u?weekday number 1(Monday)-7]" +" [+U?week number with Sunday as the first day]" +" [+V?ISO week number (i18n is \afun\a)]" +" [+w?weekday number 0(Sunday)-6]" +" [+W?week number with Monday as the first day]" +" [+x?locale date style that includes month, day and year]" +" [+X?locale time style that includes hours and minutes]" +" [+y?2-digit year (you'll be sorry)]" +" [+Y?4-digit year]" +" [+z?time zone \aSHHMM\a west of GMT offset where S is" +" \b+\b or \b-\b]" +" [+Z?time zone name]" +" [+=[=]][-+]]flag?set (default or +) or clear (-) \aflag\a" +" for the remainder of \aformat\a, or for the remainder" +" of the process if \b==\b is specified. \aflag\a may be:]{" +" [+l?enable leap second adjustments]" +" [+n?convert \b%S\b as \b%S.%N\b]" +" [+u?UTC time zone]" +" }" +" [+#?equivalent to %s]" +" [+??alternate?use \aalternate\a format if a default format" +" override has not been specified, e.g., \bls\b(1) uses" +" \"%?%l\"; export TM_OPTIONS=\"format='\aoverride\a'\"" +" to override the default]" +"}" +"[i:incremental|adjust?Set the system time in incrementatl adjustments to" +" avoid complete time shift shock. Negative adjustments still maintain" +" monotonic increasing time. Not available on all systems.]" +"[L:last?List only the last time for multiple \adate\a operands.]" +"[l:leap-seconds?Include leap seconds in time calculations. Leap seconds" +" after the ast library release date are not accounted for.]" +"[m:modify-time|mtime?List file argument modify times.]" +"[n!:network?Set network time.]" +"[p:parse?Add \aformat\a to the list of \bstrptime\b(3) parse conversion" +" formats. \aformat\a follows the same conventions as the" +" \b--format\b option, with the addition of these format" +" fields:]:[format]{" +" [+|?If the format failed before this point then restart" +" the parse with the remaining format.]" +" [+&?Call the \btmdate\b(3) heuristic parser. This is" +" is the default when \b--parse\b is omitted.]" +"}" +"[s:show?Show the date without setting the system time.]" +"[u:utc|gmt|zulu?Output dates in \acoordinated universal time\a (UTC).]" +"[U:unelapsed?Interpret each argument as \bfmtelapsed\b(3) elapsed" +" time and list the \bstrelapsed\b(3) 1/\ascale\a seconds.]#[scale]" +"[z:list-zones?List the known time zone table and exit. The table columns" +" are: country code, standard zone name, savings time zone name," +" minutes west of \bUTC\b, and savings time minutes offset. Blank" +" or empty entries are listed as \b-\b.]" + +"\n" +"\n[ +format | date ... | file ... ]\n" +"\n" + +"[+SEE ALSO?\bcrontab\b(1), \bls\b(1), \btouch\b(1), \bfmtelapsed\b(3)," +" \bstrftime\b(3), \bstrptime\b(3), \btm\b(3)]" +; + +#include <cmd.h> +#include <ls.h> +#include <proc.h> +#include <tmx.h> +#include <times.h> + +typedef struct Fmt +{ + struct Fmt* next; + char* format; +} Fmt_t; + +#ifndef ENOSYS +#define ENOSYS EINVAL +#endif + +/* + * set the system clock + * the standards wimped out here + */ + +static int +settime(const char* cmd, Time_t now, int adjust, int network) +{ + char* s; + char** argv; + char* args[5]; + char buf[128]; + + if (!adjust && !network) + return tmxsettime(now); + argv = args; + s = "/usr/bin/date"; + if (!streq(cmd, s) && (!eaccess(s, X_OK) || !eaccess(s+=4, X_OK))) + { + *argv++ = s; + if (streq(astconf("UNIVERSE", NiL, NiL), "att")) + { + tmxfmt(buf, sizeof(buf), "%m%d%H" "%M" "%Y.%S", now); + if (adjust) + *argv++ = "-a"; + } + else + { + tmxfmt(buf, sizeof(buf), "%Y" "%m%d%H" "%M.%S", now); + if (network) + *argv++ = "-n"; + if (tm_info.flags & TM_UTC) + *argv++ = "-u"; + } + *argv++ = buf; + *argv = 0; + if (!procrun(s, args)) + return 0; + } + return -1; +} + +/* + * convert s to Time_t with error checking + */ + +static Time_t +convert(register Fmt_t* f, char* s, Time_t now) +{ + char* t; + char* u; + + do + { + now = tmxscan(s, &t, f->format, &u, now, 0); + if (!*t && (!f->format || !*u)) + break; + } while (f = f->next); + if (!f || *t) + error(3, "%s: invalid date specification", f ? t : s); + return now; +} + +int +b_date(int argc, register char** argv, void* context) +{ + register int n; + register char* s; + register Fmt_t* f; + char* t; + unsigned long u; + Time_t now; + Time_t ts; + Time_t te; + Time_t e; + char buf[128]; + Fmt_t* fmts; + Fmt_t fmt; + struct stat st; + + char* cmd = argv[0]; /* original command path */ + char* format = 0; /* tmxfmt() format */ + char* string = 0; /* date string */ + int elapsed = 0; /* args are start/stop pairs */ + int filetime = 0; /* use this st_ time field */ + int increment = 0; /* incrementally adjust time */ + int last = 0; /* display the last time arg */ + Tm_zone_t* listzones = 0; /* known time zone table */ + int network = 0; /* don't set network time */ + int show = 0; /* show date and don't set */ + int unelapsed = 0; /* fmtelapsed() => strelapsed */ + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + setlocale(LC_ALL, ""); + tm_info.flags = TM_DATESTYLE; + fmts = &fmt; + fmt.format = ""; + fmt.next = 0; + for (;;) + { + switch (optget(argv, usage)) + { + case 'a': + case 'c': + case 'm': + filetime = opt_info.option[1]; + continue; + case 'd': + string = opt_info.arg; + show = 1; + continue; + case 'e': + format = "%#"; + continue; + case 'E': + elapsed = 1; + continue; + case 'f': + format = opt_info.arg; + continue; + case 'i': + increment = 1; + continue; + case 'l': + tm_info.flags |= TM_LEAP; + continue; + case 'L': + last = 1; + continue; + case 'n': + network = 1; + continue; + case 'p': + if (!(f = newof(0, Fmt_t, 1, 0))) + error(ERROR_SYSTEM|3, "out of space [format]"); + f->next = fmts; + f->format = opt_info.arg; + fmts = f; + continue; + case 's': + show = 1; + continue; + case 'u': + tm_info.flags |= TM_UTC; + continue; + case 'U': + unelapsed = (int)opt_info.num; + continue; + case 'z': + listzones = tm_data.zone; + 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) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + now = tmxgettime(); + if (listzones) + { + s = "-"; + while (listzones->standard) + { + if (listzones->type) + s = listzones->type; + sfprintf(sfstdout, "%3s %4s %4s %4d %4d\n", s, *listzones->standard ? listzones->standard : "-", listzones->daylight ? listzones->daylight : "-", listzones->west, listzones->dst); + listzones++; + show = 1; + } + } + else if (elapsed) + { + e = 0; + while (s = *argv++) + { + if (!(t = *argv++)) + { + argv--; + t = "now"; + } + ts = convert(fmts, s, now); + te = convert(fmts, t, now); + if (te > ts) + e += te - ts; + else + e += ts - te; + } + sfputr(sfstdout, fmtelapsed((unsigned long)tmxsec(e), 1), '\n'); + show = 1; + } + else if (unelapsed) + { + while (s = *argv++) + { + u = strelapsed(s, &t, unelapsed); + if (*t) + error(3, "%s: invalid elapsed time", s); + sfprintf(sfstdout, "%lu\n", u); + } + show = 1; + } + else if (filetime) + { + if (!*argv) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + n = argv[1] != 0; + while (s = *argv++) + { + if (stat(s, &st)) + error(2, "%s: not found", s); + else + { + switch (filetime) + { + case 'a': + now = tmxgetatime(&st); + break; + case 'c': + now = tmxgetctime(&st); + break; + default: + now = tmxgetmtime(&st); + break; + } + tmxfmt(buf, sizeof(buf), format, now); + if (n) + sfprintf(sfstdout, "%s: %s\n", s, buf); + else + sfprintf(sfstdout, "%s\n", buf); + show = 1; + } + } + } + else + { + if ((s = *argv) && !format && *s == '+') + { + format = s + 1; + argv++; + s = *argv; + } + if (s || (s = string)) + { + if (*argv && string) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + now = convert(fmts, s, now); + if (*argv && (s = *++argv)) + { + show = 1; + do + { + if (!last) + { + tmxfmt(buf, sizeof(buf), format, now); + sfprintf(sfstdout, "%s\n", buf); + } + now = convert(fmts, s, now); + } while (s = *++argv); + } + } + else + show = 1; + if (format || show) + { + tmxfmt(buf, sizeof(buf), format, now); + sfprintf(sfstdout, "%s\n", buf); + } + else if (settime(cmd, now, increment, network)) + error(ERROR_SYSTEM|3, "cannot set system time"); + } + while (fmts != &fmt) + { + f = fmts; + fmts = fmts->next; + free(f); + } + tm_info.flags = 0; + if (show && sfsync(sfstdout)) + error(ERROR_system(0), "write error"); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/dirname.c b/usr/src/lib/libcmd/common/dirname.c new file mode 100644 index 0000000000..f4a3c820c3 --- /dev/null +++ b/usr/src/lib/libcmd/common/dirname.c @@ -0,0 +1,116 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * dirname path [suffix] + * + * print the dirname of a pathname + */ + +static const char usage[] = +"[-?\n@(#)$Id: dirname (AT&T Research) 2000-03-07 $\n]" +USAGE_LICENSE +"[+NAME?dirname - return directory portion of file name]" +"[+DESCRIPTION?\bdirname\b treats \astring\a as a file name and returns " + "the name of the directory containing the file name by deleting " + "the last component from \astring\a.]" +"[+?If \astring\a consists solely of \b/\b characters the output will " + "be a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by " + "\bgetconf\b(1) is \b1\b and \astring\a consists of multiple " + "\b/\b characters in which case \b//\b will be output. " + "Otherwise, trailing \b/\b characters are removed, and if " + "there are no remaining \b/\b characters in \astring\a, " + "the string \b.\b will be written to standard output. " + "Otherwise, all characters following the last \b/\b are removed. " + "If the remaining string consists solely of \b/\b characters, " + "the output will be as if the original string had consisted solely " + "as \b/\b characters as described above. Otherwise, all " + "trailing slashes are removed and the output will be this string " + "unless this string is empty. If empty the output will be \b.\b.]" +"\n" +"\n string\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?Successful Completion.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bbasename\b(1), \bgetconf\b(1), \bdirname\b(3)]" +; + +#include <cmd.h> + +static void l_dirname(register Sfio_t *outfile, register const char *pathname) +{ + register const char *last; + /* go to end of path */ + for(last=pathname; *last; last++); + /* back over trailing '/' */ + while(last>pathname && *--last=='/'); + /* back over non-slash chars */ + for(;last>pathname && *last!='/';last--); + if(last==pathname) + { + /* all '/' or "" */ + if(*pathname!='/') + last = pathname = "."; + } + else + { + /* back over trailing '/' */ + for(;*last=='/' && last > pathname; last--); + } + /* preserve // */ + if(last!=pathname && pathname[0]=='/' && pathname[1]=='/') + { + while(pathname[2]=='/' && pathname<last) + pathname++; + if(last!=pathname && pathname[0]=='/' && pathname[1]=='/' && *astconf("PATH_LEADING_SLASHES",NiL,NiL)!='1') + pathname++; + } + sfwrite(outfile,pathname,last+1-pathname); + sfputc(outfile,'\n'); +} + +int +b_dirname(int argc,register char *argv[], void* context) +{ + register int n; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + argc -= opt_info.index; + if(error_info.errors || argc != 1) + error(ERROR_usage(2),"%s", optusage(NiL)); + l_dirname(sfstdout,argv[0]); + return(0); +} diff --git a/usr/src/lib/libcmd/common/expr.c b/usr/src/lib/libcmd/common/expr.c new file mode 100644 index 0000000000..1baee2435e --- /dev/null +++ b/usr/src/lib/libcmd/common/expr.c @@ -0,0 +1,535 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 + +/* + * expr.c + * Written by David Korn + * Tue Oct 31 08:48:11 EST 1995 + */ + +static const char usage[] = +"[-?\n@(#)$Id: expr (AT&T Research) 2004-05-27 $\n]" +USAGE_LICENSE +"[+NAME?expr - evaluate arguments as an expression]" +"[+DESCRIPTION?\bexpr\b evaluates an expression given as arguments and writes " + "the result to standard output. The character \b0\b will be written " + "to indicate a zero value and nothing will be written to indicate an " + "empty string.]" +"[+?Most of the functionality of \bexpr\b is provided in a more natural " + "way by the shell, \bsh\b(1), and \bexpr\b is provided primarily " + "for backward compatibility.]" +"[+?Terms of the expression must be separate arguments. A string argument is " + "one that can not be identified as an integer. Integer-valued " + "arguments may be preceded by a unary plus or minus sign. Because " + "many of the operators use characters that have special meaning to " + "the shell, they must be quoted when entered from the shell.]" + +"[+?Expressions are formed from the operators listed below in order " + "of increasing precedence within groups. All of the operators are " + "left associative. The symbols \aexpr1\a and \aexpr2\a represent " + "expressions formed from strings and integers and the following " + "operators:]{" + "[+\aexpr1\a \b|\b \aexpr2\a?Returns the evaluation of \aexpr1\a if " + "it is neither null nor 0, otherwise returns the evaluation of expr2.]" + + "[+\aexpr1\a \b&\b \aexpr2\a?Returns the evaluation of \aexpr1\a if " + "neither expression evaluates to null or 0, otherwise returns 0.]" + + "[+\aexpr1\a \aop\a \aexpr2\a?Returns the result of a decimal integer " + "comparison if both arguments are integers; otherwise, returns the " + "result of a string comparison using the locale-specific collation " + "sequence. The result of each comparison will be 1 if the specified " + "relationship is true, or 0 if the relationship is false. \aop\a " + "can be one of the following:]{" + "[+=?Equal.]" + "[+==?Equal.]" + "[+>?Greater than.]" + "[+>=?Greater than or equal to.]" + "[+<?Less than.]" + "[+<=?Less than or equal to.]" + "[+!=?Not equal to.]" + "}" + + "[+\aexpr1\a \aop\a \aexpr2\a?Where \aop\a is \b+\b or \b-\b; " + "addition or subtraction of decimal integer-valued arguments.]" + "[+\aexpr1\a \aop\a \aexpr2\a?Where \aop\a is \b*\b, \b/\b or \b%\b; " + "multiplication, division, or remainder of the decimal " + "integer-valued arguments.]" + "[+\aexpr1\a \b::\b \aexpr2\a?The matching operator : compares " + "\aexpr1\a with \aexpr2\a, which must be a BRE. Normally, " + "the matching operator returns the number of bytes matched " + "and 0 on failure. However, if the pattern contains at " + "least one sub-expression [\\( . . .\\)]], the string " + "corresponding to \\1 will be returned.]" + "[+( \aexpr1\a )?Grouping symbols. An expression can " + "be placed within parenthesis to change precedence.]" + "[+match\b \astring\a \aexpr\a?Equivalent to \astring\a \b:\b " + "\aexpr\a.]" + "[+substr\b \astring\a \apos\a \alength\a?\alength\a character " + "substring of \astring\a starting at \apos\a " + "(counting from 1).]" + "[+index\b \astring\a \achars\a?The position in \astring\a " + "(counting from 1) of the leftmost occurrence of any " + "character in \achars\a.]" + "[+length\b \astring\a?The number of characters in \astring\a.]" + "[+quote\b \atoken\a?Treat \atoken\a as a string operand.]" + "}" +"[+?For backwards compatibility, unrecognized options beginning with " + "a \b-\b will be treated as operands. Portable applications " + "should use \b--\b to indicate end of options.]" + +"\n" +"\n operand ...\n" +"\n" + +"[+EXIT STATUS?]{" + "[+0?The expression is neither null nor 0.]" + "[+1?The expression is null or 0.]" + "[+2?Invalid expressions.]" + "[+>2?An error occurred.]" + "}" +"[+SEE ALSO?\bregcomp\b(5), \bgrep\b(1), \bsh\b(1)]" +; + +#include <cmd.h> +#include <regex.h> + +#define T_ADD 0x100 +#define T_MULT 0x200 +#define T_CMP 0x400 +#define T_FUN 0x800 +#define T_OP 7 +#define T_NUM 1 +#define T_STR 2 + +#define OP_EQ (T_CMP|0) +#define OP_GT (T_CMP|1) +#define OP_LT (T_CMP|2) +#define OP_GE (T_CMP|3) +#define OP_LE (T_CMP|4) +#define OP_NE (T_CMP|5) +#define OP_PLUS (T_ADD|0) +#define OP_MINUS (T_ADD|1) +#define OP_MULT (T_MULT|0) +#define OP_DIV (T_MULT|1) +#define OP_MOD (T_MULT|2) +#define OP_INDEX (T_FUN|0) +#define OP_LENGTH (T_FUN|1) +#define OP_MATCH (T_FUN|2) +#define OP_QUOTE (T_FUN|3) +#define OP_SUBSTR (T_FUN|4) + +#define numeric(np) ((np)->type&T_NUM) + +static const struct Optable_s +{ + const char opname[3]; + int op; +} +optable[] = +{ + "|", '|', + "&", '&', + "=", OP_EQ, + "==", OP_EQ, + ">", OP_GT, + "<", OP_LT, + ">=", OP_GE, + "<=", OP_LE, + "!=", OP_NE, + "+", OP_PLUS, + "-", OP_MINUS, + "*", OP_MULT, + "/", OP_DIV, + "%", OP_MOD, + ":", ':', + "(", '(', + ")", ')' +}; + +typedef struct Node_s +{ + int type; + long num; + char *str; +} Node_t; + +typedef struct State_s +{ + int standard; + char** arglist; + char buf[36]; +} State_t; + +static int expr_or(State_t*, Node_t*); + +static int getnode(State_t* state, Node_t *np) +{ + register char* sp; + register char* cp; + register int i; + register int j; + register int k; + register int tok; + char* ep; + + if (!(cp = *state->arglist++)) + error(ERROR_exit(2), "argument expected"); + if (!state->standard) + switch (cp[0]) + { + case 'i': + if (cp[1] == 'n' && !strcmp(cp, "index")) + { + if (!(cp = *state->arglist++)) + error(ERROR_exit(2), "string argument expected"); + if (!(ep = *state->arglist++)) + error(ERROR_exit(2), "chars argument expected"); + np->num = (ep = strpbrk(cp, ep)) ? (ep - cp + 1) : 0; + np->type = T_NUM; + goto next; + } + break; + case 'l': + if (cp[1] == 'e' && !strcmp(cp, "length")) + { + if (!(cp = *state->arglist++)) + error(ERROR_exit(2), "string argument expected"); + np->num = strlen(cp); + np->type = T_NUM; + goto next; + } + break; + case 'm': + if (cp[1] == 'a' && !strcmp(cp, "match")) + { + if (!(np->str = *state->arglist++)) + error(ERROR_exit(2), "pattern argument expected"); + np->type = T_STR; + return ':'; + } + break; + case 'q': + if (cp[1] == 'u' && !strcmp(cp, "quote") && !(cp = *state->arglist++)) + error(ERROR_exit(2), "string argument expected"); + break; + case 's': + if (cp[1] == 'u' && !strcmp(cp, "substr")) + { + if (!(sp = *state->arglist++)) + error(ERROR_exit(2), "string argument expected"); + if (!(cp = *state->arglist++)) + error(ERROR_exit(2), "position argument expected"); + i = strtol(cp, &ep, 10); + if (*ep || --i <= 0) + i = -1; + if (!(cp = *state->arglist++)) + error(ERROR_exit(2), "length argument expected"); + j = strtol(cp, &ep, 10); + if (*ep) + j = -1; + k = strlen(sp); + if (i < 0 || i >= k || j < 0) + sp = ""; + else + { + sp += i; + k -= i; + if (j < k) + sp[j] = 0; + } + np->type = T_STR; + np->str = sp; + goto next; + } + break; + } + if (*cp=='(' && cp[1]==0) + { + tok = expr_or(state, np); + if (tok != ')') + error(ERROR_exit(2),"closing parenthesis missing"); + } + else + { + np->type = T_STR; + np->str = cp; + if (*cp) + { + np->num = strtol(np->str,&ep,10); + if (!*ep) + np->type |= T_NUM; + } + } + next: + if (!(cp = *state->arglist)) + return 0; + state->arglist++; + for (i=0; i < sizeof(optable)/sizeof(*optable); i++) + if (*cp==optable[i].opname[0] && cp[1]==optable[i].opname[1]) + return optable[i].op; + error(ERROR_exit(2),"%s: unknown operator argument",cp); + return 0; +} + +static int expr_cond(State_t* state, Node_t *np) +{ + register int tok = getnode(state, np); + + while (tok==':') + { + regex_t re; + regmatch_t match[2]; + int n; + Node_t rp; + char *cp; + tok = getnode(state, &rp); + if (np->type&T_STR) + cp = np->str; + else + sfsprintf(cp=state->buf,sizeof(state->buf),"%d",np->num); + np->num = 0; + np->type = T_NUM; + if (n = regcomp(&re, rp.str, REG_LEFT|REG_LENIENT)) + regfatal(&re, ERROR_exit(2), n); + if (!(n = regexec(&re, cp, elementsof(match), match, 0))) + { + if (re.re_nsub > 0) + { + np->type = T_STR; + if (match[1].rm_so >= 0) + { + np->str = cp + match[1].rm_so; + np->str[match[1].rm_eo - match[1].rm_so] = 0; + np->num = strtol(np->str,&cp,10); + if (cp!=np->str && *cp==0) + np->type |= T_NUM; + } + else + np->str = ""; + } + else + np->num = match[0].rm_eo - match[0].rm_so; + } + else if (n != REG_NOMATCH) + regfatal(&re, ERROR_exit(2), n); + else if (re.re_nsub) + { + np->str = ""; + np->type = T_STR; + } + regfree(&re); + } + return tok; +} + +static int expr_mult(State_t* state, Node_t *np) +{ + register int tok = expr_cond(state, np); + + while ((tok&~T_OP)==T_MULT) + { + Node_t rp; + int op = (tok&T_OP); + tok = expr_cond(state, &rp); + if (!numeric(np) || !numeric(&rp)) + error(ERROR_exit(2),"non-numeric argument"); + if (op && rp.num==0) + error(ERROR_exit(2),"division by zero"); + switch(op) + { + case 0: + np->num *= rp.num; + break; + case 1: + np->num /= rp.num; + break; + case 2: + np->num %= rp.num; + } + np->type = T_NUM; + } + return tok; +} + +static int expr_add(State_t* state, Node_t *np) +{ + register int tok = expr_mult(state, np); + + while ((tok&~T_OP)==T_ADD) + { + Node_t rp; + int op = (tok&T_OP); + tok = expr_mult(state, &rp); + if (!numeric(np) || !numeric(&rp)) + error(ERROR_exit(2),"non-numeric argument"); + if (op) + np->num -= rp.num; + else + np->num += rp.num; + np->type = T_NUM; + } + return tok; +} + +static int expr_cmp(State_t* state, Node_t *np) +{ + register int tok = expr_add(state, np); + + while ((tok&~T_OP)==T_CMP) + { + Node_t rp; + register char *left,*right; + char buff1[36],buff2[36]; + int op = (tok&T_OP); + tok = expr_add(state, &rp); + if (numeric(&rp) && numeric(np)) + op |= 010; + else + { + if (np->type&T_STR) + left = np->str; + else + sfsprintf(left=buff1,sizeof(buff1),"%d",np->num); + if (rp.type&T_STR) + right = rp.str; + else + sfsprintf(right=buff2,sizeof(buff2),"%d",rp.num); + } + switch(op) + { + case 0: + np->num = streq(left,right); + break; + case 1: + np->num = (strcoll(left,right)>0); + break; + case 2: + np->num = (strcoll(left,right)<0); + break; + case 3: + np->num = (strcoll(left,right)>=0); + break; + case 4: + np->num = (strcoll(left,right)<=0); + break; + case 5: + np->num = !streq(left,right); + break; + case 010: + np->num = (np->num==rp.num); + break; + case 011: + np->num = (np->num>rp.num); + break; + case 012: + np->num = (np->num<rp.num); + break; + case 013: + np->num = (np->num>=rp.num); + break; + case 014: + np->num = (np->num<=rp.num); + break; + case 015: + np->num = (np->num!=rp.num); + break; + } + np->type = T_NUM; + } + return tok; +} + +static int expr_and(State_t* state, Node_t *np) +{ + register int tok = expr_cmp(state, np); + while (tok=='&') + { + Node_t rp; + tok = expr_cmp(state, &rp); + if ((numeric(&rp) && rp.num==0) || *rp.str==0) + { + np->num = 0; + np->type=T_NUM; + } + } + return tok; +} + +static int expr_or(State_t* state, Node_t *np) +{ + register int tok = expr_and(state, np); + while (tok=='|') + { + Node_t rp; + tok = expr_and(state, &rp); + if ((numeric(np) && np->num==0) || *np->str==0) + *np = rp; + } + return tok; +} + +int +b_expr(int argc, char *argv[], void *context) +{ + State_t state; + Node_t node; + int n; + + cmdinit(argc, argv,context, ERROR_CATALOG, 0); + state.standard = !strcmp(astconf("CONFORMANCE", NiL, NiL), "standard"); +#if 0 + if (state.standard) + state.arglist = argv+1; + else +#endif + { + while (n=optget(argv, usage)) + { + /* + * NOTE: this loop ignores all but literal -- and -? + * out of kindness for obsolescent usage + * (and is ok with the standard) but strict + * getopt conformance would give usage for all + * unknown - options + */ + if(n=='?') + error(ERROR_usage(2), "%s", opt_info.arg); + if (opt_info.option[1] != '?') + break; + error(ERROR_usage(2), "%s", opt_info.arg); + } + if (error_info.errors) + error(ERROR_usage(2),"%s",optusage((char*)0)); + state.arglist = argv+opt_info.index; + } + if (expr_or(&state, &node)) + error(ERROR_exit(2),"syntax error"); + if (node.type&T_STR) + { + if (*node.str) + sfprintf(sfstdout,"%s\n",node.str); + } + else + sfprintf(sfstdout,"%d\n",node.num); + return numeric(&node)?node.num==0:*node.str==0; +} diff --git a/usr/src/lib/libcmd/common/fds.c b/usr/src/lib/libcmd/common/fds.c new file mode 100644 index 0000000000..b7e9fb2e88 --- /dev/null +++ b/usr/src/lib/libcmd/common/fds.c @@ -0,0 +1,168 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 + +static const char usage[] = +"[-?\n@(#)$Id: fds (AT&T Research) 2006-10-26 $\n]" +USAGE_LICENSE +"[+NAME?fds - list open file descriptor status]" +"[+DESCRIPTION?\bfds\b lists the status for each open file descriptor. " + "When invoked as a shell builtin it accesses the file descriptors of the " + "calling shell, otherwise it lists the file descriptors passed across " + "\bexec\b(2).]" +"[l:long?List file descriptor details.]" +"[+SEE ALSO?\blogname\b(1), \bwho\b(1), \bgetgroups\b(2)]" +; + +#include <cmd.h> +#include <ls.h> + +#include "FEATURE/sockets" + +#if defined(S_IFSOCK) && _sys_socket && _hdr_arpa_inet && _hdr_netinet_in && _lib_getsockname && _lib_getsockopt && _lib_inet_ntoa +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#else +#undef S_IFSOCK +#endif + +#ifndef minor +#define minor(x) (int)((x)&0xff) +#endif +#ifndef major +#define major(x) (int)(((unsigned int)(x)>>8)&0xff) +#endif + +int +b_fds(int argc, char** argv, void* context) +{ + register char* s; + register int i; + register char* m; + register char* x; + int flags; + int details; + struct stat st; +#ifdef S_IFSOCK + struct sockaddr_in addr; + socklen_t len; + int type; + int prot; + char num[32]; +#endif + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + details = 0; + for (;;) + { + switch (optget(argv, usage)) + { + case 'l': + details = opt_info.num; + 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)); + for (i = 0; i <= OPEN_MAX; i++) + if (fstat(i, &st)) + /* not open */; + else if (details) + { + if ((flags = fcntl(i, F_GETFL, (char*)0)) == -1) + m = "--"; + else + switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) + { + case O_RDONLY: + m = "r-"; + break; + case O_WRONLY: + m = "-w"; + break; + case O_RDWR: + m = "rw"; + break; + default: + m = "??"; + break; + } + x = (fcntl(i, F_GETFD, (char*)0) > 0) ? "x" : "-"; + if (isatty(i) && (s = ttyname(i))) + sfprintf(sfstdout, "%02d %s%s %s %s\n", i, m, x, fmtmode(st.st_mode, 0), s); +#ifdef S_IFSOCK + else if ((len = sizeof(addr)) + && !getsockname(i, (struct sockaddr*)&addr, (void*)&len) + && len == sizeof(addr) + && addr.sin_family == AF_INET +#ifdef SO_TYPE + && (len = sizeof(type)) + && !getsockopt(i, SOL_SOCKET, SO_TYPE, (void*)&type, (void*)&len) + && len == sizeof(type) +#else + && !(type = 0) +#endif +#ifdef SO_PROTOTYPE + && (len = sizeof(prot)) + && (!getsockopt(i, SOL_SOCKET, SO_PROTOTYPE, (void*)&prot, (void*)&len) || !(prot = 0)) +#else + && !(prot = 0) +#endif + ) + { + if (!st.st_mode) + st.st_mode = S_IFSOCK|S_IRUSR|S_IWUSR; + s = 0; + switch (type) + { + case SOCK_DGRAM: + s = "udp"; + break; + case SOCK_STREAM: + if (prot == 0) + s = "tcp"; +#ifdef IPPROTO_SCTP + else if (prot == IPPROTO_SCTP) + s = "sctp"; +#endif + break; + } + if (!s) + sfprintf(sfstdout, s = num, "type.%d.prot.%d", type, prot); + sfprintf(sfstdout, "%02d %s%s %s /dev/%s/%s/%d\n", i, m, x, fmtmode(st.st_mode, 0), s, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); + } +#endif + else + sfprintf(sfstdout, "%02d %s%s %s /dev/inode/%u/%u\n", i, m, x, fmtmode(st.st_mode, 0), st.st_dev, st.st_ino); + } + else + sfprintf(sfstdout, "%d\n", i); + return 0; +} diff --git a/usr/src/lib/libcmd/common/features/ids b/usr/src/lib/libcmd/common/features/ids new file mode 100644 index 0000000000..04be5755cf --- /dev/null +++ b/usr/src/lib/libcmd/common/features/ids @@ -0,0 +1,8 @@ +lib endgrent,getgrent,setgrent +lib fsid,getfsgid,isfsg sys/types.h sys/fss.h fsg.h -lfsg +mac fsid sys/types.h sys/fss.h fsg.h +cat{ + #if !_lib_fsid && _mac_fsid + #define _lib_fsid 1 + #endif +}end diff --git a/usr/src/lib/libcmd/common/features/sockets b/usr/src/lib/libcmd/common/features/sockets new file mode 100644 index 0000000000..2660abb8df --- /dev/null +++ b/usr/src/lib/libcmd/common/features/sockets @@ -0,0 +1,3 @@ +sys socket +hdr arpa.inet,netinet.in +lib getsockname,getsockopt,inet_ntoa diff --git a/usr/src/lib/libcmd/common/features/symlink b/usr/src/lib/libcmd/common/features/symlink new file mode 100644 index 0000000000..a7d86b09f5 --- /dev/null +++ b/usr/src/lib/libcmd/common/features/symlink @@ -0,0 +1,21 @@ +lib lchmod note{ lchmod implemented }end execute{ + #include <unistd.h> + #include <errno.h> + int + main() + { + lchmod("No-FiLe", 0); + return errno != ENOENT; + } +}end + +lib lchown note{ lchown implemented }end execute{ + #include <unistd.h> + #include <errno.h> + int + main() + { + lchown("No-FiLe", 0, 0); + return errno != ENOENT; + } +}end diff --git a/usr/src/lib/libcmd/common/features/utsname b/usr/src/lib/libcmd/common/features/utsname new file mode 100644 index 0000000000..5b746ce7ad --- /dev/null +++ b/usr/src/lib/libcmd/common/features/utsname @@ -0,0 +1,12 @@ +lib getdomainname,gethostid,gethostname,sethostname,syscall,systeminfo,uname +lib syssgi +mem utsname.base_rel,utsname.idnumber,utsname.m_type,utsname.nodeext sys/utsname.h +sys syscall,systeminfo,syssgi + +tst cross{ + u=`/bin/uname -o 2>/dev/null` + case $u in + '') ;; + *) echo "#define _UNAME_os_DEFAULT \"$u\" /* default os name */" ;; + esac +}end diff --git a/usr/src/lib/libcmd/common/fmt.c b/usr/src/lib/libcmd/common/fmt.c new file mode 100644 index 0000000000..2cbcc9c8ea --- /dev/null +++ b/usr/src/lib/libcmd/common/fmt.c @@ -0,0 +1,632 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 + +static const char usage[] = +"[-?\n@(#)$Id: fmt (AT&T Research) 2007-01-02 $\n]" +USAGE_LICENSE +"[+NAME?fmt - simple text formatter]" +"[+DESCRIPTION?\bfmt\b reads the input files and left justifies space " + "separated words into lines \awidth\a characters or less in length and " + "writes the lines to the standard output. The standard input is read if " + "\b-\b or no files are specified. Blank lines and interword spacing are " + "preserved in the output. Indentation is preserved, and lines with " + "identical indentation are joined and justified.]" +"[+?\bfmt\b is meant to format mail messages prior to sending, but may " + "also be useful for other simple tasks. For example, in \bvi\b(1) the " + "command \b:!}fmt\b will justify the lines in the current paragraph.]" +"[c:crown-margin?Preserve the indentation of the first two lines within " + "a paragraph, and align the left margin of each subsequent line with " + "that of the second line.]" +"[o:optget?Format concatenated \boptget\b(3) usage strings.]" +"[s:split-only?Split lines only; do not join short lines to form longer " + "ones.]" +"[u:uniform-spacing?One space between words, two after sentences.]" +"[w:width?Set the output line width to \acolumns\a.]#[columns:=72]" + "\n\n" +"[ file ... ]" + "\n\n" +"[+SEE ALSO?\bmailx\b(1), \bnroff\b(1), \btroff\b(1), \bvi\b(1), " + "\boptget\b(3)]" +; + +#include <cmd.h> +#include <ctype.h> + +typedef struct Fmt_s +{ + long flags; + char* outp; + char* outbuf; + char* endbuf; + Sfio_t* in; + Sfio_t* out; + int indent; + int nextdent; + int nwords; + int prefix; + int quote; + int retain; + int section; +} Fmt_t; + +#define INDENT 4 +#define TABSZ 8 + +#define isoption(fp,c) ((fp)->flags&(1L<<((c)-'a'))) +#define setoption(fp,c) ((fp)->flags|=(1L<<((c)-'a'))) +#define clroption(fp,c) ((fp)->flags&=~(1L<<((c)-'a'))) + +static void +outline(Fmt_t* fp) +{ + register char* cp = fp->outbuf; + int n = 0; + int c; + int d; + + if (!fp->outp) + return; + while (fp->outp[-1] == ' ') + fp->outp--; + *fp->outp = 0; + while (*cp++ == ' ') + n++; + if (n >= TABSZ) + { + n /= TABSZ; + cp = &fp->outbuf[TABSZ*n]; + while (n--) + *--cp = '\t'; + } + else + cp = fp->outbuf; + fp->nwords = 0; + if (!isoption(fp, 'o')) + sfputr(fp->out, cp, '\n'); + else if (*cp) + { + n = fp->indent; + if (*cp != '[') + { + if (*cp == ' ') + cp++; + n += INDENT; + } + while (n--) + sfputc(fp->out, ' '); + if (fp->quote) + { + if ((d = (fp->outp - cp)) <= 0) + c = 0; + else if ((c = fp->outp[-1]) == 'n' && d > 1 && fp->outp[-2] == '\\') + c = '}'; + sfprintf(fp->out, "\"%s%s\"\n", cp, c == ']' || c == '{' || c == '}' ? "" : " "); + } + else + sfputr(fp->out, cp, '\n'); + if (fp->nextdent) + { + fp->indent += fp->nextdent; + fp->endbuf -= fp->nextdent; + fp->nextdent = 0; + } + } + fp->outp = 0; +} + +static void +split(Fmt_t* fp, char* buf, int splice) +{ + register char* cp; + register char* ep; + register char* qp; + register int c = 1; + register int q = 0; + register int n; + int prefix; + + for (ep = buf; *ep == ' '; ep++); + prefix = ep - buf; + + /* + * preserve blank lines + */ + + if ((*ep == 0 || *buf == '.') && !isoption(fp, 'o')) + { + if (*ep) + prefix = strlen(buf); + outline(fp); + strcpy(fp->outbuf, buf); + fp->outp = fp->outbuf+prefix; + outline(fp); + return; + } + if (fp->prefix < prefix && !isoption(fp, 'c')) + outline(fp); + if (!fp->outp || prefix < fp->prefix) + fp->prefix = prefix; + while (c) + { + cp = ep; + while (*ep == ' ') + ep++; + if (cp != ep && isoption(fp, 'u')) + cp = ep-1; + while (c = *ep) + { + if (c == ' ') + break; + ep++; + + /* + * skip over \space + */ + + if (c == '\\' && *ep) + ep++; + } + n = (ep-cp); + if (n && isoption(fp, 'o')) + { + for (qp = cp; qp < ep; qp++) + if (*qp == '\\') + qp++; + else if (*qp == '"') + q = !q; + if (*(ep-1) == '"') + goto skip; + } + if (fp->nwords > 0 && &fp->outp[n] >= fp->endbuf && !fp->retain && !q) + outline(fp); + skip: + if (fp->nwords == 0) + { + if (fp->prefix) + memset(fp->outbuf, ' ', fp->prefix); + fp->outp = &fp->outbuf[fp->prefix]; + while (*cp == ' ') + cp++; + n = (ep-cp); + } + memcpy(fp->outp, cp, n); + fp->outp += n; + fp->nwords++; + } + if (isoption(fp, 's') || *buf == 0) + outline(fp); + else if (fp->outp) + { + /* + * two spaces at ends of sentences + */ + + if (!isoption(fp, 'o') && strchr(".:!?", fp->outp[-1])) + *fp->outp++ = ' '; + if (!splice && !fp->retain && (!fp->quote || (fp->outp - fp->outbuf) < 2 || fp->outp[-2] != '\\' || fp->outp[-1] != 'n' && fp->outp[-1] != 't' && fp->outp[-1] != ' ')) + *fp->outp++ = ' '; + } +} + +static int +dofmt(Fmt_t* fp) +{ + register int c; + int b; + int x; + int splice; + char* cp; + char* dp; + char* ep; + char* lp; + char* tp; + char buf[8192]; + + cp = 0; + while (cp || (cp = sfgetr(fp->in, '\n', 0)) && !(splice = 0) && (lp = cp + sfvalue(fp->in) - 1) || (cp = sfgetr(fp->in, '\n', SF_LASTR)) && (splice = 1) && (lp = cp + sfvalue(fp->in))) + { + if (isoption(fp, 'o')) + { + if (!isoption(fp, 'i')) + { + setoption(fp, 'i'); + b = 0; + while (cp < lp) + { + if (*cp == ' ') + b += 1; + else if (*cp == '\t') + b += INDENT; + else + break; + cp++; + } + fp->indent = roundof(b, INDENT); + } + else + while (cp < lp && (*cp == ' ' || *cp == '\t')) + cp++; + if (!isoption(fp, 'q') && cp < lp) + { + setoption(fp, 'q'); + if (*cp == '"') + { + ep = lp; + while (--ep > cp) + if (*ep == '"') + { + fp->quote = 1; + break; + } + else if (*ep != ' ' && *ep != '\t') + break; + } + } + } + again: + dp = buf; + ep = 0; + for (b = 1;; b = 0) + { + if (cp >= lp) + { + cp = 0; + break; + } + c = *cp++; + if (isoption(fp, 'o')) + { + if (c == '\\') + { + x = 0; + c = ' '; + cp--; + while (cp < lp) + { + if (*cp == '\\') + { + cp++; + if ((lp - cp) < 1) + { + c = '\\'; + break; + } + if (*cp == 'n') + { + cp++; + c = '\n'; + if ((lp - cp) > 2) + { + if (*cp == ']' || *cp == '@' && *(cp + 1) == '(') + { + *dp++ = '\\'; + *dp++ = 'n'; + c = *cp++; + break; + } + if (*cp == '\\' && *(cp + 1) == 'n') + { + cp += 2; + *dp++ = '\n'; + break; + } + } + } + else if (*cp == 't' || *cp == ' ') + { + cp++; + x = 1; + c = ' '; + } + else + { + if (x && dp != buf && *(dp - 1) != ' ') + *dp++ = ' '; + *dp++ = '\\'; + c = *cp++; + break; + } + } + else if (*cp == ' ' || *cp == '\t') + { + cp++; + c = ' '; + x = 1; + } + else + { + if (x && c != '\n' && dp != buf && *(dp - 1) != ' ') + *dp++ = ' '; + break; + } + } + if (c == '\n') + { + c = 0; + goto flush; + } + if (c == ' ' && (dp == buf || *(dp - 1) == ' ')) + continue; + } + else if (c == '"') + { + if (b || cp >= lp) + { + if (fp->quote) + continue; + fp->section = 0; + } + } + else if (c == '\a') + { + *dp++ = '\\'; + c = 'a'; + } + else if (c == '\b') + { + *dp++ = '\\'; + c = 'b'; + } + else if (c == '\f') + { + *dp++ = '\\'; + c = 'f'; + } + else if (c == '\v') + { + *dp++ = '\\'; + c = 'v'; + } + else if (c == ']' && (cp >= lp || *cp != ':' && *cp != '#' && *cp != '!')) + { + if (cp < lp && *cp == ']') + { + cp++; + *dp++ = c; + } + else + { + fp->section = 1; + fp->retain = 0; + flush: + *dp++ = c; + *dp = 0; + split(fp, buf, 0); + outline(fp); + goto again; + } + } + else if (fp->section) + { + if (c == '[') + { + if (b) + fp->retain = 1; + else + { + cp--; + c = 0; + goto flush; + } + fp->section = 0; + } + else if (c == '{') + { + x = 1; + for (tp = cp; tp < lp; tp++) + { + if (*tp == '[' || *tp == '\n') + break; + if (*tp == ' ' || *tp == '\t' || *tp == '"') + continue; + if (*tp == '\\' && (lp - tp) > 1) + { + if (*++tp == 'n') + break; + if (*tp == 't' || *tp == '\n') + continue; + } + x = 0; + break; + } + if (x) + { + if (fp->endbuf > (fp->outbuf + fp->indent + 2*INDENT)) + fp->nextdent = 2*INDENT; + goto flush; + } + else + fp->section = 0; + } + else if (c == '}') + { + if (fp->indent && (b || *(cp - 2) != 'f')) + { + if (b) + { + fp->indent -= 2*INDENT; + fp->endbuf += 2*INDENT; + } + else + { + cp--; + c = 0; + } + goto flush; + } + else + fp->section = 0; + } + else if (c == ' ' || c == '\t') + continue; + else + fp->section = 0; + } + else if (c == '?' && (cp >= lp || *cp != '?')) + { + if (fp->retain) + { + cp--; + while (cp < lp && *cp != ' ' && *cp != '\t' && *cp != ']' && dp < &buf[sizeof(buf)-3]) + *dp++ = *cp++; + if (cp < lp && (*cp == ' ' || *cp == '\t')) + *dp++ = *cp++; + *dp = 0; + split(fp, buf, 0); + dp = buf; + ep = 0; + fp->retain = 0; + if (fp->outp >= fp->endbuf) + outline(fp); + continue; + } + } + else if (c == ' ' || c == '\t') + for (c = ' '; *cp == ' ' || *cp == '\t'; cp++); + } + else if (c == '\b') + { + if (dp > buf) + { + dp--; + if (ep) + ep--; + } + continue; + } + else if (c == '\t') + { + /* + * expand tabs + */ + + if (!ep) + ep = dp; + c = isoption(fp, 'o') ? 1 : TABSZ - (dp - buf) % TABSZ; + if (dp >= &buf[sizeof(buf) - c - 3]) + { + cp--; + break; + } + while (c-- > 0) + *dp++ = ' '; + continue; + } + else if (!isprint(c)) + continue; + if (dp >= &buf[sizeof(buf) - 3]) + { + tp = dp; + while (--tp > buf) + if (isspace(*tp)) + { + cp -= dp - tp; + dp = tp; + break; + } + ep = 0; + break; + } + if (c != ' ') + ep = 0; + else if (!ep) + ep = dp; + *dp++ = c; + } + if (ep) + *ep = 0; + else + *dp = 0; + split(fp, buf, splice); + } + return 0; +} + +int +b_fmt(int argc, char** argv, void *context) +{ + register int n; + char* cp; + Fmt_t fmt; + char outbuf[8 * 1024]; + + fmt.flags = 0; + fmt.out = sfstdout; + fmt.outbuf = outbuf; + fmt.outp = 0; + fmt.endbuf = &outbuf[72]; + fmt.indent = 0; + fmt.nextdent = 0; + fmt.nwords = 0; + fmt.prefix = 0; + fmt.quote = 0; + fmt.retain = 0; + fmt.section = 1; + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) + switch (n) + { + case 'c': + case 'o': + case 's': + case 'u': + setoption(&fmt, n); + break; + case 'w': + if (opt_info.num < TABSZ || opt_info.num>= sizeof(outbuf)) + error(2, "width out of range"); + fmt.endbuf = &outbuf[opt_info.num]; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if (error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (isoption(&fmt, 'o')) + setoption(&fmt, 'c'); + if (isoption(&fmt, 's')) + clroption(&fmt, 'u'); + if (cp = *argv) + argv++; + do { + if (!cp || streq(cp, "-")) + fmt.in = sfstdin; + else if (!(fmt.in = sfopen(NiL, cp, "r"))) + { + error(ERROR_system(0), "%s: cannot open", cp); + error_info.errors = 1; + continue; + } + dofmt(&fmt); + if (fmt.in != sfstdin) + sfclose(fmt.in); + } while (cp = *argv++); + outline(&fmt); + if (sfsync(sfstdout)) + error(ERROR_system(0), "write error"); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/fold.c b/usr/src/lib/libcmd/common/fold.c new file mode 100644 index 0000000000..459ad35850 --- /dev/null +++ b/usr/src/lib/libcmd/common/fold.c @@ -0,0 +1,242 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * fold + */ + +static const char usage[] = +"[-?\n@(#)$Id: fold (AT&T Research) 2004-11-18 $\n]" +USAGE_LICENSE +"[+NAME?fold - fold lines]" +"[+DESCRIPTION?\bfold\b is a filter that folds lines from its input, " + "breaking the lines to have a maximum of \awidth\a column " + "positions (or bytes if the \b-b\b option is specified). Lines " + "are broken by the insertion of a newline character such that " + "each output line is the maximum width possible that does not " + "exceed the specified number of column positions, (or bytes). A line " + "will not be broken in the middle of a character.] " +"[+?Unless the \b-b\b option is specified, the following will be treated " + "specially:]{" + "[+carriage-return?The current count of line width will be set " + "to zero. \bfold\b will not insert a newline immediately " + "before or after a carriage-return.]" + "[+backspace?If positive, the current count of line width will be " + "decremented by one. \bfold\b will not insert a newline " + "immediately before or after a backspace.]" + "[+tab?Each tab character encountered will advance the column " + "position to the next tab stop. Tab stops are at each " + "column position \an\a, where \an\a modulo 8 equals 1.]" + "}" +"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bfold\b " + "reads from standard input. The start of the file is defined " + "as the current offset.]" + +"[b:bytes?Count bytes rather than columns so that each carriage-return, " + "backspace, and tab counts as 1.]" +"[c:continue?Emit \atext\a at line splits.]:[text:='\\n']" +"[d:delimiter?Break at \adelim\a boundaries.]:[delim]" +"[s:spaces?Break at word boundaries. If the line contains any blanks, " + "(spaces or tabs), within the first \awidth\a column positions or " + "bytes, the line is broken after the last blank meeting the " + "\awidth\a constraint.]" +"[w:width]#[width:=80?Use a maximum line length of \awidth\a columns " + "instead of the default.]" +"\n" +"\n[file ...]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files processed successfully.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bpaste\b(1)]" +; + + +#include <cmd.h> + +#define WIDTH 80 +#define TABSIZE 8 + +#define T_EOF 1 +#define T_NL 2 +#define T_BS 3 +#define T_TAB 4 +#define T_SP 5 +#define T_RET 6 + +static void fold(Sfio_t *in, Sfio_t *out, register int width, const char *cont, size_t contsize, char *cols) +{ + register char *cp, *first; + register int n, col=0, x=0; + register char *last_space=0; + cols[0] = 0; + for (;;) + { + if (!(cp = sfgetr(in,'\n',0))) + { + if (!(cp = sfgetr(in,'\n',-1)) || (n = sfvalue(in)) <= 0) + break; + x = cp[--n]; + cp[n] = '\n'; + } + /* special case -b since no column adjustment is needed */ + if(cols['\b']==0 && (n=sfvalue(in))<=width) + { + sfwrite(out,cp,n); + continue; + } + first = cp; + col = 0; + last_space = 0; + for(;;) + { + while((n=cols[*(unsigned char*)cp++])==0); + while((cp-first) > (width-col)) + { + if(last_space) + col = last_space - first; + else + col = width-col; + sfwrite(out,first,col); + first += col; + col = 0; + last_space = 0; + if(cp>first+1 || (n!=T_NL && n!=T_BS)) + sfwrite(out, cont, contsize); + } + switch(n) + { + case T_NL: + if(x) + *(cp-1) = x; + break; + case T_RET: + col = 0; + continue; + case T_BS: + if((cp+(--col)-first)>0) + col--; + continue; + case T_TAB: + n = (TABSIZE-1) - (cp+col-1-first)&(TABSIZE-1); + col +=n; + if((cp-first) > (width-col)) + { + sfwrite(out,first,(--cp)-first); + sfwrite(out, cont, contsize); + first = cp; + col = TABSIZE-1; + last_space = 0; + continue; + } + if(cols[' ']) + last_space = cp; + continue; + case T_SP: + last_space = cp; + continue; + default: + continue; + } + break; + } + sfwrite(out,first,cp-first); + } +} + +int +b_fold(int argc, char *argv[], void* context) +{ + register int n, width=WIDTH; + register Sfio_t *fp; + register char *cp; + char *cont="\n"; + size_t contsize = 1; + char cols[1<<CHAR_BIT]; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + memset(cols, 0, sizeof(cols)); + cols['\t'] = T_TAB; + cols['\b'] = T_BS; + cols['\n'] = T_NL; + cols['\r'] = T_RET; + for (;;) + { + switch (optget(argv, usage)) + { + case 0: + break; + case 'b': + cols['\r'] = cols['\b'] = 0; + cols['\t'] = cols[' ']; + continue; + case 'c': + contsize = stresc(cont = strdup(opt_info.arg)); + continue; + case 'd': + if (n = *opt_info.arg) + cols[n] = T_SP; + continue; + case 's': + cols[' '] = T_SP; + if(cols['\t']==0) + cols['\t'] = T_SP; + continue; + case 'w': + if ((width = opt_info.num) <= 0) + error(2, "%d: width must be positive", opt_info.num); + continue; + case ':': + error(2, "%s", opt_info.arg); + continue; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + continue; + } + break; + } + argv += opt_info.index; + argc -= opt_info.index; + if(error_info.errors) + error(ERROR_usage(2),"%s", optusage(NiL)); + if(cp = *argv) + argv++; + do + { + if(!cp || streq(cp,"-")) + fp = sfstdin; + else if(!(fp = sfopen(NiL,cp,"r"))) + { + error(ERROR_system(0),"%s: cannot open",cp); + error_info.errors = 1; + continue; + } + fold(fp,sfstdout,width,cont,contsize,cols); + if(fp!=sfstdin) + sfclose(fp); + } + while(cp= *argv++); + return(error_info.errors); +} diff --git a/usr/src/lib/libcmd/common/getconf.c b/usr/src/lib/libcmd/common/getconf.c new file mode 100644 index 0000000000..d30d002c0b --- /dev/null +++ b/usr/src/lib/libcmd/common/getconf.c @@ -0,0 +1,393 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * getconf - get configuration values + */ + +static const char usage[] = +"[-?\n@(#)$Id: getconf (AT&T Research) 2007-02-07 $\n]" +USAGE_LICENSE +"[+NAME?getconf - get configuration values]" +"[+DESCRIPTION?\bgetconf\b displays the system configuration value for" +" \aname\a. If \aname\a is a filesystem specific variable then" +" the value is determined relative to \apath\a or the current" +" directory if \apath\a is omitted. If \avalue\a is specified then" +" \bgetconf\b attempts to change the process local value to \avalue\a." +" \b-\b may be used in place of \apath\a when it is not relevant." +" Only \bwritable\b variables may be set; \breadonly\b variables" +" cannot be changed.]" +"[+?The current value for \aname\a is written to the standard output. If" +" \aname\a is valid but undefined then \bundefined\b is written to" +" the standard output. If \aname\a is invalid or an error occurs in" +" determining its value, then a diagnostic written to the standard error" +" and \bgetconf\b exits with a non-zero exit status.]" +"[+?More than one variable may be set or queried by providing the \aname\a" +" \apath\a \avalue\a 3-tuple for each variable, specifying \b-\b for" +" \avalue\a when querying.]" +"[+?If no operands are specified then all known variables are written in" +" \aname\a=\avalue\a form to the standard output, one per line." +" Only one of \b--call\b, \b--name\b or \b--standard\b may be specified.]" +"[+?This implementation uses the \bastgetconf\b(3) string interface to the native" +" \bsysconf\b(2), \bconfstr\b(2), \bpathconf\b(2), and \bsysinfo\b(2)" +" system calls. If \bgetconf\b on \b$PATH\b is not the default native" +" \bgetconf\b, named by \b$(getconf GETCONF)\b, then \bastgetconf\b(3)" +" checks only \bast\b specific extensions and the native system calls;" +" invalid options and/or names not supported by \bastgetconf\b(3) cause" +" the \bgetconf\b on \b$PATH\b to be executed.]" + +"[a:all?Call the native \bgetconf\b(1) with option \b-a\b.]" +"[b:base?List base variable name sans call and standard prefixes.]" +"[c:call?Display variables with call prefix that matches \aRE\a. The call" +" prefixes are:]:[RE]{" +" [+CS?\bconfstr\b(2)]" +" [+PC?\bpathconf\b(2)]" +" [+SC?\bsysconf\b(2)]" +" [+SI?\bsysinfo\b(2)]" +" [+XX?Constant value.]" +"}" +"[d:defined?Only display defined values when no operands are specified.]" +"[l:lowercase?List variable names in lower case.]" +"[n:name?Display variables with name that match \aRE\a.]:[RE]" +"[p:portable?Display the named \bwritable\b variables and values in a form that" +" can be directly executed by \bsh\b(1) to set the values. If \aname\a" +" is omitted then all \bwritable\b variables are listed.]" +"[q:quote?\"...\" quote values.]" +"[r:readonly?Display the named \breadonly\b variables in \aname\a=\avalue\a form." +" If \aname\a is omitted then all \breadonly\b variables are listed.]" +"[s:standard?Display variables with standard prefix that matches \aRE\a." +" Use the \b--table\b option to view all standard prefixes, including" +" local additions. The standard prefixes available on all systems" +" are:]:[RE]{" +" [+AES]" +" [+AST]" +" [+C]" +" [+GNU]" +" [+POSIX]" +" [+SVID]" +" [+XBS5]" +" [+XOPEN]" +" [+XPG]" +"}" +"[t:table?Display the internal table that contains the name, standard," +" standard section, and system call symbol prefix for each variable.]" +"[w:writable?Display the named \bwritable\b variables in \aname\a=\avalue\a" +" form. If \aname\a is omitted then all \bwritable\b variables are" +" listed.]" +"[v:specification?Call the native \bgetconf\b(1) with option" +" \b-v\b \aname\a.]:[name]" + +"\n" +"\n[ name [ path [ value ] ] ... ]\n" +"\n" + +"[+ENVIRONMENT]{" +" [+_AST_FEATURES?Process local writable values that are different from" +" the default are stored in the \b_AST_FEATURES\b environment" +" variable. The \b_AST_FEATURES\b value is a space-separated" +" list of \aname\a \apath\a \avalue\a 3-tuples, where" +" \aname\a is the system configuration name, \apath\a is the" +" corresponding path, \b-\b if no path is applicable, and" +" \avalue\a is the system configuration value.]" +"}" +"[+SEE ALSO?\bpathchk\b(1), \bconfstr\b(2), \bpathconf\b(2)," +" \bsysconf\b(2), \bastgetconf\b(3)]" +; + +#include <cmd.h> +#include <proc.h> +#include <ls.h> + +typedef struct Path_s +{ + char* path; + int len; +} Path_t; + +int +b_getconf(int argc, char** argv, void* context) +{ + register char* name; + register char* path; + register char* value; + register char* s; + register char* t; + char* pattern; + char* native; + char* cmd; + Path_t* e; + Path_t* p; + int flags; + int n; + int i; + int m; + int q; + char** oargv; + char buf[PATH_MAX]; + Path_t std[64]; + struct stat st0; + struct stat st1; + + static const char empty[] = "-"; + static const Path_t equiv[] = { { "/bin", 4 }, { "/usr/bin", 8 } }; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + oargv = argv; + if (*(native = astconf("GETCONF", NiL, NiL)) != '/') + native = 0; + flags = 0; + name = 0; + pattern = 0; + for (;;) + { + switch (optget(argv, usage)) + { + case 'a': + if (native) + goto defer; + continue; + case 'b': + flags |= ASTCONF_base; + continue; + case 'c': + flags |= ASTCONF_matchcall; + pattern = opt_info.arg; + continue; + case 'd': + flags |= ASTCONF_defined; + continue; + case 'l': + flags |= ASTCONF_lower; + continue; + case 'n': + flags |= ASTCONF_matchname; + pattern = opt_info.arg; + continue; + case 'p': + flags |= ASTCONF_parse; + continue; + case 'q': + flags |= ASTCONF_quote; + continue; + case 'r': + flags |= ASTCONF_read; + continue; + case 's': + flags |= ASTCONF_matchstandard; + pattern = opt_info.arg; + continue; + case 't': + flags |= ASTCONF_table; + continue; + case 'v': + if (native) + goto defer; + continue; + case 'w': + flags |= ASTCONF_write; + continue; + case ':': + if (native) + goto defer; + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (!(name = *argv)) + path = 0; + else if (streq(name, empty)) + { + name = 0; + if (path = *++argv) + { + argv++; + if (streq(path, empty)) + path = 0; + } + } + if (error_info.errors || !name && *argv) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (!name) + astconflist(sfstdout, path, flags, pattern); + else + { + flags = native ? (ASTCONF_system|ASTCONF_error) : 0; + do + { + if (!(path = *++argv)) + value = 0; + else + { + if (streq(path, empty)) + { + path = 0; + flags = 0; + } + if ((value = *++argv) && (streq(value, empty))) + { + value = 0; + flags = 0; + } + } + s = astgetconf(name, path, value, flags, errorf); + if (error_info.errors) + break; + if (!s) + goto defer; + if (!value) + { + if (flags & ASTCONF_write) + { + sfputr(sfstdout, name, ' '); + sfputr(sfstdout, path ? path : empty, ' '); + } + sfputr(sfstdout, s, '\n'); + } + } while (*argv && (name = *++argv)); + } + return error_info.errors != 0; + + defer: + + /* + * defer to argv[0] if absolute and it exists + */ + + if ((cmd = oargv[0]) && *cmd == '/' && !access(cmd, X_OK)) + goto found; + + /* + * defer to the first getconf on $PATH that is also on the standard PATH + */ + + e = std; + s = astconf("PATH", NiL, NiL); + q = !stat(equiv[0].path, &st0) && !stat(equiv[1].path, &st1) && st0.st_ino == st1.st_ino && st0.st_dev == st1.st_dev; + m = 0; + do + { + for (t = s; *s && *s != ':'; s++); + if ((n = s - t) && *t == '/') + { + if (q) + for (i = 0; i < 2; i++) + if (n == equiv[i].len && !strncmp(t, equiv[i].path, n)) + { + if (m & (i+1)) + t = 0; + else + { + m |= (i+1); + if (!(m & (!i+1))) + { + m |= (!i+1); + e->path = t; + e->len = n; + e++; + if (e >= &std[elementsof(std)]) + break; + t = equiv[!i].path; + n = equiv[!i].len; + } + } + } + if (t) + { + e->path = t; + e->len = n; + e++; + } + } + while (*s == ':') + s++; + } while (*s && e < &std[elementsof(std)]); + if (e < &std[elementsof(std)]) + { + e->len = strlen(e->path = "/usr/sbin"); + if (++e < &std[elementsof(std)]) + { + e->len = strlen(e->path = "/sbin"); + e++; + } + } + if (s = getenv("PATH")) + do + { + for (t = s; *s && *s != ':'; s++); + if ((n = s - t) && *t == '/') + { + for (p = std; p < e; p++) + if (p->len == n && !strncmp(t, p->path, n)) + { + sfsprintf(buf, sizeof(buf), "%-*.*s/%s", n, n, t, error_info.id); + if (!access(buf, X_OK)) + { + cmd = buf; + goto found; + } + } + } + while (*s == ':') + s++; + } while (*s); + + /* + * defer to the first getconf on the standard PATH + */ + + for (p = std; p < e; p++) + { + sfsprintf(buf, sizeof(buf), "%-*.*s/%s", p->len, p->len, p->path, error_info.id); + if (!access(buf, X_OK)) + { + cmd = buf; + goto found; + } + } + + /* + * out of deferrals + */ + + if (name) + error(4, "%s: unknown name -- no native getconf(1) to defer to", name); + else + error(4, "no native getconf(1) to defer to"); + return 2; + + found: + + /* + * don't blame us for crappy diagnostics + */ + + if ((n = procrun(cmd, oargv)) >= EXIT_NOEXEC) + error(ERROR_SYSTEM|2, "%s: exec error [%d]", cmd, n); + return n; +} diff --git a/usr/src/lib/libcmd/common/head.c b/usr/src/lib/libcmd/common/head.c new file mode 100644 index 0000000000..707d0478a8 --- /dev/null +++ b/usr/src/lib/libcmd/common/head.c @@ -0,0 +1,150 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * output the beginning portion of one or more files + */ + +static const char usage[] = +"[-?\n@(#)$Id: head (AT&T Research) 2006-09-27 $\n]" +USAGE_LICENSE +"[+NAME?head - output beginning portion of one or more files ]" +"[+DESCRIPTION?\bhead\b copies one or more input files to standard " + "output stopping at a designated point for each file or to the end of " + "the file whichever comes first. Copying ends at the point indicated by " + "the options. By default a header of the form \b==> \b\afilename\a\b " + "<==\b is output before all but the first file but this can be changed " + "with the \b-q\b and \b-v\b options.]" +"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bhead\b " + "copies from standard input starting at the current location.]" +"[+?The option argument for \b-c\b, and \b-s\b can optionally be " + "followed by one of the following characters to specify a different unit " + "other than a single byte:]" + "{" + "[+b?512 bytes.]" + "[+k?1-killobyte.]" + "[+m?1-megabyte.]" + "}" +"[+?For backwards compatibility, \b-\b\anumber\a is equivalent to \b-n\b " + "\anumber\a.]" +"[n:lines?Copy \alines\a lines from each file.]#[lines:=10]" +"[c:bytes?Copy \achars\a bytes from each file.]#[chars]" +"[q:quiet|silent?Never ouput filename headers.]" +"[s:skip?Skip \askip\a characters or lines from each file before " + "copying.]#[skip]" +"[v:verbose?Always ouput filename headers.]" + "\n\n" +"[ file ... ]" + "\n\n" +"[+EXIT STATUS?]" + "{" + "[+0?All files copied successfully.]" + "[+>0?One or more files did not copy.]" + "}" +"[+SEE ALSO?\bcat\b(1), \btail\b(1)]" +; + +#include <cmd.h> + +int +b_head(int argc, register char** argv, void* context) +{ + static const char header_fmt[] = "\n==> %s <==\n"; + + register Sfio_t* fp; + register char* cp; + register off_t keep = 10; + register off_t skip = 0; + register int delim = '\n'; + int header = 1; + char* format = (char*)header_fmt+1; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + for (;;) + { + switch (optget(argv, usage)) + { + case 'c': + delim = -1; + /*FALLTHROUGH*/ + case 'n': + if (opt_info.offset && argv[opt_info.index][opt_info.offset] == 'c') + { + delim = -1; + opt_info.offset++; + } + if ((keep = opt_info.number) <=0) + error(2, "%s: %I*d: positive numeric option argument expected", opt_info.name, sizeof(keep), keep); + continue; + case 'q': + header = argc; + continue; + case 'v': + header = 0; + continue; + case 's': + skip = opt_info.number; + continue; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + continue; + case ':': + error(2, "%s", opt_info.arg); + continue; + } + break; + } + argv += opt_info.index; + argc -= opt_info.index; + if (error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (cp = *argv) + argv++; + do + { + if (!cp || streq(cp, "-")) + { + cp = "/dev/stdin"; + fp = sfstdin; + sfset(fp, SF_SHARE, 1); + } + else if (!(fp = sfopen(NiL, cp, "r"))) + { + error(ERROR_system(0), "%s: cannot open", cp); + continue; + } + if (argc > header) + sfprintf(sfstdout, format, cp); + format = (char*)header_fmt; + if (skip > 0) + sfmove(fp, NiL, skip, delim); + if (sfmove(fp, sfstdout, keep, delim) < 0 && errno != EPIPE) + error(ERROR_system(0), "%s: read error", cp); + if (fp != sfstdin) + sfclose(fp); + } while (cp = *argv++); + if (sfsync(sfstdout)) + error(ERROR_system(0), "write error"); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/id.c b/usr/src/lib/libcmd/common/id.c new file mode 100644 index 0000000000..b880659ad5 --- /dev/null +++ b/usr/src/lib/libcmd/common/id.c @@ -0,0 +1,468 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Research + * + * id + */ + +static const char usage[] = +"[-?\n@(#)$Id: id (AT&T Research) 2004-06-11 $\n]" +USAGE_LICENSE +"[+NAME?id - return user identity]" +"[+DESCRIPTION?If no \auser\a operand is specified \bid\b writes user and " + "group IDs and the corresponding user and group names of the " + "invoking process to standard output. If the effective and " + "real IDs do not match, both are written. Any supplementary " + "groups the current process belongs to will also be written.]" +"[+?If a \auser\a operand is specified and the process has permission, " + "the user and group IDs and any supplementary group IDs of the " + "selected user will be written to standard output.]" +"[+?If any options are specified, then only a portion of the information " + "is written.]" +"[n:name?Write the name instead of the numeric ID.]" +"[r:real?Writes real ID instead of the effective ID.]" +"[[a?This option is ignored.]" +"[g:group?Writes only the group ID.]" +"[u:user?Writes only the user ID.]" +"[G:groups?Writes only the supplementary group IDs.]" +"[s:fair-share?Writes fair share scheduler IDs and groups on systems that " + "support fair share scheduling.]" +"\n" +"\n[user]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?Successful completion.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\blogname\b(1), \bwho\b(1), \bgetgroups\b(2)]" +; + +#include <cmd.h> + +#include "FEATURE/ids" + +#include <grp.h> +#include <pwd.h> + +#if _lib_fsid +#if _lib_getfsgid && ( _sys_fss || _hdr_fsg ) +#define fss_grp fs_grp +#define fss_id fs_id +#define fss_mem fs_mem +#define fss_passwd fs_passwd +#define fss_shares fs_shares +#if _sys_fss +#include <sys/fss.h> +#endif +#if _hdr_fsg +#include <fsg.h> +#endif +#if !_lib_isfsg && !defined(isfsg) +#define isfsg(p) (!(p)->fs_id&&!(p)->fs_shares&&(!(p)->fs_passwd||!*(p)->fs_passwd)) +#endif +#else +#undef _lib_fsid +#endif +#endif + +#define power2(n) (!((n)&((n)-1))) + +#define GG_FLAG (1<<0) +#define G_FLAG (1<<1) +#define N_FLAG (1<<2) +#define R_FLAG (1<<3) +#define U_FLAG (1<<4) +#define S_FLAG (1<<5) +#define O_FLAG (1<<6) +#define X_FLAG (1<<7) + +#if _lib_fsid +static void +getfsids(Sfio_t* sp, const char* name, int flags, register int lastchar) +{ + register struct fsg* fs; + register char* s; + register char** p; + char** x; + + if (lastchar) + { + if (flags & O_FLAG) flags = 1; + else flags = 0; + } + else if (flags & N_FLAG) flags = 1; + else flags = -1; + setfsgent(); + while (fs = getfsgnam(name)) + if (!isfsg(fs)) + { + if (p = fs->fs_mem) + { + if (flags > 0) x = 0; + else + { + register char** q; + register char* t; + register int n; + + n = 0; + q = p; + while (s = *q++) + n += strlen(s) + 1; + if (!(x = newof(0, char*, q - p, n))) + break; + s = (char*)(x + (q - p)); + q = x; + while (t = *p++) + { + *q++ = s; + while (*s++ = *t++); + } + *q = 0; + p = x; + } + while (s = *p++) + { + if (lastchar == '=') + { + lastchar = ','; + sfputr(sp, " fsid=", -1); + } + else if (!lastchar) lastchar = ' '; + else sfputc(sp, lastchar); + if (flags > 0) sfprintf(sp, "%s", s); + else + { + setfsgent(); + while (fs = getfsgnam(s)) + if (isfsg(fs)) + { + if (flags < 0) sfprintf(sp, "%u", fs->fs_id); + else sfprintf(sp, "%u(%s)", fs->fs_id, s); + break; + } + } + } + if (x) free(x); + } + break; + } + endfsgent(); + if (lastchar == ' ') sfputc(sp, '\n'); +} +#endif + +static void +putid(Sfio_t* sp, int flags, const char* label, const char* name, long number) +{ + sfprintf(sp, "%s=", label); + if (flags & O_FLAG) + { + if (name) sfputr(sp, name, -1); + else sfprintf(sp, "%lu", number); + } + else + { + sfprintf(sp, "%u", number); + if (name) sfprintf(sp, "(%s)", name); + } +} + +static int +getids(Sfio_t* sp, const char* name, register int flags) +{ + register struct passwd* pw; + register struct group* grp; + register int i; + register int j; + register int k; +#if _lib_fsid + register struct fsg* fs; + const char* fs_name; + int fs_id; +#endif + char** p; + char* s; + int lastchar; + int ngroups = 0; + const char* gname; + uid_t user; + uid_t euid; + gid_t group; + gid_t egid; + + static gid_t* groups; + + if (flags & GG_FLAG) + { + static int maxgroups; + + /* + * get supplemental groups if required + */ + + if (!maxgroups) + { + /* + * first time + */ + + if ((maxgroups = getgroups(0, groups)) <= 0) + maxgroups = NGROUPS_MAX; + if (!(groups = newof(0, gid_t, maxgroups + 1, 0))) + error(ERROR_exit(1), "out of space [group array]"); + } + ngroups = getgroups(maxgroups, groups); + for (i = j = 0; i < ngroups; i++) + { + for (k = 0; k < j && groups[k] != groups[i]; k++); + if (k >= j) groups[j++] = groups[i]; + } + ngroups = j; + } + if (name) + { + flags |= X_FLAG; + if (!(flags & N_FLAG) || (flags & (G_FLAG|GG_FLAG))) + { + if (!(pw = getpwnam(name))) + { + user = strtol(name, &s, 0); + if (*s || !(pw = getpwuid(user))) + error(ERROR_exit(1), "%s: name not found", name); + name = pw->pw_name; + } + user = pw->pw_uid; + group = pw->pw_gid; + } +#if _lib_fsid + if (!(flags & N_FLAG) || (flags & S_FLAG)) + { + setfsgent(); + do + { + if (!(fs = getfsgnam(name))) + error(ERROR_exit(1), "%u: fss name not found", name); + } while (isfsg(fs)); + fs_id = fs->fs_id; + } +#endif + } + else + { + if (flags & G_FLAG) + group = (flags & R_FLAG) ? getgid() : getegid(); + if (flags & (GG_FLAG|N_FLAG|U_FLAG)) + user = (flags & R_FLAG) ? getuid() : geteuid(); +#if _lib_fsid + if (flags & S_FLAG) + fs_id = fsid(0); +#endif + if (flags & N_FLAG) + name = (pw = getpwuid(user)) ? pw->pw_name : (char*)0; + } + if (ngroups == 1 && groups[0] == group) + ngroups = 0; + if ((flags & N_FLAG) && (flags & G_FLAG)) + gname = (grp = getgrgid(group)) ? grp->gr_name : (char*)0; +#if _lib_fsid + if ((flags & N_FLAG) && (flags & S_FLAG)) + { + setfsgent(); + fs_name = (fs = getfsgid(fs_id)) ? fs->fs_grp : (char*)0; + } +#endif + if ((flags & (U_FLAG|G_FLAG|S_FLAG)) == (U_FLAG|G_FLAG|S_FLAG)) + { + putid(sp, flags, "uid", name, user); + putid(sp, flags, " gid", gname, group); + if ((flags & X_FLAG) && name) + { +#if _lib_getgrent +#if _lib_setgrent + setgrent(); +#endif + lastchar = '='; + while (grp = getgrent()) + if (p = grp->gr_mem) + while (s = *p++) + if (streq(s, name)) + { + if (lastchar == '=') + sfputr(sp, " groups", -1); + sfputc(sp, lastchar); + lastchar = ','; + if (flags & O_FLAG) + sfprintf(sp, "%s", grp->gr_name); + else sfprintf(sp, "%u(%s)", grp->gr_gid, grp->gr_name); + } +#if _lib_endgrent + endgrent(); +#endif +#endif +#if _lib_fsid + getfsids(sp, name, flags, '='); +#endif + } + else + { + if ((euid = geteuid()) != user) + putid(sp, flags, " euid", (pw = getpwuid(euid)) ? pw->pw_name : (char*)0, euid); + if ((egid = getegid()) != group) + putid(sp, flags, " egid", (grp = getgrgid(egid)) ? grp->gr_name : (char*)0, egid); + if (ngroups > 0) + { + sfputr(sp, " groups", -1); + lastchar = '='; + for (i = 0; i < ngroups; i++) + { + group = groups[i]; + sfputc(sp, lastchar); + if (grp = getgrgid(group)) + { + if (flags & O_FLAG) sfprintf(sp, "%s", grp->gr_name); + else sfprintf(sp, "%u(%s)", group, grp->gr_name); + } + else sfprintf(sp, "%u", group); + lastchar = ','; + } + } +#if _lib_fsid + putid(sp, flags, " fsid", fs_name, fs_id); +#endif + } + sfputc(sp,'\n'); + return(0); + } + if (flags & U_FLAG) + { + if ((flags & N_FLAG) && name) sfputr(sp, name, '\n'); + else sfprintf(sp, "%u\n", user); + } + else if (flags & G_FLAG) + { + if ((flags & N_FLAG) && gname) sfputr(sp, gname, '\n'); + else sfprintf(sp, "%u\n", group); + } + else if (flags & GG_FLAG) + { + if ((flags & X_FLAG) && name) + { +#if _lib_getgrent +#if _lib_setgrent + setgrent(); +#endif + i = 0; + while (grp = getgrent()) + if (p = grp->gr_mem) + while (s = *p++) + if (streq(s, name)) + { + if (i++) sfputc(sp, ' '); + if (flags & N_FLAG) sfprintf(sp, "%s", grp->gr_name); + else sfprintf(sp, "%u", grp->gr_gid); + } +#if _lib_endgrent + endgrent(); +#endif + if (i) sfputc(sp, '\n'); +#endif + } + else if (ngroups > 0) + { + for (i = 0;;) + { + group = groups[i]; + if ((flags & N_FLAG) && (grp = getgrgid(group))) + sfprintf(sp, "%s", grp->gr_name); + else sfprintf(sp, "%u", group); + if (++i >= ngroups) break; + sfputc(sp, ' '); + } + sfputc(sp, '\n'); + } + } +#if _lib_fsid + else if (flags & S_FLAG) + { + if ((flags & X_FLAG) && name) getfsids(sp, name, flags, 0); + else if ((flags & N_FLAG) && fs_name) sfputr(sp, fs_name, '\n'); + else sfprintf(sp, "%u\n", fs_id); + } +#endif + return(0); +} + +int +b_id(int argc, char *argv[], void* context) +{ + register int flags = 0; + register int n; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'a': + break; + case 'G': + flags |= GG_FLAG; + break; + case 'g': + flags |= G_FLAG; + break; + case 'n': + flags |= N_FLAG; + break; + case 'r': + flags |= R_FLAG; + break; + case 's': + flags |= S_FLAG; + break; + case 'u': + flags |= U_FLAG; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + argc -= opt_info.index; + n = (flags & (GG_FLAG|G_FLAG|S_FLAG|U_FLAG)); + if (!power2(n)) + error(2, "incompatible options selected"); + if (error_info.errors || argc > 1) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (!(flags & ~(N_FLAG|R_FLAG))) + { + if (flags & N_FLAG) flags |= O_FLAG; + flags |= (U_FLAG|G_FLAG|N_FLAG|R_FLAG|S_FLAG|GG_FLAG); + } + error_info.errors = getids(sfstdout, *argv, flags); + return(error_info.errors); +} diff --git a/usr/src/lib/libcmd/common/join.c b/usr/src/lib/libcmd/common/join.c new file mode 100644 index 0000000000..1687c898ca --- /dev/null +++ b/usr/src/lib/libcmd/common/join.c @@ -0,0 +1,816 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Research + * + * join + */ + +static const char usage[] = +"[-?\n@(#)$Id: join (AT&T Research) 2006-10-31 $\n]" +USAGE_LICENSE +"[+NAME?join - relational database operator]" +"[+DESCRIPTION?\bjoin\b performs an \aequality join\a on the files \afile1\a " + "and \afile2\a and writes the resulting joined files to standard " + "output. By default, a field is delimited by one or more spaces " + "and tabs with leading spaces and/or tabs ignored. The \b-t\b option " + "can be used to change the field delimiter.]" +"[+?The \ajoin field\a is a field in each file on which files are compared. " + "By default \bjoin\b writes one line in the output for each pair " + "of lines in \afiles1\a and \afiles2\a that have identical join " + "fields. The default output line consists of the join field, " + "then the remaining fields from \afile1\a, then the remaining " + "fields from \afile2\a, but this can be changed with the \b-o\b " + "option. The \b-a\b option can be used to add unmatched lines " + "to the output. The \b-v\b option can be used to output only " + "unmatched lines.]" +"[+?The files \afile1\a and \afile2\a must be ordered in the collating " + "sequence of \bsort -b\b on the fields on which they are to be " + "joined otherwise the results are unspecified.]" +"[+?If either \afile1\a or \afile2\a is \b-\b, \bjoin\b " + "uses standard input starting at the current location.]" + +"[e:empty]:[string?Replace empty output fields in the list selected with" +" \b-o\b with \astring\a.]" +"[o:output]:[list?Construct the output line to comprise the fields specified " + "in a blank or comma separated list \alist\a. Each element in " + "\alist\a consists of a file number (either 1 or 2), a period, " + "and a field number or \b0\b representing the join field. " + "As an obsolete feature multiple occurrences of \b-o\b can " + "be specified.]" +"[t:separator|tabs]:[delim?Use \adelim\a as the field separator for both input" +" and output.]" +"[1:j1]#[field?Join on field \afield\a of \afile1\a. Fields start at 1.]" +"[2:j2]#[field?Join on field \afield\a of \afile2\a. Fields start at 1.]" +"[j:join]#[field?Equivalent to \b-1\b \afield\a \b-2\b \afield\a.]" +"[a:unpairable]#[fileno?Write a line for each unpairable line in file" +" \afileno\a, where \afileno\a is either 1 or 2, in addition to the" +" normal output. If \b-a\b options appear for both 1 and 2, then " + "all unpairable lines will be output.]" +"[v:suppress]#[fileno?Write a line for each unpairable line in file" +" \afileno\a, where \afileno\a is either 1 or 2, instead of the normal " + "output. If \b-v\b options appear for both 1 and 2, then " + "all unpairable lines will be output.] ]" +"[i:ignorecase?Ignore case in field comparisons.]" +"[B!:mmap?Enable memory mapped reads instead of buffered.]" + +"[+?The following obsolete option forms are also recognized: \b-j\b \afield\a" +" is equivalent to \b-1\b \afield\a \b-2\b \afield\a, \b-j1\b \afield\a" +" is equivalent to \b-1\b \afield\a, and \b-j2\b \afield\a is" +" equivalent to \b-2\b \afield\a.]" + +"\n" +"\nfile1 file2\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?Both files processed successfully.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bcut\b(1), \bcomm\b(1), \bpaste\b(1), \bsort\b(1), \buniq\b(1)]" +; + +#include <cmd.h> +#include <sfdisc.h> + +#define C_FILE1 001 +#define C_FILE2 002 +#define C_COMMON 004 +#define C_ALL (C_FILE1|C_FILE2|C_COMMON) + +#define NFIELD 10 +#define JOINFIELD 2 + +#define S_DELIM 1 +#define S_SPACE 2 +#define S_NL 3 + +typedef struct +{ + Sfio_t* iop; + char* name; + char* recptr; + int reclen; + int field; + int fieldlen; + int nfields; + int maxfields; + int spaces; + int hit; + int discard; + char** fieldlist; +} File_t; + +typedef struct +{ + unsigned char state[1<<CHAR_BIT]; + Sfio_t* outfile; + int* outlist; + int outmode; + int ooutmode; + char* nullfield; + int delim; + int buffered; + int ignorecase; + char* same; + int samesize; + File_t file[2]; +} Join_t; + +static void +done(register Join_t* jp) +{ + if (jp->file[0].iop && jp->file[0].iop != sfstdin) + sfclose(jp->file[0].iop); + if (jp->file[1].iop && jp->file[1].iop != sfstdin) + sfclose(jp->file[1].iop); + if (jp->outlist) + free(jp->outlist); + if (jp->file[0].fieldlist) + free(jp->file[0].fieldlist); + if (jp->file[1].fieldlist) + free(jp->file[1].fieldlist); + if (jp->same) + free(jp->same); + free(jp); +} + +static Join_t* +init(void) +{ + register Join_t* jp; + + if (jp = newof(0, Join_t, 1, 0)) + { + jp->state[' '] = jp->state['\t'] = S_SPACE; + jp->delim = -1; + jp->nullfield = 0; + if (!(jp->file[0].fieldlist = newof(0, char*, NFIELD + 1, 0)) || + !(jp->file[1].fieldlist = newof(0, char*, NFIELD + 1, 0))) + { + done(jp); + return 0; + } + jp->file[0].maxfields = NFIELD; + jp->file[1].maxfields = NFIELD; + jp->outmode = C_COMMON; + } + return jp; +} + +static int +getolist(Join_t* jp, const char* first, char** arglist) +{ + register const char* cp = first; + char** argv = arglist; + register int c; + int* outptr; + int* outmax; + int nfield = NFIELD; + char* str; + + outptr = jp->outlist = newof(0, int, NFIELD + 1, 0); + outmax = outptr + NFIELD; + while (c = *cp++) + { + if (c==' ' || c=='\t' || c==',') + continue; + str = (char*)--cp; + if (*cp=='0' && ((c=cp[1])==0 || c==' ' || c=='\t' || c==',')) + { + str++; + c = JOINFIELD; + goto skip; + } + if (cp[1]!='.' || (*cp!='1' && *cp!='2') || (c=strtol(cp+2,&str,10)) <=0) + { + error(2,"%s: invalid field list",first); + break; + } + c--; + c <<=2; + if (*cp=='2') + c |=1; + skip: + if (outptr >= outmax) + { + jp->outlist = newof(jp->outlist, int, 2 * nfield + 1, 0); + outptr = jp->outlist + nfield; + nfield *= 2; + outmax = jp->outlist + nfield; + } + *outptr++ = c; + cp = str; + } + /* need to accept obsolescent command syntax */ + while (1) + { + if (!(cp= *argv) || cp[1]!='.' || (*cp!='1' && *cp!='2')) + { + if (*cp=='0' && cp[1]==0) + { + c = JOINFIELD; + goto skip2; + } + break; + } + str = (char*)cp; + c = strtol(cp+2, &str,10); + if (*str || --c<0) + break; + argv++; + c <<= 2; + if (*cp=='2') + c |=1; + skip2: + if (outptr >= outmax) + { + jp->outlist = newof(jp->outlist, int, 2 * nfield + 1, 0); + outptr = jp->outlist + nfield; + nfield *= 2; + outmax = jp->outlist + nfield; + } + *outptr++ = c; + } + *outptr = -1; + return argv-arglist; +} + +/* + * read in a record from file <index> and split into fields + */ +static unsigned char* +getrec(Join_t* jp, int index, int discard) +{ + register unsigned char* sp = jp->state; + register File_t* fp = &jp->file[index]; + register char** ptr = fp->fieldlist; + register char** ptrmax = ptr + fp->maxfields; + register char* cp; + register int n = 0; + + if (cmdquit()) + return 0; + if (discard && fp->discard) + sfraise(fp->iop, SFSK_DISCARD, NiL); + fp->spaces = 0; + fp->hit = 0; + if (!(cp = sfgetr(fp->iop, '\n', 0))) + { + jp->outmode &= ~(1<<index); + return 0; + } + fp->recptr = cp; + fp->reclen = sfvalue(fp->iop); + if (jp->delim=='\n') /* handle new-line delimiter specially */ + { + *ptr++ = cp; + cp += fp->reclen; + } + else while (n!=S_NL) /* separate into fields */ + { + if (ptr >= ptrmax) + { + n = 2*fp->maxfields; + fp->fieldlist = newof(fp->fieldlist, char*, n + 1, 0); + ptr = fp->fieldlist + fp->maxfields; + fp->maxfields = n; + ptrmax = fp->fieldlist+n; + } + *ptr++ = cp; + if (jp->delim<=0 && sp[*(unsigned char*)cp]==S_SPACE) + { + fp->spaces = 1; + while (sp[*(unsigned char*)cp++]==S_SPACE); + cp--; + } + while ((n=sp[*(unsigned char*)cp++])==0); + } + *ptr = cp; + fp->nfields = ptr - fp->fieldlist; + if ((n=fp->field) < fp->nfields) + { + cp = fp->fieldlist[n]; + /* eliminate leading spaces */ + if (fp->spaces) + { + while (sp[*(unsigned char*)cp++]==S_SPACE); + cp--; + } + fp->fieldlen = (fp->fieldlist[n+1]-cp)-1; + return (unsigned char*)cp; + } + fp->fieldlen = 0; + return (unsigned char*)""; +} + +#if DEBUG_TRACE +static unsigned char* u1,u2,u3; +#define getrec(p,n,d) (u1 = getrec(p, n, d), sfprintf(sfstdout, "[G%d#%d@%I*d:%-.8s]", __LINE__, n, sizeof(Sfoff_t), sftell(p->file[n].iop), u1), u1) +#endif + +/* + * print field <n> from file <index> + */ +static int +outfield(Join_t* jp, int index, register int n, int last) +{ + register File_t* fp = &jp->file[index]; + register char* cp; + register char* cpmax; + register int size; + register Sfio_t* iop = jp->outfile; + + if (n < fp->nfields) + { + cp = fp->fieldlist[n]; + cpmax = fp->fieldlist[n+1]; + } + else + cp = 0; + if ((n=jp->delim)<=0) + { + if (fp->spaces) + { + /*eliminate leading spaces */ + while (jp->state[*(unsigned char*)cp++]==S_SPACE); + cp--; + } + n = ' '; + } + if (last) + n = '\n'; + if (cp) + size = cpmax-cp; + else + size = 0; + if (size==0) + { + if (!jp->nullfield) + sfputc(iop,n); + else if (sfputr(iop,jp->nullfield,n) < 0) + return -1; + } + else + { + last = cp[size-1]; + cp[size-1] = n; + if (sfwrite(iop,cp,size) < 0) + return -1; + cp[size-1] = last; + } + return 0; +} + +#if DEBUG_TRACE +static int i1,i2,i3; +#define outfield(p,i,n,f) (sfprintf(sfstdout, "[F%d#%d:%d,%d]", __LINE__, i1=i, i2=n, i3=f), outfield(p, i1, i2, i3)) +#endif + +static int +outrec(register Join_t* jp, int mode) +{ + register File_t* fp; + register int i; + register int j; + register int k; + register int n; + int* out; + + if (mode < 0 && jp->file[0].hit++) + return 0; + if (mode > 0 && jp->file[1].hit++) + return 0; + if (out = jp->outlist) + { + while ((n = *out++) >= 0) + { + if (n == JOINFIELD) + { + i = mode >= 0; + j = jp->file[i].field; + } + else + { + i = n & 1; + j = (mode<0 && i || mode>0 && !i) ? + jp->file[i].nfields : + n >> 2; + } + if (outfield(jp, i, j, *out < 0) < 0) + return -1; + } + return 0; + } + k = jp->file[0].nfields; + if (mode >= 0) + k += jp->file[1].nfields - 1; + for (i=0; i<2; i++) + { + fp = &jp->file[i]; + if (mode>0 && i==0) + { + k -= (fp->nfields - 1); + continue; + } + n = fp->field; + if (mode||i==0) + { + /* output join field first */ + if (outfield(jp,i,n,!--k) < 0) + return -1; + if (!k) + return 0; + for (j=0; j<n; j++) + { + if (outfield(jp,i,j,!--k) < 0) + return -1; + if (!k) + return 0; + } + j = n + 1; + } + else + j = 0; + for (;j<fp->nfields; j++) + { + if (j!=n && outfield(jp,i,j,!--k) < 0) + return -1; + if (!k) + return 0; + } + } + return 0; +} + +#if DEBUG_TRACE +#define outrec(p,n) (sfprintf(sfstdout, "[R#%d,%d,%lld,%lld:%-.*s{%d}:%-.*s{%d}]", __LINE__, i1=n, lo, hi, jp->file[0].fieldlen, cp1, jp->file[0].hit, jp->file[1].fieldlen, cp2, jp->file[1].hit), outrec(p, i1)) +#endif + +static int +join(Join_t* jp) +{ + register unsigned char* cp1; + register unsigned char* cp2; + register int n1; + register int n2; + register int n; + register int cmp; + register int same; + int o2; + Sfoff_t lo = -1; + Sfoff_t hi = -1; + + if ((cp1 = getrec(jp, 0, 0)) && (cp2 = getrec(jp, 1, 0)) || (cp2 = 0)) + { + n1 = jp->file[0].fieldlen; + n2 = jp->file[1].fieldlen; + same = 0; + for (;;) + { + n = n1 < n2 ? n1 : n2; +#if DEBUG_TRACE + if (!n && !(cmp = n1 < n2 ? -1 : (n1 > n2)) || n && !(cmp = (int)*cp1 - (int)*cp2) && !(cmp = jp->ignorecase ? strncasecmp((char*)cp1, (char*)cp2, n) : memcmp(cp1, cp2, n))) + cmp = n1 - n2; +sfprintf(sfstdout, "[C#%d:%d(%c-%c),%d,%lld,%lld%s]", __LINE__, cmp, *cp1, *cp2, same, lo, hi, (jp->outmode & C_COMMON) ? ",COMMON" : ""); + if (!cmp) +#else + if (!n && !(cmp = n1 < n2 ? -1 : (n1 > n2)) || n && !(cmp = (int)*cp1 - (int)*cp2) && !(cmp = jp->ignorecase ? strncasecmp((char*)cp1, (char*)cp2, n) : memcmp(cp1, cp2, n)) && !(cmp = n1 - n2)) +#endif + { + if (!(jp->outmode & C_COMMON)) + { + if (cp1 = getrec(jp, 0, 1)) + { + n1 = jp->file[0].fieldlen; + same = 1; + continue; + } + if ((jp->ooutmode & (C_FILE1|C_FILE2)) != C_FILE2) + break; + if (sfseek(jp->file[0].iop, (Sfoff_t)-jp->file[0].reclen, SEEK_CUR) < 0 || !(cp1 = getrec(jp, 0, 0))) + { + error(ERROR_SYSTEM|2, "%s: seek error", jp->file[0].name); + return -1; + } + } + else if (outrec(jp, 0) < 0) + return -1; + else if (lo < 0 && (jp->outmode & C_COMMON)) + { + if ((lo = sfseek(jp->file[1].iop, (Sfoff_t)0, SEEK_CUR)) < 0) + { + error(ERROR_SYSTEM|2, "%s: seek error", jp->file[1].name); + return -1; + } + lo -= jp->file[1].reclen; + } + if (cp2 = getrec(jp, 1, lo < 0)) + { + n2 = jp->file[1].fieldlen; + continue; + } +#if DEBUG_TRACE +sfprintf(sfstdout, "[2#%d:0,%lld,%lld]", __LINE__, lo, hi); +#endif + } + else if (cmp > 0) + { + if (same) + { + same = 0; + next: + if (n2 > jp->samesize) + { + jp->samesize = roundof(n2, 16); + if (!(jp->same = newof(jp->same, char, jp->samesize, 0))) + { + error(ERROR_SYSTEM|2, "out of space"); + return -1; + } + } + memcpy(jp->same, cp2, o2 = n2); + if (!(cp2 = getrec(jp, 1, 0))) + break; + n2 = jp->file[1].fieldlen; + if (n2 == o2 && *cp2 == *jp->same && !memcmp(cp2, jp->same, n2)) + goto next; + continue; + } + if (hi >= 0) + { + if (sfseek(jp->file[1].iop, hi, SEEK_SET) != hi) + { + error(ERROR_SYSTEM|2, "%s: seek error", jp->file[1].name); + return -1; + } + hi = -1; + } + else if ((jp->outmode & C_FILE2) && outrec(jp, 1) < 0) + return -1; + lo = -1; + if (cp2 = getrec(jp, 1, 1)) + { + n2 = jp->file[1].fieldlen; + continue; + } +#if DEBUG_TRACE +sfprintf(sfstdout, "[2#%d:0,%lld,%lld]", __LINE__, lo, hi); +#endif + } + else if (same) + { + same = 0; + if (!(cp1 = getrec(jp, 0, 0))) + break; + n1 = jp->file[0].fieldlen; + continue; + } + if (lo >= 0) + { + if ((hi = sfseek(jp->file[1].iop, (Sfoff_t)0, SEEK_CUR)) < 0 || + (hi -= jp->file[1].reclen) < 0 || + sfseek(jp->file[1].iop, lo, SEEK_SET) != lo || + !(cp2 = getrec(jp, 1, 0))) + { + error(ERROR_SYSTEM|2, "%s: seek error", jp->file[1].name); + return -1; + } + n2 = jp->file[1].fieldlen; + lo = -1; + if (jp->file[1].discard) + sfseek(jp->file[1].iop, (Sfoff_t)-1, SEEK_SET); + } + else if (!cp2) + break; + else if ((jp->outmode & C_FILE1) && outrec(jp, -1) < 0) + return -1; + if (!(cp1 = getrec(jp, 0, 1))) + break; + n1 = jp->file[0].fieldlen; + } + } +#if DEBUG_TRACE +sfprintf(sfstdout, "[X#%d:?,%p,%p,%d%,%d,%d%s]", __LINE__, cp1, cp2, cmp, lo, hi, (jp->outmode & C_COMMON) ? ",COMMON" : ""); +#endif + if (cp2) + { + if (hi >= 0 && + sfseek(jp->file[1].iop, (Sfoff_t)0, SEEK_CUR) < hi && + sfseek(jp->file[1].iop, hi, SEEK_SET) != hi) + { + error(ERROR_SYSTEM|2, "%s: seek error", jp->file[1].name); + return -1; + } +#if DEBUG_TRACE +sfprintf(sfstdout, "[O#%d:%02o:%02o]", __LINE__, jp->ooutmode, jp->outmode); +#endif + cp1 = (!cp1 && cmp && hi < 0 && !jp->file[1].hit && ((jp->ooutmode ^ C_ALL) <= 1 || jp->outmode == 2)) ? cp2 : getrec(jp, 1, 0); + cmp = 1; + n = 1; + } + else + { + cmp = -1; + n = 0; + } +#if DEBUG_TRACE +sfprintf(sfstdout, "[X#%d:%d,%p,%p,%d,%02o,%02o%s]", __LINE__, n, cp1, cp2, cmp, jp->ooutmode, jp->outmode, (jp->outmode & C_COMMON) ? ",COMMON" : ""); +#endif + if (!cp1 || !(jp->outmode & (1<<n))) + { + if (cp1 && jp->file[n].iop == sfstdin) + sfseek(sfstdin, (Sfoff_t)0, SEEK_END); + return 0; + } + if (outrec(jp, cmp) < 0) + return -1; + do + { + if (!getrec(jp, n, 1)) + return 0; + } while (outrec(jp, cmp) >= 0); + return -1; +} + +int +b_join(int argc, char** argv, void* context) +{ + register int n; + register char* cp; + register Join_t* jp; + char* e; + +#if !DEBUG_TRACE + cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); +#endif + if (!(jp = init())) + error(ERROR_system(1),"out of space"); + for (;;) + { + switch (n = optget(argv, usage)) + { + case 0: + break; + case 'j': + /* + * check for obsolete "-j1 field" and "-j2 field" + */ + + if (opt_info.offset == 0) + { + cp = argv[opt_info.index - 1]; + for (n = strlen(cp) - 1; n > 0 && cp[n] != 'j'; n--); + n = cp[n] == 'j'; + } + else + n = 0; + if (n) + { + if (opt_info.num!=1 && opt_info.num!=2) + error(2,"-jfileno field: fileno must be 1 or 2"); + n = '0' + opt_info.num; + if (!(cp = argv[opt_info.index])) + { + argc = 0; + break; + } + opt_info.num = strtol(cp, &e, 10); + if (*e) + { + argc = 0; + break; + } + opt_info.index++; + } + else + { + jp->file[0].field = (int)(opt_info.num-1); + n = '2'; + } + /*FALLTHROUGH*/ + case '1': + case '2': + if (opt_info.num <=0) + error(2,"field number must positive"); + jp->file[n-'1'].field = (int)(opt_info.num-1); + continue; + case 'v': + jp->outmode &= ~C_COMMON; + /*FALLTHROUGH*/ + case 'a': + if (opt_info.num!=1 && opt_info.num!=2) + error(2,"%s: file number must be 1 or 2", opt_info.name); + jp->outmode |= 1<<(opt_info.num-1); + continue; + case 'e': + jp->nullfield = opt_info.arg; + continue; + case 'o': + /* need to accept obsolescent command syntax */ + n = getolist(jp, opt_info.arg, argv+opt_info.index); + opt_info.index += n; + continue; + case 't': + jp->state[' '] = jp->state['\t'] = 0; + n= *(unsigned char*)opt_info.arg; + jp->state[n] = S_DELIM; + jp->delim = n; + continue; + case 'i': + jp->ignorecase = !opt_info.num; + continue; + case 'B': + jp->buffered = !opt_info.num; + continue; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + done(jp); + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + argc -= opt_info.index; + if (error_info.errors || argc!=2) + { + done(jp); + error(ERROR_usage(2),"%s", optusage(NiL)); + } + jp->ooutmode = jp->outmode; + jp->file[0].name = cp = *argv++; + if (streq(cp,"-")) + { + if (sfseek(sfstdin,(Sfoff_t)0,SEEK_CUR) < 0) + { + if (sfdcseekable(sfstdin)) + error(ERROR_warn(0),"%s: seek may fail",cp); + else + jp->file[0].discard = 1; + } + jp->file[0].iop = sfstdin; + } + else if (!(jp->file[0].iop = sfopen(NiL, cp, "r"))) + { + done(jp); + error(ERROR_system(1),"%s: cannot open",cp); + } + jp->file[1].name = cp = *argv; + if (streq(cp,"-")) + { + if (sfseek(sfstdin,(Sfoff_t)0,SEEK_CUR) < 0) + { + if (sfdcseekable(sfstdin)) + error(ERROR_warn(0),"%s: seek may fail",cp); + else + jp->file[1].discard = 1; + } + jp->file[1].iop = sfstdin; + } + else if (!(jp->file[1].iop = sfopen(NiL, cp, "r"))) + { + done(jp); + error(ERROR_system(1),"%s: cannot open",cp); + } + if (jp->buffered) + { + sfsetbuf(jp->file[0].iop, jp->file[0].iop, SF_UNBOUND); + sfsetbuf(jp->file[1].iop, jp->file[0].iop, SF_UNBOUND); + } + jp->state['\n'] = S_NL; + jp->outfile = sfstdout; + if (!jp->outlist) + jp->nullfield = 0; + if (join(jp) < 0) + { + done(jp); + error(ERROR_system(1),"write error"); + } + else if (jp->file[0].iop==sfstdin || jp->file[1].iop==sfstdin) + sfseek(sfstdin,(Sfoff_t)0,SEEK_END); + done(jp); + return error_info.errors; +} diff --git a/usr/src/lib/libcmd/common/llib-lcmd b/usr/src/lib/libcmd/common/llib-lcmd index c9de823563..8908e1c639 100644 --- a/usr/src/lib/libcmd/common/llib-lcmd +++ b/usr/src/lib/libcmd/common/llib-lcmd @@ -18,22 +18,68 @@ * * CDDL HEADER END */ - -/* LINTLIBRARY */ -/* PROTOLIB1 */ - /* - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * + * lib/libcmd/common/llib-lcmd + * */ #pragma ident "%Z%%M% %I% %E% SMI" +/*LINTLIBRARY*/ +/*PROTOLIB1*/ + +#include <cmd.h> +#include <cmdext.h> + +/* automatically generated data start here */ +extern int STANDALONE(int, char**, void*); +extern int cmdrecurse(int, char**, int, char**); +extern int b_basename(int, char**, void*); +extern int b_cat(int, char**, void*); +extern int b_chgrp(int, char**, void*); +extern int b_chmod(int, char**, void*); +extern int b_chown(int, char**, void*); +extern int b_cmp(int, char**, void*); +extern int b_comm(int, char**, void*); +extern int b_cp(int, char**, void*); +extern int b_cut(int, char**, void*); +extern int b_date(int, char**, void*); +extern int b_dirname(int, char**, void*); +extern int b_expr(int, char**, void*); +extern int b_fds(int, char**, void*); +extern int b_fmt(int, char**, void*); +extern int b_fold(int, char**, void*); +extern int b_getconf(int, char**, void*); +extern int b_head(int, char**, void*); +extern int b_id(int, char**, void*); +extern int b_join(int, char**, void*); +extern int b_ln(int, char**, void*); +extern int b_logname(int, char**, void*); +extern int b_mkdir(int, char**, void*); +extern int b_mkfifo(int, char**, void*); +extern int b_mv(int, char**, void*); +extern int b_paste(int, char**, void*); +extern int b_pathchk(int, char**, void*); +extern int b_rev(int, char**, void*); +extern int b_rm(int, char**, void*); +extern int b_rmdir(int, char**, void*); +extern int b_stty(int, char**, void*); +extern int b_tail(int, char**, void*); +extern int b_tee(int, char**, void*); +extern int b_tty(int, char**, void*); +extern int b_uname(int, char**, void*); +extern int b_uniq(int, char**, void*); +extern int b_wc(int, char**, void*); +/* end of automatically generated data */ + /* - * The def*() functions are the historical contents of libcmd. - * They have been moved to libc and libcmd contains only filter - * symbols for them, redirecting to libc. + * The |def*()| functions are the historical contents of Solaris + * libcmd. They have been moved to libc and libcmd contains only + * filter symbols for them, redirecting to libc. */ -int defopen(char *fn); -char *defread(char *cp); -int defcntl(int cmd, int newflags); +extern int defopen(char *fn); +extern char *defread(char *cp); +extern int defcntl(int cmd, int newflags); diff --git a/usr/src/lib/libcmd/common/ln.c b/usr/src/lib/libcmd/common/ln.c new file mode 100644 index 0000000000..9adb32e3fd --- /dev/null +++ b/usr/src/lib/libcmd/common/ln.c @@ -0,0 +1,35 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * cp/ln/mv -- copy/link/move files + */ + +#include <cmd.h> + +int +b_ln(int argc, register char** argv, void* context) +{ + return b_cp(argc, argv, context); +} diff --git a/usr/src/lib/libcmd/common/logname.c b/usr/src/lib/libcmd/common/logname.c new file mode 100644 index 0000000000..fe771e95ed --- /dev/null +++ b/usr/src/lib/libcmd/common/logname.c @@ -0,0 +1,78 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Research + * + * logname + */ + +static const char usage[] = +"[-?\n@(#)$Id: logname (AT&T Research) 1999-04-30 $\n]" +USAGE_LICENSE +"[+NAME?logname - return the user's login name]" +"[+DESCRIPTION?\blogname\b writes the users's login name to standard " + "output. The login name is the string that is returned by the " + "\bgetlogin\b(2) function. If \bgetlogin\b(2) does not return " + "successfully, the corresponding to the real user id of the calling " + "process is used instead.]" + +"\n" +"\n\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?Successful Completion.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bgetlogin\b(2)]" +; + + +#include <cmd.h> + +int +b_logname(int argc, char** argv, void* context) +{ + register char* logname; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + for (;;) + { + switch (optget(argv, usage)) + { + case ':': + error(2, "%s", opt_info.arg); + continue; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + continue; + } + break; + } + if (error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (!(logname = getlogin())) + logname = fmtuid(getuid()); + sfputr(sfstdout, logname, '\n'); + return 0; +} + diff --git a/usr/src/lib/libcmd/common/mapfile-vers b/usr/src/lib/libcmd/common/mapfile-vers deleted file mode 100644 index 891b6fd3a2..0000000000 --- a/usr/src/lib/libcmd/common/mapfile-vers +++ /dev/null @@ -1,35 +0,0 @@ -# -# CDDL HEADER START -# -# The contents of this file are subject to the terms of the -# Common Development and Distribution License (the "License"). -# You may not use this file except in compliance with the License. -# -# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE -# or http://www.opensolaris.org/os/licensing. -# See the License for the specific language governing permissions -# and limitations under the License. -# -# When distributing Covered Code, include this CDDL HEADER in each -# file and include the License file at usr/src/OPENSOLARIS.LICENSE. -# If applicable, add the following below this CDDL HEADER, with the -# fields enclosed by brackets "[]" replaced with your own identifying -# information: Portions Copyright [yyyy] [name of copyright owner] -# -# CDDL HEADER END -# -# -# Copyright 2006 Sun Microsystems, Inc. All rights reserved. -# Use is subject to license terms. -# -# ident "%Z%%M% %I% %E% SMI" -# - -SUNWprivate_1.1 { - global: - defcntl = FUNCTION FILTER libc.so.1; - defopen = FUNCTION FILTER libc.so.1; - defread = FUNCTION FILTER libc.so.1; - local: - *; -}; diff --git a/usr/src/lib/libcmd/common/md5sum.c b/usr/src/lib/libcmd/common/md5sum.c new file mode 100644 index 0000000000..11977d008e --- /dev/null +++ b/usr/src/lib/libcmd/common/md5sum.c @@ -0,0 +1,35 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * md5sum -- implemented by cksum + */ + +#include <cmd.h> + +int +b_md5sum(int argc, register char** argv, void* context) +{ + return b_cksum(argc, argv, context); +} diff --git a/usr/src/lib/libcmd/common/mkdir.c b/usr/src/lib/libcmd/common/mkdir.c new file mode 100644 index 0000000000..df47adc11c --- /dev/null +++ b/usr/src/lib/libcmd/common/mkdir.c @@ -0,0 +1,152 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * mkdir + */ + +static const char usage[] = +"[-?\n@(#)$Id: mkdir (AT&T Research) 2006-08-27 $\n]" +USAGE_LICENSE +"[+NAME?mkdir - make directories]" +"[+DESCRIPTION?\bmkdir\b creates one or more directories. By " + "default, the mode of created directories is \ba=rwx\b minus the " + "bits set in the \bumask\b(1).]" +"[m:mode]:[mode?Set the mode of created directories to \amode\a. " + "\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative " + "modes assume an initial mode of \ba=rwx\b.]" +"[p:parents?Create any missing intermediate pathname components. For " + "each dir operand that does not name an existing directory, effects " + "equivalent to those caused by the following command shall occur: " + "\vmkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]] " + "dir\v where the \b-m\b mode option represents that option supplied to " + "the original invocation of \bmkdir\b, if any. Each dir operand that " + "names an existing directory shall be ignored without error.]" +"\n" +"\ndirectory ...\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All directories created successfully, or the \b-p\b option " + "was specified and all the specified directories now exist.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]" +; + +#include <cmd.h> +#include <ls.h> + +#define DIRMODE (S_IRWXU|S_IRWXG|S_IRWXO) + +int +b_mkdir(int argc, char** argv, void* context) +{ + register char* arg; + register int n; + register mode_t mode = DIRMODE; + register mode_t mask = 0; + register int mflag = 0; + register int pflag = 0; + char* name; + mode_t dmode; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'p': + pflag = 1; + break; + case 'm': + mflag = 1; + mode = strperm(arg = opt_info.arg, &opt_info.arg, mode); + if (*opt_info.arg) + error(ERROR_exit(0), "%s: invalid mode", arg); + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if (error_info.errors || !*argv) + error(ERROR_usage(2), "%s", optusage(NiL)); + mask = umask(0); + if (mflag || pflag) + { + dmode = DIRMODE & ~mask; + if (!mflag) + mode = dmode; + dmode |= S_IWUSR | S_IXUSR; + } + else + { + mode &= ~mask; + umask(mask); + mask = 0; + } + while (arg = *argv++) + { + if (mkdir(arg, mode) < 0) + { + if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR)) + { + error(ERROR_system(0), "%s:", arg); + continue; + } + if (errno == EEXIST) + continue; + + /* + * -p option, preserve intermediates + * first eliminate trailing /'s + */ + + n = strlen(arg); + while (n > 0 && arg[--n] == '/'); + arg[n + 1] = 0; + for (name = arg, n = *arg; n;) + { + /* skip over slashes */ + while (*arg == '/') + arg++; + /* skip to next component */ + while ((n = *arg) && n != '/') + arg++; + *arg = 0; + if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0) + { + *arg = n; + error(ERROR_system(0), "%s:", name); + break; + } + *arg = n; + } + } + } + if (mask) + umask(mask); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/mkfifo.c b/usr/src/lib/libcmd/common/mkfifo.c new file mode 100644 index 0000000000..cffcab1e81 --- /dev/null +++ b/usr/src/lib/libcmd/common/mkfifo.c @@ -0,0 +1,89 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * mkfifo + */ + + +static const char usage[] = +"[-?\n@(#)$Id: mkfifo (AT&T Research) 1999-04-20 $\n]" +USAGE_LICENSE +"[+NAME?mkfifo - make FIFOs (named pipes)]" +"[+DESCRIPTION?\bmkfifo\b creates one or more FIFO's. By " + "default, the mode of created FIFO is \ba=rw\b minus the " + "bits set in the \bumask\b(1).]" +"[m:mode]:[mode?Set the mode of created FIFO to \amode\a. " + "\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative " + "modes assume an initial mode of \ba=rw\b.]" +"\n" +"\nfile ...\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All FIFO's created successfully.]" + "[+>0?One or more FIFO's could not be created.]" +"}" +"[+SEE ALSO?\bchmod\b(1), \bumask\b(1)]" +; + +#include <cmd.h> +#include <ls.h> + +#define RWALL (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) + +int +b_mkfifo(int argc, char *argv[], void* context) +{ + register char *arg; + register mode_t mode=RWALL, mask=0; + register int n; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'm': + mode = strperm(arg=opt_info.arg,&opt_info.arg,mode); + if(*opt_info.arg) + error(ERROR_exit(0),"%s: invalid mode",arg); + break; + case ':': + error(2, "%s",opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s",opt_info.arg); + break; + } + argv += opt_info.index; + if(error_info.errors || !*argv) + error(ERROR_usage(2),"%s",optusage(NiL)); + while(arg = *argv++) + { + if(mkfifo(arg,mode) < 0) + error(ERROR_system(0),"%s:",arg); + } + if(mask) + umask(mask); + return(error_info.errors!=0); +} + diff --git a/usr/src/lib/libcmd/common/mv.c b/usr/src/lib/libcmd/common/mv.c new file mode 100644 index 0000000000..cf4e6785dd --- /dev/null +++ b/usr/src/lib/libcmd/common/mv.c @@ -0,0 +1,35 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * cp/ln/mv -- copy/link/move files + */ + +#include <cmd.h> + +int +b_mv(int argc, register char** argv, void* context) +{ + return b_cp(argc, argv, context); +} diff --git a/usr/src/lib/libcmd/common/paste.c b/usr/src/lib/libcmd/common/paste.c new file mode 100644 index 0000000000..5aaeb369ac --- /dev/null +++ b/usr/src/lib/libcmd/common/paste.c @@ -0,0 +1,247 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * paste [-s] [-d delim] [file] ... + * + * paste lines from files together + */ + +static const char usage[] = +"[-?\n@(#)$Id: paste (AT&T Research) 1999-06-22 $\n]" +USAGE_LICENSE +"[+NAME?paste - merge lines of files]" +"[+DESCRIPTION?\bpaste\b concatenates the corresponding lines of a " + "given input file and writes the resulting lines to standard " + "output. By default \bpaste\b replaces the newline character of " + "every line other than the last input file with the TAB character.]" +"[+?Unless the \b-s\b option is specified, if an end-of-file is encountered " + "on one or more input files, but not all input files, \bpaste\b " + "behaves as if empty lines were read from the file(s) on which " + "end-of-file was detected.]" +"[+?Unless the \b-s\b option is specified, \bpaste\b is limited by " + "the underlying operating system on how many \afile\a operands " + "can be specified.]" +"[+?If no \afile\a operands are given or if the \afile\a is \b-\b, \bpaste\b " + "reads from standard input. The start of the file is defined as the " + "current offset.]" + +"[s:serial?Paste the lines of one file at a time rather than one line " + "from each file. In this case if the \b-d\b option is " + "specified the delimiter will be reset to the first in the " + "list at the beginning of each file.]" +"[d:delimiters]:[list?\alist\a specifies a list of delimiters. These " + "delimiters are used circularly instead of TAB to replace " + "the newline character of the input lines. Unless the \b-s\b " + "option is specified, the delimiter will be reset to the first " + "element of \alist\a each time a line is processed from each file. " + "The delimiter characters corresponding to \alist\a will be found " + "by treating \alist\a as an ANSI-C string, except that the \b\\0\b " + "sequence will insert the empty string instead of the null character.]" +"\n" +"\n[file ...]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files processed successfully.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bcut\b(1), \bcat\b(1), \bjoin\b(1)]" +; + + +#include <cmd.h> + +/* + * paste the lines of the <nstreams> defined in <streams> and put results + * to <out> + */ + +static int paste(int nstream,Sfio_t* streams[],Sfio_t *out, register const char *delim,int dlen) +{ + register const char *cp; + register int d, n, more=1; + register Sfio_t *fp; + do + { + d = (dlen>0?0:-1); + for(n=more-1,more=0; n < nstream;) + { + if(fp=streams[n]) + { + if(cp = sfgetr(fp,'\n',0)) + { + if(n==0) + more = 1; + else if(!more) /* first stream with output */ + { + if(dlen==1) + sfnputc(out, *delim, n); + else if(dlen>0) + { + for(d=n; d>dlen; d-=dlen) + sfwrite(out,delim,dlen); + if(d) + sfwrite(out,delim,d); + } + more = n+1; + } + if(sfwrite(out,cp,sfvalue(fp)-((n+1)<nstream)) < 0) + return(-1); + } + else + streams[n] = 0; + } + if(++n<nstream && more && d>=0) + { + register int c; + if(d >= dlen) + d = 0; + if(c=delim[d++]) + sfputc(out,c); + } + else if(n==nstream && !streams[n-1] && more) + sfputc(out,'\n'); + } + } + while(more); + return(0); +} + +/* + * Handles paste -s, for file <in> to file <out> using delimiters <delim> + */ +static int spaste(Sfio_t *in,register Sfio_t* out,register const char *delim,int dlen) +{ + register const char *cp; + register int d=0; + if(cp = sfgetr(in,'\n',0)) + { + if(sfwrite(out,cp,sfvalue(in)-1) < 0) + return(-1); + } + while(cp=sfgetr(in, '\n',0)) + { + if(dlen) + { + register int c; + if(d >= dlen) + d = 0; + if(c=delim[d++]) + sfputc(out,c); + } + if(sfwrite(out,cp,sfvalue(in)-1) < 0) + return(-1); + } + sfputc(out,'\n'); + return(0); +} + +int +b_paste(int argc,register char *argv[], void* context) +{ + register int n, sflag=0; + register Sfio_t *fp, **streams; + register char *cp, *delim; + int dlen; + char defdelim[2]; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + delim = 0; + while (n = optget(argv, usage)) switch (n) + { + case 'd': + delim = opt_info.arg; + break; + case 's': + sflag++; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if(error_info.errors) + error(ERROR_usage(2),"%s", optusage(NiL)); + if(delim) + dlen = stresc(delim); + else + { + *(delim = defdelim) = '\t'; + dlen = 1; + } + if(cp = *argv) + { + n = argc - opt_info.index; + argv++; + } + else + n = 1; + if(!sflag) + { + if (!(streams = (Sfio_t**)stakalloc(n*sizeof(Sfio_t*)))) + error(ERROR_exit(1), "out of space"); + n = 0; + } + do + { + if(!cp || streq(cp,"-")) + fp = sfstdin; + else if(!(fp = sfopen(NiL,cp,"r"))) + { + error(ERROR_system(0),"%s: cannot open",cp); + error_info.errors = 1; + } + if(fp && sflag) + { + if(spaste(fp,sfstdout,delim,dlen) < 0) + { + error(ERROR_system(0),"write failed"); + error_info.errors = 1; + } + if(fp!=sfstdin) + sfclose(fp); + } + else + streams[n++] = fp; + } + while(cp= *argv++); + if(!sflag) + { + if(error_info.errors==0 && paste(n,streams,sfstdout,delim,dlen) < 0) + { + error(ERROR_system(0),"write failed"); + error_info.errors = 1; + } + while(--n>=0) + { + if((fp=streams[n]) && fp!=sfstdin) + sfclose(fp); + } + } + return(error_info.errors); +} + diff --git a/usr/src/lib/libcmd/common/pathchk.c b/usr/src/lib/libcmd/common/pathchk.c new file mode 100644 index 0000000000..fff22f52bf --- /dev/null +++ b/usr/src/lib/libcmd/common/pathchk.c @@ -0,0 +1,248 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * pathchk + * + * Written by David Korn + */ + +static const char usage[] = +"[-?\n@(#)$Id: pathchk (AT&T Research) 2006-09-19 $\n]" +USAGE_LICENSE +"[+NAME?pathchk - check pathnames for portability]" +"[+DESCRIPTION?\bpathchk\b checks each \apathname\a to see if it " + "is valid and/or portable. A \apathname\a is valid if it " + "can be used to access or create a file without causing syntax " + "errors. A file is portable, if no truncation will result on " + "any conforming POSIX.1 implementation.]" +"[+?By default \bpathchk\b checks each component of each \apathname\a " + "based on the underlying file system. A diagnostic is written " + "to standard error for each pathname that:]{" + "[+-?Is longer than \b$(getconf PATH_MAX)\b bytes.]" + "[+-?Contains any component longer than \b$(getconf NAME_MAX)\b bytes.]" + "[+-?Contains any directory component in a directory that is " + "not searchable.]" + "[+-?Contains any character in any component that is not valid in " + "its containing directory.]" + "[+-?Is empty.]" + "}" +"[p:portability?Instead of performing length checks on the underlying " + "file system, write a diagnostic for each pathname operand that:]{" + "[+-?Is longer than \b$(getconf _POSIX_PATH_MAX)\b bytes.]" + "[+-?Contains any component longer than " + "\b$(getconf _POSIX_NAME_MAX)\b bytes.]" + "[+-?Contains any character in any component that is not in the " + "portable filename character set.]" +#if 0 + "[+-?Contains any component with \b-\b as the first character.]" +#endif + "[+-?Is empty.]" + "}" +"\n" +"\npathname ...\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All \apathname\a operands passed all of the checks.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bgetconf\b(1), \bcreat\b(2), \bpathchk\b(2)]" +; + + +#include <cmd.h> +#include <ls.h> + +#define isport(c) (((c)>='a' && (c)<='z') || ((c)>='A' && (c)<='Z') || ((c)>='0' && (c)<='9') || (strchr("._-",(c))!=0) ) + +/* + * call pathconf and handle unlimited sizes + */ +static long mypathconf(const char *path, int op) +{ + register long r; + + static const char* ops[] = { "NAME_MAX", "PATH_MAX" }; + + errno=0; + if((r=strtol(astconf(ops[op], path, NiL), NiL, 0))<0 && errno==0) + return(LONG_MAX); + return(r); +} + +/* + * returns 1 if <path> passes test + */ +static int pathchk(char* path, int mode) +{ + register char *cp=path, *cpold; + register int c; + register long r,name_max,path_max; + char buf[2]; + + if(!*path) + { + error(2,"path is empty"); + return(0); + } + if(mode) + { + name_max = _POSIX_NAME_MAX; + path_max = _POSIX_PATH_MAX; + } + else + { + char tmp[2]; + name_max = path_max = 0; + tmp[0] = (*cp=='/'? '/': '.'); + tmp[1] = 0; + if((r=mypathconf(tmp, 0)) > _POSIX_NAME_MAX) + name_max = r; + if((r=mypathconf(tmp, 1)) > _POSIX_PATH_MAX) + path_max = r; + if(*cp!='/') + { + if(name_max==0||path_max==0) + { + if(!(cpold = getcwd((char*)0, 0)) && errno == EINVAL && (cpold = newof(0, char, PATH_MAX, 0)) && !getcwd(cpold, PATH_MAX)) + { + free(cpold); + cpold = 0; + } + if(cpold) + { + cp = cpold + strlen(cpold); + while(name_max==0 || path_max==0) + { + if(cp>cpold) + while(--cp>cpold && *cp=='/'); + *++cp = 0; + if(name_max==0 && (r=mypathconf(cpold, 0)) > _POSIX_NAME_MAX) + name_max = r; + if(path_max==0 && (r=mypathconf(cpold, 1)) > _POSIX_PATH_MAX) + path_max=r; + if(--cp==cpold) + { + free(cpold); + break; + } + while(*cp!='/') + cp--; + } + cp=path; + } + } + while(*cp=='/') + cp++; + } + if(name_max==0) + name_max=_POSIX_NAME_MAX; + if(path_max==0) + path_max=_POSIX_PATH_MAX; + while(*(cpold=cp)) + { + while((c= *cp++) && c!='/'); + if((cp-cpold) > name_max) + goto err; + errno=0; + cp[-1] = 0; + r = mypathconf(path, 0); + if((cp[-1]=c)==0) + cp--; + else while(*cp=='/') + cp++; + if(r>=0) + name_max=(r<_POSIX_NAME_MAX?_POSIX_NAME_MAX:r); + else if(errno==EINVAL) + continue; +#ifdef ENAMETOOLONG + else if(errno==ENAMETOOLONG) + { + error(2,"%s: pathname too long",path); + return(0); + } +#endif /*ENAMETOOLONG*/ + else + break; + } + } + while(*(cpold=cp)) + { + if(mode && *cp == '-') + { + error(2,"%s: path component begins with '-'",path,fmtquote(buf, NiL, "'", 1, 0)); + return(0); + } + while((c= *cp++) && c!='/') + if(mode && !isport(c)) + { + buf[0] = c; + buf[1] = 0; + error(2,"%s: '%s' not in portable character set",path,fmtquote(buf, NiL, "'", 1, 0)); + return(0); + } + if((cp-cpold) > name_max) + goto err; + if(c==0) + break; + while(*cp=='/') + cp++; + } + if((cp-path) >= path_max) + { + error(2,"%s: pathname too long",path); + return(0); + } + return(1); +err: + error(2,"%s: component name %.*s too long",path,cp-cpold-1,cpold); + return(0); +} + +int +b_pathchk(int argc, char** argv, void* context) +{ + register int n, mode=0; + register char *cp; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'p': + mode = 1; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if(*argv==0 || error_info.errors) + error(ERROR_usage(2),"%s", optusage((char*)0)); + while(cp = *argv++) + { + if(!pathchk(cp,mode)) + error_info.errors=1; + } + return(error_info.errors); +} diff --git a/usr/src/lib/libcmd/common/placeholder.c b/usr/src/lib/libcmd/common/placeholder.c deleted file mode 100644 index f4b2bc015c..0000000000 --- a/usr/src/lib/libcmd/common/placeholder.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * CDDL HEADER START - * - * The contents of this file are subject to the terms of the - * Common Development and Distribution License (the "License"). - * You may not use this file except in compliance with the License. - * - * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE - * or http://www.opensolaris.org/os/licensing. - * See the License for the specific language governing permissions - * and limitations under the License. - * - * When distributing Covered Code, include this CDDL HEADER in each - * file and include the License file at usr/src/OPENSOLARIS.LICENSE. - * If applicable, add the following below this CDDL HEADER, with the - * fields enclosed by brackets "[]" replaced with your own identifying - * information: Portions Copyright [yyyy] [name of copyright owner] - * - * CDDL HEADER END - */ - -/* - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. - */ - -#pragma ident "%Z%%M% %I% %E% SMI" - -/* - * This is a placeholder file, containing nothing. - * It is here because the build of a library is structured such that - * at least one compilable file is required and that file must not be - * totally empty (else it draws an "empty compilation unit" warning). - * This is why the include of <stdio.h> appears below. - * - * This file should be deleted or renamed to something useful - * when the ksh93 project is integrated into libcmd. - */ - -#include <stdio.h> diff --git a/usr/src/lib/libcmd/common/rev.c b/usr/src/lib/libcmd/common/rev.c new file mode 100644 index 0000000000..ce3f2ae425 --- /dev/null +++ b/usr/src/lib/libcmd/common/rev.c @@ -0,0 +1,128 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * rev [-l] [file ...] + * + * reverse the characters or lines of one or more files + * + * David Korn + * AT&T Laboratories + * dgk@research.att.com + * + */ + +static const char usage[] = +"[-?\n@(#)$Id: rev (AT&T Research) 1999-04-10 $\n]" +USAGE_LICENSE +"[+NAME?rev - reverse the characters or lines of one or more files]" +"[+DESCRIPTION?\brev\b copies one or more files to standard output " + "reversing the order of characters on every line of the file " + "or reversing the order of lines of the file if \b-l\b is specified.]" +"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \brev\b " + "copies from standard input starting at the current offset.]" +"[l:line?Reverse the lines of the file.]" + +"\n" +"\n[file ...]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files copied successfully.]" + "[+>0?One or more files did not copy.]" +"}" +"[+SEE ALSO?\bcat\b(1), \btail\b(1)]" +; + +#include <cmd.h> +#include <rev.h> + +/* + * reverse the characters within a line + */ +static int rev_char(Sfio_t *in, Sfio_t *out) +{ + register int c; + register char *ep, *bp, *cp; + register int n; + while(cp = bp = sfgetr(in,'\n',0)) + { + ep = bp + (n=sfvalue(in)) -1; + while(ep > bp) + { + c = *--ep; + *ep = *bp; + *bp++ = c; + } + if(sfwrite(out,cp,n)<0) + return(-1); + } + return(0); +} + +int +b_rev(int argc, register char** argv, void* context) +{ + register Sfio_t *fp; + register char *cp; + register int n, line=0; + NOT_USED(argc); + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'l': + line=1; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if(error_info.errors) + error(ERROR_usage(2),"%s",optusage((char*)0)); + n=0; + if(cp = *argv) + argv++; + do + { + if(!cp || streq(cp,"-")) + fp = sfstdin; + else if(!(fp = sfopen((Sfio_t*)0,cp,"r"))) + { + error(ERROR_system(0),"%s: cannot open",cp); + n=1; + continue; + } + if(line) + line = rev_line(fp,sfstdout,sftell(fp)); + else + line = rev_char(fp,sfstdout); + if(fp!=sfstdin) + sfclose(fp); + if(line < 0) + error(ERROR_system(1),"write failed"); + } + while(cp= *argv++); + return(n); +} diff --git a/usr/src/lib/libcmd/common/rev.h b/usr/src/lib/libcmd/common/rev.h new file mode 100644 index 0000000000..1ea6d55277 --- /dev/null +++ b/usr/src/lib/libcmd/common/rev.h @@ -0,0 +1,34 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 + +/* + * rev common definitions + */ + +#ifndef _REVLIB_H +#define _REVLIB_H + +#define rev_line _cmd_revline + +extern int rev_line(Sfio_t*, Sfio_t*, off_t); + +#endif diff --git a/usr/src/lib/libcmd/common/revlib.c b/usr/src/lib/libcmd/common/revlib.c new file mode 100644 index 0000000000..3693d15b2d --- /dev/null +++ b/usr/src/lib/libcmd/common/revlib.c @@ -0,0 +1,112 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * common support for tail and rev + */ + +#include <cmd.h> +#include <rev.h> + +#define BUFSIZE SF_BUFSIZE +#define rounddown(n,size) (((n)-1)&~((size)-1)) + +/* + * copy the lines starting at offset <start> from in <in> to <out> + * in reverse order + */ +int rev_line(Sfio_t *in, Sfio_t *out, off_t start) +{ + register char *cp, *cpold; + register int n, nleft=0; + char buff[BUFSIZE]; + off_t offset; + if(sfseek(in,(off_t)0,SEEK_CUR) < 0) + { + Sfio_t *tmp = sftmp(4*SF_BUFSIZE); + if(!tmp) + return(-1); + if(start>0 && sfmove(in, (Sfio_t*)0, start, -1) != start) + return(-1); + if(sfmove(in, tmp, SF_UNBOUND, -1) < 0 || !sfeof(in) || sferror(tmp)) + return(-1); + in = tmp; + start=0; + } + if((offset = sfseek(in,(off_t)0,SEEK_END)) <= start) + return(0); + offset = rounddown(offset,BUFSIZE); + while(1) + { + n = BUFSIZE; + if(offset < start) + { + n -= (start-offset); + offset = start; + } + sfseek(in, offset, SEEK_SET); + if((n=sfread(in, buff, n)) <=0) + break; + cp = buff+n; + n = *buff; + *buff = '\n'; + while(1) + { + cpold = cp; + if(nleft==0) + cp--; + if(cp==buff) + { + nleft= 1; + break; + } + while(*--cp != '\n'); + if(cp==buff && n!='\n') + { + *cp = n; + nleft += cpold-cp; + break; + } + else + cp++; + if(sfwrite(out,cp,cpold-cp) < 0) + return(-1); + if(nleft) + { + if(nleft==1) + sfputc(out,'\n'); + else if(sfmove(in,out,nleft,-1) != nleft) + return(-1); + nleft = 0; + } + } + if(offset <= start) + break; + offset -= BUFSIZE; + } + if(nleft) + { + sfseek(in, start, SEEK_SET); + if(sfmove(in,out,nleft,-1) != nleft) + return(-1); + } + return(0); +} diff --git a/usr/src/lib/libcmd/common/rm.c b/usr/src/lib/libcmd/common/rm.c new file mode 100644 index 0000000000..62cf8a8b2f --- /dev/null +++ b/usr/src/lib/libcmd/common/rm.c @@ -0,0 +1,414 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * rm [-fir] [file ...] + */ + +static const char usage[] = +"[-?\n@(#)$Id: rm (AT&T Research) 2006-11-21 $\n]" +USAGE_LICENSE +"[+NAME?rm - remove files]" +"[+DESCRIPTION?\brm\b removes the named \afile\a arguments. By default it" +" does not remove directories. If a file is unwritable, the" +" standard input is a terminal, and the \b--force\b option is not" +" given, \brm\b prompts the user for whether to remove the file." +" An affirmative response (\by\b or \bY\b) removes the file, a quit" +" response (\bq\b or \bQ\b) causes \brm\b to exit immediately, and" +" all other responses skip the current file.]" + +"[c|F:clear|clobber?Clear the contents of each file before removing by" +" writing a 0 filled buffer the same size as the file, executing" +" \bfsync\b(2) and closing before attempting to remove. Implemented" +" only on systems that support \bfsync\b(2).]" +"[d:directory?\bremove\b(3) (or \bunlink\b(2)) directories rather than" +" \brmdir\b(2), and don't require that they be empty before removal." +" The caller requires sufficient privilege, not to mention a strong" +" constitution, to use this option. Even though the directory must" +" not be empty, \brm\b still attempts to empty it before removal.]" +"[f:force?Ignore nonexistent files and never prompt the user.]" +"[i:interactive|prompt?Prompt whether to remove each file." +" An affirmative response (\by\b or \bY\b) removes the file, a quit" +" response (\bq\b or \bQ\b) causes \brm\b to exit immediately, and" +" all other responses skip the current file.]" +"[r|R:recursive?Remove the contents of directories recursively.]" +"[u:unconditional?If \b--recursive\b and \b--force\b are also enabled then" +" the owner read, write and execute modes are enabled (if not already" +" enabled) for each directory before attempting to remove directory" +" contents.]" +"[v:verbose?Print the name of each file before removing it.]" + +"\n" +"\nfile ...\n" +"\n" + +"[+SEE ALSO?\bmv\b(1), \brmdir\b(2), \bunlink\b(2), \bremove\b(3)]" +; + +#include <cmd.h> +#include <ls.h> +#include <fts.h> +#include <fs3d.h> + +#define RM_ENTRY 1 + +#define beenhere(f) (((f)->fts_number>>1)==(f)->fts_statp->st_nlink) +#define isempty(f) (!((f)->fts_number&RM_ENTRY)) +#define nonempty(f) ((f)->fts_parent->fts_number|=RM_ENTRY) +#define pathchunk(n) roundof(n,1024) +#define retry(f) ((f)->fts_number=((f)->fts_statp->st_nlink<<1)) + +typedef struct State_s /* program state */ +{ + int clobber; /* clear out file data first */ + int directory; /* remove(dir) not rmdir(dir) */ + int force; /* force actions */ + int fs3d; /* 3d enabled */ + int interactive; /* prompt for approval */ + int recursive; /* remove subtrees too */ + int terminal; /* attached to terminal */ + int uid; /* caller uid */ + int unconditional; /* enable dir rwx on preorder */ + int verbose; /* display each file */ +#if _lib_fsync + char buf[SF_BUFSIZE];/* clobber buffer */ +#endif +} State_t; + +/* + * remove a single file + */ + +static int +rm(State_t* state, register FTSENT* ent) +{ + register char* path; + register int n; + int v; + struct stat st; + + if (cmdquit()) + return -1; + if (ent->fts_info == FTS_NS || ent->fts_info == FTS_ERR || ent->fts_info == FTS_SLNONE) + { + if (!state->force) + error(2, "%s: not found", ent->fts_path); + } + else if (state->fs3d && iview(ent->fts_statp)) + fts_set(NiL, ent, FTS_SKIP); + else switch (ent->fts_info) + { + case FTS_DNR: + case FTS_DNX: + if (state->unconditional) + { + if (!chmod(ent->fts_name, (ent->fts_statp->st_mode & S_IPERM)|S_IRWXU)) + { + fts_set(NiL, ent, FTS_AGAIN); + break; + } + error_info.errors++; + } + else if (!state->force) + error(2, "%s: cannot %s directory", ent->fts_path, (ent->fts_info & FTS_NR) ? "read" : "search"); + else + error_info.errors++; + fts_set(NiL, ent, FTS_SKIP); + nonempty(ent); + break; + case FTS_D: + case FTS_DC: + path = ent->fts_name; + if (path[0] == '.' && (!path[1] || path[1] == '.' && !path[2]) && (ent->fts_level > 0 || path[1])) + { + fts_set(NiL, ent, FTS_SKIP); + if (!state->force) + error(2, "%s: cannot remove", ent->fts_path); + else + error_info.errors++; + break; + } + if (!state->recursive) + { + fts_set(NiL, ent, FTS_SKIP); + error(2, "%s: directory", ent->fts_path); + break; + } + if (!beenhere(ent)) + { + if (state->unconditional && (ent->fts_statp->st_mode ^ S_IRWXU)) + chmod(path, (ent->fts_statp->st_mode & S_IPERM)|S_IRWXU); + if (ent->fts_level > 0) + { + char* s; + + if (ent->fts_accpath == ent->fts_name || !(s = strrchr(ent->fts_accpath, '/'))) + v = !stat(".", &st); + else + { + path = ent->fts_accpath; + *s = 0; + v = !stat(path, &st); + *s = '/'; + } + if (v) + v = st.st_nlink <= 2 || st.st_ino == ent->fts_parent->fts_statp->st_ino && st.st_dev == ent->fts_parent->fts_statp->st_dev || strchr(astconf("PATH_ATTRIBUTES", path, NiL), 'l'); + } + else + v = 1; + if (v) + { + if (state->interactive) + { + if ((v = astquery(-1, "remove directory %s? ", ent->fts_path)) < 0) + return -1; + if (v > 0) + { + fts_set(NiL, ent, FTS_SKIP); + nonempty(ent); + } + } + if (ent->fts_info == FTS_D) + break; + } + else + { + ent->fts_info = FTS_DC; + error(1, "%s: hard link to directory", ent->fts_path); + } + } + else if (ent->fts_info == FTS_D) + break; + /*FALLTHROUGH*/ + case FTS_DP: + if (isempty(ent) || state->directory) + { + path = ent->fts_name; + if (path[0] != '.' || path[1]) + { + path = ent->fts_accpath; + if (state->verbose) + sfputr(sfstdout, ent->fts_path, '\n'); + if ((ent->fts_info == FTS_DC || state->directory) ? remove(path) : rmdir(path)) + switch (errno) + { + case EEXIST: +#if defined(ENOTEMPTY) && (ENOTEMPTY) != (EEXIST) + case ENOTEMPTY: +#endif + if (ent->fts_info == FTS_DP && !beenhere(ent)) + { + retry(ent); + fts_set(NiL, ent, FTS_AGAIN); + break; + } + /*FALLTHROUGH*/ + default: + nonempty(ent); + if (!state->force) + error(ERROR_SYSTEM|2, "%s: directory not removed", ent->fts_path); + else + error_info.errors++; + break; + } + } + else if (!state->force) + error(2, "%s: cannot remove", ent->fts_path); + else + error_info.errors++; + } + else + { + nonempty(ent); + if (!state->force) + error(2, "%s: directory not removed", ent->fts_path); + else + error_info.errors++; + } + break; + default: + path = ent->fts_accpath; + if (state->verbose) + sfputr(sfstdout, ent->fts_path, '\n'); + if (state->interactive) + { + if ((v = astquery(-1, "remove %s? ", ent->fts_path)) < 0) + return -1; + if (v > 0) + { + nonempty(ent); + break; + } + } + else if (!state->force && state->terminal && S_ISREG(ent->fts_statp->st_mode)) + { + if ((n = open(path, O_RDWR)) < 0) + { + if ( +#ifdef ENOENT + errno != ENOENT && +#endif +#ifdef EROFS + errno != EROFS && +#endif + (v = astquery(-1, "override protection %s for %s? ", +#ifdef ETXTBSY + errno == ETXTBSY ? "``running program''" : +#endif + ent->fts_statp->st_uid != state->uid ? "``not owner''" : + fmtmode(ent->fts_statp->st_mode & S_IPERM, 0) + 1, ent->fts_path)) < 0) + return -1; + if (v > 0) + { + nonempty(ent); + break; + } + } + else + close(n); + } +#if _lib_fsync + if (state->clobber && S_ISREG(ent->fts_statp->st_mode) && ent->fts_statp->st_size > 0) + { + if ((n = open(path, O_WRONLY)) < 0) + error(ERROR_SYSTEM|2, "%s: cannot clear data", ent->fts_path); + else + { + off_t c = ent->fts_statp->st_size; + + for (;;) + { + if (write(n, state->buf, sizeof(state->buf)) != sizeof(state->buf)) + { + error(ERROR_SYSTEM|2, "%s: data clear error", ent->fts_path); + break; + } + if (c <= sizeof(state->buf)) + break; + c -= sizeof(state->buf); + } + fsync(n); + close(n); + } + } +#endif + if (remove(path)) + { + nonempty(ent); + if (!state->force || state->interactive) + error(ERROR_SYSTEM|2, "%s: not removed", ent->fts_path); + else + error_info.errors++; + } + break; + } + return 0; +} + +int +b_rm(int argc, register char** argv, void* context) +{ + State_t state; + FTS* fts; + FTSENT* ent; + int set3d; + + cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); + memset(&state, 0, sizeof(state)); + state.fs3d = fs3d(FS3D_TEST); + state.terminal = isatty(0); + for (;;) + { + switch (optget(argv, usage)) + { + case 'd': + state.directory = 1; + continue; + case 'f': + state.force = 1; + state.interactive = 0; + continue; + case 'i': + state.interactive = 1; + state.force = 0; + continue; + case 'r': + case 'R': + state.recursive = 1; + continue; + case 'F': +#if _lib_fsync + state.clobber = 1; +#else + error(1, "%s not implemented on this system", opt_info.name); +#endif + continue; + case 'u': + state.unconditional = 1; + continue; + case 'v': + state.verbose = 1; + 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 (*argv && streq(*argv, "-") && !streq(*(argv - 1), "--")) + argv++; + if (error_info.errors || !*argv) + error(ERROR_USAGE|4, "%s", optusage(NiL)); + + /* + * do it + */ + + if (state.interactive) + state.verbose = 0; + state.uid = geteuid(); + state.unconditional = state.unconditional && state.recursive && state.force; + if (state.recursive && state.fs3d) + { + set3d = state.fs3d; + state.fs3d = 0; + fs3d(0); + } + else + set3d = 0; + if (fts = fts_open(argv, FTS_PHYSICAL, NiL)) + { + while ((ent = fts_read(fts)) && !rm(&state, ent)); + fts_close(fts); + } + else if (!state.force) + error(ERROR_SYSTEM|2, "%s: cannot remove", argv[0]); + if (set3d) + fs3d(set3d); + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/rmdir.c b/usr/src/lib/libcmd/common/rmdir.c new file mode 100644 index 0000000000..4edcda4df8 --- /dev/null +++ b/usr/src/lib/libcmd/common/rmdir.c @@ -0,0 +1,122 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * rmdir + */ + +static const char usage[] = +"[-?\n@(#)$Id: rmdir (AT&T Research) 2006-08-24 $\n]" +USAGE_LICENSE +"[+NAME?rmdir - remove empty directories]" +"[+DESCRIPTION?\brmdir\b deletes each given directory. The directory " + "must be empty; containing no entries other than \b.\b or \b..\b. " + "If a directory and a subdirectory of that directory are specified " + "as operands, the subdirectory must be specified before the parent " + "so that the parent directory will be empty when \brmdir\b attempts " + "to remove it.]" +"[e:ignore-fail-on-non-empty?Ignore each non-empty directory failure.]" +"[p:parents?Remove each explicit \adirectory\a argument directory that " + "becomes empty after its child directories are removed.]" +"[s:suppress?Suppress the message printed on the standard error when " + "\b-p\b is in effect.]" +"\n" +"\ndirectory ...\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All directories deleted successfully.]" + "[+>0?One or more directories could not be deleted.]" +"}" +"[+SEE ALSO?\bmkdir\b(1), \brm\b(1), \brmdir\b(2), \bunlink\b(2)]" +; + +#include <cmd.h> + +int +b_rmdir(int argc, char** argv, void* context) +{ + register char* dir; + register char* end; + register int n; + int eflag = 0; + int pflag = 0; + int sflag = 0; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'e': + eflag = 1; + break; + case 'p': + pflag = 1; + break; + case 's': + sflag = 1; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if (error_info.errors || !*argv) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (!pflag) + sflag = 0; + while (dir = *argv++) + { + end = dir; + if (pflag) end += strlen(dir); + n = 0; + for (;;) + { + if (rmdir(dir) < 0) + { + if (!eflag || errno != EEXIST +#ifdef ENOTEMPTY + && errno != ENOTEMPTY +#endif + ) + { + if (sflag) + error_info.errors++; + else + error(ERROR_system(0), "%s: cannot remove", dir); + } + break; + } + if (n) *end = '/'; + else n = 1; + do if (end <= dir) goto next; while (*--end != '/'); + do if (end <= dir) goto next; while (*(end - 1) == '/' && end--); + *end = 0; + } + next: ; + } + return(error_info.errors != 0); +} + diff --git a/usr/src/lib/libcmd/common/stty.c b/usr/src/lib/libcmd/common/stty.c new file mode 100644 index 0000000000..ef53c61bf7 --- /dev/null +++ b/usr/src/lib/libcmd/common/stty.c @@ -0,0 +1,943 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * stty.c + * Written by David Korn + * Tue Apr 4 10:46:00 EDT 1995 + */ + +static const char usage[] = +"[-?@(#)$Id: stty (AT&T Research) 2006-10-31 $\n]" +USAGE_LICENSE +"[+NAME?stty - set or get terminal modes]" +"[+DESCRIPTION?\bstty\b sets certain terminal I/O modes for the device " + "that is the current standard input; without arguments, it writes " + "the settings of certain modes to standard output.]" + +"[a:all?Writes to standard output all of the mode settings.]" +"[g:save?Writes the current settings to standard output in a form that can " + "be used as an argument to another \bstty\b command. The \brows\b " + "and \bcolumns\b values are not included.]" +"\n" +"\n[mode ...]\n" +"\n" +"[+EXTENDED DESCRIPTION?Modes are specified either as a single name or " + "as a name followed by a value. As indicated below, many of the " + "mode names can be preceded by a \b-\b to negate its meaning. " + "Modes are listed by group corresponding to field in the " + "\btermios\b structure defined in \b<termios.h>\b. Modes " + "in the last group are implemented using options in the previous " + "groups. Note that many combinations of modes make no sense, but " + "no sanity checking is performed. The modes are selected from the " + "following:]{\fabc\f}" + +"[+EXIT STATUS?]{" + "[+0?All modes reported or set successfully.]" + "[+>0?Standard input not a terminaol or one or more modes failed.]" +"}" +"[+SEE ALSO?\btegetattr\b(2), \btcsetattr\b(2), \bioctl\b(2)]" +; + + +#include <cmd.h> +#include <ccode.h> +#include <ctype.h> +#include <ast_tty.h> +#if _sys_ioctl +#include <sys/ioctl.h> +#endif + +#define C(x) ERROR_catalog(x) + +#ifndef _POSIX_VDISABLE +# define _POSIX_VDISABLE 0 +#endif + +#ifndef NCCS +# ifdef NCC +# define NCCS NCC +# else +# define NCCS elementsof(((struct termio*)0)->c_cc) +# endif +#endif + +/* command options */ +#define A_FLAG 1 +#define G_FLAG 2 + +/* termios fields */ +#define C_FLAG 1 +#define C_LINE 2 +#define C_SPEED 3 +#define I_FLAG 4 +#define O_FLAG 5 +#define L_FLAG 6 +#define T_CHAR 7 +#define W_SIZE 8 + +#define BIT 1 +#define BITS 2 +#define NUM 3 +#define CHAR 4 +#define SPEED 5 +#define SIZE 6 +#define MIXED 7 +#define SANE 8 +#define COOKED 9 +#define CASE 10 +#define TABS 11 +#define WIND 12 + +#undef SS /* who co-opted this namespace? */ + +#define IG 0x0001 /* ignore display */ +#define NL 0x0002 /* entry ends line of display */ +#define SS 0x0004 /* set in sane mode */ +#define US 0x0010 /* unset in sane mode */ + +typedef struct tty_s +{ + const char name[8]; + unsigned char type; + unsigned char field; + short flags; + unsigned long mask; + unsigned long val; + const char description[76]; +} Tty_t; + +static const Tty_t Ttable[] = +{ +#ifdef CBAUD +{ "ispeed", NUM, C_SPEED,0, CBAUD, 0, C("\an\a is the input baud rate") }, +{ "ospeed", NUM, C_SPEED,0, CBAUD, 0, C("\an\a is the output baud rate") }, +{ "speed", NUM, C_SPEED,IG, CBAUD }, +#endif +{ "0", SPEED, C_FLAG, 0, B0 }, +{ "50", SPEED, C_FLAG, 0, B50 }, +{ "75", SPEED, C_FLAG, 0, B75 }, +{ "110", SPEED, C_FLAG, 0, B110 }, +{ "134", SPEED, C_FLAG, 0, B134 }, +{ "150", SPEED, C_FLAG, 0, B150 }, +{ "200", SPEED, C_FLAG, 0, B200 }, +{ "300", SPEED, C_FLAG, 0, B300 }, +{ "600", SPEED, C_FLAG, 0, B600 }, +{ "1200", SPEED, C_FLAG, 0, B1200 }, +{ "1800", SPEED, C_FLAG, 0, B1800 }, +{ "2400", SPEED, C_FLAG, 0, B2400 }, +{ "4800", SPEED, C_FLAG, 0, B4800 }, +{ "9600", SPEED, C_FLAG, 0, B9600 }, +{ "19200", SPEED, C_FLAG, 0, B19200 }, +{ "38400", SPEED, C_FLAG, 0, B38400 }, + +#ifdef TIOCSWINSZ +{ "rows", WIND, W_SIZE, IG, 0, 24, C("\an\a is the number of lines for display") }, +{ "cols", WIND, W_SIZE, IG, 1, 80, C("\an\a is the number of columns for display") }, +{ "columns", WIND, W_SIZE, IG, 1, 80, C("Same as \bcols\b") }, +#endif +{ "intr", CHAR, T_CHAR, SS, VINTR, 'C', C("Send an interrupt signal") }, +{ "quit", CHAR, T_CHAR, SS, VQUIT, '|', C("Send a quit signal") }, +{ "erase", CHAR, T_CHAR, SS, VERASE, 'H', C("Erase the last character entered") }, +{ "kill", CHAR, T_CHAR, NL|SS, VKILL, 'U', C("Erase the current line") }, +{ "eof", CHAR, T_CHAR, SS, VEOF, 'D', C("Send an end of file") }, +#ifdef VEOL2 +{ "eol2", CHAR, T_CHAR, US, VEOL2, _POSIX_VDISABLE, C("Alternate character to end the line") }, +#endif /* VEOL2 */ +#ifdef VSWTCH +{ "swtch", CHAR, T_CHAR, US, VSWTCH, _POSIX_VDISABLE, C("Switch to a different shell layer") }, +#endif /* VSWTCH */ +{ "eol", CHAR, T_CHAR, NL|US, VEOL, _POSIX_VDISABLE, C("End the line") }, +#ifdef VSTART +{ "start", CHAR, T_CHAR, SS, VSTART, 'Q', C("Restart the output after stopping it") }, +#endif /* VSTART */ +#ifdef VSTOP +{ "stop", CHAR, T_CHAR, SS, VSTOP, 'S', C("Stop the output") }, +#endif /* VSTOP */ +#ifdef VDSUSP +{ "dsusp", CHAR, T_CHAR, SS, VDSUSP, 'Y', C("Send a terminal stop signal after flushing the input") }, +#endif /* VDSUSP */ +#ifdef VSUSP +{ "susp", CHAR, T_CHAR, NL|SS, VSUSP, 'Z', C("Send a terminal stop signal") }, +#endif /* VSUSP */ +#ifdef VREPRINT +{ "rprnt", CHAR, T_CHAR, SS, VREPRINT, 'R', C("Redraw the current line") }, +#endif /* VREPRINT */ +#ifdef VDISCARD +{ "flush", CHAR, T_CHAR, SS, VDISCARD, 'O', C("Discard output") }, +#endif /* VDISCARD */ +#ifdef VWERASE +{ "werase", CHAR, T_CHAR, SS, VWERASE, 'W', C("Erase the last word entered") }, +#endif /* VWERASE */ +#ifdef VLNEXT +{ "lnext", CHAR, T_CHAR, NL|SS, VLNEXT, 'V', C("Enter the next input character literally") }, +#endif /* VLNEXT */ + +#if _mem_c_line_termios +{ "line", NUM, C_LINE, 0, 0, 0, C("Line discipline number") }, +#endif +{ "min", NUM, T_CHAR, 0, VMIN, 0, C("Mininmum number of characters to read in raw mode") }, +{ "time", NUM, T_CHAR, 0, VTIME, 0, C("Number of .1 second intervals with raw mode") }, + +{ "parenb", BIT, C_FLAG, 0, PARENB, PARENB, C("Enable (disable) parity generation and detection") }, +{ "parodd", BIT, C_FLAG, 0, PARODD, PARODD, C("Use odd (even) parity") }, +#ifdef PAREXT +{ "parext", BIT, C_FLAG, 0, PAREXT, PAREXT }, +#endif /* PAREXT */ +#ifdef CREAD +{ "cread", BIT, C_FLAG, SS, CREAD, CREAD, C("Enable (disable) input") }, +#endif /* CREAD */ +{ "cs5", SIZE, C_FLAG, 0, CSIZE, CS5 , C("Char size 5") }, +{ "cs6", SIZE, C_FLAG, 0, CSIZE, CS6 , C("Char size 6") }, +{ "cs7", SIZE, C_FLAG, 0, CSIZE, CS7 , C("Char size 7") }, +{ "cs8", SIZE, C_FLAG, 0, CSIZE, CS8 , C("Char size 8") }, +{ "hupcl", BIT, C_FLAG, 0, HUPCL, HUPCL, C("Hangup (do not hangup) connection on last close") }, +{ "hup", BIT, C_FLAG, IG, HUPCL, HUPCL, C("Same as \bhupcl\b") }, +{ "cstopb", BIT, C_FLAG, 0, CSTOPB, CSTOPB, C("Use two (one) stop bits") }, +#ifdef CRTSCTS +{ "crtscts", BIT, C_FLAG, 0, CRTSCTS, CRTSCTS, C("Enable (disable) RTS/CTS handshaking") }, +#endif /* CRTSCTS */ +{ "clocal", BIT, C_FLAG, NL, CLOCAL, CLOCAL, C("Disable (enable) modem control signals") }, + +{ "ignbrk", BIT, I_FLAG, US, IGNBRK, IGNBRK, C("Ignore (do not ignore) break characters") }, +{ "brkint", BIT, I_FLAG, SS, BRKINT, BRKINT, C("Generate (do not generate) INTR signal on break") }, +{ "ignpar", BIT, I_FLAG, 0, IGNPAR, IGNPAR, C("Ignore (do not ignore) characters with parity errors") }, +{ "parmrk", BIT, I_FLAG, 0, PARMRK, PARMRK, C("Mark (do not mark) parity errors") }, +{ "inpck", BIT, I_FLAG, 0, INPCK, INPCK, C("Enable (disable) input parity checking") }, +{ "istrip", BIT, I_FLAG, 0, ISTRIP, ISTRIP, C("Clear (do not clear) high bit of input characters") }, +{ "inlcr", BIT, I_FLAG, US, INLCR, INLCR, C("Translate (do not translate) carriage return to newline") }, +{ "igncr", BIT, I_FLAG, US, IGNCR, IGNCR, C("Ignore (do not ignore) carriage return") }, +#ifdef IUCLC +{ "iuclc", BIT, I_FLAG, US, IUCLC, IUCLC, C("Map (do not map) upper-case to lower case") }, +#endif /* IUCLC */ +{ "ixon", BIT, I_FLAG, 0, IXON, IXON, C("Enable (disable) XON/XOFF flow control. \bstop\b character stops output") }, +#ifdef IXANY +{ "ixany", BIT, I_FLAG, US, IXANY, IXANY, C("Any character (only start character) can restart output.") }, +{ "decctlq", BIT, I_FLAG, IG, IXANY, 0, C("Same as \b-ixany\b") }, +#endif /* IXANY */ +{ "ixoff", BIT, I_FLAG, US, IXOFF, IXOFF, C("Disable (enable) XON/XOFF flow control") }, +#ifdef IMAXBEL +{ "imaxbel", BIT, I_FLAG, SS, IMAXBEL, IMAXBEL, C("Beep (do not beep) if a character arrives with full input buffer") }, +#endif /* IMAXBEL */ +{ "icrnl", BIT, I_FLAG, NL|SS, ICRNL, ICRNL, C("Translate (do not translate) carriage return to newline") }, + +{ "isig", BIT, L_FLAG, SS, ISIG, ISIG, C("Enable (disable) \bintr\b, \bquit\b, and \bsusp\b special characters") }, +{ "icanon", BIT, L_FLAG, SS, ICANON, ICANON, C("Enable (disable) \berase\b, \bkill\b, \bwerase\b, and \brprnt\b special characters") }, +{ "icannon", BIT, L_FLAG, SS, ICANON, ICANON }, +#ifdef IEXTEN +{ "iexten", BIT, L_FLAG, SS, IEXTEN, IEXTEN, C("Enable (disable) non-POSIX special characters") }, +#endif /* IEXTEN */ +{ "echo", BIT, L_FLAG, SS, ECHO|ECHONL, ECHO|ECHONL, C("Echo (do not echo) input characters") }, +{ "echoe", BIT, L_FLAG, SS, ECHOE, ECHOE, C("Echo (do not echo) erase characters as backspace-space-backspace") }, +{ "echok", BIT, L_FLAG, SS, ECHOK, ECHOK, C("Echo (do not echo) a newline after a kill character") }, +#ifdef ECHOKE +{ "echoke", BIT, L_FLAG, SS, ECHOKE, ECHOKE, C("Echo (do not echo) a newline after a kill character") }, +#endif +{ "lfkc", BIT, L_FLAG, IG, ECHOK, ECHOK, C("Same as \bechok\b (\b-echok\b); obsolete") }, +{ "echonl", BIT, L_FLAG, SS, ECHONL, ECHONL,"Echo (do not echo) newline even if not echoing other character" }, +#ifdef ECHOCTL +{ "echoctl", BIT, L_FLAG, SS, ECHOCTL, ECHOCTL, C("Echo (do not echo) control characters as \b^\b\ac\a") }, +#else +#define ECHOCTL 0 +#endif /* ECHOCTL */ +#ifdef ECHOPRT +{ "echoprt", BIT, L_FLAG, US, ECHOPRT, ECHOPRT, C("Echo (do not echo) erased characters backward, between '\\' and '/'") }, +#else +#define ECHOPRT 0 +#endif /* ECHOPRT */ +#ifdef XCASE +{ "xcase", BIT, L_FLAG, US, XCASE, XCASE, C("Enable (disable) \bicanon\b uppercase as lowercase with '\\' prefix") }, +#endif /* XCASE */ +#ifdef DEFECHO +{ "defecho", BIT, L_FLAG, 0, DEFECHO, DEFECHO }, +#endif /* DEFECHO */ +#ifdef FLUSHO +{ "flusho", BIT, L_FLAG, 0, FLUSHO, FLUSHO, C("Discard (do not discard) written data. Cleared by subsequent input") }, +#endif /* FLUSHO */ +#ifdef PENDIN +{ "pendin", BIT, L_FLAG, 0, PENDIN, PENDIN, C("Redisplay pending input at next read and then automatically clear \bpendin\b") }, +#endif /* PENDIN */ +{ "noflsh", BIT, L_FLAG, US, NOFLSH, NOFLSH, C("Disable (enable) flushing after \bintr\b and \bquit\b special characters") }, +#ifdef TOSTOP +{ "tostop", BIT, L_FLAG, NL|US, TOSTOP, TOSTOP, C("Stop (do not stop) background jobs that try to write to the terminal") }, +#endif /* TOSTOP */ +#ifdef OLCUC +{ "olcuc", BIT, O_FLAG, US, OLCUC, OLCUC, C("Translate (do not translate) lowercase characters to uppercase") }, +#endif /* OLCUC */ +#ifdef ONLCR +{ "onlcr", BIT, O_FLAG, SS, ONLCR, ONLCR, C("Translate (do not translate) newline to carriage return-newline") }, +#endif /* ONLCR */ +#ifdef ONLRET +{ "onlret", BIT, O_FLAG, US, ONLRET, ONLRET, C("Newline performs (does not perform) a carriage return") }, +#endif /* ONLRET */ +#ifdef OCRNL +{ "ocrnl", BIT, O_FLAG, US, OCRNL, OCRNL, C("Translate (do not translate) carriage return to newline") }, +#endif /* OCRNL */ +#ifdef ONOCR +{ "onocr", BIT, O_FLAG, US, ONOCR, ONOCR, C("Do not (do) print carriage returns in the first column") }, +#endif /* ONOCR */ +#ifdef OFILL +{ "ofill", BIT, O_FLAG, US, OFILL, OFILL, C("Use fill characters (use timing) for delays") }, +#endif /* OFILL */ +#ifdef OFDEL +{ "ofdel", BIT, O_FLAG, US, OFDEL, OFDEL, C("Use DEL (NUL) as fill characters for delays") }, +#endif /* OFDEL */ +{ "opost", BIT, O_FLAG, SS, OPOST, OPOST, C(" Postprocess (do not postprocess) output") }, +#ifdef CRDLY +{ "cr0", BITS, O_FLAG, IG|SS, CRDLY, CR0 }, +{ "cr1", BITS, O_FLAG, US, CRDLY, CR1 }, +{ "cr2", BITS, O_FLAG, US, CRDLY, CR2 }, +{ "cr3", BITS, O_FLAG, US, CRDLY, CR3 }, +#endif +#ifdef NLDLY +{ "nl0", BITS, O_FLAG, IG|US, NLDLY, NL0 }, +{ "nl1", BITS, O_FLAG, US, NLDLY, NL1 }, +#endif +#ifdef TABDLY +{ "tabs", TABS, O_FLAG, IG, TABDLY, TAB3, C("Preserve (expand to spaces) tabs") }, +{ "tab0", BITS, O_FLAG, IG|SS, TABDLY, TAB0 }, +{ "tab1", BITS, O_FLAG, US, TABDLY, TAB1 }, +{ "tab2", BITS, O_FLAG, US, TABDLY, TAB2 }, +{ "tab3", BITS, O_FLAG, US, TABDLY, TAB3 }, +#endif +#ifdef BSDLY +{ "bs0", BITS, O_FLAG, IG|SS, BSDLY, BS0 }, +{ "bs1", BITS, O_FLAG, US, BSDLY, BS1 }, +#endif +#ifdef VTDLY +{ "vt0", BITS, O_FLAG, IG|SS, VTDLY, VT0 }, +{ "vt1", BITS, O_FLAG, US, VTDLY, VT1 }, +#endif +#ifdef FFDLY +{ "ff0", BITS, O_FLAG, IG|SS, FFDLY, FF0 }, +{ "ff1", BITS, O_FLAG, US, FFDLY, FF1 }, +#endif +{ "", MIXED, O_FLAG, NL|IG }, + +{ "evenp", MIXED, C_FLAG, IG, PARENB, 0, C("Same as \bparenb -parodd cs7\b") }, +{ "oddp", MIXED, C_FLAG, IG, PARODD, 0, C("Same as \bparenb parodd cs7\b") }, +{ "parity", MIXED, C_FLAG, IG, 0, 0, C("Same as parenb \b-parodd cs7\b") }, +{ "ek", MIXED, C_FLAG, IG, 0, 0, C("Reset the \berase\b and \bkill\b special characters to their default values") }, +{ "sane", SANE, C_FLAG, IG, 0, 0, C("Reset all modes to some reasonable values") }, +{ "cooked", COOKED, C_FLAG, IG, 0, 0, C("Disable raw input and output") }, +{ "raw", COOKED, C_FLAG, IG, 0, 0, C("Enable raw input and output") }, +{ "lcase", CASE, C_FLAG, IG, 0 , 0, C("Set \bxcase\b, \biuclc\b, and \bolcuc\b") }, +{ "LCASE", CASE, C_FLAG, IG, 0 , 0, C("Same as \blcase\b") } +}; + +#if CC_NATIVE == CC_ASCII +#define cntl(x) (((x)=='?')?0177:((x)&037)) +#else +#define cntl(x) (((x)=='?')?ccmapc(0177,CC_ASCII,CC_NATIVE):ccmapc(ccmapc(x,CC_NATIVE,CC_ASCII)&037,CC_ASCII,CC_NATIVE)) +#endif + +static void sane(register struct termios *sp) +{ + register const Tty_t* tp; + + for (tp = Ttable; tp < &Ttable[elementsof(Ttable)]; tp++) + if (tp->flags & (SS|US)) + switch (tp->type) + { + case BIT: + case BITS: + switch (tp->field) + { + case C_FLAG: + if (tp->flags & SS) + sp->c_cflag |= tp->mask; + else + sp->c_cflag &= ~tp->mask; + break; + case I_FLAG: + if (tp->flags & SS) + sp->c_iflag |= tp->mask; + else + sp->c_iflag &= ~tp->mask; + break; + case O_FLAG: + if (tp->flags & SS) + sp->c_oflag |= tp->mask; + else + sp->c_oflag &= ~tp->mask; + break; + case L_FLAG: + if (tp->flags & SS) + sp->c_lflag |= tp->mask; + else + sp->c_lflag &= ~tp->mask; + break; + } + break; + case CHAR: + sp->c_cc[tp->mask] = cntl(tp->val); + break; + } +} + +static int gin(char *arg,struct termios *sp) +{ + register int i; + if(*arg++ != ':') + return(0); + sp->c_iflag = strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + sp->c_oflag = strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + sp->c_cflag = strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + sp->c_lflag = strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + for(i=0;i< NCCS; i++) + { + sp->c_cc[i] = strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + } +#if _mem_c_line_termios + sp->c_line = +#endif + strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + i = strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + cfsetispeed(sp, i); + i = strtol(arg,&arg,16); + if(*arg++ != ':') + return(0); + cfsetospeed(sp, i); + if(*arg) + return(0); + return(1); +} + +static void gout(struct termios *sp) +{ + register int i; + sfprintf(sfstdout,":%x",sp->c_iflag); + sfprintf(sfstdout,":%x",sp->c_oflag); + sfprintf(sfstdout,":%x",sp->c_cflag); + sfprintf(sfstdout,":%x",sp->c_lflag); + for(i=0;i< NCCS; i++) + sfprintf(sfstdout,":%x",sp->c_cc[i]); +#if _mem_c_line_termios + sfprintf(sfstdout,":%x", sp->c_line); +#else + sfprintf(sfstdout,":%x", 0); +#endif + sfprintf(sfstdout,":%x",cfgetispeed(sp)); + sfprintf(sfstdout,":%x",cfgetospeed(sp)); + sfprintf(sfstdout,":\n"); +} + +static void output(struct termios *sp, int flags) +{ + const Tty_t *tp; + struct termios tty; + register int delim = ' '; + register int i,off,off2; + char schar[2]; + unsigned int ispeed = cfgetispeed(sp); + unsigned int ospeed = cfgetospeed(sp); + if(flags&G_FLAG) + { + gout(sp); + return; + } + tty = *sp; + sane(&tty); + for(i=0; i < elementsof(Ttable); i++) + { + tp= &Ttable[i]; + if(tp->flags&IG) + { + if(tp->flags&NL) + sfputc(sfstdout,'\n'); + continue; + } + switch(tp->type) + { + case BIT: + case BITS: + off = 1; + switch(tp->field) + { + case C_FLAG: + if(sp->c_cflag&tp->mask) + off = 0; + if(tty.c_cflag&tp->mask) + off2 = 0; + break; + case I_FLAG: + if(sp->c_iflag&tp->mask) + off = 0; + if(tty.c_iflag&tp->mask) + off2 = 0; + break; + case O_FLAG: + if((sp->c_oflag&tp->mask)==tp->val) + off = 0; + if(tty.c_oflag&tp->mask) + off2 = 0; + break; + case L_FLAG: + if(sp->c_lflag&tp->mask) + off = 0; + if(tty.c_lflag&tp->mask) + off2 = 0; + } + if(tp->flags&NL) + delim = '\n'; + if(!flags && off==off2) + continue; + if(!off) + sfprintf(sfstdout,"%s%c",tp->name,delim); + else if(tp->type==BIT) + sfprintf(sfstdout,"-%s%c",tp->name,delim); + delim = ' '; + break; + + case CHAR: + off = sp->c_cc[tp->mask]; + if(tp->flags&NL) + delim = '\n'; + if(!flags && off==(unsigned char)tty.c_cc[tp->mask]) + continue; + if(off==_POSIX_VDISABLE) + sfprintf(sfstdout,"%s = <undef>;%c",tp->name,delim); + else if(isprint(off&0xff)) + sfprintf(sfstdout,"%s = %c;%c",tp->name,off,delim); + else +#if CC_NATIVE == CC_ASCII + sfprintf(sfstdout,"%s = ^%c;%c",tp->name,off==0177?'?':(off^0100),delim); +#else + { + off = ccmapc(off, CC_NATIVE, CC_ASCII); + sfprintf(sfstdout,"%s = ^%c;%c",tp->name,off==0177?'?':ccmapc(off^0100,CC_ASCII,CC_NATIVE),delim); + } +#endif + delim = ' '; + break; + case SIZE: + if((sp->c_cflag&CSIZE)!=tp->mask) + continue; + if(flags || (sp->c_cflag&CSIZE) != (tty.c_cflag&CSIZE)) + sfprintf(sfstdout,"%s ",tp->name); + break; + case SPEED: + if(tp->mask==ispeed) + { + if(ispeed!=ospeed) + schar[0]='i'; + else + schar[0]=0; + } + else if(tp->mask==ospeed) + schar[0]='o'; + else + continue; + schar[1] = 0; +#ifdef TIOCSWINSZ + { + struct winsize win; + off = ioctl(0,TIOCGWINSZ,&win); + if(off>=0) + sfprintf(sfstdout,"%sspeed %s baud; rows %d; columns %d;\n",schar,tp->name,win.ws_row,win.ws_col); + } + if(off<0) +#endif + sfprintf(sfstdout,"%sspeed %s baud;\n",schar,tp->name); + } + } + if(delim=='\n') + sfputc(sfstdout,'\n'); +} + +static const Tty_t *lookup(const char *name) +{ + register int i; + for(i=0; i < elementsof(Ttable); i++) + { + if(strcmp(Ttable[i].name,name)==0) + return(&Ttable[i]); + } + return(0); + +} + +static const Tty_t *getspeed(unsigned long val) +{ + register int i; + for(i=0; i < elementsof(Ttable); i++) + { + if(Ttable[i].type==SPEED && Ttable[i].mask==val) + return(&Ttable[i]); + } + return(0); +} + +static int gettchar(register const char *cp) +{ + if(*cp==0) + return(-1); + if(cp[1]==0) + return((unsigned)cp[0]); + if(*cp=='^' && cp[1] && cp[2]==0) + { + switch(cp[1]) + { + case '-': + return(-1); + default: + return(cntl(cp[1])); + } + } + if(streq(cp,"undef") || streq(cp,"<undef>")) + return(-1); + return(*((unsigned char*)cp)); +} + +static void set(char *argv[], struct termios *sp) +{ + const Tty_t *tp; + register int c,off; + char *cp; + char *ep; + while(cp = *argv++) + { + off = 0; + if(*cp=='-') + { + cp++; + off=1; + } + if(!(tp=lookup(cp)) || (off && (tp->type!=BIT) && (tp->type!=TABS))) + error(ERROR_exit(1),"%s: unknown mode",cp); + switch(tp->type) + { + case CHAR: + if(off) + error(ERROR_exit(1),"%s: unknown mode",cp); + if(!*argv) + error(ERROR_exit(1),"missing argument to %s",cp); + c = gettchar(*argv++); + if(c>=0) + sp->c_cc[tp->mask] = c; + else + sp->c_cc[tp->mask] = _POSIX_VDISABLE; + break; + case BIT: case BITS: + switch(tp->field) + { + case C_FLAG: + if(off) + sp->c_cflag &= ~tp->mask; + else + sp->c_cflag |= tp->mask; + break; + case I_FLAG: + if(off) + sp->c_iflag &= ~tp->mask; + else + sp->c_iflag |= tp->mask; + break; + case O_FLAG: + sp->c_oflag &= ~tp->mask; + sp->c_oflag |= tp->val; + break; + case L_FLAG: + if(off) + sp->c_lflag &= ~tp->mask; + else + sp->c_lflag |= tp->mask; + break; + } + break; + case TABS: + sp->c_oflag &= ~tp->mask; + if(off) + sp->c_oflag |= tp->val; + break; +#ifdef TIOCSWINSZ + case WIND: + { + struct winsize win; + int n; + if(ioctl(0,TIOCGWINSZ,&win)<0) + error(ERROR_system(1),"cannot set %s",tp->name); + if(!(cp= *argv)) + { + sfprintf(sfstdout,"%d\n",tp->mask?win.ws_col:win.ws_row); + break; + } + argv++; + n=strtol(cp,&cp,10); + if(*cp) + error(ERROR_system(1),"%d: invalid number of %s",argv[-1],tp->name); + if(tp->mask) + win.ws_col = n; + else + win.ws_row = n; + if(ioctl(0,TIOCSWINSZ,&win)<0) + error(ERROR_system(1),"cannot set %s",tp->name); + break; + } +#endif + case NUM: + cp = *argv; + if (!cp) + { + if (tp->field == C_SPEED) + { + if (tp = getspeed(*tp->name == 'i' ? cfgetispeed(sp) : cfgetospeed(sp))) + sfprintf(sfstdout, "%s\n", tp->name); + break; + } + error(ERROR_exit(1), "%s: missing numeric argument", tp->name); + } + argv++; + c = (int)strtol(cp, &ep, 10); + if (*ep) + error(ERROR_exit(1), "%s: %s: numeric argument expected", tp->name, cp); + switch (tp->field) + { +#if _mem_c_line_termios + case C_LINE: + sp->c_line = c; + break; +#endif + case C_SPEED: + if(getspeed(c)) + { + if (*tp->name != 'o') + cfsetispeed(sp, c); + if (*tp->name != 'i') + cfsetospeed(sp, c); + } + else + error(ERROR_exit(1), "%s: %s: invalid speed", tp->name, cp); + break; + case T_CHAR: + sp->c_cc[tp->mask] = c; + break; + } + break; + case SPEED: + cfsetospeed(sp, tp->mask); + cfsetispeed(sp, tp->mask); + break; + case SIZE: + sp->c_cflag &= ~CSIZE; + sp->c_cflag |= tp->mask; + break; + case SANE: + sane(sp); + break; +#if defined(OLCUC) && defined(IUCLC) + case CASE: + if(off) + { + sp->c_iflag |= IUCLC; + sp->c_oflag |= OLCUC; + } + else + { + sp->c_iflag &= ~IUCLC; + sp->c_oflag &= ~OLCUC; + } + break; +#endif /* OLCUC && IUCLC */ + } + } +} + + +static void listchars(Sfio_t *sp,int type) +{ + int i,c; + c = (type==CHAR?'c':'n'); + for(i=0; i < elementsof(Ttable); i++) + { + if(Ttable[i].type==type && *Ttable[i].description) + sfprintf(sp,"[+%s \a%c\a?%s.]",Ttable[i].name,c,Ttable[i].description); + } +} + +static void listgroup(Sfio_t *sp,int type, const char *description) +{ + int i; + sfprintf(sp,"[+"); + for(i=0; i < elementsof(Ttable); i++) + { + if(Ttable[i].type==type) + sfprintf(sp,"%s ",Ttable[i].name); + } + sfprintf(sp,"?%s.]",description); +} + +static void listmask(Sfio_t *sp,unsigned int mask,const char *description) +{ + int i; + sfprintf(sp,"[+"); + for(i=0; i < elementsof(Ttable); i++) + { + if(Ttable[i].mask==mask && Ttable[i].type==BITS) + sfprintf(sp,"%s ",Ttable[i].name); + } + sfprintf(sp,"?%s.]",description); +} + +static void listfields(Sfio_t *sp,int field) +{ + int i; + for(i=0; i < elementsof(Ttable); i++) + { + if(Ttable[i].field==field && Ttable[i].type==BIT && *Ttable[i].description) + sfprintf(sp,"[+%s (-%s)?%s.]",Ttable[i].name,Ttable[i].name,Ttable[i].description); + } +} + +static void listmode(Sfio_t *sp,const char *name) +{ + sfprintf(sp,"[+%s?%s.]",name,lookup(name)->description); +} + +static int infof(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp) +{ + NoP(op); + NoP(s); + NoP(dp); + sfprintf(sp,"[+Control Modes.]{"); + listfields(sp,C_FLAG); + listgroup(sp,SPEED,"Attempt to set input and output baud rate to number given. A value of \b0\b causes immediate hangup"); + listchars(sp,NUM); + listgroup(sp,SIZE,"Number of bits in a character"); + sfprintf(sp,"}[+Input Modes.]{"); + listfields(sp,I_FLAG); + sfprintf(sp,"}[+Output Modes.]{"); + listfields(sp,O_FLAG); +#ifdef CRDLY + listmask(sp,CRDLY,"Carriage return delay style"); +#endif +#ifdef NLDLY + listmask(sp,NLDLY,"Newline delay style"); +#endif +#ifdef TABDLY + listmask(sp,TABDLY,"Horizontal tab delay style"); +#endif +#ifdef BSDLY + listmask(sp,BSDLY,"Backspace delay style"); +#endif +#ifdef FFDLY + listmask(sp,FFDLY,"Form feed delay style"); +#endif +#ifdef VTDLY + listmask(sp,VTDLY,"Vertical tab delay style"); +#endif + sfprintf(sp,"}[+Local Modes.]{"); + listfields(sp,L_FLAG); + sfprintf(sp,"}[+Control Assignments.?If \ac\a is \bundef\b or an empty " + "string then the control assignment is disabled.]{"); + listchars(sp,WIND); + listchars(sp,CHAR); + sfprintf(sp,"}[+Combination Modes.]{"); + listmode(sp,"ek"); + listmode(sp,"evenp"); + listmode(sp,"lcase"); + listmode(sp,"oddp"); + listmode(sp,"parity"); + listmode(sp,"sane"); + listmode(sp,"tabs"); + listmode(sp,"LCASE"); + sfputc(sp,'}'); + return(1); +} + +int +b_stty(int argc, char** argv, void* context) +{ + struct termios tty; + register int n; + register int flags = 0; + const Tty_t* tp; + Optdisc_t disc; + + cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_INTERACTIVE); + if (tcgetattr(0, &tty) < 0) + error(ERROR_system(1),"not a tty"); + memset(&disc, 0, sizeof(disc)); + disc.version = OPT_VERSION; + disc.infof = infof; + opt_info.disc = &disc; + for (;;) + { + switch (n = optget(argv, usage)) + { + case 'a': + case 'g': + if (!opt_info.offset || !argv[opt_info.index][opt_info.offset]) + { + switch (n) + { + case 'a': + flags |= A_FLAG; + break; + case 'g': + flags |= G_FLAG; + break; + } + continue; + } + /*FALLTHROUGH*/ + case ':': + if (!opt_info.offset) + error(2, "%s", opt_info.arg); + else if (!(tp = lookup(argv[opt_info.index]+1)) || (tp->type != BIT && tp->type != TABS)) + error(ERROR_exit(1), "%s: unknown mode", argv[opt_info.index]); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (error_info.errors || (flags && *argv)) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (*argv) + { + if (!argv[1] && **argv == ':') + gin(*argv, &tty); + else + set(argv, &tty); + if (tcsetattr(0, TCSANOW, &tty) < 0) + error(ERROR_system(1), "cannot set tty"); + } + else + output(&tty, flags); + return error_info.errors; +} diff --git a/usr/src/lib/libcmd/common/sum.c b/usr/src/lib/libcmd/common/sum.c new file mode 100644 index 0000000000..3fa2e520e2 --- /dev/null +++ b/usr/src/lib/libcmd/common/sum.c @@ -0,0 +1,35 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * Glenn Fowler + * AT&T Research + * + * sum -- implemented by cksum + */ + +#include <cmd.h> + +int +b_sum(int argc, register char** argv, void* context) +{ + return b_cksum(argc, argv, context); +} diff --git a/usr/src/lib/libcmd/common/sync.c b/usr/src/lib/libcmd/common/sync.c new file mode 100644 index 0000000000..d977048766 --- /dev/null +++ b/usr/src/lib/libcmd/common/sync.c @@ -0,0 +1,79 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Research + */ + +static const char usage[] = +"[-?\n@(#)$Id: sync (AT&T Research) 2006-10-04 $\n]" +USAGE_LICENSE +"[+NAME?sync - schedule file system updates]" +"[+DESCRIPTION?\bsync\b calls \bsync\b(2), which causes all information " + "in memory that updates file systems to be scheduled for writing out to " + "all file systems. The writing, although scheduled, is not necessarily " + "complete upon return from \bsync\b.]" +"[+?Since \bsync\b(2) has no failure indication, \bsync\b only fails for " + "option/operand syntax errors, or when \bsync\b(2) does not return, in " + "which case \bsync\b also does not return.]" +"[+?At minimum \bsync\b should be called before halting the system. Most " + "systems provide graceful shutdown procedures that include \bsync\b -- " + "use them if possible.]" +"[+EXIT STATUS?]" + "{" + "[+0?\bsync\b(2) returned.]" + "[+>0?Option/operand syntax error.]" + "}" +"[+SEE ALSO?\bsync\b(2), \bshutdown\b(8)]" +; + +#include <cmd.h> +#include <ls.h> + +int +b_sync(int argc, char** argv, void* context) +{ + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + for (;;) + { + switch (optget(argv, usage)) + { + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (error_info.errors || *argv) + error(ERROR_usage(2), "%s", optusage(NiL)); +#if _lib_sync + sync(); +#else + error(ERROR_usage(2), "failed -- the native system does not provide a sync(2) call"); +#endif + return 0; +} diff --git a/usr/src/lib/libcmd/common/tail.c b/usr/src/lib/libcmd/common/tail.c new file mode 100644 index 0000000000..83de0f7c91 --- /dev/null +++ b/usr/src/lib/libcmd/common/tail.c @@ -0,0 +1,683 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 + +/* + * print the tail of one or more files + * + * David Korn + * Glenn Fowler + */ + +static const char usage[] = +"+[-?\n@(#)$Id: tail (AT&T Research) 2006-10-18 $\n]" +USAGE_LICENSE +"[+NAME?tail - output trailing portion of one or more files ]" +"[+DESCRIPTION?\btail\b copies one or more input files to standard output " + "starting at a designated point for each file. Copying starts " + "at the point indicated by the options and is unlimited in size.]" +"[+?By default a header of the form \b==> \b\afilename\a\b <==\b " + "is output before all but the first file but this can be changed " + "with the \b-q\b and \b-v\b options.]" +"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \btail\b " + "copies from standard input. The start of the file is defined " + "as the current offset.]" +"[+?The option argument for \b-c\b can optionally be " + "followed by one of the following characters to specify a different " + "unit other than a single byte:]{" + "[+b?512 bytes.]" + "[+k?1-kilobyte.]" + "[+m?1-megabyte.]" + "}" +"[+?For backwards compatibility, \b-\b\anumber\a is equivalent to " + "\b-n\b \anumber\a and \b+\b\anumber\a is equivalent to " + "\b-n -\b\anumber\a.]" + +"[n:lines]:[lines:=10?Copy \alines\a lines from each file. A negative value " + "for \alines\a indicates an offset from the start of the file.]" +"[c:bytes]:?[chars?Copy \achars\a bytes from each file. A negative value " + "for \achars\a indicates an offset from the start of the file.]" +"[f:forever|follow?Loop forever trying to read more characters as the " + "end of each file to copy new data. Ignored if reading from a pipe " + "or fifo.]" +"[h!:headers?Output filename headers.]" +"[L:log?When a \b--forever\b file times out via \b--timeout\b, verify that " + "the curent file has not been renamed and replaced by another file " + "of the same name (a common log file practice) before giving up on " + "the file.]" +"[q:quiet?Don't output filename headers. For GNU compatibility.]" +"[r:reverse?Output lines in reverse order.]" +"[s:silent?Don't warn about timeout expiration and log file changes.]" +"[t:timeout?Stop checking after \atimeout\a elapses with no additional " + "\b--forever\b output. A separate elapsed time is maintained for " + "each file operand. There is no timeout by default. The default " + "\atimeout\a unit is seconds. \atimeout\a may be a catenation of 1 " + "or more integers, each followed by a 1 character suffix. The suffix " + "may be omitted from the last integer, in which case it is " + "interpreted as seconds. The supported suffixes are:]:[timeout]{" + "[+s?seconds]" + "[+m?minutes]" + "[+h?hours]" + "[+d?days]" + "[+w?weeks]" + "[+M?months]" + "[+y?years]" + "[+S?scores]" + "}" +"[v:verbose?Always ouput filename headers.]" +"\n" +"\n[file ...]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files copied successfully.]" + "[+>0?One or more files did not copy.]" +"}" +"[+SEE ALSO?\bcat\b(1), \bhead\b(1), \brev\b(1)]" +; + +#include <cmd.h> +#include <ctype.h> +#include <ls.h> +#include <tm.h> +#include <rev.h> + +#define COUNT (1<<0) +#define ERROR (1<<1) +#define FOLLOW (1<<2) +#define HEADERS (1<<3) +#define LOG (1<<4) +#define NEGATIVE (1<<5) +#define POSITIVE (1<<6) +#define REVERSE (1<<7) +#define SILENT (1<<8) +#define TIMEOUT (1<<9) +#define VERBOSE (1<<10) + +#define NOW (unsigned long)time(NiL) + +#define LINES 10 + +#ifdef S_ISSOCK +#define FIFO(m) (S_ISFIFO(m)||S_ISSOCK(m)) +#else +#define FIFO(m) S_ISFIFO(m) +#endif + +struct Tail_s; typedef struct Tail_s Tail_t; + +struct Tail_s +{ + Tail_t* next; + char* name; + Sfio_t* sp; + Sfoff_t last; + unsigned long expire; + long dev; + long ino; +}; + +/* + * if file is seekable, position file to tail location and return offset + * otherwise, return -1 + */ + +static Sfoff_t +tailpos(register Sfio_t* fp, register Sfoff_t number, int delim) +{ + register size_t n; + register Sfoff_t offset; + register Sfoff_t first; + register Sfoff_t last; + register char* s; + register char* t; + struct stat st; + + last = sfsize(fp); + if ((first = sfseek(fp, (Sfoff_t)0, SEEK_CUR)) < 0) + return last || fstat(sffileno(fp), &st) || st.st_size || FIFO(st.st_mode) ? -1 : 0; + if (delim < 0) + { + if ((offset = last - number) < first) + return first; + return offset; + } + if ((offset = last - SF_BUFSIZE) < first) + offset = first; + for (;;) + { + sfseek(fp, offset, SEEK_SET); + n = last - offset; + if (!(s = sfreserve(fp, n, SF_LOCKR))) + return -1; + t = s + n; + while (t > s) + if (*--t == delim && number-- <= 0) + { + sfread(fp, s, 0); + return offset + (t - s) + 1; + } + sfread(fp, s, 0); + if (offset == first) + break; + last = offset; + if ((offset = last - SF_BUFSIZE) < first) + offset = first; + } + return first; +} + +/* + * this code handles tail from a pipe without any size limits + */ + +static void +pipetail(Sfio_t* infile, Sfio_t* outfile, Sfoff_t number, int delim) +{ + register Sfio_t* out; + register Sfoff_t n; + register Sfoff_t nleft = number; + register size_t a = 2 * SF_BUFSIZE; + register int fno = 0; + Sfoff_t offset[2]; + Sfio_t* tmp[2]; + + if (delim < 0 && a > number) + a = number; + out = tmp[0] = sftmp(a); + tmp[1] = sftmp(a); + offset[0] = offset[1] = 0; + while ((n = sfmove(infile, out, number, delim)) > 0) + { + offset[fno] = sftell(out); + if ((nleft -= n) <= 0) + { + out = tmp[fno= !fno]; + sfseek(out, (Sfoff_t)0, SEEK_SET); + nleft = number; + } + } + if (nleft == number) + { + offset[fno] = 0; + fno= !fno; + } + sfseek(tmp[0], (Sfoff_t)0, SEEK_SET); + + /* + * see whether both files are needed + */ + + if (offset[fno]) + { + sfseek(tmp[1], (Sfoff_t)0, SEEK_SET); + if ((n = number - nleft) > 0) + sfmove(tmp[!fno], NiL, n, delim); + if ((n = offset[!fno] - sftell(tmp[!fno])) > 0) + sfmove(tmp[!fno], outfile, n, -1); + } + else + fno = !fno; + sfmove(tmp[fno], outfile, offset[fno], -1); + sfclose(tmp[0]); + sfclose(tmp[1]); +} + +/* + * (re)initialize a tail stream + */ + +static int +init(Tail_t* tp, Sfoff_t number, int delim, int flags) +{ + Sfoff_t offset; + struct stat st; + + if (tp->sp) + { + offset = 0; + if (tp->sp == sfstdin) + tp->sp = 0; + } + else if (!number) + offset = 0; + else + offset = 1; + if (!tp->name || streq(tp->name, "-")) + { + tp->name = "/dev/stdin"; + tp->sp = sfstdin; + } + else if (!(tp->sp = sfopen(tp->sp, tp->name, "r"))) + { + error(ERROR_system(0), "%s: cannot open", tp->name); + return -1; + } + sfset(tp->sp, SF_SHARE, 0); + if (offset) + { + if ((offset = tailpos(tp->sp, number, delim)) < 0) + { + error(ERROR_SYSTEM|2, "%s: cannot position file to tail", tp->name); + goto bad; + } + sfseek(tp->sp, offset, SEEK_SET); + } + tp->last = offset; + if (flags & LOG) + { + if (fstat(sffileno(tp->sp), &st)) + { + error(ERROR_system(0), "%s: cannot stat", tp->name); + goto bad; + } + tp->dev = st.st_dev; + tp->ino = st.st_ino; + } + return 0; + bad: + if (tp->sp != sfstdin) + sfclose(tp->sp); + tp->sp = 0; + return -1; +} + +/* + * convert number with validity diagnostics + */ + +static intmax_t +num(register const char* s, char** e, int* f, int o) +{ + intmax_t number; + char* t; + int c; + + *f &= ~(ERROR|NEGATIVE|POSITIVE); + if ((c = *s) == '-') + { + *f |= NEGATIVE; + s++; + } + else if (c == '+') + { + *f |= POSITIVE; + s++; + } + while (*s == '0' && isdigit(*(s + 1))) + s++; + errno = 0; + number = strtonll(s, &t, NiL, 0); + if (!o && t > s && *(t - 1) == 'l') + t--; + if (t == s) + number = LINES; + if (o && *t) + { + number = 0; + *f |= ERROR; + error(2, "-%c: %s: invalid numeric argument -- unknown suffix", o, s); + } + else if (errno) + { + *f |= ERROR; + if (o) + error(2, "-%c: %s: invalid numeric argument -- out of range", o, s); + else + error(2, "%s: invalid numeric argument -- out of range", s); + } + else + { + *f |= COUNT; + if (c == '-') + number = -number; + } + if (e) + *e = t; + return number; +} + +int +b_tail(int argc, char** argv, void* context) +{ + static const char header_fmt[] = "\n==> %s <==\n"; + + register Sfio_t* ip; + register int n; + register int i; + register int delim = '\n'; + int flags = HEADERS; + char* s; + char* t; + char* r; + char* e; + char* file; + Sfoff_t offset; + Sfoff_t number = LINES; + unsigned long timeout = 0; + struct stat st; + const char* format = header_fmt+1; + size_t z; + Sfio_t* op; + register Tail_t* fp; + register Tail_t* pp; + register Tail_t* hp; + Tail_t* files; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + for (;;) + { + switch (n = optget(argv, usage)) + { + case 'c': + delim = -1; + if (opt_info.arg && *opt_info.arg=='f' && !*(opt_info.arg+1)) + { + flags |= FOLLOW; + continue; + } + /*FALLTHROUGH*/ + case 'n': + case 'N': + flags |= COUNT; + if (s = opt_info.arg) + number = num(s, &s, &flags, n); + else + { + number = LINES; + flags &= ~(ERROR|NEGATIVE|POSITIVE); + s = ""; + } + if (n=='c' && *s=='f') + { + s++; + flags |= FOLLOW; + } + if (flags & ERROR) + continue; + if (flags & (NEGATIVE|POSITIVE)) + number = -number; + if (opt_info.option[0]=='+') + number = -number; + continue; + case 'f': + flags |= FOLLOW; + continue; + case 'h': + if (opt_info.num) + flags |= HEADERS; + else + flags &= ~HEADERS; + continue; + case 'L': + flags |= LOG; + continue; + case 'q': + flags &= ~HEADERS; + continue; + case 'r': + flags |= REVERSE; + continue; + case 's': + flags |= SILENT; + continue; + case 't': + flags |= TIMEOUT; + timeout = strelapsed(opt_info.arg, &s, 1); + if (*s) + error(ERROR_exit(1), "%s: invalid elapsed time", opt_info.arg); + continue; + case 'v': + flags |= VERBOSE; + continue; + case ':': + /* handle old style arguments */ + r = s = argv[opt_info.index]; + number = num(s, &t, &flags, 0); + for (;;) + { + switch (*t++) + { + case 0: + opt_info.offset = t - r - 1; + if (number) + number = -number; + break; + case 'c': + delim = -1; + continue; + case 'f': + flags |= FOLLOW; + continue; + case 'l': + delim = '\n'; + continue; + case 'r': + flags |= REVERSE; + continue; + default: + error(2, "%s: invalid suffix", t - 1); + opt_info.offset = strlen(r); + break; + } + break; + } + continue; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (!*argv) + { + flags &= ~HEADERS; + if (fstat(0, &st)) + error(ERROR_system(0), "/dev/stdin: cannot stat"); + else if (FIFO(st.st_mode)) + flags &= ~FOLLOW; + } + else if (!*(argv + 1)) + flags &= ~HEADERS; + if (flags & REVERSE) + { + if (delim < 0) + error(2, "--reverse requires line mode"); + else if (!(flags & COUNT)) + number = 0; + flags &= ~FOLLOW; + } + if ((flags & (FOLLOW|TIMEOUT)) == TIMEOUT) + { + flags &= ~TIMEOUT; + timeout = 0; + error(ERROR_warn(0), "--timeout ignored for --noforever"); + } + if ((flags & (LOG|TIMEOUT)) == LOG) + { + flags &= ~LOG; + error(ERROR_warn(0), "--log ignored for --notimeout"); + } + if (error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (flags & FOLLOW) + { + if (!(fp = (Tail_t*)stakalloc(argc * sizeof(Tail_t)))) + error(ERROR_system(1), "out of space"); + files = 0; + s = *argv; + do + { + fp->name = s; + fp->sp = 0; + if (!init(fp, number, delim, flags)) + { + fp->expire = timeout ? (NOW + timeout + 1) : 0; + if (files) + pp->next = fp; + else + files = fp; + pp = fp; + fp++; + } + } while (s && (s = *++argv)); + if (!files) + return error_info.errors != 0; + pp->next = 0; + hp = 0; + for (;;) + { + if (sfsync(sfstdout)) + error(ERROR_system(1), "write error"); + sleep(1); + n = 0; + pp = 0; + fp = files; + while (fp) + { + if (fstat(sffileno(fp->sp), &st)) + error(ERROR_system(0), "%s: cannot stat", fp->name); + else if (st.st_size > fp->last) + { + n = 1; + if (timeout) + fp->expire = NOW + timeout; + z = st.st_size - fp->last; + i = 0; + if ((s = sfreserve(fp->sp, z, SF_LOCKR)) || (z = sfvalue(fp->sp)) && (s = sfreserve(fp->sp, z, SF_LOCKR)) && (i = 1)) + { + r = 0; + for (e = (t = s) + z; t < e; t++) + if (*t == '\n') + r = t; + if (r || i && (r = e)) + { + if ((flags & (HEADERS|VERBOSE)) && hp != fp) + { + hp = fp; + sfprintf(sfstdout, format, fp->name); + format = header_fmt; + } + z = r - s + 1; + fp->last += z; + sfwrite(sfstdout, s, z); + } + else + z = 0; + sfread(fp->sp, s, z); + } + goto next; + } + else if (!timeout || fp->expire > NOW) + goto next; + else + { + if (flags & LOG) + { + i = 3; + while (--i && stat(fp->name, &st)) + sleep(1); + if (i && (fp->dev != st.st_dev || fp->ino != st.st_ino) && !init(fp, 0, 0, flags)) + { + if (!(flags & SILENT)) + error(ERROR_warn(0), "%s: log file change", fp->name); + fp->expire = NOW + timeout; + goto next; + } + } + if (!(flags & SILENT)) + error(ERROR_warn(0), "%s: %s timeout", fp->name, fmtelapsed(timeout, 1)); + } + if (fp->sp && fp->sp != sfstdin) + sfclose(fp->sp); + if (pp) + pp = pp->next = fp->next; + else if (!(files = files->next)) + return error_info.errors != 0; + fp = fp->next; + continue; + next: + pp = fp; + fp = fp->next; + } + } + } + else + { + if (file = *argv) + argv++; + do + { + if (!file || streq(file, "-")) + { + file = "/dev/stdin"; + ip = sfstdin; + } + else if (!(ip = sfopen(NiL, file, "r"))) + { + error(ERROR_system(0), "%s: cannot open", file); + continue; + } + if (flags & (HEADERS|VERBOSE)) + sfprintf(sfstdout, format, file); + format = header_fmt; + if (number < 0 || !number && (flags & POSITIVE)) + { + sfset(ip, SF_SHARE, !(flags & FOLLOW)); + if (number < -1) + sfmove(ip, NiL, -number - 1, delim); + if (flags & REVERSE) + rev_line(ip, sfstdout, sfseek(ip, (Sfoff_t)0, SEEK_CUR)); + else + sfmove(ip, sfstdout, SF_UNBOUND, -1); + } + else + { + sfset(ip, SF_SHARE, 0); + if ((offset = tailpos(ip, number, delim)) >= 0) + { + if (flags & REVERSE) + rev_line(ip, sfstdout, offset); + else + { + sfseek(ip, offset, SEEK_SET); + sfmove(ip, sfstdout, SF_UNBOUND, -1); + } + } + else + { + op = (flags & REVERSE) ? sftmp(4*SF_BUFSIZE) : sfstdout; + pipetail(ip, op, number, delim); + if (flags & REVERSE) + { + sfseek(op, (Sfoff_t)0, SEEK_SET); + rev_line(op, sfstdout, (Sfoff_t)0); + sfclose(op); + } + flags = 0; + } + } + if (ip != sfstdin) + sfclose(ip); + } while (file = *argv++); + } + return error_info.errors != 0; +} diff --git a/usr/src/lib/libcmd/common/tee.c b/usr/src/lib/libcmd/common/tee.c new file mode 100644 index 0000000000..243d6d0932 --- /dev/null +++ b/usr/src/lib/libcmd/common/tee.c @@ -0,0 +1,181 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * tee + */ + +static const char usage[] = +"[-?\n@(#)$Id: tee (AT&T Research) 2006-10-10 $\n]" +USAGE_LICENSE +"[+NAME?tee - duplicate standard input]" +"[+DESCRIPTION?\btee\b copies standard input to standard output " + "and to zero or more files. The options determine whether " + "the specified files are overwritten or appended to. The " + "\btee\b utility does not buffer output. If writes to any " + "\afile\a fail, writes to other files continue although \btee\b " + "will exit with a non-zero exit status.]" +"[+?The number of \afile\a operands that can be specified is limited " + "by the underlying operating system.]" +"[a:append?Append the standard input to the given files rather " + "than overwriting them.]" +"[i:ignore-interrupts?Ignore SIGINT signal.]" +"[l:linebuffer?Set the standard output to be line buffered.]" +"\n" +"\n[file ...]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files copies successfully.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bcat\b(1), \bsignal\b(3)]" +; + + +#include <cmd.h> +#include <ls.h> +#include <sig.h> + +typedef struct Tee_s +{ + Sfdisc_t disc; + int fd[1]; +} Tee_t; + +/* + * This discipline writes to each file in the list given in handle + */ + +static ssize_t tee_write(Sfio_t* fp, const void* buf, size_t n, Sfdisc_t* handle) +{ + register const char* bp; + register const char* ep; + register int* hp = ((Tee_t*)handle)->fd; + register int fd = sffileno(fp); + register ssize_t r; + + do + { + bp = (const char*)buf; + ep = bp + n; + while (bp < ep) + { + if ((r = write(fd, bp, ep - bp)) <= 0) + return(-1); + bp += r; + } + } while ((fd = *hp++) >= 0); + return(n); +} + +int +b_tee(int argc, register char** argv, void* context) +{ + register Tee_t* tp = 0; + register int oflag = O_WRONLY|O_TRUNC|O_CREAT|O_BINARY; + register int n; + register int* hp; + register char* cp; + int line; + Sfdisc_t tee_disc; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + line = -1; + while (n = optget(argv, usage)) switch (n) + { + case 'a': + oflag &= ~O_TRUNC; + oflag |= O_APPEND; + break; + case 'i': + signal(SIGINT, SIG_IGN); + break; + case 'l': + line = sfset(sfstdout, 0, 0) & SF_LINE; + if ((line == 0) == (opt_info.num == 0)) + line = -1; + else + sfset(sfstdout, SF_LINE, !!opt_info.num); + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + if(error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + argv += opt_info.index; + argc -= opt_info.index; + + /* + * for backward compatibility + */ + + if (*argv && streq(*argv, "-")) + { + signal(SIGINT, SIG_IGN); + argv++; + argc--; + } + if (argc > 0) + { + if (!(tp = (Tee_t*)stakalloc(sizeof(Tee_t) + argc * sizeof(int)))) + error(ERROR_exit(1), "no space"); + memset(&tee_disc, 0, sizeof(tee_disc)); + tee_disc.writef = tee_write; + tp->disc = tee_disc; + hp = tp->fd; + while (cp = *argv++) + { + if ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) + error(ERROR_system(0), "%s: cannot create", cp); + else hp++; + } + if (hp == tp->fd) + tp = 0; + else + { + *hp = -1; + sfdisc(sfstdout, &tp->disc); + } + } + if (sfmove(sfstdin, sfstdout, SF_UNBOUND, -1) < 0 || !sfeof(sfstdin) || sfsync(sfstdout)) + error(ERROR_system(1), "cannot copy"); + + /* + * close files and free resources + */ + + if (tp) + { + sfdisc(sfstdout, NiL); + if (line >= 0) + sfset(sfstdout, SF_LINE, line); + for(hp = tp->fd; (n = *hp) >= 0; hp++) + close(n); + } + return(error_info.errors); +} diff --git a/usr/src/lib/libcmd/common/tty.c b/usr/src/lib/libcmd/common/tty.c new file mode 100644 index 0000000000..22115b2343 --- /dev/null +++ b/usr/src/lib/libcmd/common/tty.c @@ -0,0 +1,94 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * tty + */ + +static const char usage[] = +"[-?\n@(#)$Id: tty (AT&T Research) 2007-03-11 $\n]" +USAGE_LICENSE +"[+NAME?tty - write the name of the terminal to standard output]" +"[+DESCRIPTION?\btty\b writes the name of the terminal that is connected " + "to standard input onto standard output. If the standard input is not " + "a terminal, \"\bnot a tty\b\" will be written to standard output.]" +"[l:line-number?Write the synchronous line number of the terminal on a " + "separate line following the terminal name line. If the standard " + "input is not a synchronous terminal then " + "\"\bnot on an active synchronous line\b\" is written.]" +"[s:silent|quiet?Disable the terminal name line. Use \b[[ -t 0 ]]]]\b instead.]" +"[+EXIT STATUS?]{" + "[+0?Standard input is a tty.]" + "[+1?Standard input is not a tty.]" + "[+2?Invalid arguments.]" + "[+3?A an error occurred.]" +"}" +; + + +#include <cmd.h> + +#if _mac_STWLINE +#include <sys/stermio.h> +#endif + +int +b_tty(int argc, char *argv[], void* context) +{ + register int n,sflag=0,lflag=0; + register char *tty; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'l': + lflag++; + break; + case 's': + sflag++; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + if(error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + if(!(tty=ttyname(0))) + { + tty = ERROR_translate(0, 0, 0, "not a tty"); + error_info.errors++; + } + if(!sflag) + sfputr(sfstdout,tty,'\n'); +#if _mac_STWLINE + if(lflag && (n = ioctl(0, STWLINE, 0)) >= 0) + error(ERROR_OUTPUT, 1, "synchronous line %d", n); + else +#endif + error(ERROR_OUTPUT, 1, "not on an active synchronous line"); + return(error_info.errors); +} diff --git a/usr/src/lib/libcmd/common/uname.c b/usr/src/lib/libcmd/common/uname.c new file mode 100644 index 0000000000..97cdf9100f --- /dev/null +++ b/usr/src/lib/libcmd/common/uname.c @@ -0,0 +1,512 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * Glenn Fowler + * AT&T Research + * + * uname + */ + +static const char usage[] = +"[-?\n@(#)$Id: uname (AT&T Research) 2007-01-22 $\n]" +USAGE_LICENSE +"[+NAME?uname - identify the current system ]" +"[+DESCRIPTION?By default \buname\b writes the operating system name to" +" standard output. When options are specified, one or more" +" system characteristics are written to standard output, space" +" separated, on a single line. When more than one option is specifed" +" the output is in the order specfied by the \b-A\b option below." +" Unsupported option values are listed as \a[option]]\a. If any unknown" +" options are specified then the local \b/usr/bin/uname\b is called.]" +"[+?If any \aname\a operands are specified then the \bsysinfo\b(2) values" +" for each \aname\a are listed, separated by space, on one line." +" \bgetconf\b(1), a pre-existing \astandard\a interface, provides" +" access to the same information; vendors should spend more time" +" using standards than inventing them.]" +"[+?Selected information is printed in the same order as the options below.]" +"[a:all?Equivalent to \b-snrvmpio\b.]" +"[s:system|sysname|kernel-name?The detailed kernel name. This is the default.]" +"[n:nodename?The hostname or nodename.]" +"[r:release|kernel-release?The kernel release level.]" +"[v:version|kernel-version?The kernel version level.]" +"[m:machine?The name of the hardware type the system is running on.]" +"[p:processor?The name of the processor instruction set architecture.]" +"[i:implementation|platform|hardware-platform?The hardware implementation;" +" this is \b--host-id\b on some systems.]" +"[o:operating-system?The generic operating system name.]" +"[h:host-id|id?The host id in hex.]" +"[d:domain?The domain name returned by \agetdomainname\a(2).]" +"[R:extended-release?The extended release name.]" +"[A:everything?Equivalent to \b-snrvmpiohdR\b.]" +"[f:list?List all \bsysinfo\b(2) names and values, one per line.]" +"[S:sethost?Set the hostname or nodename to \aname\a. No output is" +" written to standard output.]:[name]" + +"[+SEE ALSO?\bhostname\b(1), \bgetconf\b(1), \buname\b(2)," +" \bsysconf\b(2), \bsysinfo\b(2)]" +; + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide getdomainname gethostid gethostname sethostname +#else +#define getdomainname ______getdomainname +#define gethostid ______gethostid +#define gethostname ______gethostname +#define sethostname ______sethostname +#endif + +#include <cmd.h> +#include <ctype.h> +#include <proc.h> + +#include "FEATURE/utsname" + +#define MAXHOSTNAME 64 + +#if _lib_uname && _sys_utsname + +#include <sys/utsname.h> + +#endif + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide getdomainname gethostid gethostname sethostname +#else +#undef getdomainname +#undef gethostid +#undef gethostname +#undef sethostname +#endif + +#if _lib_getdomainname +extern int getdomainname(char*, size_t); +#endif +#if _lib_gethostid +extern int gethostid(void); +#endif +#if _lib_gethostname +extern int gethostname(char*, size_t); +#endif +#if _lib_sethostname +extern int sethostname(const char*, size_t); +#endif + +#ifndef HOSTTYPE +#define HOSTTYPE "unknown" +#endif + +static const char hosttype[] = HOSTTYPE; + +#if !_lib_uname || !_sys_utsname + +#if defined(__STDPP__) +#define SYSNAME #(getprd machine) +#define RELEASE #(getprd release) +#define VERSION #(getprd version) +#define MACHINE #(getprd architecture) +#else +#define SYSNAME "" +#define RELEASE "" +#define VERSION "" +#define MACHINE "" +#endif + +struct utsname +{ + char* sysname; + char nodename[MAXHOSTNAME]; + char* release; + char* version; + char* machine; +}; + +int +uname(register struct utsname* ut) +{ +#ifdef HOSTTYPE + char* sys = 0; + char* arch = 0; + + if (*hosttype) + { + static char buf[sizeof(hosttype)]; + + strcpy(buf, hosttype); + sys = buf; + if (arch = strchr(sys, '.')) + { + *arch++ = 0; + if (!*arch) + arch = 0; + } + if (!*sys) + sys = 0; + } +#endif +#ifdef _lib_gethostname + if (gethostname(ut->nodename, sizeof(ut->nodename) - 1)) + return -1; +#else + strncpy(ut->nodename, "local", sizeof(ut->nodename) - 1); +#endif +#ifdef HOSTTYPE + if (!(ut->sysname = sys)) +#endif + if (!*(ut->sysname = SYSNAME)) + ut->sysname = ut->nodename; +#ifdef HOSTTYPE + if (!(ut->machine = arch)) +#endif + ut->machine = MACHINE; + ut->release = RELEASE; + ut->version = VERSION; + return 0; +} + +#endif + +#define OPT_system (1<<0) +#define OPT_nodename (1<<1) +#define OPT_release (1<<2) +#define OPT_version (1<<3) +#define OPT_machine (1<<4) +#define OPT_processor (1<<5) + +#define OPT_STANDARD 6 + +#define OPT_implementation (1<<6) +#define OPT_operating_system (1<<7) + +#define OPT_ALL 8 + +#define OPT_hostid (1<<8) +#define OPT_vendor (1<<9) +#define OPT_domain (1<<10) +#define OPT_machine_type (1<<11) +#define OPT_base (1<<12) +#define OPT_extended_release (1<<13) +#define OPT_extra (1<<14) + +#define OPT_TOTAL 15 + +#define OPT_all (1L<<29) +#define OPT_total (1L<<30) +#define OPT_standard ((1<<OPT_STANDARD)-1) + +#ifndef MACHINE +#if defined(__STDPP__) +#define MACHINE #(getprd architecture) +#else +#define MACHINE "" +#endif +#endif + +#ifndef HOSTTYPE +#define HOSTTYPE "unknown" +#endif + +#define extra(m) do \ + { \ + if ((char*)&ut.m[sizeof(ut.m)] > last) \ + last = (char*)&ut.m[sizeof(ut.m)]; \ + } while(0) + +#define output(f,v,u) do \ + { \ + if ((flags&(f))&&(*(v)||(flags&(OPT_all|OPT_total))==OPT_all&&((f)&OPT_standard)||!(flags&(OPT_all|OPT_total)))) \ + { \ + if (sep) \ + sfputc(sfstdout, ' '); \ + else \ + sep = 1; \ + if (*(v)) \ + sfputr(sfstdout, v, -1); \ + else \ + sfprintf(sfstdout, "[%s]", u); \ + } \ + } while (0) + +int +b_uname(int argc, char** argv, void* context) +{ + register long flags = 0; + register int sep = 0; + register int n; + register char* s; + char* t; + char* e; + char* sethost = 0; + int list = 0; + struct utsname ut; + char buf[257]; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + for (;;) + { + switch (optget(argv, usage)) + { + case 'a': + flags |= OPT_all|((1L<<OPT_ALL)-1); + continue; + case 'b': + flags |= OPT_base; + continue; + case 'c': + flags |= OPT_vendor; + continue; + case 'd': + flags |= OPT_domain; + continue; + case 'f': + list = 1; + continue; + case 'h': + flags |= OPT_hostid; + continue; + case 'i': + flags |= OPT_implementation; + continue; + case 'm': + flags |= OPT_machine; + continue; + case 'n': + flags |= OPT_nodename; + continue; + case 'o': + flags |= OPT_operating_system; + continue; + case 'p': + flags |= OPT_processor; + continue; + case 'r': + flags |= OPT_release; + continue; + case 's': + flags |= OPT_system; + continue; + case 't': + flags |= OPT_machine_type; + continue; + case 'v': + flags |= OPT_version; + continue; + case 'x': + flags |= OPT_extra; + continue; + case 'A': + flags |= OPT_total|((1L<<OPT_TOTAL)-1); + continue; + case 'R': + flags |= OPT_extended_release; + continue; + case 'S': + sethost = opt_info.arg; + continue; + case ':': + s = "/usr/bin/uname"; + if (!streq(argv[0], s) && (!eaccess(s, X_OK) || !eaccess(s+=4, X_OK))) + { + argv[0] = s; + return procrun(s, argv); + } + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + break; + } + argv += opt_info.index; + if (error_info.errors || *argv && (flags || sethost) || sethost && flags) + error(ERROR_usage(2), "%s", optusage(NiL)); + if (sethost) + { +#if _lib_sethostname + if (sethostname(sethost, strlen(sethost) + 1)) +#else +#ifdef ENOSYS + errno = ENOSYS; +#else + errno = EPERM; +#endif +#endif + error(ERROR_system(1), "%s: cannot set host name", sethost); + } + else if (list) + astconflist(sfstdout, NiL, ASTCONF_base|ASTCONF_defined|ASTCONF_lower|ASTCONF_quote|ASTCONF_matchcall, "CS|SI"); + else if (*argv) + { + e = &buf[sizeof(buf)-1]; + while (s = *argv++) + { + t = buf; + *t++ = 'C'; + *t++ = 'S'; + *t++ = '_'; + while (t < e && (n = *s++)) + *t++ = islower(n) ? toupper(n) : n; + *t = 0; + sfprintf(sfstdout, "%s%c", *(t = astconf(buf, NiL, NiL)) ? t : "unknown", *argv ? ' ' : '\n'); + } + } + else + { + s = buf; + if (!flags) + flags = OPT_system; + memzero(&ut, sizeof(ut)); + if (uname(&ut) < 0) + error(ERROR_usage(2), "information unavailable"); + output(OPT_system, ut.sysname, "sysname"); + if (flags & OPT_nodename) + { +#if !_mem_nodeext_utsname && _lib_gethostname + if (sizeof(ut.nodename) > 9 || gethostname(s, sizeof(buf))) +#endif + s = ut.nodename; + output(OPT_nodename, s, "nodename"); + } + output(OPT_release, ut.release, "release"); + output(OPT_version, ut.version, "version"); + output(OPT_machine, ut.machine, "machine"); + if (flags & OPT_processor) + { + if (!*(s = astconf("ARCHITECTURE", NiL, NiL))) + s = ut.machine; + output(OPT_processor, s, "processor"); + } + if (flags & OPT_implementation) + { + if (!*(s = astconf("PLATFORM", NiL, NiL)) && !*(s = astconf("HW_NAME", NiL, NiL))) + { + if (t = strchr(hosttype, '.')) + t++; + else + t = (char*)hosttype; + strncpy(s = buf, t, sizeof(buf) - 1); + } + output(OPT_implementation, s, "implementation"); + } + if (flags & OPT_operating_system) + { + s = astconf("OPERATING_SYSTEM", NiL, NiL); + if (!*s) +#ifdef _UNAME_os_DEFAULT + s = _UNAME_os_DEFAULT; +#else + s = ut.sysname; +#endif + output(OPT_operating_system, s, "operating-system"); + } + if (flags & OPT_extended_release) + { + s = astconf("RELEASE", NiL, NiL); + output(OPT_extended_release, s, "extended-release"); + } +#if _mem_idnumber_utsname + output(OPT_hostid, ut.idnumber, "hostid"); +#else + if (flags & OPT_hostid) + { + if (!*(s = astconf("HW_SERIAL", NiL, NiL))) +#if _lib_gethostid + sfsprintf(s = buf, sizeof(buf), "%08x", gethostid()); +#else + /*NOP*/; +#endif + output(OPT_hostid, s, "hostid"); + } +#endif + if (flags & OPT_vendor) + { + s = astconf("HW_PROVIDER", NiL, NiL); + output(OPT_vendor, s, "vendor"); + } + if (flags & OPT_domain) + { + if (!*(s = astconf("SRPC_DOMAIN", NiL, NiL))) +#if _lib_getdomainname + getdomainname(s, sizeof(buf)); +#else + /*NOP*/; +#endif + output(OPT_domain, s, "domain"); + } +#if _mem_m_type_utsname + s = ut.m_type; +#else + s = astconf("MACHINE", NiL, NiL); +#endif + output(OPT_machine_type, s, "m_type"); +#if _mem_base_rel_utsname + s = ut.base_rel; +#else + s = astconf("BASE", NiL, NiL); +#endif + output(OPT_base, s, "base_rel"); + if (flags & OPT_extra) + { + char* last = (char*)&ut; + + extra(sysname); + extra(nodename); + extra(release); + extra(version); + extra(machine); +#if _mem_idnumber_utsname + extra(idnumber); +#endif +#if _mem_m_type_utsname + extra(m_type); +#endif +#if _mem_base_rel_utsname + extra(base_rel); +#endif + if (last < ((char*)(&ut + 1))) + { + s = t = last; + while (s < (char*)(&ut + 1)) + { + if (!(n = *s++)) + { + if ((s - t) > 1) + { + if (sep) + sfputc(sfstdout, ' '); + else + sep = 1; + sfputr(sfstdout, t, -1); + } + t = s; + } + else if (!isprint(n)) + break; + } + } + } + if (sep) + sfputc(sfstdout, '\n'); + } + return error_info.errors; +} diff --git a/usr/src/lib/libcmd/common/uniq.c b/usr/src/lib/libcmd/common/uniq.c new file mode 100644 index 0000000000..e6e0da4667 --- /dev/null +++ b/usr/src/lib/libcmd/common/uniq.c @@ -0,0 +1,302 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * uniq + * + * Written by David Korn + */ + +static const char usage[] = +"[-?\n@(#)$Id: uniq (AT&T Research) 2006-08-28 $\n]" +USAGE_LICENSE +"[+NAME?uniq - Report or filter out repeated lines in a file]" +"[+DESCRIPTION?\buniq\b reads an input, comparing adjacent lines, and " + "writing one copy of each input line on the output. The second " + "and succeeding copies of the repeated adjacent lines are not " + "written.]" +"[+?If the output file, \aoutfile\a, is not specified, \buniq\b writes " + "to standard output. If no \ainfile\a is given, or if the \ainfile\a " + "is \b-\b, \buniq\b reads from standard input with the start of " + "the file is defined as the current offset.]" +"[c:count?Output the number of times each line occurred along with " + "the line.]" +"[d:repeated|duplicates?Output the first of each duplicate line.]" +"[D:all-repeated?Output all duplicate lines as a group with an empty " + "line delimiter specified by \adelimit\a:]:?[delimit:=none]" + "{" + "[n:none?Do not delimit duplicate groups.]" + "[p:prepend?Prepend an empty line before each group.]" + "[s:separate?Separate each group with an empty line.]" + "}" +"[f:skip-fields]#[fields?\afields\a is the number of fields to skip over " + "before checking for uniqueness. A field is the minimal string matching " + "the BRE \b[[:blank:]]]]*[^[:blank:]]]]*\b.]" +"[i:ignore-case?Ignore case in comparisons.]" +"[s:skip-chars]#[chars?\achars\a is the number of characters to skip over " + "before checking for uniqueness. If specified along with \b-f\b, " + "the first \achars\a after the first \afields\a are ignored. If " + "the \achars\a specifies more characters than are on the line, " + "an empty string will be used for comparison.]" +"[u:unique?Output unique lines.]" +"[w:check-chars]#[chars?\achars\a is the number of characters to compare " + "after skipping any specified fields and characters.]" +"\n" +"\n[infile [outfile]]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?The input file was successfully processed.]" + "[+>0?An error occurred.]" +"}" +"[+SEE ALSO?\bsort\b(1), \bgrep\b(1)]" +; + +#include <cmd.h> + +#define C_FLAG 1 +#define D_FLAG 2 +#define U_FLAG 4 + +#define CWIDTH 4 +#define MAXCNT 9999 + +typedef int (*Compare_f)(const char*, const char*, size_t); + +static int uniq(Sfio_t *fdin, Sfio_t *fdout, int fields, int chars, int width, int mode, int* all, Compare_f compare) +{ + register int n, f, outsize=0; + register char *cp, *ep, *bufp, *outp; + char *orecp, *sbufp=0, *outbuff; + int reclen,oreclen= -1,count=0,cwidth=0,sep,next; + if(mode&C_FLAG) + cwidth = CWIDTH+1; + while(1) + { + if(bufp = sfgetr(fdin,'\n',0)) + n = sfvalue(fdin); + else if(bufp = sfgetr(fdin,'\n',SF_LASTR)) + { + n = sfvalue(fdin); + bufp = memcpy(fmtbuf(n + 1), bufp, n); + bufp[n++] = '\n'; + } + else + n = 0; + if(n) + { + cp = bufp; + ep = cp + n; + if(f=fields) + while(f-->0 && cp<ep) /* skip over fields */ + { + while(cp<ep && *cp==' ' || *cp=='\t') + cp++; + while(cp<ep && *cp!=' ' && *cp!='\t') + cp++; + } + if(chars) + cp += chars; + if((reclen = n - (cp-bufp)) <=0) + { + reclen = 1; + cp = bufp + sfvalue(fdin)-1; + } + else if(width >= 0 && width < reclen) + reclen = width; + } + else + reclen=-2; + if(reclen==oreclen && (!reclen || !(*compare)(cp,orecp,reclen))) + { + count++; + if (!all) + continue; + next = count; + } + else + { + next = 0; + if(outsize>0) + { + if(((mode&D_FLAG)&&count==0) || ((mode&U_FLAG)&&count)) + { + if(outp!=sbufp) + sfwrite(fdout,outp,0); + } + else + { + if(cwidth) + { + outp[CWIDTH] = ' '; + if(count<MAXCNT) + { + sfsprintf(outp,cwidth,"%*d",CWIDTH,count+1); + outp[CWIDTH] = ' '; + } + else + { + outsize -= (CWIDTH+1); + if(outp!=sbufp) + { + if(!(sbufp=fmtbuf(outsize))) + return(1); + memcpy(sbufp,outp+CWIDTH+1,outsize); + sfwrite(fdout,outp,0); + outp = sbufp; + } + else + outp += CWIDTH+1; + sfprintf(fdout,"%4d ",count+1); + } + } + if(sfwrite(fdout,outp,outsize) != outsize) + return(1); + } + } + } + if(n==0) + break; + if(count = next) + { + if(sfwrite(fdout,outp,outsize) != outsize) + return(1); + if(*all >= 0) + *all = 1; + sep = 0; + } + else + sep = all && *all > 0; + /* save current record */ + if (!(outbuff = sfreserve(fdout, 0, 0)) || (outsize = sfvalue(fdout)) < 0) + return(1); + outp = outbuff; + if(outsize < n+cwidth+sep) + { + /* no room in outp, clear lock and use side buffer */ + sfwrite(fdout,outp,0); + if(!(sbufp = outp=fmtbuf(outsize=n+cwidth+sep))) + return(1); + } + else + outsize = n+cwidth+sep; + memcpy(outp+cwidth+sep,bufp,n); + if(sep) + outp[cwidth] = '\n'; + oreclen = reclen; + orecp = outp+cwidth+sep + (cp-bufp); + } + return(0); +} + +int +b_uniq(int argc, char** argv, void* context) +{ + register int n, mode=0; + register char *cp; + int fields=0, chars=0, width=-1; + Sfio_t *fpin, *fpout; + int* all = 0; + int sep; + Compare_f compare = (Compare_f)memcmp; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv, usage)) switch (n) + { + case 'c': + mode |= C_FLAG; + break; + case 'd': + mode |= D_FLAG; + break; + case 'D': + mode |= D_FLAG; + switch ((int)opt_info.num) + { + case 'p': + sep = 1; + break; + case 's': + sep = 0; + break; + default: + sep = -1; + break; + } + all = &sep; + break; + case 'i': + compare = (Compare_f)strncasecmp; + break; + case 'u': + mode |= U_FLAG; + break; + case 'f': + if(*opt_info.option=='-') + fields = opt_info.num; + else + chars = opt_info.num; + break; + case 's': + chars = opt_info.num; + break; + case 'w': + width = opt_info.num; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if(all && (mode&C_FLAG)) + error(2, "-c and -D are mutually exclusive"); + if(error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + if((cp = *argv) && (argv++,!streq(cp,"-"))) + { + if(!(fpin = sfopen(NiL,cp,"r"))) + error(ERROR_system(1),"%s: cannot open",cp); + } + else + fpin = sfstdin; + if(cp = *argv) + { + argv++; + if(!(fpout = sfopen(NiL,cp,"w"))) + error(ERROR_system(1),"%s: cannot create",cp); + } + else + fpout = sfstdout; + if(*argv) + { + error(2, "too many arguments"); + error(ERROR_usage(2), "%s", optusage(NiL)); + } + error_info.errors = uniq(fpin,fpout,fields,chars,width,mode,all,compare); + if(fpin!=sfstdin) + sfclose(fpin); + if(fpout!=sfstdout) + sfclose(fpout); + return(error_info.errors); +} + diff --git a/usr/src/lib/libcmd/common/wc.c b/usr/src/lib/libcmd/common/wc.c new file mode 100644 index 0000000000..9d4b2567c7 --- /dev/null +++ b/usr/src/lib/libcmd/common/wc.c @@ -0,0 +1,186 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * count the number of bytes, words, and lines in a file + */ + +static const char usage[] = +"[-?\n@(#)$Id: wc (AT&T Research) 2006-08-25 $\n]" +USAGE_LICENSE +"[+NAME?wc - print the number of bytes, words, and lines in files]" +"[+DESCRIPTION?\bwc\b reads one or more input files and, by default, " + "for each file writes a line containing the number of newlines, " + "\aword\as, and bytes contained in each file followed by the " + "file name to standard output in that order. A \aword\a is " + "defined to be a non-zero length string delimited by \bisspace\b(3) " + "characters.]" +"[+?If more than one file is specified, \bwc\b writes a total count " + "for all of the named files with \btotal\b written instead " + "of the file name.]" +"[+?By default, \bwc\b writes all three counts. Options can specified " + "so that only certain counts are written. The options \b-c\b " + "and \b-m\b are mutually exclusive.]" +"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bwc\b " + "reads from standard input and no filename is written to standard " + "output. The start of the file is defined as the current offset.]" +"[l:lines?List the line counts.]" +"[w:words?List the word counts.]" +"[c:bytes|chars:chars?List the byte counts.]" +"[m|C:multibyte-chars?List the character counts.]" +"[q:quiet?Suppress invalid multibyte character warnings.]" +"[L:longest-line|max-line-length?List the longest line length.]" +"\n" +"\n[file ...]\n" +"\n" +"[+EXIT STATUS?]{" + "[+0?All files processed successfully.]" + "[+>0?One or more files failed to open or could not be read.]" +"}" +"[+SEE ALSO?\bcat\b(1), \bisspace\b(3)]" +; + + +#include <cmd.h> +#include <wc.h> +#include <ls.h> + +#define ERRORMAX 125 + +static void printout(register Wc_t *wp, register char *name,register int mode) +{ + if(mode&WC_LINES) + sfprintf(sfstdout," %7I*d",sizeof(wp->lines),wp->lines); + if(mode&WC_WORDS) + sfprintf(sfstdout," %7I*d",sizeof(wp->words),wp->words); + if(mode&WC_CHARS) + sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->chars); + if(mode&WC_LONGEST) + sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->longest); + if(name) + sfprintf(sfstdout," %s",name); + sfputc(sfstdout,'\n'); +} + +int +b_wc(int argc,register char **argv, void* context) +{ + register char *cp; + register int mode=0, n; + register Wc_t *wp; + Sfio_t *fp; + Sfoff_t tlines=0, twords=0, tchars=0; + struct stat statb; + + cmdinit(argc, argv, context, ERROR_CATALOG, 0); + while (n = optget(argv,usage)) switch (n) + { + case 'c': + mode |= WC_CHARS; + break; + case 'l': + mode |= WC_LINES; + break; + case 'L': + mode |= WC_LONGEST; + break; + case 'm': + case 'C': + mode |= WC_MBYTE; + break; + case 'q': + mode |= WC_QUIET; + break; + case 'w': + mode |= WC_WORDS; + break; + case ':': + error(2, "%s", opt_info.arg); + break; + case '?': + error(ERROR_usage(2), "%s", opt_info.arg); + break; + } + argv += opt_info.index; + if (error_info.errors) + error(ERROR_usage(2), "%s", optusage(NiL)); + if(mode&WC_MBYTE) + { + if(mode&WC_CHARS) + error(2, "-c and -C are mutually exclusive"); + mode |= WC_CHARS; + if(!mbwide()) + { + mode &= ~WC_MBYTE; + setlocale(LC_CTYPE, "C"); + } + } + if(!(mode&(WC_WORDS|WC_CHARS|WC_LINES|WC_MBYTE|WC_LONGEST))) + mode |= (WC_WORDS|WC_CHARS|WC_LINES); + if(!(wp = wc_init(mode))) + error(3,"internal error"); + if(!(mode&WC_WORDS)) + { + memzero(wp->space, (1<<CHAR_BIT)); + wp->space['\n'] = -1; + } + if(cp = *argv) + argv++; + do + { + if(!cp || streq(cp,"-")) + fp = sfstdin; + else if(!(fp = sfopen(NiL,cp,"r"))) + { + error(ERROR_system(0),"%s: cannot open",cp); + continue; + } + if(cp) + n++; + if(!(mode&(WC_WORDS|WC_LINES|WC_MBYTE|WC_LONGEST)) && fstat(sffileno(fp),&statb)>=0 + && S_ISREG(statb.st_mode)) + { + wp->chars = statb.st_size - lseek(sffileno(fp),0L,1); + lseek(sffileno(fp),0L,2); + } + else + wc_count(wp, fp, cp); + if(fp!=sfstdin) + sfclose(fp); + tchars += wp->chars; + twords += wp->words; + tlines += wp->lines; + printout(wp,cp,mode); + } + while(cp= *argv++); + if(n>1) + { + wp->lines = tlines; + wp->chars = tchars; + wp->words = twords; + printout(wp,"total",mode); + } + return(error_info.errors<ERRORMAX?error_info.errors:ERRORMAX); +} + diff --git a/usr/src/lib/libcmd/common/wc.h b/usr/src/lib/libcmd/common/wc.h new file mode 100644 index 0000000000..0296a898f8 --- /dev/null +++ b/usr/src/lib/libcmd/common/wc.h @@ -0,0 +1,57 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * header for wc library interface + */ + +#ifndef _WC_H +#define _WC_H + +#include <ast.h> + +#define WC_LINES 0x01 +#define WC_WORDS 0x02 +#define WC_CHARS 0x04 +#define WC_MBYTE 0x08 +#define WC_LONGEST 0x10 +#define WC_QUIET 0x20 + +typedef struct +{ + signed char space[1<<CHAR_BIT]; + Sfoff_t words; + Sfoff_t lines; + Sfoff_t chars; + Sfoff_t longest; + int mode; +} Wc_t; + +#define wc_count _cmd_wccount +#define wc_init _cmd_wcinit + +extern Wc_t* wc_init(int); +extern int wc_count(Wc_t*, Sfio_t*, const char*); + +#endif /* _WC_H */ diff --git a/usr/src/lib/libcmd/common/wclib.c b/usr/src/lib/libcmd/common/wclib.c new file mode 100644 index 0000000000..c16e4efc87 --- /dev/null +++ b/usr/src/lib/libcmd/common/wclib.c @@ -0,0 +1,197 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1992-2007 AT&T Knowledge Ventures * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Knowledge Ventures * +* * +* 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 +/* + * David Korn + * AT&T Bell Laboratories + * + * library interface for word count + */ + +#include <cmd.h> +#include <wc.h> +#include <ctype.h> + +#if _hdr_wchar && _hdr_wctype + +#include <wchar.h> +#include <wctype.h> + +#else + +#ifndef iswspace +#define iswspace(x) isspace(x) +#endif + +#endif + +#define endline(c) (((signed char)-1)<0?(c)<0:(c)==((char)-1)) +#define mbok(p,n) (((n)<1)?0:mbwide()?((*ast.mb_towc)(NiL,(char*)(p),n)>=0):1) + +Wc_t *wc_init(int mode) +{ + register int n; + register int w; + Wc_t* wp; + + if(!(wp = (Wc_t*)stakalloc(sizeof(Wc_t)))) + return(0); + wp->mode = mode; + w = mode & WC_WORDS; + for(n=(1<<CHAR_BIT);--n >=0;) + wp->space[n] = w ? !!isspace(n) : 0; + wp->space['\n'] = -1; + return(wp); +} + +/* + * compute the line, word, and character count for file <fd> + */ +int wc_count(Wc_t *wp, Sfio_t *fd, const char* file) +{ + register signed char *space = wp->space; + register unsigned char *cp; + register Sfoff_t nchars; + register Sfoff_t nwords; + register Sfoff_t nlines; + register Sfoff_t eline; + register Sfoff_t longest; + register ssize_t c; + register unsigned char *endbuff; + register int lasttype = 1; + unsigned int lastchar; + unsigned char *buff; + wchar_t x; + + sfset(fd,SF_WRITE,1); + nlines = nwords = nchars = 0; + wp->longest = 0; + if (wp->mode & (WC_LONGEST|WC_MBYTE)) + { + longest = 0; + eline = -1; + cp = buff = endbuff = 0; + for (;;) + { + if (!mbok(cp, endbuff-cp)) + { + if (buff) + sfread(fd, buff, cp-buff); + if (!(buff = (unsigned char*)sfreserve(fd, SF_UNBOUND, SF_LOCKR))) + break; + endbuff = (cp = buff) + sfvalue(fd); + } + nchars++; + x = mbchar(cp); + if (x == -1) + { + if (eline != nlines && !(wp->mode & WC_QUIET)) + { + error_info.file = (char*)file; + error_info.line = eline = nlines; + error(ERROR_SYSTEM|1, "invalid multibyte character"); + error_info.file = 0; + error_info.line = 0; + } + } + else if (x == '\n') + { + if ((nchars - longest) > wp->longest) + wp->longest = nchars - longest; + longest = nchars; + nlines++; + lasttype = 1; + } + else if (iswspace(x)) + lasttype = 1; + else if (lasttype) + { + lasttype = 0; + nwords++; + } + } + } + else + { + for (;;) + { + /* fill next buffer and check for end-of-file */ + if (!(buff = (unsigned char*)sfreserve(fd, 0, 0)) || (c = sfvalue(fd)) <= 0) + break; + sfread(fd,(char*)(cp=buff),c); + nchars += c; + /* check to see whether first character terminates word */ + if(c==1) + { + if(endline(lasttype)) + nlines++; + if((c = space[*cp]) && !lasttype) + nwords++; + lasttype = c; + continue; + } + if(!lasttype && space[*cp]) + nwords++; + lastchar = cp[--c]; + cp[c] = '\n'; + endbuff = cp+c; + c = lasttype; + /* process each buffer */ + for (;;) + { + /* process spaces and new-lines */ + do if (endline(c)) + { + for (;;) + { + /* check for end of buffer */ + if (cp > endbuff) + goto eob; + nlines++; + if (*cp != '\n') + break; + cp++; + } + } while (c = space[*cp++]); + /* skip over word characters */ + while(!(c = space[*cp++])); + nwords++; + } + eob: + if((cp -= 2) >= buff) + c = space[*cp]; + else + c = lasttype; + lasttype = space[lastchar]; + /* see if was in word */ + if(!c && !lasttype) + nwords--; + } + if(endline(lasttype)) + nlines++; + else if(!lasttype) + nwords++; + } + wp->chars = nchars; + wp->words = nwords; + wp->lines = nlines; + return(0); +} |