#include <stdio.h>
#include <sys/file.h>
#include <ctype.h>
/* a program to parse the course desc file into files by individual
   subjects. */
FILE *fopen(),*outfile;

newfile(crs)
char *crs;
{
char filename[30];

fprintf(outfile,"\n"); /* ending blank line */
if (crs != NULL)
  fclose(outfile);
sprintf(filename,"%s.d",crs);
outfile = fopen(filename,"w");
}

main(argc,argv)
int argc;
char *argv[];
{
char line[200],*cp,firstword[20];
FILE *infile;
int num;

if (argc < 2) {
  printf("need to give the input file path \n"); 
  exit(); }

infile = fopen(argv[1],"r");
if (!infile)
  {
    printf("Couldn't open input file %s\n",argv[1]);
    exit();
  }
outfile = fopen("tmp.d","a");

while (fgets(line,100,infile) != NULL)
  {
 /*   printf("line = <%s>\n",line); */
  if (strcmp(line,"\n") == 0) {
    for(; strcmp(line,"\n") == 0 ; fgets(line,100,infile)); 
    cp = (char *) index(line,' ');
    if ((cp) && isdigit(line[0]))
      {
	strncpy(firstword,line,(cp - line));
        printf("title ? = <%s>\n",line); 
	firstword[cp-line] = '\0';
	newfile(firstword);
	printf("file = <%s>\n",firstword);
      }
    else
      {
	fclose(outfile);
	outfile = fopen("tmp.d","a");
      }
  }
  fprintf(outfile,"%s",line);

}    
fclose(outfile);
fclose(infile);

}
