ftp.nice.ch/pub/next/tools/usenet/splitter.N.b.tar.gz#/splitter/splitter.c

This is splitter.c in view mode; [Download] [Up]

#include <stdio.h>
#include <strings.h>
#include <sys/param.h>
#define MAX_PREFIX_LENGTH 6
#define MAX_FILENAME_LENGTH 70		/* change here for longer filenames */

void main(int argc,char *argv[])
{
  char filename[MAXPATHLEN+1];
  char from_line[2048];
  char subject_line[2048];
  char copy_filename[MAX_FILENAME_LENGTH+1];	/* max limited for tar */
  char buffer[2048];
  char prefix[MAX_PREFIX_LENGTH+2];
  char from[20];
  char subject[20], subject1[20];
  char reply[20],reply1[20], reply2[20], reply3[20];
  int i,j,subject_length,prefix_length,stop_flag,total,offset,max_length;
  FILE *infile,*outfile;
  
  /* load test header strings, account for missing spaces, etc. */
  sprintf(from, "From:");
  sprintf(subject, "Subject: ");
  sprintf(subject1, "Subject:");
  sprintf(reply, "Subject: Re: ");
  sprintf(reply1, "Subject:Re:");
  sprintf(reply2, "Subject: Re:");
  sprintf(reply3, "Subject:Re: ");
  
  if(argc > 2){	      /* check for correct no. of arguments */
  /* get desired prefix for all generated files */
  strcpy(prefix, argv[1]);
  
  for(i=2; i<argc; i++){	/* loop until all files have been processed */
    strcpy(filename, argv[i]);
    
    /* open archived file for reading */
    if((infile = fopen(filename, "r")) != NULL){
      total = 0;
      printf("Successfully opened %s for input...now splitting\n", filename);
      stop_flag = 0;
      fgets(from_line, 2047, infile); /* read first line */

      /* continue reading until an error or EOF */
      do{
        copy_filename[0] = '\0';	/* reset filename */
        /* add prefix */
	prefix_length = strlen(prefix);
	for(j=0; j<prefix_length; j++)
	  copy_filename[j] = prefix[j];
	copy_filename[j] = '\0';	/* null termination */
	strcat(copy_filename, "_");	/* concatenate underscore */

        fgets(subject_line, 2047, infile); /* read subject line */
        subject_length = strlen(subject_line);
	
	/* the test order is important here */
	if(strncmp(subject_line, subject1, 8) == 0){
	  offset = 8;
	}
	if(strncmp(subject_line, subject, 9) == 0){
	  offset = 9;
	}
	if(strncmp(subject_line, reply2, 12) == 0){
	  offset = 12;
	}
	if(strncmp(subject_line, reply, 13) == 0){
	  offset = 13;
	}
	if(strncmp(subject_line, reply1, 11) == 0){
	  offset = 11;
	}
	if(strncmp(subject_line, reply3, 12) == 0){
	  offset = 12;
	}

	
	/* replace 'bad' characters with a '_' */
	max_length = MAX_FILENAME_LENGTH- prefix_length+ offset- 1;
	if((subject_length- 1+ prefix_length- offset) < MAX_FILENAME_LENGTH)
	  max_length = subject_length-1;
	
        for(j=offset; j<max_length; j++){
	  if(subject_line[j] != '.' && subject_line[j] != '[' && 
	     subject_line[j] != ']' && subject_line[j] != ' ' &&
	     subject_line[j] != '/')
	    strncat(copy_filename, &subject_line[j], 1);
	  else
	    strcat(copy_filename, "_");
	}
	      
	/* open an output file with prefix+modified_subject as name */
	/* note this appends to a file with identical subject line */
	outfile = fopen(copy_filename, "a");
	total++;	

	/* write the from line, subject lines, and content to outfile */
	fputs(from_line, outfile);
	fputs(subject_line, outfile);
	
	for(;;){
	  if(fgets(buffer, 2047, infile) != NULL){
	    if(strncmp(buffer, from, 5) != 0){
	      fputs(buffer, outfile);
	    }
	    else{
	      fclose(outfile);
	      strcpy(from_line, buffer);
	      break;		/* next message found */
	    }
	  }
	  else{
            stop_flag = 1;	/* EOF reached */
	    fclose(infile);
	    printf("  -- %d messages.\n", total);
	    break;
	  }
	}
      }
      while(stop_flag == 0);	/* continue until EOF */
    }
    else{
      printf("Error opening %s for input\n");
      printf("  -- continuing with next file...\n");
    }
  }
  }
  else{
    printf("Form of command:\n");
    printf("splitter prefix filename1 filename2 ...\n");
    printf("(prefix can be up to 6 characters)\n");
  }
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.