staden-lg/src/indexseqlibs/excludewords.c

104 lines
1.7 KiB
C
Raw Normal View History

2021-12-04 13:07:58 +08:00
/*--- excludewords ---*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 100
static FILE *ex;
static FILE *in;
static FILE *out;
static int line_comp(char *ex, char *in, int l)
/*
2023-04-14 02:02:58 +08:00
** Compare exclude line with cinline
2021-12-04 13:07:58 +08:00
*/
{
return strncmp(ex,in+11,l);
}
static void process()
/*
** Do the work
*/
{
char exline[MAXLINE];
2023-04-14 02:02:58 +08:00
char cinline[MAXLINE];
2021-12-04 13:07:58 +08:00
char *exok, *inok;
int compare;
exok = fgets(exline,MAXLINE,ex);
2023-04-14 02:02:58 +08:00
inok = fgets(cinline,MAXLINE,in);
2021-12-04 13:07:58 +08:00
while (exok && inok) {
compare = line_comp(exok,inok,MAXLINE);
if (compare < 0)
exok = fgets(exline,MAXLINE,ex);
else if (compare > 0) {
2023-04-14 02:02:58 +08:00
fprintf(out,"%s",cinline);
inok = fgets(cinline,MAXLINE,in);
2021-12-04 13:07:58 +08:00
} else
2023-04-14 02:02:58 +08:00
inok = fgets(cinline,MAXLINE,in);
2021-12-04 13:07:58 +08:00
}
2023-04-14 02:02:58 +08:00
while (inok = fgets(cinline,MAXLINE,in))
fprintf(out,"%s",cinline);
2021-12-04 13:07:58 +08:00
}
int main(int argc, char *argv[])
/*
** Open files
*/
{
if (argc < 2 || argc > 4) {
fprintf(stderr,"Usage: excludewords word_file [file_in [file_out]]\n");
exit(2);
}
/* set default files */
in = stdin; out = stdout;
switch (argc) {
case 4:
if ( (out = fopen(argv[3],"w")) == NULL ) {
fprintf(stderr,"excludewords: Cannot open file %s for output\n",argv[3]);
exit(1);
}
case 3:
if ( (in = fopen(argv[2],"r")) == NULL) {
fprintf(stderr,"excludewords: Cannot open file %s for input\n",argv[2]);
exit(1);
}
case 2:
if ( (ex = fopen(argv[1],"r")) == NULL) {
fprintf(stderr,"excludewords: Cannot open exclude file %s\n",argv[1]);
exit(1);
}
}
process();
switch (argc) {
case 4: fclose(out);
case 3: fclose(in);
case 2: fclose(ex);
}
return 0;
}