home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1434 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  52.4 KB

  1. From: istvan@hhb.UUCP (Istvan Mohos)
  2. Newsgroups: alt.sources
  3. Subject: Subject: ILIB Unix Toolkit in C
  4. Message-ID: <550@hhb.UUCP>
  5. Date: 8 Jun 90 20:54:01 GMT
  6.  
  7.  
  8. ---- Cut Here and unpack ----
  9. #!/bin/sh
  10. # This is part 04 of a multipart archive
  11. if touch 2>&1 | fgrep '[-amc]' > /dev/null
  12.  then TOUCH=touch
  13.  else TOUCH=true
  14. fi
  15. # ============= iex/group.c ==============
  16. echo "x - extracting iex/group.c (Text)"
  17. sed 's/^X//' << 'SHAR_EOF' > iex/group.c &&
  18. X#ifdef NEVER
  19. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/group
  20. Xexit 0
  21. X#endif
  22. X
  23. X/* write alphanumerically sorted and grouped lines to stdout */
  24. X#include "ilib.h"
  25. X
  26. Xmain (argc, argv)
  27. Xint argc;
  28. Xchar *argv[];
  29. X{
  30. X    char *buffer;       /* for storing entire input */
  31. X    char *list;         /* pointer list to lines of input */
  32. X    char **listptr;     /* pointer to pointers of 'list' */
  33. X    int bytes;          /* byte count of input */
  34. X    int lines;          /* count of pointers to lines of input */
  35. X    int modval;         /* maximum count of items in a group */
  36. X
  37. X    if (argc < 2 || (modval = atoi (argv[1])) == 0)
  38. X        fprintf(stderr,
  39. X        "Usage: %s number|negative_number [file]\n\n%s%s%s%s", argv[0],
  40. X        "Alphanumerically sort lines of file (or stdin) to stdout\n",
  41. X        "into related groups of <number> items maximally.\n",
  42. X        "<negative_number> specifies reverse sort.\n",
  43. X        "1 or -1 specifies no spacing between groups.\n\n"),
  44. X        exit (0);
  45. X
  46. X    /* procure input */
  47. XQ   if ((bytes = ifilter (argv[2], &buffer)) < 0)
  48. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  49. X    /* create list of pointers to lines of input */
  50. XQ   if ((lines = illistn (buffer, buffer + bytes, &list)) < 0)
  51. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  52. X    /* sort pointers to dereference strings in alphanumeric order */
  53. XQ   inumsort ((char **)list, lines);
  54. X
  55. X    /* output sorted list, backward or forward */
  56. X    if (modval < 0) {
  57. X        modval = abs (modval);
  58. X        listptr = (char **)list;
  59. X        for (listptr += lines-1; --lines; listptr--)
  60. X            output (*listptr, *(listptr-1), modval);
  61. X    }
  62. X    else
  63. X        for (listptr = (char **)list; --lines; listptr++)
  64. X            output (*listptr, *(listptr+1), modval);
  65. X    output (*listptr, (char *)NULL, modval); /* last item on list */
  66. X
  67. X    exit(0);
  68. X}
  69. X
  70. Xoutput (ptr1, ptr2, modval)
  71. Xchar *ptr1, *ptr2;
  72. Xint modval;
  73. X{
  74. X    if (modval == 1)
  75. X        puts (ptr1);
  76. X    else {
  77. XQ       switch (igroup (ptr1, ptr2, modval)) {
  78. X            case SPACE_LINE:
  79. X                printf ("\n%s\n", ptr1);
  80. X                break;
  81. X            case LINE_ONLY:
  82. X            default:
  83. X                puts (ptr1);
  84. X                break;
  85. X            case LINE_SPACE:
  86. X                printf ("%s\n\n", ptr1);
  87. X                break;
  88. X        }
  89. X    }
  90. X}
  91. SHAR_EOF
  92. $TOUCH -am 0605075390 iex/group.c &&
  93. chmod 0755 iex/group.c ||
  94. echo "restore of iex/group.c failed"
  95. set `wc -c iex/group.c`;Wc_c=$1
  96. if test "$Wc_c" != "2031"; then
  97.     echo original size 2031, current size $Wc_c
  98. fi
  99. # ============= iex/inum.c ==============
  100. echo "x - extracting iex/inum.c (Text)"
  101. sed 's/^X//' << 'SHAR_EOF' > iex/inum.c &&
  102. X#ifdef NEVER
  103. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/inum
  104. Xexit 0
  105. X#endif
  106. X
  107. X/* interactively specify custom line numbering sequence */
  108. X#include "ilib.h"
  109. X#define NUL    0    /* the ASCII 0 byte */
  110. X
  111. Xmain (argc,argv)
  112. Xint argc;
  113. Xchar *argv[];
  114. X{
  115. X    FILE *fp;
  116. X    static char prepend[IHALFK];
  117. X    static char append[IHALFK];
  118. X    static char tail[IHALFK];
  119. X    char rsvp[IHALFK];
  120. X    char line[IHALFK];
  121. X    char build[IHALFK*4];
  122. X    char format[32];
  123. X    int  linecount = 100;
  124. X    int  thisnum = 1;
  125. X    int  increment = 1;
  126. X    int  justify = 1;
  127. X    int  existfile = 1;
  128. X    int  aftertext = 0;
  129. X    int  zerofill = 0;
  130. X    char base;
  131. X
  132. X    if (NULCHARP (argv[1])) /* no input file */
  133. X        existfile = 0;
  134. X    else if ((fp = fopen(argv[1], "r")) == NULL)
  135. X        fprintf (stderr, "Can't read %s\n", argv[1]), exit (1);
  136. X
  137. X    if (!existfile) {
  138. XQ       iinput (rsvp, "how many lines?______________");
  139. X        if (*rsvp != NUL)
  140. X            linecount = abs (atoi (rsvp));
  141. X    }
  142. X    else {
  143. XQ       iinput (rsvp, "before or after text (b/a) ?_");
  144. X        if (*rsvp == 'a' || *rsvp == 'A')
  145. X            aftertext = 1;
  146. X    }
  147. XQ   iinput (prepend,  "prepend this string to count_");
  148. XQ   iinput (rsvp,     "begin count at_______________");
  149. X    if (*rsvp != NUL)
  150. X        thisnum = atoi (rsvp);
  151. XQ   iinput (rsvp,     "increment count by___________");
  152. X    if (*rsvp != NUL)
  153. X        increment = atoi (rsvp);
  154. XQ   iinput (rsvp,     "justify width (-N for left)__");
  155. X    justify = atoi (rsvp);
  156. X    if (justify > 1) {
  157. XQ       iinput (rsvp, "zero fill (y/n)______________");
  158. X        if (*rsvp == 'y' || *rsvp == 'Y')
  159. X            zerofill = 1;
  160. X    }
  161. XQ   iinput (rsvp,     "decimal:d hex:x/X octal:o ?__");
  162. X    base = *rsvp;
  163. X    if (base != 'x' && base != 'X' && base != 'o')
  164. X        base = 'd';
  165. XQ   iinput (append,   "append this string to count__");
  166. X    if (existfile)
  167. XQ       iinput (tail, "tail string past line________");
  168. X
  169. X    if (justify > 1 && zerofill)
  170. X        sprintf(format, "%cs%c.%d%c%cs", '%', '%', justify, base, '%');
  171. X    else
  172. X        sprintf(format, "%cs%c%d%c%cs", '%', '%', justify, base, '%');
  173. X
  174. X    if (existfile) {
  175. X        while (fgets (line, IHALFK, fp) != NULL) {
  176. X            line[strlen (line) -1] = NUL; /* kill newline */
  177. X            sprintf (build, format, prepend, thisnum, append);
  178. X            if (aftertext)
  179. X               printf ("%s%s%s\n", line, build, tail);
  180. X            else
  181. X               printf ("%s%s%s\n", build, line, tail);
  182. X            thisnum += increment;
  183. X        }
  184. X    }
  185. X    else
  186. X        for (strcat (format, "\n"); linecount--; thisnum += increment)
  187. X            printf(format, prepend, thisnum, append);
  188. X
  189. X    exit (0);
  190. X}
  191. SHAR_EOF
  192. $TOUCH -am 0605075390 iex/inum.c &&
  193. chmod 0755 iex/inum.c ||
  194. echo "restore of iex/inum.c failed"
  195. set `wc -c iex/inum.c`;Wc_c=$1
  196. if test "$Wc_c" != "2352"; then
  197.     echo original size 2352, current size $Wc_c
  198. fi
  199. # ============= iex/isdef.c ==============
  200. echo "x - extracting iex/isdef.c (Text)"
  201. sed 's/^X//' << 'SHAR_EOF' > iex/isdef.c &&
  202. X#ifdef NEVER
  203. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/isdef
  204. Xexit 0
  205. X#endif
  206. X
  207. X/* find token in list of definitions recognized by C compiler */
  208. X#include "ilib.h"
  209. X
  210. Xmain (argc, argv)
  211. Xint argc;
  212. Xchar *argv[];
  213. X{
  214. X    char fbuf[ITWOK];     /* buffer for writing C code */
  215. X    char *ptr = fbuf;     /* working pointer into fbuf */
  216. X    char cmd[IONEK];      /* buffer for "system" command line */
  217. X    char tmpfile[32];     /* full name of temporary file of C code */
  218. X    char template[14];    /* storage for mktemp template */
  219. X    char **token = argv;  /* pointer to cmd line parameters */
  220. X    char *mktemp();
  221. X
  222. X    if (argc < 2)
  223. X        printf ("Usage: %s token ...\n%s\n\n", *token,
  224. X        "find token in list of definitions recognized by C compiler"),
  225. X            exit(0);
  226. X
  227. X    /* install C code in buffer for testing presence of definitions */
  228. X    strcpy (ptr, "main (){\n");
  229. XQ   icue (&ptr);
  230. X
  231. X    for (++token; !NULCHARP (*token); token++) {
  232. X        sprintf (ptr,
  233. X            "#ifdef %s\nprintf(\"\%-16s is defined\\n\");\n#else\n",
  234. X            *token, *token);
  235. XQ       icue (&ptr);
  236. X        sprintf (ptr, "printf(\"\%-16s not defined\\n\");\n#endif\n",
  237. X            *token);
  238. XQ       icue (&ptr);
  239. X    }
  240. X
  241. X    strcpy (ptr, "exit(0);}\n");
  242. XQ   icue (&ptr);
  243. X
  244. X    /* make temporary file name, write buffer to temporary file */
  245. X    strcpy (template, "isdefXXXXXX");
  246. X    sprintf (tmpfile, "/tmp/%s.c", mktemp (template));
  247. X    if (iwrite (tmpfile, fbuf, ptr) != ptr - fbuf)
  248. X        fprintf (stderr, "%s temporary file:\n%s\n",
  249. X            ierbuf, tmpfile), exit (1);
  250. X
  251. X    /* compile, execute, clean up */
  252. X    sprintf (cmd, "/bin/cc %s -o /tmp/%s; /tmp/%s",
  253. X        tmpfile, template, template);
  254. X    if (system (cmd))
  255. X        fprintf (stderr, "'system' command failed\n"), exit (2);
  256. X    unlink (tmpfile);
  257. X    *(tmpfile + strlen (tmpfile) - 2) = '\0';
  258. X    unlink (tmpfile);
  259. X    exit (0);
  260. X}
  261. SHAR_EOF
  262. $TOUCH -am 0605084790 iex/isdef.c &&
  263. chmod 0755 iex/isdef.c ||
  264. echo "restore of iex/isdef.c failed"
  265. set `wc -c iex/isdef.c`;Wc_c=$1
  266. if test "$Wc_c" != "1721"; then
  267.     echo original size 1721, current size $Wc_c
  268. fi
  269. # ============= iex/lcat.c ==============
  270. echo "x - extracting iex/lcat.c (Text)"
  271. sed 's/^X//' << 'SHAR_EOF' > iex/lcat.c &&
  272. X#ifdef NEVER
  273. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/lcat
  274. Xexit 0
  275. X#endif
  276. X
  277. X/* cat line N or lines N through N+M of input to stdout */
  278. X#include "ilib.h"
  279. X#define TOEND -1
  280. X
  281. Xmain (argc,argv)
  282. Xint argc;
  283. Xchar *argv[];
  284. X{
  285. X    char **argp = argv;
  286. X    char *buf;             /* to file buffer malloc'd by iread */
  287. XQ   char *lpbuf;           /* to line pointers malloc'd by illistn */
  288. X    char *end;             /* point to one byte past buffer */
  289. XQ   char **ptr;            /* working pointer in lpbuf */
  290. X    int filesize;          /* size of malloc'd text buffer */
  291. X    int lcount;            /* number of lines in file */
  292. X    int n;                 /* cat this line */
  293. X    int m = 0;             /* number of extra lines to print */
  294. X    int cnt;               /* working counter */
  295. X
  296. X    if (argc < 2)
  297. X        fprintf (stderr, "Usage: %s N [+|+M] [file]\n\%s%s%s", *argp,
  298. X        "\tcat Nth line of input/file (to stdout) or\n",
  299. X        "\tcat input/file from line N to end or\n",
  300. X        "\tcat input/file from line N through line N+M\n\n"), exit(0);
  301. X
  302. X    if ((n = atoi (*++argp)) < 1)
  303. X        n = abs (n);
  304. X    if (!NULCHARP(*++argp) && *(*argp) == '+' && (m=atoi(*argp++)) < 1)
  305. X        m = TOEND;
  306. X
  307. X    if ((filesize = ifilter (*argp, &buf)) < 1)
  308. X        fprintf (stderr, "%s %s\n", ierbuf, *argp), exit (1);
  309. X    end = buf + filesize;
  310. X
  311. X    /* set up pointers to lines of file */
  312. XQ   if ((lcount = illistn (buf, end, &lpbuf)) < 1)
  313. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  314. X    if (lcount < n) /* not enough lines */
  315. X        exit (0);
  316. XQ   ptr = (char **)lpbuf + n-1;
  317. X
  318. X    if (n+m > lcount)
  319. X        m = TOEND;
  320. X    switch (m) {
  321. X        case 0:
  322. X            puts (*ptr);
  323. X            break;
  324. X        case TOEND:
  325. X            for (DOUNCOUN ((lcount-n+1), cnt); ptr++)
  326. X                puts (*ptr);
  327. X            break;
  328. X        default:
  329. X            for (DOUNCOUN ((m+1), cnt); ptr++)
  330. X                puts (*ptr);
  331. X            break;
  332. X    }
  333. X    exit (0);
  334. X}
  335. SHAR_EOF
  336. $TOUCH -am 0605075390 iex/lcat.c &&
  337. chmod 0755 iex/lcat.c ||
  338. echo "restore of iex/lcat.c failed"
  339. set `wc -c iex/lcat.c`;Wc_c=$1
  340. if test "$Wc_c" != "1737"; then
  341.     echo original size 1737, current size $Wc_c
  342. fi
  343. # ============= iex/lhash.c ==============
  344. echo "x - extracting iex/lhash.c (Text)"
  345. sed 's/^X//' << 'SHAR_EOF' > iex/lhash.c &&
  346. X#ifdef NEVER
  347. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/lhash
  348. X(/bin/rm -f $BIN/uhash)
  349. Xln $BIN/lhash $BIN/uhash
  350. Xexit 0
  351. X#endif
  352. X
  353. X/* output formatted sum of bytes of each line of input */
  354. X#include "ilib.h"
  355. X
  356. X#define  UHASH   377
  357. X#define  PLAIN  'p'
  358. X#define  LEAD   'l'
  359. X#define  TRAIL  't'
  360. X#define  MAXC    (IONEK-1)
  361. X
  362. Xmain (argc, argv)
  363. Xint argc;
  364. Xchar *argv[];
  365. X{
  366. X    char lbuf[MAXC+1];      /* buffer for input string */
  367. X    char form[MAXC+1];      /* buffer for format string */
  368. X    char **opt = &argv[1];  /* command line options */
  369. X    int  formtype;          /* PLAIN, LEAD or TRAIL */
  370. X    int  optchar;           /* option flag */
  371. X    int  modval = 0;        /* output full sum if zero */
  372. X    int  upcase = 0;        /* upcase each byte if TRUE */
  373. X
  374. X    *form = 0;
  375. X    while ((optchar = iopt (&opt)))
  376. X        switch (optchar) {
  377. X            case 'm':
  378. X                if ((modval = atoi (*opt)) < 1)
  379. X                    usage (), exit (1);
  380. X                break;
  381. X            case 'p':
  382. X            case 'l':
  383. X            case 't':
  384. X                strcpy (form, *opt);
  385. X                strcat (form, "\n");
  386. X                formtype = optchar;
  387. X                break;
  388. X            default:
  389. X                usage (), exit (1);
  390. X        }
  391. X
  392. X    if (!NULCHARP (*opt)) /* further, non-option arguments */
  393. X        usage (), exit (1);
  394. XQ   if (ihasharg (argv[0], '/') == UHASH)
  395. X        upcase = 1;
  396. X
  397. X    if (!*form)
  398. X        while (gets (lbuf) != NULL)
  399. XQ           printf ("%d\n", ihash (lbuf, modval, upcase));
  400. X    else
  401. X        while (gets (lbuf) != NULL)
  402. X            switch (formtype) {
  403. X                case PLAIN:
  404. XQ                   printf (form, ihash (lbuf, modval, upcase));
  405. X                    break;
  406. X                case LEAD:
  407. XQ                   printf (form, ihash (lbuf, modval, upcase), lbuf);
  408. X                    break;
  409. X                case TRAIL:
  410. XQ                   printf (form, lbuf, ihash (lbuf, modval, upcase));
  411. X                    break;
  412. X            }
  413. X    exit(0);
  414. X}
  415. X
  416. Xusage ()
  417. X{
  418. X    fprintf(stderr,
  419. X"Usage: lhash|uhash [-m modval] [-p|l|t printf_string]\n%s%s%s%s%s%s\n",
  420. X"\tsum byte values or upper case byte values of each line of input,\n",
  421. X"\toutput sum or \"sum modulo modval\" if -m is specified,\n",
  422. X"\tprint using the \"printf\" format string if -p|l|t is specified:\n",
  423. X"\t-p (plain) should contain int format specifier for hash value,\n",
  424. X"\t-l (lead) int and str formats, for hash value and input string,\n",
  425. X"\t-t (trail) str and int formats, for input string and hash value.\n");
  426. X}
  427. SHAR_EOF
  428. $TOUCH -am 0605075390 iex/lhash.c &&
  429. chmod 0755 iex/lhash.c ||
  430. echo "restore of iex/lhash.c failed"
  431. set `wc -c iex/lhash.c`;Wc_c=$1
  432. if test "$Wc_c" != "2181"; then
  433.     echo original size 2181, current size $Wc_c
  434. fi
  435. # ============= iex/mung.c ==============
  436. echo "x - extracting iex/mung.c (Text)"
  437. sed 's/^X//' << 'SHAR_EOF' > iex/mung.c &&
  438. X#ifdef NEVER
  439. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/mung
  440. Xexit 0
  441. X#endif
  442. X
  443. X/* transform every string1 within files into string2 */
  444. X#include "ilib.h"
  445. X
  446. Xmain (argc, argv)
  447. Xint argc;
  448. Xchar *argv[];
  449. X{
  450. X    char **file;
  451. X    int  len1, len2;
  452. X    int  item, changed;
  453. X
  454. X    if (argc < 4)
  455. X        fprintf(stderr,
  456. X"Usage: %s string1 string2 files\n\n%s%s\n", argv[0],
  457. X"\tTransform every <string1> in each file into <string2>; or if\n",
  458. X"\tstring1 is blank, report the <string2> count of each file.\n"),
  459. X        exit(1);
  460. X
  461. X    /* convert control sequences into control characters */
  462. X    len1 = ifonetic (argv[1]);
  463. X    len2 = ifonetic (argv[2]);
  464. X
  465. X    /* print count of string2, files are not changed */
  466. X    if (len1 == 0) {
  467. X        if (len2) {
  468. X            for (file = &argv[3]; !NULCHARP (*file); file++) {
  469. X                /* malloc or read/directory permission errors don't
  470. X                   damage the file, scanning can continue */
  471. X                if ((item = countstring (argv[2], len2, *file)) == -1)
  472. XQ                   fprintf (stderr, "%s %s\n", ierbuf, *file);
  473. X                else if (item)
  474. X                    printf ("%s: %d\n", *file, item);
  475. X            }
  476. X        }
  477. X        else
  478. X            fprintf(stderr, "string1 or string2 must be non-null\n");
  479. X        exit (0);
  480. X    }
  481. X
  482. X    for (file = &argv[3]; !NULCHARP (*file); file++, item = 0) {
  483. X        /* if string2 longer than string1, count occurrences of string1
  484. X           in file, to predict buffer size for the transformed file */
  485. X        if (len2 > len1) {
  486. X            if ((item = countstring (argv[1], len1, *file)) == -1) {
  487. XQ               fprintf (stderr, "%s %s\n", ierbuf, *file);
  488. X                continue;
  489. X            }
  490. X            if (!item)
  491. X                continue;
  492. X        }
  493. X        changed = mung (argv[1], len1, argv[2], len2, *file, item);
  494. X
  495. X        if (changed < 0) {
  496. XQ           if (idamage (changed))
  497. XQ               fprintf (stderr, "%s %s\n%s", ierbuf, *file,
  498. X                "file damage probable ... aborting\n\n"), exit (1);
  499. XQ           fprintf (stderr, "%s %s\n", ierbuf, *file);
  500. X        }
  501. X        else if (changed)
  502. X            printf ("%s - %d\n", *file, changed);
  503. X    }
  504. X
  505. X    exit(0);
  506. X}
  507. X
  508. Xint
  509. Xcountstring (string, len, file)
  510. Xchar *string, *file;
  511. Xint len;
  512. X{
  513. X    char *buffer;
  514. X    char *fromptr, *endptr;
  515. X    int  item;
  516. X
  517. X    if ((item = iread(file, &buffer)) < 0)
  518. X        return (-1);
  519. X    endptr = buffer + item;
  520. X
  521. X    /* read buffer and count matches */
  522. X    if (len == 1) {
  523. X        for (fromptr = buffer, item = 0; fromptr < endptr; fromptr++)
  524. X            if (*fromptr == *string)
  525. X                ++item;
  526. X    }
  527. X    else {
  528. X        --len;
  529. X        for (fromptr = buffer, item = 0; fromptr < endptr; fromptr++)
  530. X            if (*fromptr == *string)
  531. X                if (ibcmp (fromptr+1, string+1, len) == 0)
  532. X                    ++item, fromptr += len;
  533. X    }
  534. X    free(buffer);
  535. X    return (item);
  536. X}
  537. X
  538. Xint
  539. Xmung (string1, len1, string2, len2, file, item)
  540. Xchar *string1, *string2, *file;
  541. Xint len1, len2, item;
  542. X{
  543. X    int insiz, totsiz, newsiz, extra;
  544. X    char *source, *target;
  545. X    char *buffer, *tmp;
  546. X    char *s1end = string1 + len1 - 1;
  547. X    char *backlimit;
  548. X    char *realloc();
  549. X
  550. X    /* read file into buffer -- no file damage on error */
  551. X    if ((insiz = iread(file, &buffer)) < 0)
  552. XQ       return (ierflag);
  553. X
  554. X    /* if string2 longer than string1 and item nonzero, calculate
  555. X       and allocate extra space needed -- no file damage on error */
  556. X    if (len2 > len1) {
  557. X        extra = (len2 - len1) * item;
  558. X        totsiz = insiz + extra;
  559. X        tmp = buffer;
  560. X        if ((buffer = realloc (tmp, (unsigned)totsiz)) == NULL) {
  561. X            free (tmp);
  562. XQ           return (ierror ("while requesting expansion space"));
  563. X        }
  564. X    }
  565. X    else
  566. X        totsiz = insiz;
  567. X
  568. X    source = buffer + insiz; /* one byte past end of text */
  569. X    target = buffer + totsiz;
  570. X
  571. X    /* do the substitution */
  572. X    if (len1 == 1) {
  573. X        for (item = 0; --source >= buffer;)
  574. X            if (*source == *s1end) {
  575. X                target -= len2;
  576. X                ibcopy (target, string2, len2);
  577. X                ++item;
  578. X            }
  579. X            else
  580. X                *--target = *source;
  581. X    }
  582. X    else {
  583. X        /* leftmost byte in buffer that may be overlaid with the
  584. X           rightmost byte of string1 */
  585. X        --len1;
  586. X        backlimit = buffer + len1;
  587. X        for (item = 0; --source >= backlimit;)
  588. X            if (*source == *s1end) {
  589. X                if (ibcmp (source-len1, string1, len1) == 0) {
  590. X                    target -= len2;
  591. X                    ibcopy (target, string2, len2);
  592. X                    source -= len1;
  593. X                    ++item;
  594. X                }
  595. X                else
  596. X                    *--target = *source;
  597. X            }
  598. X            else
  599. X                *--target = *source;
  600. X
  601. X        /* copy remaining string on left */
  602. X        while (source >= buffer)
  603. X            *--target = *source--;
  604. X    }
  605. X
  606. X    /* write buffer back only if changed -- error may destroy file */
  607. X    if (item) {
  608. X        newsiz = buffer + totsiz - target;
  609. X        if (iwrite (file, target, buffer + totsiz) != newsiz) {
  610. X            free (buffer);
  611. XQ           return (ierflag);
  612. X        }
  613. X    }
  614. X
  615. X    free(buffer);
  616. X    return (item);
  617. X}
  618. SHAR_EOF
  619. $TOUCH -am 0605075390 iex/mung.c &&
  620. chmod 0755 iex/mung.c ||
  621. echo "restore of iex/mung.c failed"
  622. set `wc -c iex/mung.c`;Wc_c=$1
  623. if test "$Wc_c" != "4344"; then
  624.     echo original size 4344, current size $Wc_c
  625. fi
  626. # ============= iex/ncat.c ==============
  627. echo "x - extracting iex/ncat.c (Text)"
  628. sed 's/^X//' << 'SHAR_EOF' > iex/ncat.c &&
  629. X#ifdef NEVER
  630. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/ncat
  631. Xexit 0
  632. X#endif
  633. X
  634. X/* line number files separately, add "spooler" string before each file */
  635. X#include "ilib.h"
  636. X#define SPACE 32
  637. Xchar sep[IONEK];        /* buffer for expanded separator string */
  638. X
  639. Xmain (argc, argv)
  640. Xint argc;
  641. Xchar *argv[];
  642. X{
  643. X    char *separator;    /* pointer to command line's separator */
  644. X    char **argp = argv+1;
  645. X    char *buf;           /* buffer to hold a file at a time */
  646. X    char *end;           /* next byte after buffer */
  647. X    char *lp;            /* string pointer into buf */
  648. X    int  optchar;        /* shown as 's' --- actual value irrelevant */
  649. X    int  fsiz;           /* byte count of file */
  650. X    int  lsiz;           /* byte count of line */
  651. X    int  lc;             /* line counter */
  652. X    int  seplen;         /* length of separator */
  653. X    int  convlen;        /* length of expanded separator */
  654. X
  655. X    if (argc < 2)
  656. X        fprintf(stderr,
  657. X"Usage: %s [-s separator] files\n%s%s%s", argv[0],
  658. X"\tprint individually line-numbered <files> to stdout, prepend each\n",
  659. X"\twith an optional <separator> line.  Each '$' byte of <separator>\n",
  660. X"\tis a wild card, a place holder for the current file name.\n\n"),
  661. Xexit(0);
  662. X
  663. X    if (optchar = iopt (&argp)) {
  664. X        separator = *argp++;
  665. X        seplen = ifonetic (separator);
  666. X    }
  667. X
  668. X    /* process each file remaining on the command line, in a loop */
  669. X    for (; !NULCHARP (*argp); argp++) {
  670. X        if ((fsiz = iread (*argp, &buf)) < 0)
  671. X            fprintf (stderr, "%s: %s\n",ierbuf, *argp), exit (1);
  672. X        end = buf + fsiz;
  673. X        if (optchar) {
  674. X            convlen = convdollar (separator, seplen, *argp);
  675. X            printf ("%*.*s", convlen, convlen, sep);
  676. X        }
  677. XQ       for (lp = buf, lc = 0; (lsiz = iline (lp, end)) > 0; lp += lsiz)
  678. X            printf ("%6d%c%c%s\n", ++lc, SPACE, SPACE, lp);
  679. X        free (buf);
  680. X    }
  681. X    exit(0);
  682. X}
  683. X
  684. Xint
  685. Xconvdollar (string, len, fname)
  686. Xchar *string, *fname;
  687. Xint len;
  688. X{
  689. X    char *block = string + len;
  690. X    char *bp = sep;
  691. X
  692. X    for (; string < block; string++) {
  693. X        if (*string == '$') {
  694. X            strcpy (bp, fname);
  695. X            bp = inull (bp);
  696. X        }
  697. X        else
  698. X            *bp++ = *string;
  699. X    }
  700. X    *bp = '\0'; /* null terminate */
  701. X    return (bp - sep);
  702. X}
  703. SHAR_EOF
  704. $TOUCH -am 0605075390 iex/ncat.c &&
  705. chmod 0755 iex/ncat.c ||
  706. echo "restore of iex/ncat.c failed"
  707. set `wc -c iex/ncat.c`;Wc_c=$1
  708. if test "$Wc_c" != "2061"; then
  709.     echo original size 2061, current size $Wc_c
  710. fi
  711. # ============= iex/nest.c ==============
  712. echo "x - extracting iex/nest.c (Text)"
  713. sed 's/^X//' << 'SHAR_EOF' > iex/nest.c &&
  714. X#ifdef NEVER
  715. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/nest
  716. X(/bin/rm -f $BIN/nestinfo)
  717. X/bin/ln $BIN/nest $BIN/nestinfo
  718. Xexit 0
  719. X#endif
  720. X
  721. X/* print nesting information for delimiter pair */
  722. X#include "ilib.h"
  723. X#define  NESTINFO 614
  724. X
  725. Xmain (argc, argv)
  726. Xint argc;
  727. Xchar *argv[];
  728. X{
  729. X    char *buf;          /* for storing entire input */
  730. X    char *end;          /* pointer to byte past input */
  731. X    char *origlines;    /* pointer list returned by illistn */
  732. X    char **op;          /* to pointers of list */
  733. X    int  *infopairs;    /* pointer to list mallocd by inest */
  734. X    int  *ip;           /* pointer to integers of list */
  735. X    int  olcnt;         /* original line count */
  736. X    int  ret;           /* returned by inest */
  737. X    int  ii = 0;        /* scratch counter, mismatch flag */
  738. X    int  ij = 0;        /* one-shot counter of original lines */
  739. X    int  lcnt = 0;      /* count left delimiters */
  740. X    int  rcnt = 0;      /* count right delimiters */
  741. X    int  bytes;         /* byte count of input */
  742. X    char *adr;          /* make address from offset */
  743. X
  744. X    if (argc < 3)
  745. X        fprintf(stderr,
  746. X        "Usage:  %s ldelim rdelim [file]\n%s", argv[0],
  747. X        "\tPrint nesting information for delimiter pair.\n\n"), exit(1);
  748. X
  749. X    ifonetic (argv[1]);
  750. X    ifonetic (argv[2]);
  751. X
  752. X    /* procure input */
  753. X    if ((bytes = ifilter (argv[3], &buf)) < 0)
  754. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  755. X    end = buf + bytes;
  756. X
  757. XQ   /* mark original lines before stripping out comments */
  758. X    if ((olcnt = illistn (buf, end, &origlines)) < 0)
  759. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  760. X
  761. X    /* restore newlines nulled out by illistn */
  762. X    itran (buf, end, 0, '\n');
  763. X
  764. XQ   istripcom (buf, end, "/*", "*/");
  765. X    istripdq (buf, end);
  766. X    istripsq (buf, end);
  767. X
  768. XQ   if ((ret = inest (buf, end, argv[1], argv[2], &infopairs)) < 0)
  769. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  770. X    if (!ret)
  771. X        exit (0);
  772. X
  773. X    /* if command name was nestinfo, output single line summary */
  774. XQ   if (ihasharg (argv[0], '/') == NESTINFO) {
  775. X        for (ip = infopairs; *ip; ip += 2)
  776. X            if (*(ip+1) > 0)
  777. X                lcnt++;
  778. X            else if (*(ip+1) < 0)
  779. X                rcnt++;
  780. X            else /* zero value shows negative nestlev, mismatch */
  781. X                rcnt++, ii = 1;
  782. X        printf ("%d %s %d %s %s\n", lcnt, argv[1], rcnt, argv[2],
  783. X            ii ? "leading right" :
  784. X            ((lcnt == rcnt) ? "matched" : "mismatched")), exit (0);
  785. X    }
  786. X
  787. X    /* else: output line-numbered, tab-formatted nesting list;
  788. X       since the next delimiter cannot be on an earlier line,
  789. X       contrast the address of the next delimiter only from the
  790. X       address of the line that most recently failed to be lower
  791. X       than the address of the delimiter.
  792. X    */
  793. X    op = (char **)origlines;
  794. X    for (ip = infopairs; *ip; ip += 2) {
  795. X        for (adr = buf + *ip; ij < olcnt; ij++, op++)
  796. X            if (*op > adr)
  797. X                break;
  798. X        printf ("%d", ij);
  799. X        for (ii = abs (*(ip+1)); --ii >= 0; putchar ('\t'));
  800. X        puts (*(ip+1)>0 ? argv[1] : argv[2]);
  801. X    }
  802. X    exit(0);
  803. X}
  804. SHAR_EOF
  805. $TOUCH -am 0605075390 iex/nest.c &&
  806. chmod 0755 iex/nest.c ||
  807. echo "restore of iex/nest.c failed"
  808. set `wc -c iex/nest.c`;Wc_c=$1
  809. if test "$Wc_c" != "2799"; then
  810.     echo original size 2799, current size $Wc_c
  811. fi
  812. # ============= iex/nulcat.c ==============
  813. echo "x - extracting iex/nulcat.c (Text)"
  814. sed 's/^X//' << 'SHAR_EOF' > iex/nulcat.c &&
  815. X#ifdef NEVER
  816. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/nulcat
  817. Xexit 0
  818. X#endif
  819. X
  820. X/* write input to stdout, changing NUL bytes to '@' */
  821. X#include "ilib.h"
  822. X
  823. Xmain (argc, argv)
  824. Xint argc;
  825. Xchar *argv[];
  826. X{
  827. X    char buffer[BUFSIZ];
  828. X    int bytes;
  829. X
  830. X    /* rename file to stdin */
  831. X    if (argc > 1) {
  832. X        if (freopen (argv[1], "r", stdin) == NULL) {
  833. X            ierror ("at setting up filter in main");
  834. X            fprintf (stderr, "%s\n", ierbuf), exit (1);
  835. X        }
  836. X    }
  837. X    while ((bytes = fread (buffer, 1, BUFSIZ, stdin)) > 0)
  838. XQ       idump (buffer, buffer+bytes, '@');
  839. X
  840. X    exit(0);
  841. X}
  842. SHAR_EOF
  843. $TOUCH -am 0605075390 iex/nulcat.c &&
  844. chmod 0755 iex/nulcat.c ||
  845. echo "restore of iex/nulcat.c failed"
  846. set `wc -c iex/nulcat.c`;Wc_c=$1
  847. if test "$Wc_c" != "541"; then
  848.     echo original size 541, current size $Wc_c
  849. fi
  850. # ============= iex/objed.c ==============
  851. echo "x - extracting iex/objed.c (Text)"
  852. sed 's/^X//' << 'SHAR_EOF' > iex/objed.c &&
  853. X#ifdef NEVER
  854. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/objed
  855. Xexit 0
  856. X#endif
  857. X
  858. X/* binary file editor */
  859. X#include <sys/types.h>
  860. X#include <sys/stat.h>
  861. X#include "ilib.h"
  862. X#define SPACE      32
  863. X#define ISHEX(c)   (((c)>47&&(c)<58) || ((c)>64&&(c)<71))
  864. X#define ISPRINT(c) ((c)>32&&(c)<127)
  865. X
  866. Xmain (argc,argv)
  867. Xint argc;
  868. Xchar *argv[];
  869. X{
  870. X    FILE *fopen(), *asc;
  871. X    char *mksys(), *realloc(), *mktemp();
  872. X    char **ufile = &argv[1];
  873. X    char edfile[32];    /* path/name of file converted for edit */
  874. X    char *obj;          /* address of malloc'd buffer of object file */
  875. X    char *oend;         /* sentinel pointer at end of obj */
  876. X    char *line;         /* buffer for line-by-line conversion */
  877. X    char *syscmd = "vi";
  878. X    int sysflag = 0;    /* if true, syscmd specified on command line */
  879. X    int optchar;
  880. X    int objsiz;         /* byte size of object file */
  881. X    int linesiz;        /* byte size of malloc'd line */
  882. X    int nostring = 0;   /* if true, don't print strings in edit buf */
  883. X    int bytes = 24;     /* number of original bytes per edit buf line */
  884. X    struct stat sbuf;   /* to see if user changed ASCII file */
  885. X    time_t st_mtime;    /* file last modify time */
  886. X
  887. X    if (argc < 2)
  888. Xcmderr:
  889. X        fprintf(stderr,
  890. X"\nUsage: %s [-] [-b N] [-e cmd] file\n\n%s%s%s%s%s%s%s%s\n", argv[0],
  891. X"\tConvert 'file' to ASCII and edit it (using vi editor by default),\n",
  892. X"\tafterwards restore edited copy to binary, and write back to disk.\n",
  893. X"\tBinary bytes are edited as (space separated) pairs of hex digits,\n",
  894. X"\t'isprint' bytes are shown as single characters, two spaces apart,\n",
  895. X"\t'isprint' byte strings three or longer, are printed contiguously.\n",
  896. X"\t-       forces even 'isprint' bytes to be edited in the hex form.\n",
  897. X"\t-b N    edit N original bytes per line instead of the default 24.\n",
  898. X"\t-c cmd  use 'cmd' instead of vi (embedded '$'s stand for 'file').\n"
  899. X        ), exit(1);
  900. X
  901. X    while (optchar = iopt (&ufile)) {
  902. X        switch (optchar) {
  903. X        default:
  904. X            fprintf (stderr, "Bad option -%c\n", optchar);
  905. X            exit (1);
  906. X        case '-':
  907. X            nostring = 1;
  908. X            break;
  909. X        case 'b':
  910. X            if ((bytes = atoi (*ufile)) < 1)
  911. X                fprintf (stderr, "Minimum 1 byte per line\n"), exit(1);
  912. X            break;
  913. X        case 'e':
  914. X            sysflag = 1;
  915. X            syscmd = *ufile;
  916. X            ifonetic (*ufile);
  917. X            break;
  918. X        }
  919. X    }
  920. X    if (NULCHARP (*ufile))
  921. X        goto cmderr;
  922. X    if ((objsiz = iread (*ufile, &obj)) < 0)
  923. X        fprintf (stderr, "%s %s\n", ierbuf, *ufile), exit (1);
  924. X    oend = obj + objsiz;
  925. X
  926. X    linesiz = bytes << 4; /* 16 times */
  927. X    if ((line = malloc ((unsigned) linesiz+1)) == NULL)
  928. X        fprintf (stderr, "Can't allocate line buffer\n"), exit (1);
  929. X
  930. X    /* make temporary file for converted data, open for write */
  931. X    sprintf (edfile, "/tmp/%s", mktemp ("objed.XXXXXX"));
  932. X    if ((asc = fopen (edfile, "w")) == NULL)
  933. X        fprintf (stderr, "Can't write to %s\n", edfile), exit (1);
  934. X
  935. X    /* convert object file into ASCII in temporary file */
  936. X    breakobj (obj, oend, edfile, asc, line, bytes, nostring);
  937. X    fclose (asc);
  938. X
  939. X    if (stat (edfile, &sbuf) == -1)
  940. X        fprintf (stderr, "Can't stat %s\n", edfile), exit (1);
  941. X    st_mtime = sbuf.st_mtime;
  942. X
  943. X    /* create system command, apply to ASCII version of object */
  944. X    system (mksys (sysflag, syscmd, edfile));
  945. X
  946. X    /* reconstruct object from ASCII version, write if different */
  947. X    if (stat (edfile, &sbuf) == -1)
  948. X        fprintf (stderr, "Can't stat %s\n", edfile), exit (1);
  949. X    if (st_mtime == sbuf.st_mtime) { /* no change during syscmd */
  950. X        unlink (edfile);
  951. X        exit(0);
  952. X    }
  953. X    if ((asc = fopen (edfile, "r")) == NULL)
  954. X        fprintf (stderr, "Can't read %s\n", edfile), exit (1);
  955. X
  956. X    /* reuse "bytes" for new size */
  957. X    fprintf (stderr, "Restoring %s from edit buffer %s\n",
  958. X        *ufile, edfile);
  959. X    if ((bytes = newsize (asc, line, linesiz)) < 0)
  960. X        fprintf (stderr, "Edit line too long\n"), exit (1);
  961. X    if (bytes > objsiz &&
  962. X    (obj = realloc (obj, (unsigned) bytes)) == NULL)
  963. X        fprintf (stderr, "No buffer space for recreating object\n"),
  964. X            exit (1);
  965. X    reconst (obj, asc, line, linesiz);
  966. XQ   if (iwrite (*ufile, obj, obj+bytes) < 0)
  967. X        fprintf (stderr, "%s restore %s\n", ierbuf, *ufile),
  968. X            exit (1);
  969. X
  970. X    unlink (edfile);
  971. X    exit(0);
  972. X}
  973. X
  974. Xbreakobj (obj, oend, edfile, asc, line, bytes, nostring)
  975. Xunsigned char *obj, *oend;
  976. Xchar *edfile, *line;
  977. XFILE *asc;
  978. Xint bytes, nostring;
  979. X{
  980. X    char *number = "0123456789ABCDEF";
  981. X    unsigned char *oj;  /* working pointer in obj */
  982. X    unsigned char *es;  /* past last byte of string in obj */
  983. X    unsigned char *mx;  /* past last byte that may be considered */
  984. X    char *lp = line;    /* pointer into line */
  985. X    int slen;           /* length of string found in obj */
  986. X    int space;          /* obj char positions remaining in line */
  987. X    int lesser;         /* smaller of slen and space */
  988. X    int bc = 0;         /* byte counter in line */
  989. X
  990. X    for (oj = obj -1; ++oj < oend;) {
  991. X        if (++bc < bytes) {
  992. X            if (nostring || *oj < 33 || *oj > 126) {
  993. X                *lp++ = *(number + (*oj >> 4));  /* div by 16 */
  994. X                *lp++ = *(number + (*oj & 0xf)); /* mod 16 */
  995. X                *lp++ = SPACE;
  996. X            }
  997. X            else if (!nostring) { /* possible string in obj */
  998. X
  999. X                /* if a string (of all 'isprint' characters) longer
  1000. X                   than two bytes will fit in the current line,
  1001. X                   install the maximum length string immediately
  1002. X                */
  1003. X                if ((mx = oj + bytes-bc+1) > oend)
  1004. X                    mx = oend;
  1005. X                for (es = oj; ++es < mx && ISPRINT(*es););
  1006. X                if ((slen=es-oj) > 2 && (space=bytes-bc+1) > 2) {
  1007. X                    lesser = (slen < space) ? slen : space;
  1008. X                    for (; --lesser >= 0; *lp++ = *oj++, bc++);
  1009. X                    --oj; /* prior to reincrement in outermost loop */
  1010. X                    if (bc >= bytes) {
  1011. X                        bc = 0;
  1012. X                        *lp++ = '\n';
  1013. X                        if (!fwrite (line, 1, lp-line, asc))
  1014. X                            fprintf (stderr, "Can't write to %s\n",
  1015. X                            edfile), exit (1);
  1016. X                        lp = line;
  1017. X                    }
  1018. X                    else
  1019. X                        --bc, *lp++ = SPACE;
  1020. X                }
  1021. X                else /* not long enough or no space; print byte */
  1022. X                    *lp++ = *oj, *lp++ = SPACE, *lp++ = SPACE;
  1023. X
  1024. X            }
  1025. X        }
  1026. X        else {
  1027. X            bc = 0;
  1028. X            if (nostring || *oj < 33 || *oj > 126) {
  1029. X                *lp++ = *(number + (*oj >> 4));  /* div by 16 */
  1030. X                *lp++ = *(number + (*oj & 0xf)); /* mod 16 */
  1031. X                *lp++ = '\n';
  1032. X            }
  1033. X            else if (!nostring)
  1034. X                *lp++ = *oj, *lp++ = '\n';
  1035. X
  1036. X            if (!fwrite (line, 1, lp-line, asc))
  1037. X                fprintf (stderr, "Can't write to %s\n", edfile),
  1038. X                    exit (1);
  1039. X            lp = line;
  1040. X        }
  1041. X    }
  1042. X    if (bc) { /* write out partial last line */
  1043. X        *(lp-1) = '\n';
  1044. X        if (!fwrite (line, 1, lp-line, asc))
  1045. X            fprintf (stderr, "Can't write to %s\n", edfile),
  1046. X                exit (1);
  1047. X    }
  1048. X}
  1049. X
  1050. Xchar *
  1051. Xmksys (sysflag, usr, edfile)
  1052. Xint sysflag;
  1053. Xchar *usr, *edfile;
  1054. X{
  1055. X    char *sp;      /* to manufactured command */
  1056. X    char *up;      /* to original usr command string */
  1057. X    char *cmd;     /* to address of buffer allocated for new cmd */
  1058. X    int ref = 1;   /* number of times file name is mentioned */
  1059. X    int flen = strlen (edfile);
  1060. X
  1061. X    for (up = usr; *up; up++)
  1062. X        if (*up == '$')
  1063. X            ++ref;
  1064. X    if ((cmd = malloc ((unsigned)strlen(usr) + ref * flen +2)) == NULL)
  1065. X        fprintf (stderr, "No buffer space for syscmd\n"), exit (1);
  1066. X
  1067. X    if (!sysflag) {
  1068. X        sprintf (cmd, "%s%c%s", usr, SPACE, edfile);
  1069. X        return (cmd);
  1070. X    }
  1071. X
  1072. X    for (sp = cmd, up = usr; *up; up++)
  1073. X        if (*up == '$')
  1074. X            strcpy (sp, edfile), sp += flen;
  1075. X        else
  1076. X           *sp++ = *up;
  1077. X    if (!--ref)
  1078. X        sprintf (sp, "%c%s", SPACE, edfile);
  1079. X    return (cmd);
  1080. X}
  1081. X
  1082. Xnewsize (asc, line, linesiz)
  1083. XFILE *asc;
  1084. Xchar *line;
  1085. Xint linesiz;
  1086. X{
  1087. X    char bf[IHALFK];
  1088. X    char *endl = line + linesiz;
  1089. X    char *lp;
  1090. X    char *np;
  1091. X    int len = 0;      /* new expected length of object */
  1092. X    int wlen;         /* length of single token */
  1093. X
  1094. X    for (; fgets (line, linesiz, asc) != NULL; ) {
  1095. X        /* if line is too long (not ending in '\n'), terminate */
  1096. X        for (lp = line; lp < endl && *lp != '\n'; lp++);
  1097. X        if (lp == endl)
  1098. X            return (-1);
  1099. X        for (np = line; (np = ianytok (np, lp, bf)) != NULL;)
  1100. X            if ((wlen=strlen(bf)) == 2 && ISHEX(*bf) && ISHEX(bf[1]))
  1101. X                ++len;
  1102. X            else
  1103. X                len += wlen;
  1104. X    }
  1105. X    rewind (asc);
  1106. X    return (len);
  1107. X}
  1108. X
  1109. Xreconst (obj, asc, line, linesiz)
  1110. XFILE *asc;
  1111. Xunsigned char *obj;
  1112. Xchar *line;
  1113. Xint linesiz;
  1114. X{
  1115. X    char bf[IHALFK];
  1116. X    unsigned char *op = obj;
  1117. X    char *lp;         /* end of fgets string */
  1118. X    char *np;         /* token pointer in line */
  1119. X    int bval;         /* new binary value */
  1120. X    int wlen;         /* length of single token */
  1121. X
  1122. X    for (; fgets (line, linesiz, asc) != NULL; ) {
  1123. X        lp = inull (line);
  1124. X        for (np = line; (np = ianytok (np, lp, bf)) != NULL;) {
  1125. X            if ((wlen=strlen(bf)) == 2 && ISHEX(*bf) && ISHEX(bf[1])) {
  1126. X                bval = (*bf - ((*bf < 65) ? 48 : 55)) << 4;
  1127. X                bval += (bf[1] - ((bf[1] < 65) ? 48 : 55));
  1128. X                *op++ = bval;
  1129. X            }
  1130. X            else {
  1131. X                strcpy ((char *)op, bf);
  1132. X                op += wlen;
  1133. X            }
  1134. X        }
  1135. X    }
  1136. X}
  1137. SHAR_EOF
  1138. $TOUCH -am 0605075390 iex/objed.c &&
  1139. chmod 0755 iex/objed.c ||
  1140. echo "restore of iex/objed.c failed"
  1141. set `wc -c iex/objed.c`;Wc_c=$1
  1142. if test "$Wc_c" != "8383"; then
  1143.     echo original size 8383, current size $Wc_c
  1144. fi
  1145. # ============= iex/pathfiles.c ==============
  1146. echo "x - extracting iex/pathfiles.c (Text)"
  1147. sed 's/^X//' << 'SHAR_EOF' > iex/pathfiles.c &&
  1148. X#ifdef NEVER
  1149. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/pathfiles
  1150. X(/bin/rm -f $BIN/rpath $BIN/wpath $BIN/xpath $BIN/fpath)
  1151. X/bin/ln $BIN/pathfiles $BIN/rpath
  1152. X/bin/ln $BIN/pathfiles $BIN/wpath
  1153. X/bin/ln $BIN/pathfiles $BIN/xpath
  1154. X/bin/ln $BIN/pathfiles $BIN/fpath
  1155. Xexit 0
  1156. X#endif
  1157. X
  1158. X/* list readable/writable/executable files of $PATH or given paths */
  1159. X#include "ilib.h"
  1160. X#define FPATH 371
  1161. X#define RPATH 383
  1162. X#define WPATH 388
  1163. X#define XPATH 389
  1164. X
  1165. Xchar *getenv();
  1166. Xmain (argc, argv)
  1167. Xchar *argv[];
  1168. Xint argc;
  1169. X{
  1170. X    char *mktemp();
  1171. X    char *nextok;               /* to get next dir in $PATH */
  1172. X    char newpath[ITWOK];        /* copy command line arguments */
  1173. X    char *path = newpath;       /* point to whichever path */
  1174. X    char *buf;                  /* buffer to read tmpfile into */
  1175. X    char *list;                 /* pointers to lines of /tmp file */
  1176. X    char **lp;                  /* pointer into list */
  1177. X    char **dirs = &argv[1];     /* dirs in command line */
  1178. X    char tmpname[20];           /* temporary file for ls output */
  1179. X    static char tokbuf[IONEK];
  1180. X    int lines;                  /* in /tmp listing */
  1181. X    int tsiz;                   /* token size */
  1182. X    int bsiz;                   /* buffer size */
  1183. X    int mode;
  1184. X    int context = ihasharg (argv[0], '/');
  1185. X
  1186. X    switch (context) {
  1187. X        default:
  1188. X            fprintf(stderr,
  1189. X"\nUsage:  fpath | rpath | wpath | xpath  [pathdir ...]\n%s%s\n",
  1190. X"\tPrint full path of files | readable | writable | executable files\n",
  1191. X"\tfound in the list of specified 'pathdirs', or in $PATH.\n"), exit(0);
  1192. X        case FPATH:
  1193. X            mode = F_OK;
  1194. X            break;
  1195. X        case RPATH:
  1196. X            mode = R_OK;
  1197. X            break;
  1198. X        case WPATH:
  1199. X            mode = W_OK;
  1200. X            break;
  1201. X        case XPATH:
  1202. X            mode = X_OK;
  1203. X            break;
  1204. X    }
  1205. X    strcpy (tmpname, "/tmp/");
  1206. X    strcat (tmpname, mktemp ("pathXXXXXX"));
  1207. X
  1208. X    if (argc > 1)
  1209. X        for (*path = 0; !NULCHARP (*dirs); dirs++)
  1210. X            strcat (path, *dirs), strcat (path, " "); /* space at end */
  1211. X    else if ((path = getenv("PATH")) == NULL)
  1212. X        fprintf (stderr, "PATH variable not set\n"), exit (1);
  1213. X
  1214. XQ   itok (INITOKF, path, (char *)NULL);
  1215. X    /* space,colon delimiters to tokenize either types of path */
  1216. XQ   while (tsiz = itok (ITOKF, " :", (char *)NULL, &nextok)) {
  1217. XQ       sprintf (tokbuf, "/bin/ls -a '%*.*s' > %s",
  1218. X            tsiz, tsiz, nextok, tmpname);
  1219. X        system (tokbuf);
  1220. X        if ((bsiz = iread (tmpname, &buf)) < 0)
  1221. X            continue;
  1222. X        if ((lines = illistn (buf, buf + bsiz, &list)) < 0)
  1223. X            puts (ierbuf), exit (1);
  1224. X        for (lp = (char **)list; --lines >= 0; lp++) {
  1225. X            if (strcmp (*lp, ".") && strcmp (*lp, "..")) {
  1226. XQ               sprintf (tokbuf, "%*.*s/%s", tsiz, tsiz, nextok, *lp);
  1227. X                if (access (tokbuf, mode) != -1)
  1228. X                    puts (tokbuf);
  1229. X            }
  1230. X        }
  1231. X        free (list);
  1232. X        free (buf);
  1233. X    }
  1234. X    unlink (tmpname);
  1235. X    exit(0);
  1236. X}
  1237. SHAR_EOF
  1238. $TOUCH -am 0605075390 iex/pathfiles.c &&
  1239. chmod 0755 iex/pathfiles.c ||
  1240. echo "restore of iex/pathfiles.c failed"
  1241. set `wc -c iex/pathfiles.c`;Wc_c=$1
  1242. if test "$Wc_c" != "2655"; then
  1243.     echo original size 2655, current size $Wc_c
  1244. fi
  1245. # ============= iex/q.c ==============
  1246. echo "x - extracting iex/q.c (Text)"
  1247. sed 's/^X//' << 'SHAR_EOF' > iex/q.c &&
  1248. X#ifdef NEVER
  1249. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/q
  1250. Xexit 0
  1251. X#endif
  1252. X
  1253. X/* manage stack of /tmp files for stdout segments, report filename */
  1254. X#include "ilib.h"
  1255. X#include <sys/types.h>          /* to interpret struct stat */
  1256. X#include <sys/stat.h>           /* get last modified time from stat() */
  1257. X#define PATH  "/tmp/"
  1258. X#define SPACE 32                /* make ASCII SPACE visible in C code */
  1259. X
  1260. Xmain (argc, argv)
  1261. Xchar *argv[];
  1262. Xint argc;
  1263. X{
  1264. X    struct stat sbuf;           /* for info on 'last modified time' */
  1265. X    time_t oldest = 0x7fffffff; /* Unix time, in seconds since 1970 */
  1266. X    time_t filetime;            /* 'last modified time' of curr file */
  1267. X    char *qlist = "0123456789"; /* list of ending bytes of file names */
  1268. X    char *qindx;                /* at last (counter) byte of qfile */
  1269. X    char qfile[32];             /* buffer for curr file name */
  1270. XQ   char cmd[IONEK];            /* 1K, defined in ilib.h */
  1271. X    char *cmdptr;               /* working in cmd buffer */
  1272. X    int uid;                    /* user id of person executing q */
  1273. X    int cnt;                    /* scratch counter */
  1274. X    int unused = 0;             /* file not in use flag */
  1275. X    int oldmark;                /* flag oldest (least recent) file */
  1276. X
  1277. X    /* make file name, position qindx at the last (counter) byte */
  1278. X    uid = getuid();
  1279. X    sprintf (qfile, "%sqtmp%.4d", PATH, uid);
  1280. X    qindx = qfile + strlen (qfile);
  1281. X    *(qindx+1) = '\0';
  1282. X
  1283. X    /* if no arguments after command name, remove all queued up files */
  1284. X    if (argc < 2) {
  1285. X        for (cnt = 10; --cnt >= 0;)
  1286. X            *qindx = *(qlist + cnt), unlink(qfile);
  1287. X        exit(0);
  1288. X    }
  1289. X    /* expand phonetic control sequences in command arguments */
  1290. X    for (cnt = 1; cnt < argc; cnt++)
  1291. XQ       ifonetic (argv[cnt]);
  1292. X
  1293. X    /* look for next available file */
  1294. X    for (cnt = 10; --cnt >= 0;) {
  1295. X        *qindx = *(qlist + cnt);
  1296. X        /* first choice is file that not yet exists */
  1297. X        if (stat (qfile, &sbuf))  {
  1298. X            unused = 1;
  1299. X            break;
  1300. X        }
  1301. X        /* else find and reuse the oldest file
  1302. X           (use the stat data gotten above)
  1303. X           first file in loop begins marked "oldest"
  1304. X        */
  1305. X        if ((filetime = sbuf.st_mtime) < oldest)
  1306. X            oldest = filetime, oldmark = cnt;
  1307. X    }
  1308. X    if (!unused)
  1309. X        *qindx = *(qlist + oldmark);
  1310. X
  1311. X    /* generate string for system command */
  1312. X    for (cmdptr = cmd, cnt = 1; cnt < argc; cnt++) {
  1313. X        strcpy (cmdptr, argv[cnt]);
  1314. X        cmdptr += strlen (argv[cnt]);
  1315. X        *cmdptr++ = SPACE;
  1316. X    }
  1317. X    sprintf (cmdptr, "> %s", qfile);
  1318. X    /* exec and report */
  1319. X    system (cmd);
  1320. X    puts (qfile);
  1321. X    exit(0);
  1322. X}
  1323. SHAR_EOF
  1324. $TOUCH -am 0605075390 iex/q.c &&
  1325. chmod 0755 iex/q.c ||
  1326. echo "restore of iex/q.c failed"
  1327. set `wc -c iex/q.c`;Wc_c=$1
  1328. if test "$Wc_c" != "2424"; then
  1329.     echo original size 2424, current size $Wc_c
  1330. fi
  1331. # ============= iex/rename.c ==============
  1332. echo "x - extracting iex/rename.c (Text)"
  1333. sed 's/^X//' << 'SHAR_EOF' > iex/rename.c &&
  1334. X#ifdef NEVER
  1335. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/rename
  1336. Xexit 0
  1337. X#endif
  1338. X
  1339. X/* change string pattern1 in filenames to pattern2 */
  1340. X#include "ilib.h"
  1341. X
  1342. Xmain (argc, argv)
  1343. Xint argc;
  1344. Xchar *argv[];
  1345. X{
  1346. X    char cb[IONEK]; /* change buffer for altering file name */
  1347. X    char *fs;       /* to matching pattern1 in file name */
  1348. X    char *fe;       /* just past the matching pattern1 in file name */
  1349. X    char *pat1;     /* pattern1 in command line, or NULL */
  1350. X    char *pat2;     /* pattern2 in command line, or NULL */
  1351. X    char **file;    /* to list of files */
  1352. X    int  p1len;     /* length of pattern1, if non-NULL */
  1353. X
  1354. X    if (argc < 4)
  1355. Xfprintf (stderr, "Usage: %s pattern1 pattern2 files\n%s%s%s", argv[0],
  1356. X"\tto replace the leading pattern1 in each filename with pattern2,\n",
  1357. X"\tor if pattern1 is '-', prepend pattern2 in front of filenames,\n",
  1358. X"\tor if pattern2 is '-', delete leading pattern1 from filenames.\n"),
  1359. Xexit(1);
  1360. X
  1361. X    ifonetic (argv[1]), ifonetic (argv[2]);
  1362. X
  1363. X    if (strcmp (argv[1], argv[2]) == 0)      /* nothing to do */
  1364. X        exit (0);
  1365. X    if (strcmp ((pat2 = argv[2]), "-") == 0)
  1366. X        pat2 = NULL;
  1367. X    if (strcmp ((pat1 = argv[1]), "-") == 0)
  1368. X        pat1 = NULL;
  1369. X    else
  1370. X        p1len = strlen (pat1);
  1371. X
  1372. X    for (file = &argv[3]; !NULCHARP(*file); file++) {
  1373. X        if (NULCHARP (pat1))
  1374. X            sprintf(cb, "%s%s", pat2, *file);
  1375. XQ       else if ((fs = ianymatch (*file, inull(*file), pat1)) != NULL) {
  1376. X            fe = fs + p1len;
  1377. X            strcpy (cb, *file);
  1378. X            cb[fs - *file] = 0; /* terminate string at pat1 */
  1379. X            if (!NULCHARP (pat2))
  1380. X                strcat (cb, pat2);
  1381. X            strcat (cb, fe);
  1382. X        }
  1383. X        else
  1384. X            continue;
  1385. X
  1386. X        /* create link with new name, then delete the original */
  1387. X        if (link (*file, cb)) {
  1388. X            fprintf (stderr, "Can't create %s\n", cb);
  1389. X            continue; /* don't try to delete original */
  1390. X        }
  1391. X        if (unlink (*file))
  1392. X            fprintf (stderr, "Can't remove duplicate %s\n", *file);
  1393. X    }
  1394. X    exit(0);
  1395. X}
  1396. SHAR_EOF
  1397. $TOUCH -am 0607140190 iex/rename.c &&
  1398. chmod 0755 iex/rename.c ||
  1399. echo "restore of iex/rename.c failed"
  1400. set `wc -c iex/rename.c`;Wc_c=$1
  1401. if test "$Wc_c" != "1822"; then
  1402.     echo original size 1822, current size $Wc_c
  1403. fi
  1404. # ============= iex/rewrap.c ==============
  1405. echo "x - extracting iex/rewrap.c (Text)"
  1406. sed 's/^X//' << 'SHAR_EOF' > iex/rewrap.c &&
  1407. X#ifdef NEVER
  1408. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/rewrap
  1409. Xexit 0
  1410. X#endif
  1411. X
  1412. X/* re-list tokens of file, in place */
  1413. X#include "ilib.h"
  1414. X
  1415. X#define DEFAULT      76
  1416. X#define SPACE        32
  1417. X#define NUL          0
  1418. X
  1419. Xchar *mktemp();
  1420. Xchar bed[IHALFK];           /* buffer for token */
  1421. Xchar line[IONEK];           /* buffer for line of input */
  1422. Xchar build[sizeof(line)*2]; /* buffer for assembling output */
  1423. Xchar indent[IONEK];         /* buffer for storing input indentation */
  1424. Xchar outfname[32];          /* collect output in /tmp file */
  1425. Xchar *buildptr;             /* current point in assembled text */
  1426. Xchar *popto;                /* return here when buildptr overruns */
  1427. Xchar *tokend;               /* end of token recognized by ianytok */
  1428. Xint  cols;                  /* maximum width of output line */
  1429. Xint  indentlen;             /* size of indent in current input line */
  1430. Xint  firstinbuild;          /* flag first token of output line */
  1431. X
  1432. Xmain (argc, argv)
  1433. Xint argc;
  1434. Xchar *argv[];
  1435. X{
  1436. X    char **file = argv;
  1437. X
  1438. X    if (argc != 3)
  1439. X        fprintf(stderr, "Usage: %s maxcols file\n", *file), exit(0);
  1440. X
  1441. X    if ((cols = abs (atoi (*++file))) < 1 || cols > IONEK-1)
  1442. X        cols = DEFAULT,
  1443. X        fprintf(stderr, "%s '%s'\nUsing default width (%d columns)\n",
  1444. X            "Line width out of bounds:", *file, DEFAULT);
  1445. X
  1446. X    /* rename input file to stdin, create output file as stdout */
  1447. XQ   if (freopen (*++file, "r", stdin) == NULL)
  1448. X        fprintf (stderr, "Open failed on %s\n", *file), exit (1);
  1449. X    sprintf (outfname, "/tmp/%s", mktemp ("rewrap.XXXXXX"));
  1450. XQ   if (freopen (outfname, "w", stdout) == NULL)
  1451. X        fprintf (stderr, "Can't write to %s\n", outfname), exit (1);
  1452. X
  1453. X    buildptr = build, *buildptr = NUL, firstinbuild = 1;
  1454. X
  1455. X    /* preserve newlines in input, limit line size via fgets vs. gets */
  1456. X    while (fgets (line, sizeof (line), stdin) != NULL)
  1457. X        rewrapline();
  1458. X    if (buildptr != build)
  1459. X        puts (build);
  1460. X
  1461. XQ   fflush (stdout);
  1462. X    sprintf (line, "/bin/mv %s %s", outfname, *file);
  1463. X    if (system (line))
  1464. X        fprintf (stderr, "Can't move %s to %s\n", outfname, *file),
  1465. X        exit (1);
  1466. X    exit (0);
  1467. X}
  1468. X
  1469. Xrewrapline ()
  1470. X{
  1471. X    char *lineptr, *endline;
  1472. X
  1473. X    /* Advance to first token in line, save original indentation */
  1474. X    lineptr = line;
  1475. X    while (*lineptr == SPACE || *lineptr == '\t')
  1476. X        ++lineptr;
  1477. X
  1478. X    if (*lineptr == '\n') { /* no printing characters in input */
  1479. X        *indent = NUL, indentlen = 0;
  1480. X        if (buildptr != build)
  1481. X            puts (build);
  1482. X        buildptr = build, *buildptr = NUL, firstinbuild = 1;
  1483. X        puts ("");
  1484. X        return;
  1485. X    }
  1486. X
  1487. X    if (lineptr == line) /* no leading spaces in input */
  1488. X        *indent = NUL, indentlen = 0;
  1489. X    else {
  1490. X        indentlen = lineptr-line;
  1491. X        strncpy (indent, line, indentlen);
  1492. X        *(indent+indentlen) = NUL;
  1493. X    }
  1494. X
  1495. X    endline = lineptr + strlen(lineptr) -1;
  1496. X    *endline = NUL; /* kill newline */
  1497. X    tokend = lineptr;
  1498. X
  1499. XQ   while ((tokend = ianytok (tokend, endline, bed)) != NULL) {
  1500. X        if (firstinbuild) {
  1501. X            firstinbuild = 0;
  1502. X            strcpy (build, indent); /* begin output with indent */
  1503. X            buildptr = build + indentlen;
  1504. X            if (buildptr - build > cols) {
  1505. X                fprintf (stderr, "Skipping oversize indent\n");
  1506. X                buildptr = build;
  1507. X                *buildptr = NUL;
  1508. X                *indent = NUL, indentlen = 0;
  1509. X            }
  1510. X            strcat (buildptr, bed); /* first token in output */
  1511. X            buildptr += strlen (bed);
  1512. X            if (buildptr - build > cols) {
  1513. X                fprintf (stderr, "Oversize line: %s\n", build);
  1514. X                puts (build);
  1515. X                buildptr = build, *buildptr = NUL, firstinbuild = 1;
  1516. X            }
  1517. X            continue;
  1518. X        }
  1519. X        /* successive tokens in assembled output */
  1520. X        popto = buildptr;
  1521. X        if (*(buildptr-1) == '.') /* extra space after sentence */
  1522. X            *buildptr++ = SPACE;
  1523. X        *buildptr++ = SPACE;
  1524. X        *buildptr = NUL;
  1525. X        strcat (buildptr, bed);
  1526. X        buildptr += strlen (bed);
  1527. X        if (buildptr - build > cols) {
  1528. X            *popto = NUL;
  1529. X            puts (build);
  1530. X            buildptr = build, *buildptr = NUL, firstinbuild = 1;
  1531. X            tokend -= strlen (bed);
  1532. X        }
  1533. X    }
  1534. X}
  1535. SHAR_EOF
  1536. $TOUCH -am 0605075390 iex/rewrap.c &&
  1537. chmod 0755 iex/rewrap.c ||
  1538. echo "restore of iex/rewrap.c failed"
  1539. set `wc -c iex/rewrap.c`;Wc_c=$1
  1540. if test "$Wc_c" != "3770"; then
  1541.     echo original size 3770, current size $Wc_c
  1542. fi
  1543. # ============= iex/rot.c ==============
  1544. echo "x - extracting iex/rot.c (Text)"
  1545. sed 's/^X//' << 'SHAR_EOF' > iex/rot.c &&
  1546. X#ifdef NEVER
  1547. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/rot
  1548. X(/bin/rm -f $BIN/rotr $BIN/rotl $BIN/rotor $BIN/rotol)
  1549. X/bin/ln $BIN/rot $BIN/rotr
  1550. X/bin/ln $BIN/rot $BIN/rotl
  1551. X/bin/ln $BIN/rot $BIN/rotor
  1552. X/bin/ln $BIN/rot $BIN/rotol
  1553. Xexit 0
  1554. X#endif
  1555. X
  1556. X/* rotate input text array */
  1557. X#include "ilib.h"
  1558. X
  1559. X/* primitive hash values decode which link was used as command name */
  1560. X#define ROT     245
  1561. X#define ROTR    327
  1562. X#define ROTL    321
  1563. X#define ROTOR   406
  1564. X#define ROTOL   400
  1565. X
  1566. Xmain (argc, argv)
  1567. Xint argc;
  1568. Xchar *argv[];
  1569. X{
  1570. X    char *buffer;  /* original text */
  1571. X    char *padded;  /* copy of original, space filled to even width */
  1572. X    char *done;    /* rotated array address */
  1573. X    int  bytes;    /* buffer size */
  1574. X    int  context;  /* command name variant of current invokation */
  1575. X    int  longest;  /* longest line in original text, senza newline */
  1576. X    int  lcnt;     /* number of lines in original text */
  1577. X
  1578. X    /* decode command name */
  1579. X    if ((context = ihasharg (argv[0], '/')) == ROT)
  1580. X        fprintf (stderr, "Print rotated image to stdout:\n%s%s%s%s",
  1581. X"rotr  [file] --- rotate text 90 degrees to right\n",
  1582. X"rotl  [file] --- rotate text 90 degrees to left\n",
  1583. X"rotor [file] --- rotate text 180 deg. in depth, 90 deg. to right\n",
  1584. X"rotol [file] --- rotate text 180 deg. in depth, 90 deg. to left\n"),
  1585. X        exit(0);
  1586. X
  1587. X    /* get original text, space fill copy, free original buffer */
  1588. X    if ((bytes = ifilter (argv[1], &buffer)) < 0)
  1589. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  1590. XQ   if ((longest = itexrect (buffer, buffer+bytes, &padded, &lcnt)) < 0)
  1591. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  1592. X    free (buffer);
  1593. X
  1594. X    /* convert context to rotation type */
  1595. X    switch (context) {
  1596. X        default:
  1597. X        case ROTR:  context = IROTR;  break;
  1598. X        case ROTOR: context = IROTOR; break;
  1599. X        case ROTL:  context = IROTL;  break;
  1600. X        case ROTOL: context = IROTOL; break;
  1601. X    }
  1602. X
  1603. X    /* rotate and print */
  1604. XQ   if ((bytes = irotate (padded, longest, lcnt, &done, context)) < 0)
  1605. X        fprintf (stderr, "%s\n", ierbuf), exit (1);
  1606. X    write (1, done, bytes);
  1607. X    exit(0);
  1608. X}
  1609. SHAR_EOF
  1610. $TOUCH -am 0605075390 iex/rot.c &&
  1611. chmod 0755 iex/rot.c ||
  1612. echo "restore of iex/rot.c failed"
  1613. set `wc -c iex/rot.c`;Wc_c=$1
  1614. if test "$Wc_c" != "1982"; then
  1615.     echo original size 1982, current size $Wc_c
  1616. fi
  1617. # ============= iex/scat.c ==============
  1618. echo "x - extracting iex/scat.c (Text)"
  1619. sed 's/^X//' << 'SHAR_EOF' > iex/scat.c &&
  1620. X#ifdef NEVER
  1621. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/scat
  1622. Xexit 0
  1623. X#endif
  1624. X
  1625. X/* reprint input lines from string1, stop at line containing string2 */
  1626. X#include "ilib.h"
  1627. X
  1628. Xmain (argc,argv)
  1629. Xint argc;
  1630. Xchar *argv[];
  1631. X{
  1632. X    char **argp;
  1633. X    char *buf;             /* to file buffer malloc'd by iread */
  1634. XQ   char *lpbuf;           /* to line pointers malloc'd by illistn */
  1635. X    char *end;             /* point to one byte past buffer */
  1636. XQ   char **ptr;            /* working pointer in lpbuf */
  1637. X    char *str1 = argv[1];  /* start string given in command line */
  1638. X    char *str2 = NULL;     /* stop string given in command line */
  1639. X    int filesize;          /* size of malloc'd text buffer */
  1640. X    int lcount;            /* number of lines in file */
  1641. X    int optchar;
  1642. X
  1643. X    if (argc < 2)
  1644. X        fprintf (stderr,
  1645. X"Usage: %s string1 [-s string2] [files]\n\%s%s%s", argv[0],
  1646. X"\tcat first line of input/file (to stdout) that contains <string1>,\n",
  1647. X"\tor the section of input/file from the line containing <string1>\n",
  1648. X"\tstopping at (not including) the line that contains <string2>\n\n"),
  1649. X        exit(1);
  1650. X
  1651. X    ifonetic (argv[1]);
  1652. X    argp = argv + 2;
  1653. X    if ((optchar = iopt (&argp))) { /* looks like an option */
  1654. X        if (optchar == 's') {
  1655. X            str2 = *argp++; /* increment argp to file name */
  1656. X            ifonetic (str2);
  1657. X        }
  1658. X        else
  1659. X            argp = argv + 2; /* could be file name with leading '-' */
  1660. X    }
  1661. X
  1662. X    if ((filesize = ifilter (*argp, &buf)) < 0)
  1663. X        fprintf (stderr, "%s %s\n", ierbuf, *argp), exit (1);
  1664. X
  1665. X    do {
  1666. X        end = buf + filesize;
  1667. X        /* set up pointers to lines of file */
  1668. XQ       if ((lcount = illistn (buf, end, &lpbuf)) < 1)
  1669. X            fprintf (stderr, "%s\n", ierbuf), exit (1);
  1670. X
  1671. XQ       for (ptr = (char **)lpbuf; !NULCHARP(*ptr); ptr++) {
  1672. X            if (ianymatch (*ptr, *(ptr+1), str1) == NULL)
  1673. X                continue;
  1674. X            puts (*ptr);
  1675. X            if (str2 != NULL) {
  1676. X                for (; !NULCHARP(*++ptr);) {
  1677. X                    if (ianymatch (*ptr, *(ptr+1), str2) != NULL)
  1678. X                        break;
  1679. X                    puts (*ptr);
  1680. X                }
  1681. X            }
  1682. X            break;
  1683. X        }
  1684. X        free (buf);
  1685. X        free (lpbuf);
  1686. X
  1687. X        /* repeat for other files */
  1688. X        if (!NULCHARP (*argp) && !NULCHARP (*++argp))
  1689. X            if ((filesize = iread (*argp, &buf)) < 0)
  1690. X                fprintf (stderr, "%s %s\n", ierbuf, *argp), exit (1);
  1691. X    } while (!NULCHARP (*argp));
  1692. X
  1693. X    exit (0);
  1694. X}
  1695. SHAR_EOF
  1696. $TOUCH -am 0605075390 iex/scat.c &&
  1697. chmod 0755 iex/scat.c ||
  1698. echo "restore of iex/scat.c failed"
  1699. set `wc -c iex/scat.c`;Wc_c=$1
  1700. if test "$Wc_c" != "2170"; then
  1701.     echo original size 2170, current size $Wc_c
  1702. fi
  1703. # ============= iex/stamp.c ==============
  1704. echo "x - extracting iex/stamp.c (Text)"
  1705. sed 's/^X//' << 'SHAR_EOF' > iex/stamp.c &&
  1706. X#ifdef NEVER
  1707. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/stamp
  1708. Xexit 0
  1709. X#endif
  1710. X
  1711. X/* print four-digit stamp of current month/day */
  1712. X#include "ilib.h"
  1713. X
  1714. Xmain ()
  1715. X{
  1716. XQ   printf ("%.2d%.2d\n", itomonth(), itoday());
  1717. X    exit (0);
  1718. X}
  1719. SHAR_EOF
  1720. $TOUCH -am 0605075390 iex/stamp.c &&
  1721. chmod 0755 iex/stamp.c ||
  1722. echo "restore of iex/stamp.c failed"
  1723. set `wc -c iex/stamp.c`;Wc_c=$1
  1724. if test "$Wc_c" != "226"; then
  1725.     echo original size 226, current size $Wc_c
  1726. fi
  1727. # ============= iex/trunc.c ==============
  1728. echo "x - extracting iex/trunc.c (Text)"
  1729. sed 's/^X//' << 'SHAR_EOF' > iex/trunc.c &&
  1730. X#ifdef NEVER
  1731. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/trunc
  1732. X(/bin/rm -f $BIN/leftrunc)
  1733. Xln $BIN/trunc $BIN/leftrunc
  1734. Xexit 0
  1735. X#endif
  1736. X
  1737. X/* output lines from given column, or until given column */
  1738. X
  1739. X#include "ilib.h"
  1740. X#define  LEFTRUNC 611
  1741. X#define  MAXC     (IONEK-1)
  1742. X
  1743. Xmain (argc, argv)
  1744. Xint argc;
  1745. Xchar *argv[];
  1746. X{
  1747. X    int N;
  1748. X    int left = 0;
  1749. X    char c[MAXC+1];
  1750. X    char *end;
  1751. X
  1752. X    if (argc < 2 || (N = atoi (argv[1])) < 0 || N > MAXC)
  1753. X        fprintf (stderr, "Usage: trunc|leftrunc N [file]\n%s%s%s%d%s",
  1754. X        "\ttruncate lines of input beyond column N (to stdout) or\n",
  1755. X        "\tsuppress left N columns of each line of input;\n",
  1756. X        "\tmaximum line length: ", MAXC, " bytes.\n\n"), exit (0);
  1757. X
  1758. X    /* rename file to stdin */
  1759. X    if (argc > 2) {
  1760. X        if (freopen (argv[2], "r", stdin) == NULL) {
  1761. X            ierror ("at setting up filter in main");
  1762. X            fprintf (stderr, "%s\n", ierbuf), exit (1);
  1763. X        }
  1764. X    }
  1765. XQ   if (ihasharg (argv[0], '/') == LEFTRUNC)
  1766. X        left = 1;
  1767. X
  1768. X    /* use fgets to prevent buffer overrun */
  1769. X    while (fgets (c, MAXC, stdin) != NULL) {
  1770. X        end = inull (c);
  1771. X        *--end = '\0'; /* kill newline */
  1772. X        if (left)
  1773. X            (end-c > N) ? (puts(c+N)) : (puts(""));
  1774. X        else
  1775. X            (end-c > N) ? (c[N] = '\0', puts(c)) : (puts(c));
  1776. X    }
  1777. X    exit(0);
  1778. X}
  1779. SHAR_EOF
  1780. $TOUCH -am 0605075390 iex/trunc.c &&
  1781. chmod 0755 iex/trunc.c ||
  1782. echo "restore of iex/trunc.c failed"
  1783. set `wc -c iex/trunc.c`;Wc_c=$1
  1784. if test "$Wc_c" != "1187"; then
  1785.     echo original size 1187, current size $Wc_c
  1786. fi
  1787. # ============= iex/unlink.c ==============
  1788. echo "x - extracting iex/unlink.c (Text)"
  1789. sed 's/^X//' << 'SHAR_EOF' > iex/unlink.c &&
  1790. X#ifdef NEVER
  1791. X/bin/cc -O -DIMANFMT -I../i $0 ../i/ilib.a -o $BIN/unlink
  1792. Xexit 0
  1793. X#endif
  1794. X
  1795. X/* brute force remove */
  1796. X#include "ilib.h"
  1797. X
  1798. Xmain (argc, argv)
  1799. Xint argc;
  1800. Xchar *argv[];
  1801. X{
  1802. X    int mode;
  1803. X
  1804. X    while (--argc)
  1805. XQ       if ((mode = imode (argv[argc], ISDIR)) < 0)
  1806. X            fprintf (stderr, "File not found: %s\n", argv[argc]);
  1807. XQ       else if (mode == 1)
  1808. X            fprintf (stderr, "Skipping directory: %s\n", argv[argc]);
  1809. X        else if (unlink (argv[argc]))
  1810. X            fprintf (stderr, "No permission to remove: %s\n", argv[argc]);
  1811. X
  1812. X    exit (0);
  1813. X}
  1814. SHAR_EOF
  1815. $TOUCH -am 0605075390 iex/unlink.c &&
  1816. chmod 0755 iex/unlink.c ||
  1817. echo "restore of iex/unlink.c failed"
  1818. set `wc -c iex/unlink.c`;Wc_c=$1
  1819. if test "$Wc_c" != "512"; then
  1820.     echo original size 512, current size $Wc_c
  1821. fi
  1822. echo "End of part 4, continue with part 5"
  1823. exit 0
  1824. -- 
  1825.         Istvan Mohos
  1826.         ...uunet!pyrdc!pyrnj!hhb!istvan
  1827.         RACAL-REDAC/HHB 1000 Wyckoff Ave. Mahwah NJ 07430 201-848-8000
  1828. ======================================================================
  1829.