Annotation of researchv10no/cmd/ap/apnews/apnews1.c, revision 1.1

1.1     ! root        1: # include "stdio.h"
        !             2: # include "ctype.h"
        !             3: # include "assert.h"
        !             4: # include "signal.h"
        !             5: # include "sys/types.h"
        !             6: # include "sys/stat.h"
        !             7: # include "cbt.h"
        !             8: # include "time.h"
        !             9: char names[20][14];
        !            10: char heads[20][50];
        !            11: # define NST 200
        !            12: char storylist[NST][12];
        !            13: int stc=0;
        !            14: int intr();
        !            15: char *getlogin(), *ctime();
        !            16: struct tm *localtime();
        !            17: int day, yesterday;
        !            18: int stp=0;
        !            19: int inolim=0;
        !            20: int inodes[NST+5];
        !            21: char *getenv(), *getpwd();
        !            22: char nowfile[20]="";
        !            23: char nowhead[50];
        !            24: char pwdir[50];
        !            25: int scoring=1;
        !            26: long filtim();
        !            27: int nviewed=0;
        !            28: char *storydir = "/usr/spool/ap";
        !            29: 
        !            30: /* logging */
        !            31: char *apnlog = "/usr/spool/ap/apnlog";
        !            32: long tmstart, tmend;
        !            33: int nslug, nstory, nsearch;
        !            34: 
        !            35: main(argc,argv)
        !            36:        char *argv[];
        !            37: {
        !            38: char line[100];
        !            39: int i, lc=1;
        !            40: long l;
        !            41: char *ctp, *userp;
        !            42: char forcebuf[BUFSIZ];
        !            43: setbuf(stdout, forcebuf);
        !            44: umask(0);
        !            45: for (argc--,argv++; argc>0 && argv[0][0]=='-'; argc--,argv++)
        !            46:        switch(argv[0][1])
        !            47:                {
        !            48:                case 'f':
        !            49:                        storydir = argv[1];
        !            50:                        argv++; argc--;
        !            51:                        break;
        !            52:                case 'r':
        !            53:                        scoring=0; break;
        !            54:                }
        !            55: tmstart = l = time(0);
        !            56: day = localtime(&l)->tm_mday;
        !            57: l -= 86400; /* go back one day */
        !            58: yesterday = localtime(&l)->tm_mday;
        !            59: signal (SIGINT, intr);
        !            60: strcpy(pwdir, getpwd());
        !            61: i = chdir(storydir);
        !            62: if (i!=0)
        !            63:        {
        !            64:        fprintf(stderr, "no directory %s\n",storydir);
        !            65:        exit(1);
        !            66:        }
        !            67: top();
        !            68: extend(20);
        !            69: display();
        !            70: menu(lc);
        !            71: fflush(stdout);
        !            72: extend(30);
        !            73: while (gets(line))
        !            74:        {
        !            75:        tpos(0,0);
        !            76:        if (line[0]!=0)
        !            77:                rate();
        !            78:        switch(line[0])
        !            79:                {
        !            80:                case '0': case '1': case '2': case '3': case '4': 
        !            81:                case '5': case '6': case '7': case '8': case '9': 
        !            82:                        i = atoi(line)-1;
        !            83:                        if (i<0 || i>19) continue;
        !            84:                        pr(names[i], heads[i]);
        !            85:                        nviewed=1;
        !            86:                        nstory++;
        !            87:                        lc=0;
        !            88:                        break;
        !            89:                case 0: /* continue this story */
        !            90:                        if (lc==0)
        !            91:                                {
        !            92:                                nviewed++;
        !            93:                                pr(0,0);
        !            94:                                }
        !            95:                        else if (lc==1)
        !            96:                                display();
        !            97:                        else if (lc==2)
        !            98:                                sums();
        !            99:                        if (lc>0) menu(lc);
        !           100:                        break;
        !           101:                case '?':
        !           102:                        menu(0);
        !           103:                        break;
        !           104:                case '!':
        !           105:                        system(line+1);
        !           106:                        break;
        !           107:                case '.':
        !           108:                        disprint();
        !           109:                        break;
        !           110:                case 'a':
        !           111:                        pr(line, "");
        !           112:                        nstory++;
        !           113:                        nviewed=1;
        !           114:                        lc=0;
        !           115:                        break;
        !           116:                case '+':
        !           117:                case 'm':
        !           118:                        shorten(line+1);
        !           119:                        display();
        !           120:                        lc=1;
        !           121:                        break;
        !           122:                case 'M':
        !           123:                        sums();
        !           124:                        menu(lc=2);
        !           125:                        break;
        !           126:                case 't':
        !           127:                        top();
        !           128:                        extend(20);
        !           129:                        shorten(line+1);
        !           130:                        display();
        !           131:                        menu(lc=1);
        !           132:                        break;
        !           133:                case 'T':
        !           134:                        top();
        !           135:                        extend(20);
        !           136:                        sums();
        !           137:                        menu(lc=2);
        !           138:                        break;
        !           139:                case 'c':
        !           140:                case 'C': /* copy file to my directory */
        !           141:                        i = atoi(line+1);
        !           142:                        if (i>0)
        !           143:                                strcpy(nowfile, names[i-1]);
        !           144:                        copyto(line+1, nowfile);
        !           145:                        nviewed+= 3; /* big bonus if they copy it */
        !           146:                        break;
        !           147:                case 'D': /* delete file*/
        !           148:                        i = atoi(line+1);
        !           149:                        if (i>0)
        !           150:                                strcpy(nowfile, names[i-1]);
        !           151:                        printf("Deleting /%s/\n",nowfile);
        !           152:                        unlink(nowfile);
        !           153:                        break;
        !           154:                case 'Y':
        !           155:                case 'y': /* must get keyword list */
        !           156:                        getkeys(line[0]=='Y');
        !           157:                        break;
        !           158:                case 'S':
        !           159:                case 's': /* search for words */
        !           160:                        wdsearch (line+1, line[0]=='S');
        !           161:                        nsearch++;
        !           162:                        break;
        !           163:                case 'q':
        !           164:                        tpos(23,0);
        !           165:                        wrap();
        !           166:                }
        !           167:        fflush(stdout);
        !           168:        }
        !           169: tpos(23,0);
        !           170: wrap();
        !           171: }
        !           172: intr()
        !           173: {
        !           174: tpos(23,0);
        !           175: wrap();
        !           176: exit(0);
        !           177: }
        !           178: int llens[10] ={70,70,70,70,70,70,70,70,70,70};
        !           179: pr (s, h)
        !           180:        char *s, *h;
        !           181: {
        !           182: static FILE *f=NULL;
        !           183: char line[120];
        !           184: int i, k, k1;
        !           185: if (s && s[0]==0)
        !           186:        return;
        !           187: if (s!=0)
        !           188:        {
        !           189:        if (f!= NULL) fclose(f);
        !           190:        f = fopen(s, "r");
        !           191:        if (f==NULL)
        !           192:                {
        !           193:                printf("No file %s\n", s);
        !           194:                return;
        !           195:                }
        !           196:        strcpy(nowfile, s);
        !           197:        strcpy(nowhead, h);
        !           198:        }
        !           199: else
        !           200:        if (f==NULL) return;
        !           201: tpos(12,0);
        !           202: if (ifclear())
        !           203:        clr2end();
        !           204: for(i=0; i<10; i++)
        !           205:        {
        !           206:        if(fgets(line, 100, f)==0)
        !           207:                break;
        !           208:        trimnl(line);
        !           209:        k = strlen(line);
        !           210:        if (line[0]=='\t') k+= 7;
        !           211:        if (!ifclear() &&ifclrline())
        !           212:                clrline();
        !           213:        fputs(line, stdout);
        !           214:        if (!ifclear() && !ifclrline())
        !           215:                for(k1=llens[i]; k1>k; k1--)
        !           216:                        putchar(' ');
        !           217:        printf("\n");
        !           218:        llens[i]=k+1;
        !           219:        }
        !           220: }
        !           221: trimnl(s)
        !           222:        char *s;
        !           223: {
        !           224: while (*s) s++;
        !           225: if (*--s == '\n') *s=0;
        !           226: }
        !           227: static hfgate=0;
        !           228: static char hfbuff[5000];
        !           229: static char *hflab[400];
        !           230: static int hftop=0;
        !           231: hfast (out, snam)
        !           232:        char *out, *snam;
        !           233: {
        !           234: char *s;
        !           235: int i;
        !           236: if (hfgate==0)
        !           237:        {
        !           238:        int f, n;
        !           239:        char fn[40], *index();
        !           240:        sprintf(fn, "%s/.S%d", storydir, day);
        !           241:        f = open(fn, 0);
        !           242:        if (f<0) return(0);
        !           243:        n= read (f, hfbuff, 5000);
        !           244:        hfbuff[n]=0;
        !           245:        close(f);
        !           246:        hfgate=1;
        !           247:        s = hfbuff;
        !           248:        for(hftop=0; hftop<400; hftop++)
        !           249:                {
        !           250:                if (*s==0) break;
        !           251:                hflab[hftop] = s;
        !           252:                s = index(s, '\n');
        !           253:                if (s==NULL) break;
        !           254:                *s++ = 0;
        !           255:                }
        !           256:        }
        !           257: for(i=0; i<hftop; i++)
        !           258:        {
        !           259:        if (strncmp(snam, hflab[i], 5)==0 && hflab[i][6] !=0)
        !           260:                {
        !           261:                strcpy(out, hflab[i]+6);
        !           262:                return(1);
        !           263:                }
        !           264:        }
        !           265: return(0);
        !           266: }
        !           267: hget(s, f)
        !           268:        char s[];
        !           269:        FILE *f;
        !           270: {
        !           271: int i, c, nw;
        !           272: char line[100], *wv[40], *p;
        !           273: if (f==NULL)
        !           274:        {
        !           275:        *s=0;
        !           276:        return;
        !           277:        }
        !           278: fgets(line, 100, f);
        !           279: trimnl(line);
        !           280: if (line[1]==' ' && line[5]==' ' && line[8]=='-') /* their heading */
        !           281:        {
        !           282:        for(p=line+9; (c = *p) && c!=' '; p++)
        !           283:                *s++ = c;
        !           284:        *s=0;
        !           285:        return;
        !           286:        }
        !           287: nw = getargs(line, wv);
        !           288: strcpy(s, wv[i=pickbig(wv, nw)]);
        !           289: wv[i]="";
        !           290: strcat(s, wv[pickbig(wv,nw)]);
        !           291: return;
        !           292: }
        !           293: notnew(i, inodes, fname)
        !           294:        char *fname;
        !           295:        int *inodes;
        !           296: {
        !           297: int ino, j;
        !           298: struct stat stbuff;
        !           299: stat (fname, &stbuff);
        !           300: ino = stbuff.st_ino;
        !           301: for(j=0; j<i; j++)
        !           302:        if (ino==inodes[j])
        !           303:                return(1);
        !           304: inodes[i]=ino;
        !           305: return(0);
        !           306: }
        !           307: display()
        !           308: {
        !           309: getnam(20);
        !           310: disprint();
        !           311: nslug += 20;
        !           312: }
        !           313: disprint()
        !           314: {
        !           315: int i, kl, a;
        !           316: char bform[30];
        !           317: FILE *fl;
        !           318: startsc();
        !           319: for(i=0; i<20; i++)
        !           320:        {
        !           321:        if (names[i][0]== '\0')
        !           322:                break;
        !           323:        if (hfast(heads[i], names[i]))
        !           324:                continue;
        !           325:        fl = fopen(names[i], "r");
        !           326:        if (fl!=NULL)
        !           327:                {
        !           328:                hget(heads[i],fl);
        !           329:                fclose(fl);
        !           330:                }
        !           331:        else
        !           332:                heads[i][0]=names[i][0]=0;
        !           333:        }
        !           334: for(kl=i=0; i<10; i++)
        !           335:        {
        !           336:        a = strlen(heads[i]);
        !           337:        if (a>kl) kl=a;
        !           338:        }
        !           339: kl += 4;
        !           340: if (kl>30) kl=30;
        !           341: sprintf(bform, "%%3d: (%%5s) %%-%d.%ds ", kl, kl);
        !           342: for(i=0; i<10; i++)
        !           343:        {
        !           344:        char xheads[50], yheads[50], *scr();
        !           345:        clrline();
        !           346: 
        !           347:        if (scoring)
        !           348:                {
        !           349:                if (heads[i][0])
        !           350:                sprintf(xheads, "%.20s %s", heads[i], scr(names[i]));
        !           351:                else
        !           352:                xheads[0]=0;
        !           353:                if (heads[i+10][0])
        !           354:                sprintf(yheads, "%.20s %s", heads[i+10], scr(names[i+10]));
        !           355:                else
        !           356:                yheads[0]=0;
        !           357:                }
        !           358:        else
        !           359:                {
        !           360:                strncpy(xheads, heads[i], 20);
        !           361:                strncpy(yheads, heads[i+10], 20);
        !           362:                xheads[20]= yheads[20]=0;
        !           363:                }
        !           364:        if (strlen(names[i])<=5)
        !           365:                printf(bform, i+1, names[i], xheads);
        !           366:        else
        !           367:                printf("%3d: (%11s) %-19.19s ",i+1,names[i],xheads);
        !           368:        printf("%3d: (%5s) %.30s\n",i+11,names[i+10],yheads);
        !           369:        }
        !           370: nowfile[0]=0;
        !           371: }
        !           372: top()
        !           373: {
        !           374:        char tstories[NST][6];
        !           375:        int f, n, i, j;
        !           376:        stc = inolim = stp = 0;
        !           377:        f = open("..tlist", 0);
        !           378:        if (f<0) return;
        !           379:        n = read(f, tstories, NST*6);
        !           380:        stc = n/6;
        !           381:        /* reverse tstories */
        !           382:        for(i=0, j=stc-1; i<j; i++,j--)
        !           383:                {
        !           384:                char x[6];
        !           385:                strncpy(x,tstories[i],6);
        !           386:                strncpy(tstories[i],tstories[j],6);
        !           387:                strncpy(tstories[j],x,6);
        !           388:                }
        !           389:        close(f);
        !           390:        for(i=0; i<stc; i++)
        !           391:                {
        !           392:                strncpy(storylist[i],tstories[i],6);
        !           393:                storylist[i][6]= 0;
        !           394:                }
        !           395: }
        !           396: getnam(n)
        !           397: {
        !           398: int i;
        !           399: for (i=0; i<n; i++)
        !           400:        heads[i][0]= names[i][0]= '\0';
        !           401: for (i=0; i<n; i++)
        !           402:        {
        !           403:                try:
        !           404:                if (stp>=stc) break;
        !           405:                strcpy(names[i], storylist[stp++]);
        !           406: 
        !           407:                if (!interest(names[i])) goto try;
        !           408:                if ((names[i][0]=='a'||names[i][0]=='b'||names[i][0]=='f')
        !           409:                 && isdigit(names[i][1]))
        !           410:                        continue;
        !           411:                if (names[i][5]=='.' && names[i][6]=='a' &&
        !           412:                isdigit(names[i][7]))
        !           413:                        continue;
        !           414:                goto try;
        !           415:        }
        !           416: }
        !           417: sums()
        !           418: {
        !           419: int i;
        !           420: startsc();
        !           421: getnam(10);
        !           422: for (i=0; i<10; i++)
        !           423:        {
        !           424:        printf("%d ",i+1);
        !           425:        prsum(names[i]);
        !           426:        }
        !           427: }
        !           428: wrap()
        !           429: {
        !           430: FILE *f;
        !           431: tmend = time(0);
        !           432: f = fopen(apnlog, "a");
        !           433: if (f!=NULL)
        !           434:        {
        !           435:        fprintf(f,
        !           436:        "%s slugs %d stories %d searches %d dir %s %.16s %d secs\n",
        !           437:                getlogin(), nslug, nstory, nsearch, storydir,
        !           438:                ctime(&tmstart), tmend-tmstart);
        !           439:        fclose(f);
        !           440:        }
        !           441: exit(0);
        !           442: }
        !           443: rate()
        !           444: {
        !           445: int r;
        !           446: r = nviewed;
        !           447: if (r<0) r=0;
        !           448: if (r>5) r=5;
        !           449: upd(nowfile, r);
        !           450: }
        !           451: 
        !           452: short nread[1000];
        !           453: short neval[1000];
        !           454: short tscor[1000];
        !           455: short sqscr[1000];
        !           456: static char * scname = "Scores";
        !           457: short nvl[1000];
        !           458: int ngate=0;
        !           459: upd(nm, r)
        !           460: {
        !           461: int f, n;
        !           462: n = atoi(nm+1);
        !           463: if (n<=0 || n>999) return;
        !           464: f = open(scname, 0);
        !           465: if (f>0)
        !           466:        {
        !           467:        ngate=1;
        !           468:        read(f, nread, 2000);
        !           469:        read(f, neval, 2000);
        !           470:        read(f, tscor, 2000);
        !           471:        read(f, sqscr, 2000);
        !           472:        close(f);
        !           473:        }
        !           474: if (r>10) return;
        !           475: if (neval[n]>nread[n] || neval[n]>tscor[n] || tscor[n]>6*neval[n])
        !           476:        neval[n]=tscor[n]=sqscr[n]=0;
        !           477: nread[n]++;
        !           478: if (r>0)
        !           479:        {
        !           480:        neval[n]++;
        !           481:        tscor[n]+=r;
        !           482:        sqscr[n]+= r*r;
        !           483:        }
        !           484: f = creat(scname, 0666);
        !           485: if (f>0)
        !           486:        {
        !           487:        chmod(scname, 0666);
        !           488:        write(f, nread, 2000);
        !           489:        write (f, neval, 2000);
        !           490:        write (f, tscor, 2000);
        !           491:        write (f, sqscr, 2000);
        !           492:        close(f);
        !           493:        }
        !           494: }
        !           495: char *
        !           496: scr(s)
        !           497:        char *s;
        !           498: {
        !           499: int n;
        !           500: static char *pout="[ ]";
        !           501: double x;
        !           502: if (ngate==0) upd (" 100", 100);
        !           503: n = atoi(s+1);
        !           504: if (neval[n]==0)
        !           505:        return("");
        !           506: x = (double) tscor[n];
        !           507: x /= neval[n];
        !           508: x += 0.5;
        !           509: n = (int) x;
        !           510: if (n<=0 || n>6) return("");
        !           511: pout[1]= n+'0';
        !           512: return(pout);
        !           513: }
        !           514: ctr(s)
        !           515:        char *s;
        !           516: {
        !           517: int n;
        !           518: n = atoi(s+1);
        !           519: return(nread[n]);
        !           520: }
        !           521: int intlevel=0;
        !           522: shorten(s)
        !           523:        char *s;
        !           524: {
        !           525: if (*s!='>') intlevel=0;
        !           526: else
        !           527: intlevel = atoi(s+1);
        !           528: if (intlevel>4) intlevel=4;
        !           529: }
        !           530: interest(s)
        !           531:        char *s;
        !           532: {
        !           533: int n;
        !           534: if (intlevel<=0) return(1);
        !           535: n = atoi(s+1);
        !           536: upd("", 100);
        !           537: if (neval[n]==0) return(0);
        !           538: if ( (double)tscor[n]/neval[n] > intlevel) return(1);
        !           539: return(0);
        !           540: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.