|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <sys/stat.h> ! 3: #define TRACE printf ! 4: #define MPATH "/usr/spool/mail/" ! 5: #define TEMPFILE "/tmp/mbox.new" ! 6: #define OLDSEP "\001\001\n" ! 7: #define NEWSEP "\001\001\001\001\n" ! 8: #define MAXLEN 512 ! 9: #define TRUE 1 ! 10: #define FALSE 0 ! 11: ! 12: main(argc, argv) ! 13: int argc; ! 14: char * argv[]; ! 15: ! 16: { ! 17: struct stat sbuf; ! 18: int status; ! 19: FILE *infile; ! 20: FILE *outfile; ! 21: char filename[66]; ! 22: char fullname[66]; ! 23: char msgline [MAXLEN]; ! 24: int counter = 1; ! 25: typedef short int bool; ! 26: bool justsep; ! 27: char systemcmd [76]; ! 28: ! 29: if(argc == 1){ ! 30: printf("mailbox to convert? "); ! 31: scanf("%s", filename); ! 32: strcpy(fullname, filename); ! 33: }else{ ! 34: if (argc ==2){ ! 35: strcpy(fullname, argv[1]); ! 36: } ! 37: ! 38: if (argc == 3){ ! 39: if(strlen(argv[1]) <= 2){ ! 40: if( (argv[1][0] == 'm') || (argv[1][1] == 'm')){ ! 41: strcpy(fullname,MPATH); ! 42: strcat(fullname,argv[2]); ! 43: }else{ ! 44: printf("invalid argument %s\n", ! 45: argv[1]); ! 46: exit(1); ! 47: } ! 48: }else{ ! 49: printf("Usage: cvmail [-m] filename\n"); ! 50: exit(1); ! 51: } ! 52: } ! 53: } ! 54: ! 55: TRACE("Converting file %s\n", fullname); ! 56: /* get owner & group information */ ! 57: if( status = stat(fullname,&sbuf)){ ! 58: printf("Cannot stat file %s\n",fullname); ! 59: exit(1); ! 60: } ! 61: ! 62: if( (infile = fopen(fullname,"r")) == NULL){ ! 63: printf("Cannot open %s for reading!\n", fullname); ! 64: exit(1); ! 65: } ! 66: ! 67: if( (outfile=fopen(TEMPFILE,"w")) == NULL){ ! 68: printf("Cannot open %s for writing!\n"); ! 69: exit(1); ! 70: } ! 71: ! 72: /* print newsep at top of file */ ! 73: fprintf(outfile,NEWSEP); ! 74: ! 75: /* now loop thorugh the file. Read each line and copy it ! 76: * to the tempfile. If we encounter an oldsep, convert it ! 77: * to a newsep, acutally 2 newseps, since we end a message ! 78: * with a sep and begin a message with a sep. The 2nd sep ! 79: * becomes the start sep for the next msg. ! 80: */ ! 81: ! 82: /* justsep is a boolean use to determine if we just wrote a ! 83: * new msgsep. If we did, then we want to write another to begin ! 84: * the next message. This will prevent 2 seps from appearing ! 85: * at the end of the new file. ! 86: */ ! 87: ! 88: justsep = FALSE; ! 89: while (fgets(msgline,MAXLEN,infile) != NULL){ ! 90: ! 91: if(justsep == TRUE){ ! 92: fprintf(outfile,NEWSEP); ! 93: justsep = FALSE; ! 94: counter++; ! 95: } ! 96: ! 97: if(strcmp(OLDSEP,msgline) == 0){ ! 98: strcpy(msgline,NEWSEP); ! 99: justsep = TRUE; ! 100: } ! 101: ! 102: fputs(msgline, outfile); ! 103: } ! 104: ! 105: /* we're done reading and writing, close the files */ ! 106: fclose(infile); ! 107: fclose(outfile); ! 108: ! 109: /* delete old mailbox */ ! 110: if ( 0 != unlink(fullname)){ ! 111: printf("Could not delete old mailbox. The converted mailbox is at: %s\n",TEMPFILE); ! 112: exit(1); ! 113: } ! 114: ! 115: /* copy new mailbox to old */ ! 116: ! 117: if( 0 != link(TEMPFILE, fullname)){ ! 118: sprintf(systemcmd,"/bin/cp %s %s",TEMPFILE, fullname); ! 119: if ( system(systemcmd) == -1){ ! 120: printf("Could not copy converted mailbox to destination. The "); ! 121: printf("new mailbox is at %s\n",TEMPFILE); ! 122: exit(1); ! 123: } ! 124: } ! 125: ! 126: /* delete temporary mailbox */ ! 127: if ( 0 != unlink(TEMPFILE)){ ! 128: printf("Could not temporary workfile. "); ! 129: printf("Compare %s and %s before manually deleting\n", ! 130: TEMPFILE, fullname); ! 131: printf("before deleting the temporary file %s.\n",TEMPFILE); ! 132: exit(1); ! 133: } ! 134: ! 135: /* change ownership of new file to that of the old file */ ! 136: ! 137: chown(fullname,sbuf.st_uid, sbuf.st_gid); ! 138: chmod(fullname,00644); ! 139: ! 140: printf("Converted %d messages\n", counter); ! 141: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.