int mbx_write(char *mailbox, int md, FILE *fp, int id, long last, long pos, off_t stop, int mapping, int noisy) { register int i, j, size; off_t start; long off; register char *cp; char buffer[BUFSIZ]; off = (long) lseek (md, (off_t) 0, SEEK_CUR); j = strlen (mmdlm1); if (write (md, mmdlm1, j) != j) return NOTOK; start = lseek (md, (off_t) 0, SEEK_CUR); size = 0; fseek (fp, pos, SEEK_SET); while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) { i = strlen (buffer); for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++) continue; for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++) continue; if (write (md, buffer, i) != i) return NOTOK; pos += (long) i; if (mapping) for (cp = buffer; i-- > 0; size++) if (*cp++ == '\n') size++; } stop = lseek (md, (off_t) 0, SEEK_CUR); j = strlen (mmdlm2); if (write (md, mmdlm2, j) != j) return NOTOK; if (mapping) map_write (mailbox, md, id, last, start, stop, off, size, noisy); return OK; }
main() { char t[1000],s[100]; int line; printf("enter line:"); scanf("%s",t); printf("enter search string:"); scanf("%s",s); line=stringdex(s,t); printf("location:%d",line); }
int main (int argc, char const* argv[]) { char line[MAXLINE]; int found = 0; while (get_line(line, MAXLINE) > 0) if (stringdex(line, pattern) >= 0) { printf("%s",line); found++; } return found; }
int mbx_copy (char *mailbox, int mbx_style, int md, int fd, int mapping, char *text, int noisy) { int i, j, size; off_t start, stop; long pos; char *cp, buffer[BUFSIZ]; FILE *fp; pos = (long) lseek (md, (off_t) 0, SEEK_CUR); size = 0; switch (mbx_style) { case MMDF_FORMAT: default: j = strlen (mmdlm1); if (write (md, mmdlm1, j) != j) return NOTOK; start = lseek (md, (off_t) 0, SEEK_CUR); if (text) { i = strlen (text); if (write (md, text, i) != i) return NOTOK; for (cp = text; *cp++; size++) if (*cp == '\n') size++; } while ((i = read (fd, buffer, sizeof(buffer))) > 0) { /* valgrind noticed that stringdex depends on null termination. */ buffer[i] = '\0'; for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++) continue; for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++) continue; if (write (md, buffer, i) != i) return NOTOK; if (mapping) for (cp = buffer; i-- > 0; size++) if (*cp++ == '\n') size++; } stop = lseek (md, (off_t) 0, SEEK_CUR); j = strlen (mmdlm2); if (write (md, mmdlm2, j) != j) return NOTOK; if (mapping) map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy); return (i != NOTOK ? OK : NOTOK); case MBOX_FORMAT: if ((j = dup (fd)) == NOTOK) return NOTOK; if ((fp = fdopen (j, "r")) == NULL) { close (j); return NOTOK; } start = lseek (md, (off_t) 0, SEEK_CUR); /* If text is given, we add it to top of message */ if (text) { i = strlen (text); if (write (md, text, i) != i) return NOTOK; for (cp = text; *cp++; size++) if (*cp == '\n') size++; } for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) { /* * Check the first line, and make some changes. */ if (j == 0 && !text) { /* * Change the "Return-Path:" field (if in first line) * back to "From ". */ if (!strncmp (buffer, "Return-Path:", 12)) { char tmpbuffer[BUFSIZ]; char *tp, *ep, *fp; strncpy(tmpbuffer, buffer, sizeof(tmpbuffer)); ep = tmpbuffer + 13; if (!(fp = strchr(ep + 1, ' '))) fp = strchr(ep + 1, '\n'); tp = dctime(dlocaltimenow()); snprintf (buffer, sizeof(buffer), "From %.*s %s", (int)(fp - ep), ep, tp); } else if (!strncmp (buffer, "X-Envelope-From:", 16)) { /* * Change the "X-Envelope-From:" field * (if first line) back to "From ". */ char tmpbuffer[BUFSIZ]; char *ep; strncpy(tmpbuffer, buffer, sizeof(tmpbuffer)); ep = tmpbuffer + 17; snprintf (buffer, sizeof(buffer), "From %s", ep); } else if (strncmp (buffer, "From ", 5)) { /* * If there is already a "From " line, * then leave it alone. Else we add one. */ char tmpbuffer[BUFSIZ]; char *tp, *ep; strncpy(tmpbuffer, buffer, sizeof(tmpbuffer)); ep = "nobody@nowhere"; tp = dctime(dlocaltimenow()); snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer); } } /* * If this is not first line, and begins with * "From ", then prepend line with ">". */ if (j != 0 && strncmp (buffer, "From ", 5) == 0) { write (md, ">", 1); size++; } i = strlen (buffer); if (write (md, buffer, i) != i) { fclose (fp); return NOTOK; } if (mapping) for (cp = buffer; i-- > 0; size++) if (*cp++ == '\n') size++; } if (write (md, "\n", 1) != 1) { fclose (fp); return NOTOK; } if (mapping) size += 2; fclose (fp); lseek (fd, (off_t) 0, SEEK_END); stop = lseek (md, (off_t) 0, SEEK_CUR); if (mapping) map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy); return OK; } }