DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: C File split program on UNIX

  1. #1
    tworden Guest

    C File split program on UNIX


    Hi All,

    I am trying to write a file that will split a users sendmail mailbox on UnixWare
    into its separate mail messages.

    From is the start of a new message and Content-Length: tells how many characters
    to the next message.

    I have written a program and it compiles, but it doesn't do anything :).


    It has been years since I have written a program in C so laugh all you want
    at the method I am using and feel free to tell me what I should be doing
    differently.

    My main question is, does anyone see anything glaringly apparent to a real
    C programmer why my program doesn't do anything?

    Thanks,

    Tracy


    /*

    splitmail.c 3/17/01 Tracy Worden

    This file reads a mail file and seperates its individual mail messages into
    sepe
    rate files
    named awis1.mail awis2.mail and so on.

    */
    #ident "@(#) mailsplit v1.0a 03/17/01 Tracy Worden seperates a mail file."
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define FROMTEXT "From "
    #define FROMTEXTSIZE 5
    #define CONTENTTEXT "Content-Length:"
    #define CONTENTTEXTSIZE 15
    #define MAXLCHUNK 256

    main(argc,argv)
    int argc;
    char *argv[];
    {
    FILE *filein;
    char filein_nm[160];
    FILE *fileout;
    FILE *fileerr;
    char fileout_nm[160];
    char fileval[160];
    char ch;
    int c = 0;
    int counter = 0;
    char fromline[MAXLCHUNK];
    char contentline[MAXLCHUNK];
    char lchunk[MAXLCHUNK];
    char holdstring[MAXLCHUNK];
    char numstring[MAXLCHUNK];
    int Counter = 0;
    int iFirst = 0;
    int iOKNext = 1;
    int iMore = 0;
    int x = 0;
    int cnt = 0;
    int iContent = 0;
    char *str;

    printf("starting\n");
    strcpy(filein_nm,argv[1]);

    if((fileerr=fopen("splitmail.err","w"))==NULL) {
    printf("Cannot open error file splitmail.err. Possible permission error.\n"
    );
    return 0;
    }

    if(argc<2) {
    fprintf(fileerr,"Syntax: splitmail input_file \n");
    return 0;
    }
    if( ((strcmp(argv[1],"-?")) == 0) || ((strcmp(argv[1],"?")) == 0) ) {
    fprintf(fileerr,"Syntax: stripmail input_file\n");
    return 0;
    }

    if((filein=fopen(filein_nm,"r"))==NULL) {
    fprintf(fileerr,"Cannot open input file: '%s'. Possible permission error.\n
    ",filein_nm);
    return 0;
    }

    fprintf(fileerr,"after initial file opens\n");
    /*while (fgets(lchunk,MAXLCHUNK,filein) !=NULL && lchunk[0] != '\n') {*/
    while (fgets(lchunk,MAXLCHUNK,filein) !=NULL) {
    fprintf(fileerr,"in the while\n");
    strcpy(holdstring,lchunk);
    /* I am looking for From and if I find it I know I need to start a new
    message, but only if
    this is the first time we find a From or we have passed the end of
    the content */
    *(holdstring + FROMTEXTSIZE) = '\0';
    if (strcmp(holdstring,FROMTEXT) && iOKNext == 1) {
    fprintf(fileerr,"in the find of From\n");
    iOKNext = 0;
    if (iFirst == 0 ) {
    iFirst = 1;
    }
    else {
    //Close the first file before we open the second
    fflush(NULL);
    fclose(fileout);
    }
    //We have the start of the new file
    Counter++;
    sprintf(fileout_nm,"awis%d.mail",counter);
    printf("output file: '%s'\n",fileout_nm);
    if((fileout=fopen(fileout_nm,"w"))==NULL) {
    fprintf(stderr,"Cannot open output file: '%s'. Possible permission
    erro
    r.\r\n",fileout_nm);
    return 0;
    }
    fputs(lchunk,fileout);
    }
    else {
    fputs(lchunk,fileout);
    strcpy(holdstring,lchunk);
    /* Here I am looking for the number after the Content-Length */
    *(holdstring + CONTENTTEXTSIZE) = '\0';
    if (strcmp(holdstring,CONTENTTEXT)) {
    cnt = 0;
    str = strpbrk(lchunk,":");
    while(*str != '\0') {
    str++;
    if (isdigit(*str)) {
    *(numstring + cnt) = *str;
    cnt++;
    }
    }
    iContent = atoi(numstring);
    //Get the number and save to iContent
    for (x=1;x=iContent;x++) {
    ch = getc(filein);
    putc(ch,fileout);
    iOKNext = 1;
    }
    }
    }
    }
    fflush(NULL);
    fclose(filein);
    fclose(fileout);
    fclose(fileerr);
    return 0;
    }


    If you wish to see, here is a sample of the mailbox.

    From tworden@emc-inc.com Fri Mar 16 16:21:47 2001
    Return-Path: tworden@emc-inc.com
    Received: from emc-inc.com (qmailr@emc-inc.com)
    Received: (qmail 10192 invoked from network); 17 Mar 2001 01:08:46 -0000
    Received: from unknown (HELO tworden.emc-inc.com)
    Message-Id: <>
    X-Mailer: QUALCOMM Windows Eudora Version 5.0.2
    Date: Fri, 16 Mar 2001 16:29:20 -0800
    To: tworden@emc-inc.com
    From: Tracy Worden <tworden@emc-inc.com>
    Subject: test
    Cc:
    Mime-Version: 1.0
    Content-Type: multipart/mixed;
    boundary="=====================_26811132==_"
    Status:
    X-Status:
    X-SCO-PAD: XXXXXX
    X-SCO-UID: 3
    Content-Length: 1509

    --=====================_26811132==_
    Content-Type: text/plain; charset="us-ascii"; format=flowed

    test
    --=====================_26811132==_
    Content-Type: text/plain; charset="us-ascii"
    Content-Disposition: attachment; filename="actionpoint.txt"

    <input type=hidden name="whichform" value="screening">
    <input type=hidden name="action" value="">
    <input type=hidden name="rcn_uic" value="<%=trim(wval)%>">
    <input type=hidden name="document_year" value="<%=trim(yval)%>">
    <input type=hidden name="rcn_no" value="<%=trim(zval)%>">
    <input type=hidden name="source_acty" value="<%=trim(xval)%>">
    <table width="100%" border="0" cellPadding="2" cellSpacing="0">
    <select name="activity">
    <input name="activity_complete_date" maxlength="10" size="20" value="<%if
    FullD
    then %><%=trim(rsDI(7))%><%end if%>">
    <input name="NewProbemNo" size="12" maxlength="8" value="">
    <select name="ProblemNo">
    <select name="VolumeNo">
    <input name="problem_open_date" maxlength="10" size="20" value="<%if FullDI
    the
    %><%=trim(rsDI(7))%><%end if%>">
    <select name="ProblemType">
    <select name="ProblemCat">
    <textarea cols="40" name="problemBrief1" rows="4" wrap="VIRTUAL"><%if FullDP
    thn
    %><%=trim(rsDP(4))%><%end if%></textarea>
    <textarea cols="40" name="problemBrief2" rows="4" wrap="VIRTUAL"><%if FullDP
    thn
    %><%=trim(rsDP(5))%><%end if%></textarea>
    <select name="deficiencystatus">
    <input name="finalized_date" maxlength="10" size="20" value="<%if FullDI
    then %<
    %=trim(rsDI(3))%><%end if%>">

    --=====================_26811132==_--




  2. #2
    Danny Kalev Guest

    Re: C File split program on UNIX

    Well, I do have respect for real C programs but I'm quite surprised that
    your code even compiles. You're using the old-style (circa 20 years
    ago?) function declaration style.
    > main(argc,argv)
    > int argc;
    > char *argv[];


    This should read:
    main(int argc,char *argv[])
    under a modern compiler.
    You also want to check file permission issues.

    Danny

    tworden wrote:
    >
    > Hi All,
    >
    > I am trying to write a file that will split a users sendmail mailbox on UnixWare
    > into its separate mail messages.
    >
    > From is the start of a new message and Content-Length: tells how many characters
    > to the next message.
    >
    > I have written a program and it compiles, but it doesn't do anything :).
    >
    >
    > It has been years since I have written a program in C so laugh all you want
    > at the method I am using and feel free to tell me what I should be doing
    > differently.
    >
    > My main question is, does anyone see anything glaringly apparent to a real
    > C programmer why my program doesn't do anything?
    >
    > Thanks,
    >
    > Tracy
    >
    > /*
    >
    > splitmail.c 3/17/01 Tracy Worden
    >
    > This file reads a mail file and seperates its individual mail messages into
    > sepe
    > rate files
    > named awis1.mail awis2.mail and so on.
    >
    > */
    > #ident "@(#) mailsplit v1.0a 03/17/01 Tracy Worden seperates a mail file."
    > #include <stdio.h>
    > #include <string.h>
    > #include <ctype.h>
    > #include <stdlib.h>
    > #define FROMTEXT "From "
    > #define FROMTEXTSIZE 5
    > #define CONTENTTEXT "Content-Length:"
    > #define CONTENTTEXTSIZE 15
    > #define MAXLCHUNK 256
    >
    > main(argc,argv)
    > int argc;
    > char *argv[];
    > {
    > FILE *filein;
    > char filein_nm[160];
    > FILE *fileout;
    > FILE *fileerr;
    > char fileout_nm[160];
    > char fileval[160];
    > char ch;
    > int c = 0;
    > int counter = 0;
    > char fromline[MAXLCHUNK];
    > char contentline[MAXLCHUNK];
    > char lchunk[MAXLCHUNK];
    > char holdstring[MAXLCHUNK];
    > char numstring[MAXLCHUNK];
    > int Counter = 0;
    > int iFirst = 0;
    > int iOKNext = 1;
    > int iMore = 0;
    > int x = 0;
    > int cnt = 0;
    > int iContent = 0;
    > char *str;
    >
    > printf("starting\n");
    > strcpy(filein_nm,argv[1]);
    >
    > if((fileerr=fopen("splitmail.err","w"))==NULL) {
    > printf("Cannot open error file splitmail.err. Possible permission error.\n"
    > );
    > return 0;
    > }
    >
    > if(argc<2) {
    > fprintf(fileerr,"Syntax: splitmail input_file \n");
    > return 0;
    > }
    > if( ((strcmp(argv[1],"-?")) == 0) || ((strcmp(argv[1],"?")) == 0) ) {
    > fprintf(fileerr,"Syntax: stripmail input_file\n");
    > return 0;
    > }
    >
    > if((filein=fopen(filein_nm,"r"))==NULL) {
    > fprintf(fileerr,"Cannot open input file: '%s'. Possible permission error.\n
    > ",filein_nm);
    > return 0;
    > }
    >
    > fprintf(fileerr,"after initial file opens\n");
    > /*while (fgets(lchunk,MAXLCHUNK,filein) !=NULL && lchunk[0] != '\n') {*/
    > while (fgets(lchunk,MAXLCHUNK,filein) !=NULL) {
    > fprintf(fileerr,"in the while\n");
    > strcpy(holdstring,lchunk);
    > /* I am looking for From and if I find it I know I need to start a new
    > message, but only if
    > this is the first time we find a From or we have passed the end of
    > the content */
    > *(holdstring + FROMTEXTSIZE) = '\0';
    > if (strcmp(holdstring,FROMTEXT) && iOKNext == 1) {
    > fprintf(fileerr,"in the find of From\n");
    > iOKNext = 0;
    > if (iFirst == 0 ) {
    > iFirst = 1;
    > }
    > else {
    > //Close the first file before we open the second
    > fflush(NULL);
    > fclose(fileout);
    > }
    > //We have the start of the new file
    > Counter++;
    > sprintf(fileout_nm,"awis%d.mail",counter);
    > printf("output file: '%s'\n",fileout_nm);
    > if((fileout=fopen(fileout_nm,"w"))==NULL) {
    > fprintf(stderr,"Cannot open output file: '%s'. Possible permission
    > erro
    > r.\r\n",fileout_nm);
    > return 0;
    > }
    > fputs(lchunk,fileout);
    > }
    > else {
    > fputs(lchunk,fileout);
    > strcpy(holdstring,lchunk);
    > /* Here I am looking for the number after the Content-Length */
    > *(holdstring + CONTENTTEXTSIZE) = '\0';
    > if (strcmp(holdstring,CONTENTTEXT)) {
    > cnt = 0;
    > str = strpbrk(lchunk,":");
    > while(*str != '\0') {
    > str++;
    > if (isdigit(*str)) {
    > *(numstring + cnt) = *str;
    > cnt++;
    > }
    > }
    > iContent = atoi(numstring);
    > //Get the number and save to iContent
    > for (x=1;x=iContent;x++) {
    > ch = getc(filein);
    > putc(ch,fileout);
    > iOKNext = 1;
    > }
    > }
    > }
    > }
    > fflush(NULL);
    > fclose(filein);
    > fclose(fileout);
    > fclose(fileerr);
    > return 0;
    > }
    >
    > If you wish to see, here is a sample of the mailbox.
    >
    > From tworden@emc-inc.com Fri Mar 16 16:21:47 2001
    > Return-Path: tworden@emc-inc.com
    > Received: from emc-inc.com (qmailr@emc-inc.com)
    > Received: (qmail 10192 invoked from network); 17 Mar 2001 01:08:46 -0000
    > Received: from unknown (HELO tworden.emc-inc.com)
    > Message-Id: <>
    > X-Mailer: QUALCOMM Windows Eudora Version 5.0.2
    > Date: Fri, 16 Mar 2001 16:29:20 -0800
    > To: tworden@emc-inc.com
    > From: Tracy Worden <tworden@emc-inc.com>
    > Subject: test
    > Cc:
    > Mime-Version: 1.0
    > Content-Type: multipart/mixed;
    > boundary="=====================_26811132==_"
    > Status:
    > X-Status:
    > X-SCO-PAD: XXXXXX
    > X-SCO-UID: 3
    > Content-Length: 1509
    >
    > --=====================_26811132==_
    > Content-Type: text/plain; charset="us-ascii"; format=flowed
    >
    > test
    > --=====================_26811132==_
    > Content-Type: text/plain; charset="us-ascii"
    > Content-Disposition: attachment; filename="actionpoint.txt"
    >
    > <input type=hidden name="whichform" value="screening">
    > <input type=hidden name="action" value="">
    > <input type=hidden name="rcn_uic" value="<%=trim(wval)%>">
    > <input type=hidden name="document_year" value="<%=trim(yval)%>">
    > <input type=hidden name="rcn_no" value="<%=trim(zval)%>">
    > <input type=hidden name="source_acty" value="<%=trim(xval)%>">
    > <table width="100%" border="0" cellPadding="2" cellSpacing="0">
    > <select name="activity">
    > <input name="activity_complete_date" maxlength="10" size="20" value="<%if
    > FullD
    > then %><%=trim(rsDI(7))%><%end if%>">
    > <input name="NewProbemNo" size="12" maxlength="8" value="">
    > <select name="ProblemNo">
    > <select name="VolumeNo">
    > <input name="problem_open_date" maxlength="10" size="20" value="<%if FullDI
    > the
    > %><%=trim(rsDI(7))%><%end if%>">
    > <select name="ProblemType">
    > <select name="ProblemCat">
    > <textarea cols="40" name="problemBrief1" rows="4" wrap="VIRTUAL"><%if FullDP
    > thn
    > %><%=trim(rsDP(4))%><%end if%></textarea>
    > <textarea cols="40" name="problemBrief2" rows="4" wrap="VIRTUAL"><%if FullDP
    > thn
    > %><%=trim(rsDP(5))%><%end if%></textarea>
    > <select name="deficiencystatus">
    > <input name="finalized_date" maxlength="10" size="20" value="<%if FullDI
    > then %<
    > %=trim(rsDI(3))%><%end if%>">
    >
    > --=====================_26811132==_--


  3. #3
    Simon Sellick Guest

    Re: C File split program on UNIX


    Hi Tracey,

    You say that the program compiles and runs but doesn't do anything: can you
    be a bit more specific? Does it process its arguments OK - i.e. do you get
    the expected error messages if you give it incorrect arguments? Any other
    messages?

    Simon.

  4. #4
    tworden Guest

    Re: C File split program on UNIX


    Hi,

    Sorry for not being more specific. If I give my program the command argument
    it is expecting, the program completes, but nothing is printed to the screen
    and no files are created. If I do not give the command argument, the program
    hangs and I have to kill it.

    It does not print any of the printfs to the screen, I would expect the first
    'Starting' printf to print at least.

    Tracy



  5. #5
    tworden Guest

    Re: C File split program on UNIX


    The unixware cc compliler, doesn't seem to care, but thank you I will change
    my main declaration.

    Tracy


  6. #6
    Danny Kalev Guest

    Re: C File split program on UNIX



    tworden wrote:
    >
    > Hi,
    >
    > Sorry for not being more specific. If I give my program the command argument
    > it is expecting, the program completes, but nothing is printed to the screen
    > and no files are created. If I do not give the command argument, the program
    > hangs and I have to kill it.


    can't you run the program under a debugger and step through the code?
    Obviously, something odd is happening if you don't even see the first
    printf() output.
    >
    > It does not print any of the printfs to the screen, I would expect the first
    > 'Starting' printf to print at least.


    Could it be that the program crashes at startup for some reason, before
    it gets a chance to execute? In that case you should find a core dump
    file in the user's directory. Also, make sure you didn't redirect stdin
    and stdout in some way.

    Danny
    >
    > Tracy


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center