home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume14 / mkkey / part01 / bkey.c next >
Encoding:
C/C++ Source or Header  |  1990-07-26  |  1.7 KB  |  83 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXLINES 2000
  4.  
  5. extern int fseek(), fscanf(), sscanf();
  6. extern char *malloc();
  7. extern char *fgets();
  8. extern int strncmp();
  9.  
  10. main(argc, argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.     FILE *fp, *fopen();
  15.     unsigned int i,j;
  16.         char *array[MAXLINES];
  17.         char a[2][90], b[2][90];
  18.     int c;
  19.         int len = 0;
  20.  
  21.  
  22.     j=i=0;
  23.     if(argc == 1){
  24.         printf("Usage: %s file\n",argv[0]);
  25.         exit(1);
  26.     }
  27.     if((fp = fopen(argv[1], "r")) == 0){
  28.         printf("%s: Can't open %s\n",argv[0],argv[1]);
  29.         exit(1);
  30.     }
  31. /* make a pass through the file to determine how many lines there are */
  32.     while((c=fgetc(fp)) != EOF){
  33.         if(c == '\n')
  34.             i++;
  35.     }
  36.         if(i > MAXLINES){
  37.         printf("%s: To many lines to read\n", argv[0]);
  38.         exit(1);
  39.     }
  40.     for(j=0; j <= i; j++){
  41.         if((array[j]=malloc(90)) == 0){
  42.             printf("Can't allocate memory\n");
  43.                     exit(1);
  44.         }
  45.     }
  46. /* go back to the start of the file */
  47.     fseek(fp,0,0);
  48. /* start reading the file and placing each line into array */
  49.     j=0;
  50.     while((fgets(array[j],90,fp)) != 0){
  51.         len=strlen(array[j]);
  52.         array[j][len-1] = '\0';  /* zap the newline */
  53.         j++;
  54.     }
  55.         fclose(fp);
  56.     for(j=0; j < i; j++){
  57.         sscanf(array[j],"%s %[0-9A-Za-z. \t]",a[0],a[1]);
  58.         if(strlen(array[j]) > 65){
  59.             printf("%s\n", array[j]);
  60.             continue;
  61.         }
  62.         if(strncmp(array[j+1],a[0],strlen(a[0])) == 0){
  63.             sscanf(array[j+1],"%s%[0-9A-Za-z. \t]",b[0],b[1]);
  64.             if(strlen(array[j]) + strlen(b[1]) > 75){
  65.                 printf("%s\n", array[j]);
  66.                 printf("%s: %s\n",b[0],b[1]);
  67.             } else {
  68.                                 if(b[1][0] == ' '){
  69.                     printf("%s%s\n", array[j],b[1]);
  70.                 } else {
  71.                     printf("%s %s\n", array[j],b[1]);
  72.                 }
  73.             }
  74.             j++;
  75.         } else {
  76.             printf("%s\n", array[j]);
  77.  
  78.         }
  79.             
  80.     }
  81.     exit(0);
  82. }
  83.