1*11a8fa6cSceastha /*
2*11a8fa6cSceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3*11a8fa6cSceastha * Use is subject to license terms.
4*11a8fa6cSceastha */
5*11a8fa6cSceastha
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <assert.h>
197c478bd9Sstevel@tonic-gate #include <string.h>
207c478bd9Sstevel@tonic-gate #define TXTLEN 1000
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate char *outbuf = 0;
237c478bd9Sstevel@tonic-gate extern char *soutput;
247c478bd9Sstevel@tonic-gate extern int soutlen, iflong;
257c478bd9Sstevel@tonic-gate extern long indexdate;
267c478bd9Sstevel@tonic-gate union ptr {
277c478bd9Sstevel@tonic-gate unsigned *a;
287c478bd9Sstevel@tonic-gate long *b;
297c478bd9Sstevel@tonic-gate };
307c478bd9Sstevel@tonic-gate
31*11a8fa6cSceastha extern int corout();
32*11a8fa6cSceastha extern int fgrep();
33*11a8fa6cSceastha extern long findline();
34*11a8fa6cSceastha
35*11a8fa6cSceastha static int auxil(char *, char *);
36*11a8fa6cSceastha
37*11a8fa6cSceastha int
baddrop(unsigned * mptr,int nf,FILE * fc,int nitem,char * qitem[],char * rprog,int full)38*11a8fa6cSceastha baddrop(unsigned *mptr, int nf, FILE *fc, int nitem,
39*11a8fa6cSceastha char *qitem[], char *rprog, int full)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate /* checks list of drops for real bad drops; finds items with "deliv" */
427c478bd9Sstevel@tonic-gate int i, g, j, need, got, na, len;
437c478bd9Sstevel@tonic-gate long lp;
447c478bd9Sstevel@tonic-gate char res[100], *ar[50], output[TXTLEN], *mput;
457c478bd9Sstevel@tonic-gate union ptr master;
467c478bd9Sstevel@tonic-gate extern int colevel, reached;
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate if (iflong) {
497c478bd9Sstevel@tonic-gate master.b = (long *)mptr;
50*11a8fa6cSceastha } else {
517c478bd9Sstevel@tonic-gate master.a = mptr;
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #if D1
557c478bd9Sstevel@tonic-gate if (iflong)
567c478bd9Sstevel@tonic-gate fprintf(stderr, "in baddrop, nf %d master %ld %ld %ld\n",
577c478bd9Sstevel@tonic-gate nf, master.b[0], master.b[1], master.b[2]);
587c478bd9Sstevel@tonic-gate else
597c478bd9Sstevel@tonic-gate fprintf(stderr, "in baddrop, nf %d master %d %d %d\n",
607c478bd9Sstevel@tonic-gate nf, master.a[0], master.a[1], master.a[2]);
617c478bd9Sstevel@tonic-gate #endif
62*11a8fa6cSceastha for (i = g = 0; i < nf; i++) {
637c478bd9Sstevel@tonic-gate lp = iflong ? master.b[i] : master.a[i];
647c478bd9Sstevel@tonic-gate #if D1
657c478bd9Sstevel@tonic-gate if (iflong)
667c478bd9Sstevel@tonic-gate fprintf(stderr, "i %d master %lo lp %lo\n",
677c478bd9Sstevel@tonic-gate i, master.b[i], lp);
687c478bd9Sstevel@tonic-gate else
697c478bd9Sstevel@tonic-gate fprintf(stderr, "i %d master %o lp %lo\n",
707c478bd9Sstevel@tonic-gate i, master.a[i], lp);
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate fseek(fc, lp, 0);
737c478bd9Sstevel@tonic-gate fgets(res, 100, fc);
747c478bd9Sstevel@tonic-gate #if D1
757c478bd9Sstevel@tonic-gate fprintf(stderr, "tag %s", res);
767c478bd9Sstevel@tonic-gate #endif
77*11a8fa6cSceastha if (!auxil(res, output)) {
787c478bd9Sstevel@tonic-gate char *s;
797c478bd9Sstevel@tonic-gate int c;
807c478bd9Sstevel@tonic-gate #if D1
817c478bd9Sstevel@tonic-gate fprintf(stderr, "not auxil try rprog %c\n",
827c478bd9Sstevel@tonic-gate rprog ? 'y': 'n');
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate for (s = res; c = *s; s++)
85*11a8fa6cSceastha if (c == ';' || c == '\n') {
867c478bd9Sstevel@tonic-gate *s = 0;
877c478bd9Sstevel@tonic-gate break;
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (rprog)
91*11a8fa6cSceastha len = corout(res, output, rprog, "", TXTLEN);
92*11a8fa6cSceastha else {
937c478bd9Sstevel@tonic-gate len = findline(res, &mput, TXTLEN, indexdate);
94*11a8fa6cSceastha if (len > 0) { /* copy and free */
957c478bd9Sstevel@tonic-gate strncpy(output, mput, TXTLEN);
967c478bd9Sstevel@tonic-gate free(mput);
97*11a8fa6cSceastha } else /* insufficient memory or other... */
987c478bd9Sstevel@tonic-gate len = 0;
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate #if D1
1027c478bd9Sstevel@tonic-gate assert(len < TXTLEN);
1037c478bd9Sstevel@tonic-gate fprintf(stderr, "item %d of %d, tag %s len %d output\n%s\n..\n",
1047c478bd9Sstevel@tonic-gate i, nf, res, len, output);
1057c478bd9Sstevel@tonic-gate #endif
1067c478bd9Sstevel@tonic-gate if (len == 0)
1077c478bd9Sstevel@tonic-gate continue;
1087c478bd9Sstevel@tonic-gate need = colevel ? reached : nitem;
1097c478bd9Sstevel@tonic-gate na = 0;
1107c478bd9Sstevel@tonic-gate ar[na++] = "fgrep";
1117c478bd9Sstevel@tonic-gate ar[na++] = "-r";
1127c478bd9Sstevel@tonic-gate ar[na++] = "-n";
1137c478bd9Sstevel@tonic-gate ar[na++] = (char *)need;
1147c478bd9Sstevel@tonic-gate ar[na++] = "-i";
1157c478bd9Sstevel@tonic-gate ar[na++] = output;
1167c478bd9Sstevel@tonic-gate ar[na++] = (char *)len;
1177c478bd9Sstevel@tonic-gate for (j = 0; j < nitem; j++)
1187c478bd9Sstevel@tonic-gate ar[na++] = qitem[j];
1197c478bd9Sstevel@tonic-gate #ifdef D1
1207c478bd9Sstevel@tonic-gate fprintf(stderr, "calling fgrep len %d ar[4] %s %o %d \n",
1217c478bd9Sstevel@tonic-gate len, ar[4], ar[5], ar[6]);
1227c478bd9Sstevel@tonic-gate #endif
123*11a8fa6cSceastha if (fgrep(na, ar) == 0) {
1247c478bd9Sstevel@tonic-gate #ifdef D1
1257c478bd9Sstevel@tonic-gate fprintf(stderr, "fgrep found it\n");
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate if (iflong)
1287c478bd9Sstevel@tonic-gate master.b[g++] = master.b[i];
1297c478bd9Sstevel@tonic-gate else
1307c478bd9Sstevel@tonic-gate master.a[g++] = master.a[i];
1317c478bd9Sstevel@tonic-gate if (full >= g)
1327c478bd9Sstevel@tonic-gate if (soutput == 0)
1337c478bd9Sstevel@tonic-gate fputs(output, stdout);
1347c478bd9Sstevel@tonic-gate else
1357c478bd9Sstevel@tonic-gate strcpy(soutput, output);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate #ifdef D1
1387c478bd9Sstevel@tonic-gate fprintf(stderr, "after fgrep\n");
1397c478bd9Sstevel@tonic-gate #endif
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate return (g);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
144*11a8fa6cSceastha static int
auxil(char * res,char * output)145*11a8fa6cSceastha auxil(char *res, char *output)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate extern FILE *fd;
1487c478bd9Sstevel@tonic-gate long lp, c;
1497c478bd9Sstevel@tonic-gate int len;
150*11a8fa6cSceastha if (fd == 0)
151*11a8fa6cSceastha return (0);
152*11a8fa6cSceastha while (c = *res++) {
153*11a8fa6cSceastha if (c == ';') {
1547c478bd9Sstevel@tonic-gate sscanf(res, "%ld,%d", &lp, &len);
1557c478bd9Sstevel@tonic-gate fseek(fd, lp, 0);
1567c478bd9Sstevel@tonic-gate fgets(output, len, fd);
1577c478bd9Sstevel@tonic-gate return (1);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate return (0);
1617c478bd9Sstevel@tonic-gate }
162