home *** CD-ROM | disk | FTP | other *** search
- From: istvan@hhb.UUCP (Istvan Mohos)
- Newsgroups: alt.sources
- Subject: Subject: ILIB Unix Toolkit in C
- Message-ID: <550@hhb.UUCP>
- Date: 8 Jun 90 20:54:01 GMT
-
-
- ---- Cut Here and unpack ----
- #!/bin/sh
- # This is part 04 of a multipart archive
- if touch 2>&1 | fgrep '[-amc]' > /dev/null
- then TOUCH=touch
- else TOUCH=true
- fi
- # ============= iex/group.c ==============
- echo "x - extracting iex/group.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/group.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/group
- Xexit 0
- X#endif
- X
- X/* write alphanumerically sorted and grouped lines to stdout */
- X#include "ilib.h"
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char *buffer; /* for storing entire input */
- X char *list; /* pointer list to lines of input */
- X char **listptr; /* pointer to pointers of 'list' */
- X int bytes; /* byte count of input */
- X int lines; /* count of pointers to lines of input */
- X int modval; /* maximum count of items in a group */
- X
- X if (argc < 2 || (modval = atoi (argv[1])) == 0)
- X fprintf(stderr,
- X "Usage: %s number|negative_number [file]\n\n%s%s%s%s", argv[0],
- X "Alphanumerically sort lines of file (or stdin) to stdout\n",
- X "into related groups of <number> items maximally.\n",
- X "<negative_number> specifies reverse sort.\n",
- X "1 or -1 specifies no spacing between groups.\n\n"),
- X exit (0);
- X
- X /* procure input */
- XQ if ((bytes = ifilter (argv[2], &buffer)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X /* create list of pointers to lines of input */
- XQ if ((lines = illistn (buffer, buffer + bytes, &list)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X /* sort pointers to dereference strings in alphanumeric order */
- XQ inumsort ((char **)list, lines);
- X
- X /* output sorted list, backward or forward */
- X if (modval < 0) {
- X modval = abs (modval);
- X listptr = (char **)list;
- X for (listptr += lines-1; --lines; listptr--)
- X output (*listptr, *(listptr-1), modval);
- X }
- X else
- X for (listptr = (char **)list; --lines; listptr++)
- X output (*listptr, *(listptr+1), modval);
- X output (*listptr, (char *)NULL, modval); /* last item on list */
- X
- X exit(0);
- X}
- X
- Xoutput (ptr1, ptr2, modval)
- Xchar *ptr1, *ptr2;
- Xint modval;
- X{
- X if (modval == 1)
- X puts (ptr1);
- X else {
- XQ switch (igroup (ptr1, ptr2, modval)) {
- X case SPACE_LINE:
- X printf ("\n%s\n", ptr1);
- X break;
- X case LINE_ONLY:
- X default:
- X puts (ptr1);
- X break;
- X case LINE_SPACE:
- X printf ("%s\n\n", ptr1);
- X break;
- X }
- X }
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/group.c &&
- chmod 0755 iex/group.c ||
- echo "restore of iex/group.c failed"
- set `wc -c iex/group.c`;Wc_c=$1
- if test "$Wc_c" != "2031"; then
- echo original size 2031, current size $Wc_c
- fi
- # ============= iex/inum.c ==============
- echo "x - extracting iex/inum.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/inum.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/inum
- Xexit 0
- X#endif
- X
- X/* interactively specify custom line numbering sequence */
- X#include "ilib.h"
- X#define NUL 0 /* the ASCII 0 byte */
- X
- Xmain (argc,argv)
- Xint argc;
- Xchar *argv[];
- X{
- X FILE *fp;
- X static char prepend[IHALFK];
- X static char append[IHALFK];
- X static char tail[IHALFK];
- X char rsvp[IHALFK];
- X char line[IHALFK];
- X char build[IHALFK*4];
- X char format[32];
- X int linecount = 100;
- X int thisnum = 1;
- X int increment = 1;
- X int justify = 1;
- X int existfile = 1;
- X int aftertext = 0;
- X int zerofill = 0;
- X char base;
- X
- X if (NULCHARP (argv[1])) /* no input file */
- X existfile = 0;
- X else if ((fp = fopen(argv[1], "r")) == NULL)
- X fprintf (stderr, "Can't read %s\n", argv[1]), exit (1);
- X
- X if (!existfile) {
- XQ iinput (rsvp, "how many lines?______________");
- X if (*rsvp != NUL)
- X linecount = abs (atoi (rsvp));
- X }
- X else {
- XQ iinput (rsvp, "before or after text (b/a) ?_");
- X if (*rsvp == 'a' || *rsvp == 'A')
- X aftertext = 1;
- X }
- XQ iinput (prepend, "prepend this string to count_");
- XQ iinput (rsvp, "begin count at_______________");
- X if (*rsvp != NUL)
- X thisnum = atoi (rsvp);
- XQ iinput (rsvp, "increment count by___________");
- X if (*rsvp != NUL)
- X increment = atoi (rsvp);
- XQ iinput (rsvp, "justify width (-N for left)__");
- X justify = atoi (rsvp);
- X if (justify > 1) {
- XQ iinput (rsvp, "zero fill (y/n)______________");
- X if (*rsvp == 'y' || *rsvp == 'Y')
- X zerofill = 1;
- X }
- XQ iinput (rsvp, "decimal:d hex:x/X octal:o ?__");
- X base = *rsvp;
- X if (base != 'x' && base != 'X' && base != 'o')
- X base = 'd';
- XQ iinput (append, "append this string to count__");
- X if (existfile)
- XQ iinput (tail, "tail string past line________");
- X
- X if (justify > 1 && zerofill)
- X sprintf(format, "%cs%c.%d%c%cs", '%', '%', justify, base, '%');
- X else
- X sprintf(format, "%cs%c%d%c%cs", '%', '%', justify, base, '%');
- X
- X if (existfile) {
- X while (fgets (line, IHALFK, fp) != NULL) {
- X line[strlen (line) -1] = NUL; /* kill newline */
- X sprintf (build, format, prepend, thisnum, append);
- X if (aftertext)
- X printf ("%s%s%s\n", line, build, tail);
- X else
- X printf ("%s%s%s\n", build, line, tail);
- X thisnum += increment;
- X }
- X }
- X else
- X for (strcat (format, "\n"); linecount--; thisnum += increment)
- X printf(format, prepend, thisnum, append);
- X
- X exit (0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/inum.c &&
- chmod 0755 iex/inum.c ||
- echo "restore of iex/inum.c failed"
- set `wc -c iex/inum.c`;Wc_c=$1
- if test "$Wc_c" != "2352"; then
- echo original size 2352, current size $Wc_c
- fi
- # ============= iex/isdef.c ==============
- echo "x - extracting iex/isdef.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/isdef.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/isdef
- Xexit 0
- X#endif
- X
- X/* find token in list of definitions recognized by C compiler */
- X#include "ilib.h"
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char fbuf[ITWOK]; /* buffer for writing C code */
- X char *ptr = fbuf; /* working pointer into fbuf */
- X char cmd[IONEK]; /* buffer for "system" command line */
- X char tmpfile[32]; /* full name of temporary file of C code */
- X char template[14]; /* storage for mktemp template */
- X char **token = argv; /* pointer to cmd line parameters */
- X char *mktemp();
- X
- X if (argc < 2)
- X printf ("Usage: %s token ...\n%s\n\n", *token,
- X "find token in list of definitions recognized by C compiler"),
- X exit(0);
- X
- X /* install C code in buffer for testing presence of definitions */
- X strcpy (ptr, "main (){\n");
- XQ icue (&ptr);
- X
- X for (++token; !NULCHARP (*token); token++) {
- X sprintf (ptr,
- X "#ifdef %s\nprintf(\"\%-16s is defined\\n\");\n#else\n",
- X *token, *token);
- XQ icue (&ptr);
- X sprintf (ptr, "printf(\"\%-16s not defined\\n\");\n#endif\n",
- X *token);
- XQ icue (&ptr);
- X }
- X
- X strcpy (ptr, "exit(0);}\n");
- XQ icue (&ptr);
- X
- X /* make temporary file name, write buffer to temporary file */
- X strcpy (template, "isdefXXXXXX");
- X sprintf (tmpfile, "/tmp/%s.c", mktemp (template));
- X if (iwrite (tmpfile, fbuf, ptr) != ptr - fbuf)
- X fprintf (stderr, "%s temporary file:\n%s\n",
- X ierbuf, tmpfile), exit (1);
- X
- X /* compile, execute, clean up */
- X sprintf (cmd, "/bin/cc %s -o /tmp/%s; /tmp/%s",
- X tmpfile, template, template);
- X if (system (cmd))
- X fprintf (stderr, "'system' command failed\n"), exit (2);
- X unlink (tmpfile);
- X *(tmpfile + strlen (tmpfile) - 2) = '\0';
- X unlink (tmpfile);
- X exit (0);
- X}
- SHAR_EOF
- $TOUCH -am 0605084790 iex/isdef.c &&
- chmod 0755 iex/isdef.c ||
- echo "restore of iex/isdef.c failed"
- set `wc -c iex/isdef.c`;Wc_c=$1
- if test "$Wc_c" != "1721"; then
- echo original size 1721, current size $Wc_c
- fi
- # ============= iex/lcat.c ==============
- echo "x - extracting iex/lcat.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/lcat.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/lcat
- Xexit 0
- X#endif
- X
- X/* cat line N or lines N through N+M of input to stdout */
- X#include "ilib.h"
- X#define TOEND -1
- X
- Xmain (argc,argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char **argp = argv;
- X char *buf; /* to file buffer malloc'd by iread */
- XQ char *lpbuf; /* to line pointers malloc'd by illistn */
- X char *end; /* point to one byte past buffer */
- XQ char **ptr; /* working pointer in lpbuf */
- X int filesize; /* size of malloc'd text buffer */
- X int lcount; /* number of lines in file */
- X int n; /* cat this line */
- X int m = 0; /* number of extra lines to print */
- X int cnt; /* working counter */
- X
- X if (argc < 2)
- X fprintf (stderr, "Usage: %s N [+|+M] [file]\n\%s%s%s", *argp,
- X "\tcat Nth line of input/file (to stdout) or\n",
- X "\tcat input/file from line N to end or\n",
- X "\tcat input/file from line N through line N+M\n\n"), exit(0);
- X
- X if ((n = atoi (*++argp)) < 1)
- X n = abs (n);
- X if (!NULCHARP(*++argp) && *(*argp) == '+' && (m=atoi(*argp++)) < 1)
- X m = TOEND;
- X
- X if ((filesize = ifilter (*argp, &buf)) < 1)
- X fprintf (stderr, "%s %s\n", ierbuf, *argp), exit (1);
- X end = buf + filesize;
- X
- X /* set up pointers to lines of file */
- XQ if ((lcount = illistn (buf, end, &lpbuf)) < 1)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X if (lcount < n) /* not enough lines */
- X exit (0);
- XQ ptr = (char **)lpbuf + n-1;
- X
- X if (n+m > lcount)
- X m = TOEND;
- X switch (m) {
- X case 0:
- X puts (*ptr);
- X break;
- X case TOEND:
- X for (DOUNCOUN ((lcount-n+1), cnt); ptr++)
- X puts (*ptr);
- X break;
- X default:
- X for (DOUNCOUN ((m+1), cnt); ptr++)
- X puts (*ptr);
- X break;
- X }
- X exit (0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/lcat.c &&
- chmod 0755 iex/lcat.c ||
- echo "restore of iex/lcat.c failed"
- set `wc -c iex/lcat.c`;Wc_c=$1
- if test "$Wc_c" != "1737"; then
- echo original size 1737, current size $Wc_c
- fi
- # ============= iex/lhash.c ==============
- echo "x - extracting iex/lhash.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/lhash.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/lhash
- X(/bin/rm -f $BIN/uhash)
- Xln $BIN/lhash $BIN/uhash
- Xexit 0
- X#endif
- X
- X/* output formatted sum of bytes of each line of input */
- X#include "ilib.h"
- X
- X#define UHASH 377
- X#define PLAIN 'p'
- X#define LEAD 'l'
- X#define TRAIL 't'
- X#define MAXC (IONEK-1)
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char lbuf[MAXC+1]; /* buffer for input string */
- X char form[MAXC+1]; /* buffer for format string */
- X char **opt = &argv[1]; /* command line options */
- X int formtype; /* PLAIN, LEAD or TRAIL */
- X int optchar; /* option flag */
- X int modval = 0; /* output full sum if zero */
- X int upcase = 0; /* upcase each byte if TRUE */
- X
- X *form = 0;
- X while ((optchar = iopt (&opt)))
- X switch (optchar) {
- X case 'm':
- X if ((modval = atoi (*opt)) < 1)
- X usage (), exit (1);
- X break;
- X case 'p':
- X case 'l':
- X case 't':
- X strcpy (form, *opt);
- X strcat (form, "\n");
- X formtype = optchar;
- X break;
- X default:
- X usage (), exit (1);
- X }
- X
- X if (!NULCHARP (*opt)) /* further, non-option arguments */
- X usage (), exit (1);
- XQ if (ihasharg (argv[0], '/') == UHASH)
- X upcase = 1;
- X
- X if (!*form)
- X while (gets (lbuf) != NULL)
- XQ printf ("%d\n", ihash (lbuf, modval, upcase));
- X else
- X while (gets (lbuf) != NULL)
- X switch (formtype) {
- X case PLAIN:
- XQ printf (form, ihash (lbuf, modval, upcase));
- X break;
- X case LEAD:
- XQ printf (form, ihash (lbuf, modval, upcase), lbuf);
- X break;
- X case TRAIL:
- XQ printf (form, lbuf, ihash (lbuf, modval, upcase));
- X break;
- X }
- X exit(0);
- X}
- X
- Xusage ()
- X{
- X fprintf(stderr,
- X"Usage: lhash|uhash [-m modval] [-p|l|t printf_string]\n%s%s%s%s%s%s\n",
- X"\tsum byte values or upper case byte values of each line of input,\n",
- X"\toutput sum or \"sum modulo modval\" if -m is specified,\n",
- X"\tprint using the \"printf\" format string if -p|l|t is specified:\n",
- X"\t-p (plain) should contain int format specifier for hash value,\n",
- X"\t-l (lead) int and str formats, for hash value and input string,\n",
- X"\t-t (trail) str and int formats, for input string and hash value.\n");
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/lhash.c &&
- chmod 0755 iex/lhash.c ||
- echo "restore of iex/lhash.c failed"
- set `wc -c iex/lhash.c`;Wc_c=$1
- if test "$Wc_c" != "2181"; then
- echo original size 2181, current size $Wc_c
- fi
- # ============= iex/mung.c ==============
- echo "x - extracting iex/mung.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/mung.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/mung
- Xexit 0
- X#endif
- X
- X/* transform every string1 within files into string2 */
- X#include "ilib.h"
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char **file;
- X int len1, len2;
- X int item, changed;
- X
- X if (argc < 4)
- X fprintf(stderr,
- X"Usage: %s string1 string2 files\n\n%s%s\n", argv[0],
- X"\tTransform every <string1> in each file into <string2>; or if\n",
- X"\tstring1 is blank, report the <string2> count of each file.\n"),
- X exit(1);
- X
- X /* convert control sequences into control characters */
- X len1 = ifonetic (argv[1]);
- X len2 = ifonetic (argv[2]);
- X
- X /* print count of string2, files are not changed */
- X if (len1 == 0) {
- X if (len2) {
- X for (file = &argv[3]; !NULCHARP (*file); file++) {
- X /* malloc or read/directory permission errors don't
- X damage the file, scanning can continue */
- X if ((item = countstring (argv[2], len2, *file)) == -1)
- XQ fprintf (stderr, "%s %s\n", ierbuf, *file);
- X else if (item)
- X printf ("%s: %d\n", *file, item);
- X }
- X }
- X else
- X fprintf(stderr, "string1 or string2 must be non-null\n");
- X exit (0);
- X }
- X
- X for (file = &argv[3]; !NULCHARP (*file); file++, item = 0) {
- X /* if string2 longer than string1, count occurrences of string1
- X in file, to predict buffer size for the transformed file */
- X if (len2 > len1) {
- X if ((item = countstring (argv[1], len1, *file)) == -1) {
- XQ fprintf (stderr, "%s %s\n", ierbuf, *file);
- X continue;
- X }
- X if (!item)
- X continue;
- X }
- X changed = mung (argv[1], len1, argv[2], len2, *file, item);
- X
- X if (changed < 0) {
- XQ if (idamage (changed))
- XQ fprintf (stderr, "%s %s\n%s", ierbuf, *file,
- X "file damage probable ... aborting\n\n"), exit (1);
- XQ fprintf (stderr, "%s %s\n", ierbuf, *file);
- X }
- X else if (changed)
- X printf ("%s - %d\n", *file, changed);
- X }
- X
- X exit(0);
- X}
- X
- Xint
- Xcountstring (string, len, file)
- Xchar *string, *file;
- Xint len;
- X{
- X char *buffer;
- X char *fromptr, *endptr;
- X int item;
- X
- X if ((item = iread(file, &buffer)) < 0)
- X return (-1);
- X endptr = buffer + item;
- X
- X /* read buffer and count matches */
- X if (len == 1) {
- X for (fromptr = buffer, item = 0; fromptr < endptr; fromptr++)
- X if (*fromptr == *string)
- X ++item;
- X }
- X else {
- X --len;
- X for (fromptr = buffer, item = 0; fromptr < endptr; fromptr++)
- X if (*fromptr == *string)
- X if (ibcmp (fromptr+1, string+1, len) == 0)
- X ++item, fromptr += len;
- X }
- X free(buffer);
- X return (item);
- X}
- X
- Xint
- Xmung (string1, len1, string2, len2, file, item)
- Xchar *string1, *string2, *file;
- Xint len1, len2, item;
- X{
- X int insiz, totsiz, newsiz, extra;
- X char *source, *target;
- X char *buffer, *tmp;
- X char *s1end = string1 + len1 - 1;
- X char *backlimit;
- X char *realloc();
- X
- X /* read file into buffer -- no file damage on error */
- X if ((insiz = iread(file, &buffer)) < 0)
- XQ return (ierflag);
- X
- X /* if string2 longer than string1 and item nonzero, calculate
- X and allocate extra space needed -- no file damage on error */
- X if (len2 > len1) {
- X extra = (len2 - len1) * item;
- X totsiz = insiz + extra;
- X tmp = buffer;
- X if ((buffer = realloc (tmp, (unsigned)totsiz)) == NULL) {
- X free (tmp);
- XQ return (ierror ("while requesting expansion space"));
- X }
- X }
- X else
- X totsiz = insiz;
- X
- X source = buffer + insiz; /* one byte past end of text */
- X target = buffer + totsiz;
- X
- X /* do the substitution */
- X if (len1 == 1) {
- X for (item = 0; --source >= buffer;)
- X if (*source == *s1end) {
- X target -= len2;
- X ibcopy (target, string2, len2);
- X ++item;
- X }
- X else
- X *--target = *source;
- X }
- X else {
- X /* leftmost byte in buffer that may be overlaid with the
- X rightmost byte of string1 */
- X --len1;
- X backlimit = buffer + len1;
- X for (item = 0; --source >= backlimit;)
- X if (*source == *s1end) {
- X if (ibcmp (source-len1, string1, len1) == 0) {
- X target -= len2;
- X ibcopy (target, string2, len2);
- X source -= len1;
- X ++item;
- X }
- X else
- X *--target = *source;
- X }
- X else
- X *--target = *source;
- X
- X /* copy remaining string on left */
- X while (source >= buffer)
- X *--target = *source--;
- X }
- X
- X /* write buffer back only if changed -- error may destroy file */
- X if (item) {
- X newsiz = buffer + totsiz - target;
- X if (iwrite (file, target, buffer + totsiz) != newsiz) {
- X free (buffer);
- XQ return (ierflag);
- X }
- X }
- X
- X free(buffer);
- X return (item);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/mung.c &&
- chmod 0755 iex/mung.c ||
- echo "restore of iex/mung.c failed"
- set `wc -c iex/mung.c`;Wc_c=$1
- if test "$Wc_c" != "4344"; then
- echo original size 4344, current size $Wc_c
- fi
- # ============= iex/ncat.c ==============
- echo "x - extracting iex/ncat.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/ncat.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/ncat
- Xexit 0
- X#endif
- X
- X/* line number files separately, add "spooler" string before each file */
- X#include "ilib.h"
- X#define SPACE 32
- Xchar sep[IONEK]; /* buffer for expanded separator string */
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char *separator; /* pointer to command line's separator */
- X char **argp = argv+1;
- X char *buf; /* buffer to hold a file at a time */
- X char *end; /* next byte after buffer */
- X char *lp; /* string pointer into buf */
- X int optchar; /* shown as 's' --- actual value irrelevant */
- X int fsiz; /* byte count of file */
- X int lsiz; /* byte count of line */
- X int lc; /* line counter */
- X int seplen; /* length of separator */
- X int convlen; /* length of expanded separator */
- X
- X if (argc < 2)
- X fprintf(stderr,
- X"Usage: %s [-s separator] files\n%s%s%s", argv[0],
- X"\tprint individually line-numbered <files> to stdout, prepend each\n",
- X"\twith an optional <separator> line. Each '$' byte of <separator>\n",
- X"\tis a wild card, a place holder for the current file name.\n\n"),
- Xexit(0);
- X
- X if (optchar = iopt (&argp)) {
- X separator = *argp++;
- X seplen = ifonetic (separator);
- X }
- X
- X /* process each file remaining on the command line, in a loop */
- X for (; !NULCHARP (*argp); argp++) {
- X if ((fsiz = iread (*argp, &buf)) < 0)
- X fprintf (stderr, "%s: %s\n",ierbuf, *argp), exit (1);
- X end = buf + fsiz;
- X if (optchar) {
- X convlen = convdollar (separator, seplen, *argp);
- X printf ("%*.*s", convlen, convlen, sep);
- X }
- XQ for (lp = buf, lc = 0; (lsiz = iline (lp, end)) > 0; lp += lsiz)
- X printf ("%6d%c%c%s\n", ++lc, SPACE, SPACE, lp);
- X free (buf);
- X }
- X exit(0);
- X}
- X
- Xint
- Xconvdollar (string, len, fname)
- Xchar *string, *fname;
- Xint len;
- X{
- X char *block = string + len;
- X char *bp = sep;
- X
- X for (; string < block; string++) {
- X if (*string == '$') {
- X strcpy (bp, fname);
- X bp = inull (bp);
- X }
- X else
- X *bp++ = *string;
- X }
- X *bp = '\0'; /* null terminate */
- X return (bp - sep);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/ncat.c &&
- chmod 0755 iex/ncat.c ||
- echo "restore of iex/ncat.c failed"
- set `wc -c iex/ncat.c`;Wc_c=$1
- if test "$Wc_c" != "2061"; then
- echo original size 2061, current size $Wc_c
- fi
- # ============= iex/nest.c ==============
- echo "x - extracting iex/nest.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/nest.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/nest
- X(/bin/rm -f $BIN/nestinfo)
- X/bin/ln $BIN/nest $BIN/nestinfo
- Xexit 0
- X#endif
- X
- X/* print nesting information for delimiter pair */
- X#include "ilib.h"
- X#define NESTINFO 614
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char *buf; /* for storing entire input */
- X char *end; /* pointer to byte past input */
- X char *origlines; /* pointer list returned by illistn */
- X char **op; /* to pointers of list */
- X int *infopairs; /* pointer to list mallocd by inest */
- X int *ip; /* pointer to integers of list */
- X int olcnt; /* original line count */
- X int ret; /* returned by inest */
- X int ii = 0; /* scratch counter, mismatch flag */
- X int ij = 0; /* one-shot counter of original lines */
- X int lcnt = 0; /* count left delimiters */
- X int rcnt = 0; /* count right delimiters */
- X int bytes; /* byte count of input */
- X char *adr; /* make address from offset */
- X
- X if (argc < 3)
- X fprintf(stderr,
- X "Usage: %s ldelim rdelim [file]\n%s", argv[0],
- X "\tPrint nesting information for delimiter pair.\n\n"), exit(1);
- X
- X ifonetic (argv[1]);
- X ifonetic (argv[2]);
- X
- X /* procure input */
- X if ((bytes = ifilter (argv[3], &buf)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X end = buf + bytes;
- X
- XQ /* mark original lines before stripping out comments */
- X if ((olcnt = illistn (buf, end, &origlines)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X
- X /* restore newlines nulled out by illistn */
- X itran (buf, end, 0, '\n');
- X
- XQ istripcom (buf, end, "/*", "*/");
- X istripdq (buf, end);
- X istripsq (buf, end);
- X
- XQ if ((ret = inest (buf, end, argv[1], argv[2], &infopairs)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X if (!ret)
- X exit (0);
- X
- X /* if command name was nestinfo, output single line summary */
- XQ if (ihasharg (argv[0], '/') == NESTINFO) {
- X for (ip = infopairs; *ip; ip += 2)
- X if (*(ip+1) > 0)
- X lcnt++;
- X else if (*(ip+1) < 0)
- X rcnt++;
- X else /* zero value shows negative nestlev, mismatch */
- X rcnt++, ii = 1;
- X printf ("%d %s %d %s %s\n", lcnt, argv[1], rcnt, argv[2],
- X ii ? "leading right" :
- X ((lcnt == rcnt) ? "matched" : "mismatched")), exit (0);
- X }
- X
- X /* else: output line-numbered, tab-formatted nesting list;
- X since the next delimiter cannot be on an earlier line,
- X contrast the address of the next delimiter only from the
- X address of the line that most recently failed to be lower
- X than the address of the delimiter.
- X */
- X op = (char **)origlines;
- X for (ip = infopairs; *ip; ip += 2) {
- X for (adr = buf + *ip; ij < olcnt; ij++, op++)
- X if (*op > adr)
- X break;
- X printf ("%d", ij);
- X for (ii = abs (*(ip+1)); --ii >= 0; putchar ('\t'));
- X puts (*(ip+1)>0 ? argv[1] : argv[2]);
- X }
- X exit(0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/nest.c &&
- chmod 0755 iex/nest.c ||
- echo "restore of iex/nest.c failed"
- set `wc -c iex/nest.c`;Wc_c=$1
- if test "$Wc_c" != "2799"; then
- echo original size 2799, current size $Wc_c
- fi
- # ============= iex/nulcat.c ==============
- echo "x - extracting iex/nulcat.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/nulcat.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/nulcat
- Xexit 0
- X#endif
- X
- X/* write input to stdout, changing NUL bytes to '@' */
- X#include "ilib.h"
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char buffer[BUFSIZ];
- X int bytes;
- X
- X /* rename file to stdin */
- X if (argc > 1) {
- X if (freopen (argv[1], "r", stdin) == NULL) {
- X ierror ("at setting up filter in main");
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X }
- X }
- X while ((bytes = fread (buffer, 1, BUFSIZ, stdin)) > 0)
- XQ idump (buffer, buffer+bytes, '@');
- X
- X exit(0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/nulcat.c &&
- chmod 0755 iex/nulcat.c ||
- echo "restore of iex/nulcat.c failed"
- set `wc -c iex/nulcat.c`;Wc_c=$1
- if test "$Wc_c" != "541"; then
- echo original size 541, current size $Wc_c
- fi
- # ============= iex/objed.c ==============
- echo "x - extracting iex/objed.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/objed.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/objed
- Xexit 0
- X#endif
- X
- X/* binary file editor */
- X#include <sys/types.h>
- X#include <sys/stat.h>
- X#include "ilib.h"
- X#define SPACE 32
- X#define ISHEX(c) (((c)>47&&(c)<58) || ((c)>64&&(c)<71))
- X#define ISPRINT(c) ((c)>32&&(c)<127)
- X
- Xmain (argc,argv)
- Xint argc;
- Xchar *argv[];
- X{
- X FILE *fopen(), *asc;
- X char *mksys(), *realloc(), *mktemp();
- X char **ufile = &argv[1];
- X char edfile[32]; /* path/name of file converted for edit */
- X char *obj; /* address of malloc'd buffer of object file */
- X char *oend; /* sentinel pointer at end of obj */
- X char *line; /* buffer for line-by-line conversion */
- X char *syscmd = "vi";
- X int sysflag = 0; /* if true, syscmd specified on command line */
- X int optchar;
- X int objsiz; /* byte size of object file */
- X int linesiz; /* byte size of malloc'd line */
- X int nostring = 0; /* if true, don't print strings in edit buf */
- X int bytes = 24; /* number of original bytes per edit buf line */
- X struct stat sbuf; /* to see if user changed ASCII file */
- X time_t st_mtime; /* file last modify time */
- X
- X if (argc < 2)
- Xcmderr:
- X fprintf(stderr,
- X"\nUsage: %s [-] [-b N] [-e cmd] file\n\n%s%s%s%s%s%s%s%s\n", argv[0],
- X"\tConvert 'file' to ASCII and edit it (using vi editor by default),\n",
- X"\tafterwards restore edited copy to binary, and write back to disk.\n",
- X"\tBinary bytes are edited as (space separated) pairs of hex digits,\n",
- X"\t'isprint' bytes are shown as single characters, two spaces apart,\n",
- X"\t'isprint' byte strings three or longer, are printed contiguously.\n",
- X"\t- forces even 'isprint' bytes to be edited in the hex form.\n",
- X"\t-b N edit N original bytes per line instead of the default 24.\n",
- X"\t-c cmd use 'cmd' instead of vi (embedded '$'s stand for 'file').\n"
- X ), exit(1);
- X
- X while (optchar = iopt (&ufile)) {
- X switch (optchar) {
- X default:
- X fprintf (stderr, "Bad option -%c\n", optchar);
- X exit (1);
- X case '-':
- X nostring = 1;
- X break;
- X case 'b':
- X if ((bytes = atoi (*ufile)) < 1)
- X fprintf (stderr, "Minimum 1 byte per line\n"), exit(1);
- X break;
- X case 'e':
- X sysflag = 1;
- X syscmd = *ufile;
- X ifonetic (*ufile);
- X break;
- X }
- X }
- X if (NULCHARP (*ufile))
- X goto cmderr;
- X if ((objsiz = iread (*ufile, &obj)) < 0)
- X fprintf (stderr, "%s %s\n", ierbuf, *ufile), exit (1);
- X oend = obj + objsiz;
- X
- X linesiz = bytes << 4; /* 16 times */
- X if ((line = malloc ((unsigned) linesiz+1)) == NULL)
- X fprintf (stderr, "Can't allocate line buffer\n"), exit (1);
- X
- X /* make temporary file for converted data, open for write */
- X sprintf (edfile, "/tmp/%s", mktemp ("objed.XXXXXX"));
- X if ((asc = fopen (edfile, "w")) == NULL)
- X fprintf (stderr, "Can't write to %s\n", edfile), exit (1);
- X
- X /* convert object file into ASCII in temporary file */
- X breakobj (obj, oend, edfile, asc, line, bytes, nostring);
- X fclose (asc);
- X
- X if (stat (edfile, &sbuf) == -1)
- X fprintf (stderr, "Can't stat %s\n", edfile), exit (1);
- X st_mtime = sbuf.st_mtime;
- X
- X /* create system command, apply to ASCII version of object */
- X system (mksys (sysflag, syscmd, edfile));
- X
- X /* reconstruct object from ASCII version, write if different */
- X if (stat (edfile, &sbuf) == -1)
- X fprintf (stderr, "Can't stat %s\n", edfile), exit (1);
- X if (st_mtime == sbuf.st_mtime) { /* no change during syscmd */
- X unlink (edfile);
- X exit(0);
- X }
- X if ((asc = fopen (edfile, "r")) == NULL)
- X fprintf (stderr, "Can't read %s\n", edfile), exit (1);
- X
- X /* reuse "bytes" for new size */
- X fprintf (stderr, "Restoring %s from edit buffer %s\n",
- X *ufile, edfile);
- X if ((bytes = newsize (asc, line, linesiz)) < 0)
- X fprintf (stderr, "Edit line too long\n"), exit (1);
- X if (bytes > objsiz &&
- X (obj = realloc (obj, (unsigned) bytes)) == NULL)
- X fprintf (stderr, "No buffer space for recreating object\n"),
- X exit (1);
- X reconst (obj, asc, line, linesiz);
- XQ if (iwrite (*ufile, obj, obj+bytes) < 0)
- X fprintf (stderr, "%s restore %s\n", ierbuf, *ufile),
- X exit (1);
- X
- X unlink (edfile);
- X exit(0);
- X}
- X
- Xbreakobj (obj, oend, edfile, asc, line, bytes, nostring)
- Xunsigned char *obj, *oend;
- Xchar *edfile, *line;
- XFILE *asc;
- Xint bytes, nostring;
- X{
- X char *number = "0123456789ABCDEF";
- X unsigned char *oj; /* working pointer in obj */
- X unsigned char *es; /* past last byte of string in obj */
- X unsigned char *mx; /* past last byte that may be considered */
- X char *lp = line; /* pointer into line */
- X int slen; /* length of string found in obj */
- X int space; /* obj char positions remaining in line */
- X int lesser; /* smaller of slen and space */
- X int bc = 0; /* byte counter in line */
- X
- X for (oj = obj -1; ++oj < oend;) {
- X if (++bc < bytes) {
- X if (nostring || *oj < 33 || *oj > 126) {
- X *lp++ = *(number + (*oj >> 4)); /* div by 16 */
- X *lp++ = *(number + (*oj & 0xf)); /* mod 16 */
- X *lp++ = SPACE;
- X }
- X else if (!nostring) { /* possible string in obj */
- X
- X /* if a string (of all 'isprint' characters) longer
- X than two bytes will fit in the current line,
- X install the maximum length string immediately
- X */
- X if ((mx = oj + bytes-bc+1) > oend)
- X mx = oend;
- X for (es = oj; ++es < mx && ISPRINT(*es););
- X if ((slen=es-oj) > 2 && (space=bytes-bc+1) > 2) {
- X lesser = (slen < space) ? slen : space;
- X for (; --lesser >= 0; *lp++ = *oj++, bc++);
- X --oj; /* prior to reincrement in outermost loop */
- X if (bc >= bytes) {
- X bc = 0;
- X *lp++ = '\n';
- X if (!fwrite (line, 1, lp-line, asc))
- X fprintf (stderr, "Can't write to %s\n",
- X edfile), exit (1);
- X lp = line;
- X }
- X else
- X --bc, *lp++ = SPACE;
- X }
- X else /* not long enough or no space; print byte */
- X *lp++ = *oj, *lp++ = SPACE, *lp++ = SPACE;
- X
- X }
- X }
- X else {
- X bc = 0;
- X if (nostring || *oj < 33 || *oj > 126) {
- X *lp++ = *(number + (*oj >> 4)); /* div by 16 */
- X *lp++ = *(number + (*oj & 0xf)); /* mod 16 */
- X *lp++ = '\n';
- X }
- X else if (!nostring)
- X *lp++ = *oj, *lp++ = '\n';
- X
- X if (!fwrite (line, 1, lp-line, asc))
- X fprintf (stderr, "Can't write to %s\n", edfile),
- X exit (1);
- X lp = line;
- X }
- X }
- X if (bc) { /* write out partial last line */
- X *(lp-1) = '\n';
- X if (!fwrite (line, 1, lp-line, asc))
- X fprintf (stderr, "Can't write to %s\n", edfile),
- X exit (1);
- X }
- X}
- X
- Xchar *
- Xmksys (sysflag, usr, edfile)
- Xint sysflag;
- Xchar *usr, *edfile;
- X{
- X char *sp; /* to manufactured command */
- X char *up; /* to original usr command string */
- X char *cmd; /* to address of buffer allocated for new cmd */
- X int ref = 1; /* number of times file name is mentioned */
- X int flen = strlen (edfile);
- X
- X for (up = usr; *up; up++)
- X if (*up == '$')
- X ++ref;
- X if ((cmd = malloc ((unsigned)strlen(usr) + ref * flen +2)) == NULL)
- X fprintf (stderr, "No buffer space for syscmd\n"), exit (1);
- X
- X if (!sysflag) {
- X sprintf (cmd, "%s%c%s", usr, SPACE, edfile);
- X return (cmd);
- X }
- X
- X for (sp = cmd, up = usr; *up; up++)
- X if (*up == '$')
- X strcpy (sp, edfile), sp += flen;
- X else
- X *sp++ = *up;
- X if (!--ref)
- X sprintf (sp, "%c%s", SPACE, edfile);
- X return (cmd);
- X}
- X
- Xnewsize (asc, line, linesiz)
- XFILE *asc;
- Xchar *line;
- Xint linesiz;
- X{
- X char bf[IHALFK];
- X char *endl = line + linesiz;
- X char *lp;
- X char *np;
- X int len = 0; /* new expected length of object */
- X int wlen; /* length of single token */
- X
- X for (; fgets (line, linesiz, asc) != NULL; ) {
- X /* if line is too long (not ending in '\n'), terminate */
- X for (lp = line; lp < endl && *lp != '\n'; lp++);
- X if (lp == endl)
- X return (-1);
- X for (np = line; (np = ianytok (np, lp, bf)) != NULL;)
- X if ((wlen=strlen(bf)) == 2 && ISHEX(*bf) && ISHEX(bf[1]))
- X ++len;
- X else
- X len += wlen;
- X }
- X rewind (asc);
- X return (len);
- X}
- X
- Xreconst (obj, asc, line, linesiz)
- XFILE *asc;
- Xunsigned char *obj;
- Xchar *line;
- Xint linesiz;
- X{
- X char bf[IHALFK];
- X unsigned char *op = obj;
- X char *lp; /* end of fgets string */
- X char *np; /* token pointer in line */
- X int bval; /* new binary value */
- X int wlen; /* length of single token */
- X
- X for (; fgets (line, linesiz, asc) != NULL; ) {
- X lp = inull (line);
- X for (np = line; (np = ianytok (np, lp, bf)) != NULL;) {
- X if ((wlen=strlen(bf)) == 2 && ISHEX(*bf) && ISHEX(bf[1])) {
- X bval = (*bf - ((*bf < 65) ? 48 : 55)) << 4;
- X bval += (bf[1] - ((bf[1] < 65) ? 48 : 55));
- X *op++ = bval;
- X }
- X else {
- X strcpy ((char *)op, bf);
- X op += wlen;
- X }
- X }
- X }
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/objed.c &&
- chmod 0755 iex/objed.c ||
- echo "restore of iex/objed.c failed"
- set `wc -c iex/objed.c`;Wc_c=$1
- if test "$Wc_c" != "8383"; then
- echo original size 8383, current size $Wc_c
- fi
- # ============= iex/pathfiles.c ==============
- echo "x - extracting iex/pathfiles.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/pathfiles.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/pathfiles
- X(/bin/rm -f $BIN/rpath $BIN/wpath $BIN/xpath $BIN/fpath)
- X/bin/ln $BIN/pathfiles $BIN/rpath
- X/bin/ln $BIN/pathfiles $BIN/wpath
- X/bin/ln $BIN/pathfiles $BIN/xpath
- X/bin/ln $BIN/pathfiles $BIN/fpath
- Xexit 0
- X#endif
- X
- X/* list readable/writable/executable files of $PATH or given paths */
- X#include "ilib.h"
- X#define FPATH 371
- X#define RPATH 383
- X#define WPATH 388
- X#define XPATH 389
- X
- Xchar *getenv();
- Xmain (argc, argv)
- Xchar *argv[];
- Xint argc;
- X{
- X char *mktemp();
- X char *nextok; /* to get next dir in $PATH */
- X char newpath[ITWOK]; /* copy command line arguments */
- X char *path = newpath; /* point to whichever path */
- X char *buf; /* buffer to read tmpfile into */
- X char *list; /* pointers to lines of /tmp file */
- X char **lp; /* pointer into list */
- X char **dirs = &argv[1]; /* dirs in command line */
- X char tmpname[20]; /* temporary file for ls output */
- X static char tokbuf[IONEK];
- X int lines; /* in /tmp listing */
- X int tsiz; /* token size */
- X int bsiz; /* buffer size */
- X int mode;
- X int context = ihasharg (argv[0], '/');
- X
- X switch (context) {
- X default:
- X fprintf(stderr,
- X"\nUsage: fpath | rpath | wpath | xpath [pathdir ...]\n%s%s\n",
- X"\tPrint full path of files | readable | writable | executable files\n",
- X"\tfound in the list of specified 'pathdirs', or in $PATH.\n"), exit(0);
- X case FPATH:
- X mode = F_OK;
- X break;
- X case RPATH:
- X mode = R_OK;
- X break;
- X case WPATH:
- X mode = W_OK;
- X break;
- X case XPATH:
- X mode = X_OK;
- X break;
- X }
- X strcpy (tmpname, "/tmp/");
- X strcat (tmpname, mktemp ("pathXXXXXX"));
- X
- X if (argc > 1)
- X for (*path = 0; !NULCHARP (*dirs); dirs++)
- X strcat (path, *dirs), strcat (path, " "); /* space at end */
- X else if ((path = getenv("PATH")) == NULL)
- X fprintf (stderr, "PATH variable not set\n"), exit (1);
- X
- XQ itok (INITOKF, path, (char *)NULL);
- X /* space,colon delimiters to tokenize either types of path */
- XQ while (tsiz = itok (ITOKF, " :", (char *)NULL, &nextok)) {
- XQ sprintf (tokbuf, "/bin/ls -a '%*.*s' > %s",
- X tsiz, tsiz, nextok, tmpname);
- X system (tokbuf);
- X if ((bsiz = iread (tmpname, &buf)) < 0)
- X continue;
- X if ((lines = illistn (buf, buf + bsiz, &list)) < 0)
- X puts (ierbuf), exit (1);
- X for (lp = (char **)list; --lines >= 0; lp++) {
- X if (strcmp (*lp, ".") && strcmp (*lp, "..")) {
- XQ sprintf (tokbuf, "%*.*s/%s", tsiz, tsiz, nextok, *lp);
- X if (access (tokbuf, mode) != -1)
- X puts (tokbuf);
- X }
- X }
- X free (list);
- X free (buf);
- X }
- X unlink (tmpname);
- X exit(0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/pathfiles.c &&
- chmod 0755 iex/pathfiles.c ||
- echo "restore of iex/pathfiles.c failed"
- set `wc -c iex/pathfiles.c`;Wc_c=$1
- if test "$Wc_c" != "2655"; then
- echo original size 2655, current size $Wc_c
- fi
- # ============= iex/q.c ==============
- echo "x - extracting iex/q.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/q.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/q
- Xexit 0
- X#endif
- X
- X/* manage stack of /tmp files for stdout segments, report filename */
- X#include "ilib.h"
- X#include <sys/types.h> /* to interpret struct stat */
- X#include <sys/stat.h> /* get last modified time from stat() */
- X#define PATH "/tmp/"
- X#define SPACE 32 /* make ASCII SPACE visible in C code */
- X
- Xmain (argc, argv)
- Xchar *argv[];
- Xint argc;
- X{
- X struct stat sbuf; /* for info on 'last modified time' */
- X time_t oldest = 0x7fffffff; /* Unix time, in seconds since 1970 */
- X time_t filetime; /* 'last modified time' of curr file */
- X char *qlist = "0123456789"; /* list of ending bytes of file names */
- X char *qindx; /* at last (counter) byte of qfile */
- X char qfile[32]; /* buffer for curr file name */
- XQ char cmd[IONEK]; /* 1K, defined in ilib.h */
- X char *cmdptr; /* working in cmd buffer */
- X int uid; /* user id of person executing q */
- X int cnt; /* scratch counter */
- X int unused = 0; /* file not in use flag */
- X int oldmark; /* flag oldest (least recent) file */
- X
- X /* make file name, position qindx at the last (counter) byte */
- X uid = getuid();
- X sprintf (qfile, "%sqtmp%.4d", PATH, uid);
- X qindx = qfile + strlen (qfile);
- X *(qindx+1) = '\0';
- X
- X /* if no arguments after command name, remove all queued up files */
- X if (argc < 2) {
- X for (cnt = 10; --cnt >= 0;)
- X *qindx = *(qlist + cnt), unlink(qfile);
- X exit(0);
- X }
- X /* expand phonetic control sequences in command arguments */
- X for (cnt = 1; cnt < argc; cnt++)
- XQ ifonetic (argv[cnt]);
- X
- X /* look for next available file */
- X for (cnt = 10; --cnt >= 0;) {
- X *qindx = *(qlist + cnt);
- X /* first choice is file that not yet exists */
- X if (stat (qfile, &sbuf)) {
- X unused = 1;
- X break;
- X }
- X /* else find and reuse the oldest file
- X (use the stat data gotten above)
- X first file in loop begins marked "oldest"
- X */
- X if ((filetime = sbuf.st_mtime) < oldest)
- X oldest = filetime, oldmark = cnt;
- X }
- X if (!unused)
- X *qindx = *(qlist + oldmark);
- X
- X /* generate string for system command */
- X for (cmdptr = cmd, cnt = 1; cnt < argc; cnt++) {
- X strcpy (cmdptr, argv[cnt]);
- X cmdptr += strlen (argv[cnt]);
- X *cmdptr++ = SPACE;
- X }
- X sprintf (cmdptr, "> %s", qfile);
- X /* exec and report */
- X system (cmd);
- X puts (qfile);
- X exit(0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/q.c &&
- chmod 0755 iex/q.c ||
- echo "restore of iex/q.c failed"
- set `wc -c iex/q.c`;Wc_c=$1
- if test "$Wc_c" != "2424"; then
- echo original size 2424, current size $Wc_c
- fi
- # ============= iex/rename.c ==============
- echo "x - extracting iex/rename.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/rename.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/rename
- Xexit 0
- X#endif
- X
- X/* change string pattern1 in filenames to pattern2 */
- X#include "ilib.h"
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char cb[IONEK]; /* change buffer for altering file name */
- X char *fs; /* to matching pattern1 in file name */
- X char *fe; /* just past the matching pattern1 in file name */
- X char *pat1; /* pattern1 in command line, or NULL */
- X char *pat2; /* pattern2 in command line, or NULL */
- X char **file; /* to list of files */
- X int p1len; /* length of pattern1, if non-NULL */
- X
- X if (argc < 4)
- Xfprintf (stderr, "Usage: %s pattern1 pattern2 files\n%s%s%s", argv[0],
- X"\tto replace the leading pattern1 in each filename with pattern2,\n",
- X"\tor if pattern1 is '-', prepend pattern2 in front of filenames,\n",
- X"\tor if pattern2 is '-', delete leading pattern1 from filenames.\n"),
- Xexit(1);
- X
- X ifonetic (argv[1]), ifonetic (argv[2]);
- X
- X if (strcmp (argv[1], argv[2]) == 0) /* nothing to do */
- X exit (0);
- X if (strcmp ((pat2 = argv[2]), "-") == 0)
- X pat2 = NULL;
- X if (strcmp ((pat1 = argv[1]), "-") == 0)
- X pat1 = NULL;
- X else
- X p1len = strlen (pat1);
- X
- X for (file = &argv[3]; !NULCHARP(*file); file++) {
- X if (NULCHARP (pat1))
- X sprintf(cb, "%s%s", pat2, *file);
- XQ else if ((fs = ianymatch (*file, inull(*file), pat1)) != NULL) {
- X fe = fs + p1len;
- X strcpy (cb, *file);
- X cb[fs - *file] = 0; /* terminate string at pat1 */
- X if (!NULCHARP (pat2))
- X strcat (cb, pat2);
- X strcat (cb, fe);
- X }
- X else
- X continue;
- X
- X /* create link with new name, then delete the original */
- X if (link (*file, cb)) {
- X fprintf (stderr, "Can't create %s\n", cb);
- X continue; /* don't try to delete original */
- X }
- X if (unlink (*file))
- X fprintf (stderr, "Can't remove duplicate %s\n", *file);
- X }
- X exit(0);
- X}
- SHAR_EOF
- $TOUCH -am 0607140190 iex/rename.c &&
- chmod 0755 iex/rename.c ||
- echo "restore of iex/rename.c failed"
- set `wc -c iex/rename.c`;Wc_c=$1
- if test "$Wc_c" != "1822"; then
- echo original size 1822, current size $Wc_c
- fi
- # ============= iex/rewrap.c ==============
- echo "x - extracting iex/rewrap.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/rewrap.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/rewrap
- Xexit 0
- X#endif
- X
- X/* re-list tokens of file, in place */
- X#include "ilib.h"
- X
- X#define DEFAULT 76
- X#define SPACE 32
- X#define NUL 0
- X
- Xchar *mktemp();
- Xchar bed[IHALFK]; /* buffer for token */
- Xchar line[IONEK]; /* buffer for line of input */
- Xchar build[sizeof(line)*2]; /* buffer for assembling output */
- Xchar indent[IONEK]; /* buffer for storing input indentation */
- Xchar outfname[32]; /* collect output in /tmp file */
- Xchar *buildptr; /* current point in assembled text */
- Xchar *popto; /* return here when buildptr overruns */
- Xchar *tokend; /* end of token recognized by ianytok */
- Xint cols; /* maximum width of output line */
- Xint indentlen; /* size of indent in current input line */
- Xint firstinbuild; /* flag first token of output line */
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char **file = argv;
- X
- X if (argc != 3)
- X fprintf(stderr, "Usage: %s maxcols file\n", *file), exit(0);
- X
- X if ((cols = abs (atoi (*++file))) < 1 || cols > IONEK-1)
- X cols = DEFAULT,
- X fprintf(stderr, "%s '%s'\nUsing default width (%d columns)\n",
- X "Line width out of bounds:", *file, DEFAULT);
- X
- X /* rename input file to stdin, create output file as stdout */
- XQ if (freopen (*++file, "r", stdin) == NULL)
- X fprintf (stderr, "Open failed on %s\n", *file), exit (1);
- X sprintf (outfname, "/tmp/%s", mktemp ("rewrap.XXXXXX"));
- XQ if (freopen (outfname, "w", stdout) == NULL)
- X fprintf (stderr, "Can't write to %s\n", outfname), exit (1);
- X
- X buildptr = build, *buildptr = NUL, firstinbuild = 1;
- X
- X /* preserve newlines in input, limit line size via fgets vs. gets */
- X while (fgets (line, sizeof (line), stdin) != NULL)
- X rewrapline();
- X if (buildptr != build)
- X puts (build);
- X
- XQ fflush (stdout);
- X sprintf (line, "/bin/mv %s %s", outfname, *file);
- X if (system (line))
- X fprintf (stderr, "Can't move %s to %s\n", outfname, *file),
- X exit (1);
- X exit (0);
- X}
- X
- Xrewrapline ()
- X{
- X char *lineptr, *endline;
- X
- X /* Advance to first token in line, save original indentation */
- X lineptr = line;
- X while (*lineptr == SPACE || *lineptr == '\t')
- X ++lineptr;
- X
- X if (*lineptr == '\n') { /* no printing characters in input */
- X *indent = NUL, indentlen = 0;
- X if (buildptr != build)
- X puts (build);
- X buildptr = build, *buildptr = NUL, firstinbuild = 1;
- X puts ("");
- X return;
- X }
- X
- X if (lineptr == line) /* no leading spaces in input */
- X *indent = NUL, indentlen = 0;
- X else {
- X indentlen = lineptr-line;
- X strncpy (indent, line, indentlen);
- X *(indent+indentlen) = NUL;
- X }
- X
- X endline = lineptr + strlen(lineptr) -1;
- X *endline = NUL; /* kill newline */
- X tokend = lineptr;
- X
- XQ while ((tokend = ianytok (tokend, endline, bed)) != NULL) {
- X if (firstinbuild) {
- X firstinbuild = 0;
- X strcpy (build, indent); /* begin output with indent */
- X buildptr = build + indentlen;
- X if (buildptr - build > cols) {
- X fprintf (stderr, "Skipping oversize indent\n");
- X buildptr = build;
- X *buildptr = NUL;
- X *indent = NUL, indentlen = 0;
- X }
- X strcat (buildptr, bed); /* first token in output */
- X buildptr += strlen (bed);
- X if (buildptr - build > cols) {
- X fprintf (stderr, "Oversize line: %s\n", build);
- X puts (build);
- X buildptr = build, *buildptr = NUL, firstinbuild = 1;
- X }
- X continue;
- X }
- X /* successive tokens in assembled output */
- X popto = buildptr;
- X if (*(buildptr-1) == '.') /* extra space after sentence */
- X *buildptr++ = SPACE;
- X *buildptr++ = SPACE;
- X *buildptr = NUL;
- X strcat (buildptr, bed);
- X buildptr += strlen (bed);
- X if (buildptr - build > cols) {
- X *popto = NUL;
- X puts (build);
- X buildptr = build, *buildptr = NUL, firstinbuild = 1;
- X tokend -= strlen (bed);
- X }
- X }
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/rewrap.c &&
- chmod 0755 iex/rewrap.c ||
- echo "restore of iex/rewrap.c failed"
- set `wc -c iex/rewrap.c`;Wc_c=$1
- if test "$Wc_c" != "3770"; then
- echo original size 3770, current size $Wc_c
- fi
- # ============= iex/rot.c ==============
- echo "x - extracting iex/rot.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/rot.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/rot
- X(/bin/rm -f $BIN/rotr $BIN/rotl $BIN/rotor $BIN/rotol)
- X/bin/ln $BIN/rot $BIN/rotr
- X/bin/ln $BIN/rot $BIN/rotl
- X/bin/ln $BIN/rot $BIN/rotor
- X/bin/ln $BIN/rot $BIN/rotol
- Xexit 0
- X#endif
- X
- X/* rotate input text array */
- X#include "ilib.h"
- X
- X/* primitive hash values decode which link was used as command name */
- X#define ROT 245
- X#define ROTR 327
- X#define ROTL 321
- X#define ROTOR 406
- X#define ROTOL 400
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char *buffer; /* original text */
- X char *padded; /* copy of original, space filled to even width */
- X char *done; /* rotated array address */
- X int bytes; /* buffer size */
- X int context; /* command name variant of current invokation */
- X int longest; /* longest line in original text, senza newline */
- X int lcnt; /* number of lines in original text */
- X
- X /* decode command name */
- X if ((context = ihasharg (argv[0], '/')) == ROT)
- X fprintf (stderr, "Print rotated image to stdout:\n%s%s%s%s",
- X"rotr [file] --- rotate text 90 degrees to right\n",
- X"rotl [file] --- rotate text 90 degrees to left\n",
- X"rotor [file] --- rotate text 180 deg. in depth, 90 deg. to right\n",
- X"rotol [file] --- rotate text 180 deg. in depth, 90 deg. to left\n"),
- X exit(0);
- X
- X /* get original text, space fill copy, free original buffer */
- X if ((bytes = ifilter (argv[1], &buffer)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- XQ if ((longest = itexrect (buffer, buffer+bytes, &padded, &lcnt)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X free (buffer);
- X
- X /* convert context to rotation type */
- X switch (context) {
- X default:
- X case ROTR: context = IROTR; break;
- X case ROTOR: context = IROTOR; break;
- X case ROTL: context = IROTL; break;
- X case ROTOL: context = IROTOL; break;
- X }
- X
- X /* rotate and print */
- XQ if ((bytes = irotate (padded, longest, lcnt, &done, context)) < 0)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X write (1, done, bytes);
- X exit(0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/rot.c &&
- chmod 0755 iex/rot.c ||
- echo "restore of iex/rot.c failed"
- set `wc -c iex/rot.c`;Wc_c=$1
- if test "$Wc_c" != "1982"; then
- echo original size 1982, current size $Wc_c
- fi
- # ============= iex/scat.c ==============
- echo "x - extracting iex/scat.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/scat.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/scat
- Xexit 0
- X#endif
- X
- X/* reprint input lines from string1, stop at line containing string2 */
- X#include "ilib.h"
- X
- Xmain (argc,argv)
- Xint argc;
- Xchar *argv[];
- X{
- X char **argp;
- X char *buf; /* to file buffer malloc'd by iread */
- XQ char *lpbuf; /* to line pointers malloc'd by illistn */
- X char *end; /* point to one byte past buffer */
- XQ char **ptr; /* working pointer in lpbuf */
- X char *str1 = argv[1]; /* start string given in command line */
- X char *str2 = NULL; /* stop string given in command line */
- X int filesize; /* size of malloc'd text buffer */
- X int lcount; /* number of lines in file */
- X int optchar;
- X
- X if (argc < 2)
- X fprintf (stderr,
- X"Usage: %s string1 [-s string2] [files]\n\%s%s%s", argv[0],
- X"\tcat first line of input/file (to stdout) that contains <string1>,\n",
- X"\tor the section of input/file from the line containing <string1>\n",
- X"\tstopping at (not including) the line that contains <string2>\n\n"),
- X exit(1);
- X
- X ifonetic (argv[1]);
- X argp = argv + 2;
- X if ((optchar = iopt (&argp))) { /* looks like an option */
- X if (optchar == 's') {
- X str2 = *argp++; /* increment argp to file name */
- X ifonetic (str2);
- X }
- X else
- X argp = argv + 2; /* could be file name with leading '-' */
- X }
- X
- X if ((filesize = ifilter (*argp, &buf)) < 0)
- X fprintf (stderr, "%s %s\n", ierbuf, *argp), exit (1);
- X
- X do {
- X end = buf + filesize;
- X /* set up pointers to lines of file */
- XQ if ((lcount = illistn (buf, end, &lpbuf)) < 1)
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X
- XQ for (ptr = (char **)lpbuf; !NULCHARP(*ptr); ptr++) {
- X if (ianymatch (*ptr, *(ptr+1), str1) == NULL)
- X continue;
- X puts (*ptr);
- X if (str2 != NULL) {
- X for (; !NULCHARP(*++ptr);) {
- X if (ianymatch (*ptr, *(ptr+1), str2) != NULL)
- X break;
- X puts (*ptr);
- X }
- X }
- X break;
- X }
- X free (buf);
- X free (lpbuf);
- X
- X /* repeat for other files */
- X if (!NULCHARP (*argp) && !NULCHARP (*++argp))
- X if ((filesize = iread (*argp, &buf)) < 0)
- X fprintf (stderr, "%s %s\n", ierbuf, *argp), exit (1);
- X } while (!NULCHARP (*argp));
- X
- X exit (0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/scat.c &&
- chmod 0755 iex/scat.c ||
- echo "restore of iex/scat.c failed"
- set `wc -c iex/scat.c`;Wc_c=$1
- if test "$Wc_c" != "2170"; then
- echo original size 2170, current size $Wc_c
- fi
- # ============= iex/stamp.c ==============
- echo "x - extracting iex/stamp.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/stamp.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/stamp
- Xexit 0
- X#endif
- X
- X/* print four-digit stamp of current month/day */
- X#include "ilib.h"
- X
- Xmain ()
- X{
- XQ printf ("%.2d%.2d\n", itomonth(), itoday());
- X exit (0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/stamp.c &&
- chmod 0755 iex/stamp.c ||
- echo "restore of iex/stamp.c failed"
- set `wc -c iex/stamp.c`;Wc_c=$1
- if test "$Wc_c" != "226"; then
- echo original size 226, current size $Wc_c
- fi
- # ============= iex/trunc.c ==============
- echo "x - extracting iex/trunc.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/trunc.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/trunc
- X(/bin/rm -f $BIN/leftrunc)
- Xln $BIN/trunc $BIN/leftrunc
- Xexit 0
- X#endif
- X
- X/* output lines from given column, or until given column */
- X
- X#include "ilib.h"
- X#define LEFTRUNC 611
- X#define MAXC (IONEK-1)
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X int N;
- X int left = 0;
- X char c[MAXC+1];
- X char *end;
- X
- X if (argc < 2 || (N = atoi (argv[1])) < 0 || N > MAXC)
- X fprintf (stderr, "Usage: trunc|leftrunc N [file]\n%s%s%s%d%s",
- X "\ttruncate lines of input beyond column N (to stdout) or\n",
- X "\tsuppress left N columns of each line of input;\n",
- X "\tmaximum line length: ", MAXC, " bytes.\n\n"), exit (0);
- X
- X /* rename file to stdin */
- X if (argc > 2) {
- X if (freopen (argv[2], "r", stdin) == NULL) {
- X ierror ("at setting up filter in main");
- X fprintf (stderr, "%s\n", ierbuf), exit (1);
- X }
- X }
- XQ if (ihasharg (argv[0], '/') == LEFTRUNC)
- X left = 1;
- X
- X /* use fgets to prevent buffer overrun */
- X while (fgets (c, MAXC, stdin) != NULL) {
- X end = inull (c);
- X *--end = '\0'; /* kill newline */
- X if (left)
- X (end-c > N) ? (puts(c+N)) : (puts(""));
- X else
- X (end-c > N) ? (c[N] = '\0', puts(c)) : (puts(c));
- X }
- X exit(0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/trunc.c &&
- chmod 0755 iex/trunc.c ||
- echo "restore of iex/trunc.c failed"
- set `wc -c iex/trunc.c`;Wc_c=$1
- if test "$Wc_c" != "1187"; then
- echo original size 1187, current size $Wc_c
- fi
- # ============= iex/unlink.c ==============
- echo "x - extracting iex/unlink.c (Text)"
- sed 's/^X//' << 'SHAR_EOF' > iex/unlink.c &&
- X#ifdef NEVER
- X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/unlink
- Xexit 0
- X#endif
- X
- X/* brute force remove */
- X#include "ilib.h"
- X
- Xmain (argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X int mode;
- X
- X while (--argc)
- XQ if ((mode = imode (argv[argc], ISDIR)) < 0)
- X fprintf (stderr, "File not found: %s\n", argv[argc]);
- XQ else if (mode == 1)
- X fprintf (stderr, "Skipping directory: %s\n", argv[argc]);
- X else if (unlink (argv[argc]))
- X fprintf (stderr, "No permission to remove: %s\n", argv[argc]);
- X
- X exit (0);
- X}
- SHAR_EOF
- $TOUCH -am 0605075390 iex/unlink.c &&
- chmod 0755 iex/unlink.c ||
- echo "restore of iex/unlink.c failed"
- set `wc -c iex/unlink.c`;Wc_c=$1
- if test "$Wc_c" != "512"; then
- echo original size 512, current size $Wc_c
- fi
- echo "End of part 4, continue with part 5"
- exit 0
- --
- Istvan Mohos
- ...uunet!pyrdc!pyrnj!hhb!istvan
- RACAL-REDAC/HHB 1000 Wyckoff Ave. Mahwah NJ 07430 201-848-8000
- ======================================================================
-