11224Skas # 21224Skas 31224Skas #include "rcv.h" 41224Skas #include <sys/stat.h> 51224Skas 61224Skas /* 71224Skas * Mail -- a mail program 81224Skas * 91224Skas * More user commands. 101224Skas */ 111224Skas 12*7584Skurt static char *SccsId = "@(#)cmd2.c 2.9 07/29/82"; 131224Skas 141224Skas /* 151224Skas * If any arguments were given, go to the next applicable argument 161224Skas * following dot, otherwise, go to the next applicable message. 171224Skas * If given as first command with no arguments, print first message. 181224Skas */ 191224Skas 201224Skas next(msgvec) 211224Skas int *msgvec; 221224Skas { 231224Skas register struct message *mp; 241224Skas register int *ip, *ip2; 251224Skas int list[2], mdot; 261224Skas 271224Skas if (*msgvec != NULL) { 281224Skas 291224Skas /* 301224Skas * If some messages were supplied, find the 311224Skas * first applicable one following dot using 321224Skas * wrap around. 331224Skas */ 341224Skas 351224Skas mdot = dot - &message[0] + 1; 361470Skas 371470Skas /* 381470Skas * Find the first message in the supplied 391470Skas * message list which follows dot. 401470Skas */ 411470Skas 421224Skas for (ip = msgvec; *ip != NULL; ip++) 431224Skas if (*ip > mdot) 441224Skas break; 451224Skas if (*ip == NULL) 461224Skas ip = msgvec; 471224Skas ip2 = ip; 481224Skas do { 491224Skas mp = &message[*ip2 - 1]; 501224Skas if ((mp->m_flag & MDELETED) == 0) { 511224Skas dot = mp; 521224Skas goto hitit; 531224Skas } 541470Skas if (*ip2 != NULL) 551470Skas ip2++; 561470Skas if (*ip2 == NULL) 571470Skas ip2 = msgvec; 581224Skas } while (ip2 != ip); 591224Skas printf("No messages applicable\n"); 601224Skas return(1); 611224Skas } 621224Skas 631224Skas /* 641224Skas * If this is the first command, select message 1. 651224Skas * Note that this must exist for us to get here at all. 661224Skas */ 671224Skas 681481Skas if (!sawcom) 691224Skas goto hitit; 701224Skas 711224Skas /* 721224Skas * Just find the next good message after dot, no 731224Skas * wraparound. 741224Skas */ 751224Skas 761224Skas for (mp = dot+1; mp < &message[msgCount]; mp++) 771224Skas if ((mp->m_flag & (MDELETED|MSAVED)) == 0) 781224Skas break; 791224Skas if (mp >= &message[msgCount]) { 801224Skas printf("At EOF\n"); 811224Skas return(0); 821224Skas } 831224Skas dot = mp; 841224Skas hitit: 851224Skas /* 861224Skas * Print dot. 871224Skas */ 881224Skas 891224Skas list[0] = dot - &message[0] + 1; 901224Skas list[1] = NULL; 911224Skas return(type(list)); 921224Skas } 931224Skas 941224Skas /* 955776Skurt * Save a message in a file. Mark the message as saved 965776Skurt * so we can discard when the user quits. 971224Skas */ 981224Skas save(str) 991224Skas char str[]; 1001224Skas { 1015776Skurt 1025776Skurt return(save1(str, 1)); 1035776Skurt } 1045776Skurt 1055776Skurt /* 1065776Skurt * Copy a message to a file without affected its saved-ness 1075776Skurt */ 1085776Skurt copycmd(str) 1095776Skurt char str[]; 1105776Skurt { 1115776Skurt 1125776Skurt return(save1(str, 0)); 1135776Skurt } 1145776Skurt 1155776Skurt /* 1165776Skurt * Save/copy the indicated messages at the end of the passed file name. 1175776Skurt * If mark is true, mark the message "saved." 1185776Skurt */ 1195776Skurt save1(str, mark) 1205776Skurt char str[]; 1215776Skurt { 1221224Skas register int *ip, mesg; 1231224Skas register struct message *mp; 1245776Skurt char *file, *disp, *cmd; 1251224Skas int f, *msgvec, lc, cc, t; 1261224Skas FILE *obuf; 1271224Skas struct stat statb; 1281224Skas 1295776Skurt cmd = mark ? "save" : "copy"; 1301224Skas msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec); 1311224Skas if ((file = snarf(str, &f)) == NOSTR) 1321224Skas return(1); 1331224Skas if (!f) { 1341224Skas *msgvec = first(0, MMNORM); 1351224Skas if (*msgvec == NULL) { 1365776Skurt printf("No messages to %s.\n", cmd); 1371224Skas return(1); 1381224Skas } 1391224Skas msgvec[1] = NULL; 1401224Skas } 1411224Skas if (f && getmsglist(str, msgvec, 0) < 0) 1421224Skas return(1); 1431224Skas if ((file = expand(file)) == NOSTR) 1441224Skas return(1); 1451224Skas printf("\"%s\" ", file); 1461224Skas flush(); 1471224Skas if (stat(file, &statb) >= 0) 1481224Skas disp = "[Appended]"; 1491224Skas else 1501224Skas disp = "[New file]"; 1511224Skas if ((obuf = fopen(file, "a")) == NULL) { 1521224Skas perror(NOSTR); 1531224Skas return(1); 1541224Skas } 1551224Skas cc = lc = 0; 1561224Skas for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) { 1571224Skas mesg = *ip; 1581224Skas touch(mesg); 1591224Skas mp = &message[mesg-1]; 1607573Skurt if ((t = send(mp, obuf, 0)) < 0) { 1611224Skas perror(file); 1621224Skas fclose(obuf); 1631224Skas return(1); 1641224Skas } 1651224Skas lc += t; 1661224Skas cc += msize(mp); 1675776Skurt if (mark) 1685776Skurt mp->m_flag |= MSAVED; 1691224Skas } 1701224Skas fflush(obuf); 1711224Skas if (ferror(obuf)) 1721224Skas perror(file); 1731224Skas fclose(obuf); 1741224Skas printf("%s %d/%d\n", disp, lc, cc); 1751224Skas return(0); 1761224Skas } 1771224Skas 1781224Skas /* 1791224Skas * Write the indicated messages at the end of the passed 1801224Skas * file name, minus header and trailing blank line. 1811224Skas */ 1821224Skas 1831224Skas swrite(str) 1841224Skas char str[]; 1851224Skas { 1861224Skas register int *ip, mesg; 1871224Skas register struct message *mp; 1881224Skas register char *file, *disp; 1891224Skas char linebuf[BUFSIZ]; 1901224Skas int f, *msgvec, lc, cc, t; 1911224Skas FILE *obuf, *mesf; 1921224Skas struct stat statb; 1931224Skas 1941224Skas msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec); 1951224Skas if ((file = snarf(str, &f)) == NOSTR) 1961224Skas return(1); 1971224Skas if ((file = expand(file)) == NOSTR) 1981224Skas return(1); 1991224Skas if (!f) { 2001224Skas *msgvec = first(0, MMNORM); 2011224Skas if (*msgvec == NULL) { 2021224Skas printf("No messages to write.\n"); 2031224Skas return(1); 2041224Skas } 2051224Skas msgvec[1] = NULL; 2061224Skas } 2071224Skas if (f && getmsglist(str, msgvec, 0) < 0) 2081224Skas return(1); 2091224Skas printf("\"%s\" ", file); 2101224Skas flush(); 2111224Skas if (stat(file, &statb) >= 0) 2121224Skas disp = "[Appended]"; 2131224Skas else 2141224Skas disp = "[New file]"; 2151224Skas if ((obuf = fopen(file, "a")) == NULL) { 2161224Skas perror(NOSTR); 2171224Skas return(1); 2181224Skas } 2191224Skas cc = lc = 0; 2201224Skas for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) { 2211224Skas mesg = *ip; 2221224Skas touch(mesg); 2231224Skas mp = &message[mesg-1]; 2241224Skas mesf = setinput(mp); 2251224Skas t = mp->m_lines - 2; 2261224Skas readline(mesf, linebuf); 2271224Skas while (t-- > 0) { 2281224Skas fgets(linebuf, BUFSIZ, mesf); 2291224Skas fputs(linebuf, obuf); 2301224Skas cc += strlen(linebuf); 2311224Skas } 2321224Skas lc += mp->m_lines - 2; 2331224Skas mp->m_flag |= MSAVED; 2341224Skas } 2351224Skas fflush(obuf); 2361224Skas if (ferror(obuf)) 2371224Skas perror(file); 2381224Skas fclose(obuf); 2391224Skas printf("%s %d/%d\n", disp, lc, cc); 2401224Skas return(0); 2411224Skas } 2421224Skas 2431224Skas /* 2441224Skas * Snarf the file from the end of the command line and 2451224Skas * return a pointer to it. If there is no file attached, 2461224Skas * just return NOSTR. Put a null in front of the file 2471224Skas * name so that the message list processing won't see it, 2481224Skas * unless the file name is the only thing on the line, in 2491224Skas * which case, return 0 in the reference flag variable. 2501224Skas */ 2511224Skas 2521224Skas char * 2531224Skas snarf(linebuf, flag) 2541224Skas char linebuf[]; 2551224Skas int *flag; 2561224Skas { 2571224Skas register char *cp; 2581224Skas 2591224Skas *flag = 1; 2601224Skas cp = strlen(linebuf) + linebuf - 1; 2611224Skas 2621224Skas /* 2631224Skas * Strip away trailing blanks. 2641224Skas */ 2651224Skas 2661224Skas while (*cp == ' ' && cp > linebuf) 2671224Skas cp--; 2681224Skas *++cp = 0; 2691224Skas 2701224Skas /* 2711224Skas * Now search for the beginning of the file name. 2721224Skas */ 2731224Skas 2741224Skas while (cp > linebuf && !any(*cp, "\t ")) 2751224Skas cp--; 2761224Skas if (*cp == '\0') { 2771224Skas printf("No file specified.\n"); 2781224Skas return(NOSTR); 2791224Skas } 2801224Skas if (any(*cp, " \t")) 2811224Skas *cp++ = 0; 2821224Skas else 2831224Skas *flag = 0; 2841224Skas return(cp); 2851224Skas } 2861224Skas 2871224Skas /* 2881224Skas * Delete messages. 2891224Skas */ 2901224Skas 2911224Skas delete(msgvec) 2921224Skas int msgvec[]; 2931224Skas { 2941224Skas return(delm(msgvec)); 2951224Skas } 2961224Skas 2971224Skas /* 2981224Skas * Delete messages, then type the new dot. 2991224Skas */ 3001224Skas 3011224Skas deltype(msgvec) 3021224Skas int msgvec[]; 3031224Skas { 3041224Skas int list[2]; 3054493Skurt int lastdot; 3061224Skas 3074493Skurt lastdot = dot - &message[0] + 1; 3081224Skas if (delm(msgvec) >= 0) { 3091224Skas list[0] = dot - &message[0]; 3101224Skas list[0]++; 3114493Skurt if (list[0] > lastdot) { 3124493Skurt touch(list[0]); 3134493Skurt list[1] = NULL; 3144493Skurt return(type(list)); 3154493Skurt } 3164493Skurt printf("At EOF\n"); 3174493Skurt return(0); 3181224Skas } 3191224Skas else { 3201224Skas printf("No more messages\n"); 3211224Skas return(0); 3221224Skas } 3231224Skas } 3241224Skas 3251224Skas /* 3261224Skas * Delete the indicated messages. 3271224Skas * Set dot to some nice place afterwards. 3281224Skas * Internal interface. 3291224Skas */ 3301224Skas 3311224Skas delm(msgvec) 3321224Skas int *msgvec; 3331224Skas { 3341224Skas register struct message *mp; 3351224Skas register *ip, mesg; 3361224Skas int last; 3371224Skas 3381224Skas last = NULL; 3391224Skas for (ip = msgvec; *ip != NULL; ip++) { 3401224Skas mesg = *ip; 3411224Skas touch(mesg); 3421224Skas mp = &message[mesg-1]; 3433319Skas mp->m_flag |= MDELETED|MTOUCH; 3443319Skas mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX); 3451224Skas last = mesg; 3461224Skas } 3471224Skas if (last != NULL) { 3481224Skas dot = &message[last-1]; 3491224Skas last = first(0, MDELETED); 3501224Skas if (last != NULL) { 3511224Skas dot = &message[last-1]; 3521224Skas return(0); 3531224Skas } 3541224Skas else { 3551224Skas dot = &message[0]; 3561224Skas return(-1); 3571224Skas } 3581224Skas } 3591224Skas 3601224Skas /* 3611224Skas * Following can't happen -- it keeps lint happy 3621224Skas */ 3631224Skas 3641224Skas return(-1); 3651224Skas } 3661224Skas 3671224Skas /* 3681224Skas * Undelete the indicated messages. 3691224Skas */ 3701224Skas 3711224Skas undelete(msgvec) 3721224Skas int *msgvec; 3731224Skas { 3741224Skas register struct message *mp; 3751224Skas register *ip, mesg; 3761224Skas 3771224Skas for (ip = msgvec; ip-msgvec < msgCount; ip++) { 3781224Skas mesg = *ip; 3791224Skas if (mesg == 0) 3801224Skas return; 3811224Skas touch(mesg); 3821224Skas mp = &message[mesg-1]; 3831224Skas dot = mp; 3841224Skas mp->m_flag &= ~MDELETED; 3851224Skas } 3861224Skas } 3871224Skas 3881224Skas /* 3891224Skas * Interactively dump core on "core" 3901224Skas */ 3911224Skas 3921224Skas core() 3931224Skas { 3941224Skas register int pid; 3951224Skas int status; 3961224Skas 3971224Skas if ((pid = vfork()) == -1) { 3981224Skas perror("fork"); 3991224Skas return(1); 4001224Skas } 4011224Skas if (pid == 0) { 4027540Skurt sigchild(); 4031224Skas abort(); 4041224Skas _exit(1); 4051224Skas } 4061224Skas printf("Okie dokie"); 4071224Skas fflush(stdout); 4081224Skas while (wait(&status) != pid) 4091224Skas ; 4101224Skas if (status & 0200) 4111224Skas printf(" -- Core dumped\n"); 4121224Skas else 4131224Skas printf("\n"); 4141224Skas } 4154340Skurt 4164340Skurt /* 4174340Skurt * Clobber as many bytes of stack as the user requests. 4184340Skurt */ 4194340Skurt clobber(argv) 4204340Skurt char **argv; 4214340Skurt { 4224340Skurt register int times; 4234340Skurt 4244340Skurt if (argv[0] == 0) 4254340Skurt times = 1; 4264340Skurt else 4274340Skurt times = (atoi(argv[0]) + 511) / 512; 4287561Skurt clob1(times); 4294340Skurt } 4304340Skurt 4314340Skurt /* 4324340Skurt * Clobber the stack. 4334340Skurt */ 4347561Skurt clob1(n) 4354340Skurt { 4364340Skurt char buf[512]; 4374340Skurt register char *cp; 4384340Skurt 4394340Skurt if (n <= 0) 4404340Skurt return; 4414340Skurt for (cp = buf; cp < &buf[512]; *cp++ = 0xFF) 4424340Skurt ; 4437561Skurt clob1(n - 1); 4444340Skurt } 4457573Skurt 4467573Skurt /* 4477573Skurt * Add the given header fields to the ignored list. 4487573Skurt * If no arguments, print the current list of ignored fields. 4497573Skurt */ 4507573Skurt igfield(list) 4517573Skurt char *list[]; 4527573Skurt { 4537573Skurt char field[BUFSIZ]; 4547573Skurt register int h; 4557573Skurt register struct ignore *igp; 4567573Skurt char **ap; 4577573Skurt 4587573Skurt if (argcount(list) == 0) 4597573Skurt return(igshow()); 4607573Skurt for (ap = list; *ap != 0; ap++) { 4617582Skurt if (isign(*ap)) 4627582Skurt continue; 4637573Skurt istrcpy(field, *ap); 4647573Skurt h = hash(field); 4657573Skurt igp = (struct ignore *) calloc(1, sizeof (struct ignore)); 4667573Skurt igp->i_field = calloc(strlen(field) + 1, sizeof (char)); 4677573Skurt strcpy(igp->i_field, field); 4687573Skurt igp->i_link = ignore[h]; 4697573Skurt ignore[h] = igp; 4707573Skurt } 4717573Skurt return(0); 4727573Skurt } 4737573Skurt 4747573Skurt /* 4757573Skurt * Print out all currently ignored fields. 4767573Skurt */ 4777573Skurt igshow() 4787573Skurt { 4797582Skurt register int h, count; 4807573Skurt struct ignore *igp; 4817582Skurt char **ap, **ring; 4827582Skurt int igcomp(); 4837573Skurt 4847582Skurt count = 0; 4857573Skurt for (h = 0; h < HSHSIZE; h++) 4867582Skurt for (igp = ignore[h]; igp != 0; igp = igp->i_link) 4877582Skurt count++; 4887582Skurt if (count == 0) { 4897573Skurt printf("No fields currently being ignored.\n"); 4907582Skurt return(0); 4917582Skurt } 492*7584Skurt ring = (char **) salloc((count + 1) * sizeof (char *)); 4937582Skurt ap = ring; 4947582Skurt for (h = 0; h < HSHSIZE; h++) 4957582Skurt for (igp = ignore[h]; igp != 0; igp = igp->i_link) 4967582Skurt *ap++ = igp->i_field; 4977582Skurt *ap = 0; 4987582Skurt qsort(ring, count, sizeof (char *), igcomp); 4997582Skurt for (ap = ring; *ap != 0; ap++) 5007582Skurt printf("%s\n", *ap); 5017573Skurt return(0); 5027573Skurt } 5037582Skurt 5047582Skurt /* 5057582Skurt * Compare two names for sorting ignored field list. 5067582Skurt */ 5077582Skurt igcomp(l, r) 5087582Skurt char **l, **r; 5097582Skurt { 5107582Skurt 5117582Skurt return(strcmp(*l, *r)); 5127582Skurt } 513