⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 io.c

📁 linux下阅读源码的好工具
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. *//* ./glimpse/index/io.c */#include "glimpse.h"#include <autoconf.h>#include <stdio.h>#include <sys/stat.h>#include <errno.h>#include <dlfcn.h>extern char INDEX_DIR[MAX_LINE_LEN];extern int memory_usage;#include "utils.c"int	REAL_INDEX_BUF = DEF_REAL_INDEX_BUF,	MAX_ALL_INDEX = DEF_MAX_ALL_INDEX,	FILEMASK_SIZE = DEF_FILEMASK_SIZE,	REAL_PARTITION = DEF_REAL_PARTITION;/* Escapes single quotes in "original" string with backquote (\) s.t. it can be passed on to the shell as a file name: returns its second argument for printf *//* Called before passing any argument to the system() routine in glimpse or glimspeindex source code *//* Works only if the new name is going to be passed as argument to the shell within two ''s */char *escapesinglequote(original, new)	char	*original, *new;{	char	*oldnew = new;	while (*original != '\0') {		if (*original == '\'') {			*new ++ = '\'';	/* close existing ' : this guy will be a part of a file name starting from a ' */			*new ++ = '\\';	/* add escape character */			*new ++ = '\'';	/* add single quote from original here */		}		*new ++ = *original ++; /* start the real single quote to continute existing file name if *original was ' */	}	*new = *original;	return oldnew;}/* --------------------------------------------------------------------get_array_of_lines()input: an input filename, address of the table, maximum number of entriesof the table, and a overflow handling flag.output: a set of strings in the table.when overflow is ON, the function returns after the table is filled.otherwise the function will exit if overflow occurs.In normal return, the function returns the number of entries read.----------------------------------------------------------------------*/get_array_of_lines(inputfile, table, max_entry, overflow_ok)char *inputfile;char **table[];int  max_entry;  /* max number of entries in the table */int  overflow_ok;   /* flag for handling overflow */{	int  tx=0;    /* index for table */	FILE *file_in;	unsigned char buffer[MAX_NAME_BUF];	char *np; 		int  line_length;	int  num_lines;	if((file_in = fopen(inputfile, "r")) == NULL) {		if (overflow_ok) return 0;		fprintf(stderr, "can't open for reading: %s\n", inputfile);		exit(2);	}	fgets(buffer, MAX_NAME_BUF, file_in);	sscanf(buffer, "%d", &num_lines);	if ((num_lines < 0) || (num_lines > MaxNum24bPartition)) {		fclose(file_in);		if (overflow_ok) return 0;		fprintf(stderr, "Error in reading: %s\n", inputfile);		exit(2);	}	while(fgets(buffer, MAX_NAME_BUF, file_in)) {		line_length = strlen(buffer);		if (line_length == 1) continue;		buffer[line_length-1] = '\0';  /* discard the '\n' */#if	BG_DEBUG		np = (char *) my_malloc(sizeof(char) * (line_length + 2));#else	/*BG_DEBUG*/		np = (char *) my_malloc(sizeof(char) * (line_length + 2));#endif	/*BG_DEBUG*/		if(np == NULL) {		    int	i=0;		    fclose(file_in);		    for (i=0; i<tx; i++) {#if	BG_DEBUG			memory_usage -= (strlen(LIST_GET(table, i)) + 2);#endif	/*BG_DEBUG*/			if (LIST_GET(table, i) != NULL) {				my_free(LIST_GET(table, i), 0);				LIST_SUREGET(table, i) = NULL;			}		    }		    if (overflow_ok) {			fclose(file_in);			return 0;		    }		    fprintf(stderr, "malloc failure in get_array_of_lines\n");		    exit(2);		}		LIST_ADD(table, tx, np, char*);		tx ++;		/* table[tx++] = (unsigned char *)np; */		strcpy(np, buffer);		if(tx > max_entry) {		    fclose(file_in);		    if(overflow_ok) {			fclose(file_in);			return(tx);		    }		    fprintf(stderr, "overflow in get_array_of_lines()\n");		    exit(2);		}	}	fclose(file_in);	return(tx);   /* return number of lines read */}/* --------------------------------------------------------------------get_table():input: an input filename, address of the table, maximum number of entriesof the table, and a overflow handling flag.output: a set of integers in the table.when overflow_ok is ON, the function returns after the table is filled.otherwise the function will exit if overflow occurs.In normal return, the function returns the number of entries read.----------------------------------------------------------------------*/int get_table(inputfile, table, max_entry, overflow_ok)char *inputfile;int  table[];int  max_entry;int  overflow_ok;{	int  val = 0;	int  c = 0;	FILE *file_in;	int  tx=0;           /* number of entries read */	if((file_in = fopen(inputfile, "r")) == NULL) {		if (overflow_ok) return 0;		fprintf(stderr, "can't open %s for reading\n", inputfile);		exit(2);	}	while((c = getc(file_in)) != EOF) {		val = c << 24;		if ((c = getc(file_in)) == EOF) break;		val |= c << 16;		if ((c = getc(file_in)) == EOF) break;		val |= c << 8;		if ((c = getc(file_in)) == EOF) break;		val |= c;		if(tx > max_entry) {			if(!overflow_ok) {			    fprintf(stderr, "in get_table: table overflow\n");			    exit(2);			}			break;		}		table[tx++] = val;	}	fclose(file_in);	return(tx);}get_index_type(s, dashn, num, attr, delim)char s[];int *dashn, *num, *attr;char delim[];{	FILE *fp = fopen(s, "r");	char buf[MAX_LINE_LEN];	*dashn = *num = *attr = 0;	*delim = '\0';	if (fp == NULL) return 0;	fscanf(fp, "%s\n%%%d\n%%%d%s\n", buf, num, attr, delim);	/* printf("get_index_type(): %s %d %d %s\n", buf, num, attr, delim); */	fclose(fp);	if (strstr(buf, "1234567890")) *dashn = ON;	return *num;}/* Read offset from srcbuf first so that you can use it with srcbuf=destbuf */get_block_numbers(srcbuf, destbuf, partfp)	unsigned char *srcbuf, *destbuf;	FILE *partfp;{	int	offset, pat_size;	static int printederror = 0;	/* Does not do caching of blocks seen so far: done in OS hopefully */	offset = (srcbuf[0] << 24) |		(srcbuf[1] << 16) |		(srcbuf[2] << 8) |		(srcbuf[3]);	pat_size = decode32b(offset);	if (-1 == fseek(partfp, pat_size, 0)) {		if (!printederror) {			fprintf(stderr, "Warning! Error in the format of the index!\n");			printederror = 1;		}	}	destbuf[0] = '\n';	destbuf[1] = '\0';	destbuf[2] = '\0';	destbuf[3] = '\0';	if (fgets(destbuf, REAL_INDEX_BUF - MAX_WORD_BUF - 1, partfp) == NULL) {		destbuf[0] = '\n';		destbuf[1] = '\0';		destbuf[2] = '\0';		destbuf[3] = '\0';	}}int num_filter=0;int filter_len[MAX_FILTER];CHAR *filter[MAX_FILTER];CHAR *filter_command[MAX_FILTER];struct stat filstbuf;/* Prototype for filter entry point in a shared library -- CV 9/14/99 */typedef int (*FILTER_FUNC)(FILE *, FILE *);/* Holds addresses of entry points -- CV 9/14/99 */static FILTER_FUNC filter_func[MAX_FILTER];/* Holds shared library handles, one per filter and shared library    -- CV 9/14/99 */static void *filter_handle[MAX_FILTER];/* Loads shared filter libraries. The criterion, upon which this   function decides whether a filter is an external program or a shared   library is as follows: If the name of the filter command ends in ".so",   it assumes that the filter is a shared library; otherwise it assumes   an external program.    apply_filters() finds out what kind the ith filter is used by   looking at filter_func[i]. If it is non-null, it is a pointer to   the entry point in a shared library. Otherwise, the filter is an   external program. -- CV 9/14/99*/#ifndef	RTLD_NOW/* dummy function for old system such as SunOS-4.1.3 */static void load_dyn_filters(void) {}#elsestatic void load_dyn_filters(void){    int i, len, success;    char *so_pos;    char *error;    memset(filter_func, '\0', sizeof (filter_func));    memset(filter_handle, '\0', sizeof (filter_handle));    for (i = 0; i < num_filter; i++) {	success = 1;	len = strlen(filter_command[i]);	if (len > 4) {	    /* find location in string where .so suffix should be */	    so_pos = filter_command[i] + len - 3;	    if (strcmp(so_pos, ".so") == 0) {		/* fprintf(stderr, "Loading %s\n", filter_command[i]); */		if ((filter_handle[i] = dlopen(filter_command[i],					       RTLD_NOW)) == NULL) {		    success = 0;		    error = dlerror();		}		if (success) {		    filter_func[i] = dlsym(filter_handle[i], "filter_func");		    if ((error = dlerror()) != NULL)			success = 0;		}		if (! success) {		    fputs("Warning: Error while loading filter\n", stderr);		    fputs(error, stderr); 		    if (filter_handle[i] != NULL) {                       /* lib was already loaded  when error happened */			dlclose(filter_handle[i]);		    } 		    /* Since an error occurred, should we disable the                       filter entirely for this command?  If yes, HOW? -- CV */		}	    }	}    }}#endif	/* RTLD_NOW *//* Applies a filter to a single file, based on whether the filter is a   command or in a shared library.   filter_number is the filter's index in the filter table.   Returns 0 on success, passes on error code from the filter otherwise   -- CV 9/14/99 */static int apply_one_filter(int filter_number, const char *in_name,		     const char *out_name){    int ret = 0;    if (filter_func[filter_number] != NULL) { /* in shared library */	FILE *in = NULL, *out = NULL;	in = fopen(in_name, "r");	if (in != NULL) out = fopen(out_name, "w"); 	if (in == NULL || out == NULL) ret = errno;	else ret = (*filter_func[filter_number])(in, out);	if (out != NULL) fclose(out);	if (in != NULL) fclose(in);    }     else { /* in external program */	char escaped_in[MAX_LINE_LEN], escaped_out[MAX_LINE_LEN];		char command[2 * MAX_LINE_LEN];	escapesinglequote(in_name, escaped_in);	escapesinglequote(out_name, escaped_out);	sprintf(command, "exec %s '%s' > '%s'", filter_command[filter_number],		escaped_in, escaped_out);	/* FIXME: use snprintf() where available to avoid stupid	   buffer overruns. But security risk should be low, because	   no user-supplied data goes in here. -- CV 9/14/99. */	ret = system(command);    }    return ret;}/* Copies a file verbatim. It could be a little better optimized by   using fread() and fwrite(), but speed is not critical here.   Returns 0 on success, error code otherwise.   -- CV 9/14/99*/static int copy_file(const char *source, const char *destination) {    FILE *src = NULL, *dest = NULL;    int c, ret = 0;    src = fopen(source, "r");    if (src != NULL) dest = fopen(destination, "w");    if (src == NULL || dest == NULL) 	ret = errno;    else {	/* FIXME: Better error checking here. But it's not really critical,	   because these are only temporary files.  -- CV 9/14/99 */	while ((c = fgetc(src)) != EOF)	    fputc(c, dest);    }    if (dest != NULL) fclose(dest);    if (src != NULL) fclose(src);    return ret;}read_filters(index_dir, dofilter)char	*index_dir;int	dofilter;{    int len;    int patlen;    int patpos;    int commandpos;    FILE *filterfile;    char filterbuf[MAX_LINE_LEN];    char tempbuf[MAX_LINE_LEN];    char s[MAX_LINE_LEN];    num_filter = 0;    memset(filter, '\0', sizeof(CHAR *) * MAX_FILTER);    memset(filter_command, '\0', sizeof(CHAR *) * MAX_FILTER);    memset(filter_len, '\0', sizeof(int) * MAX_FILTER);    if (!dofilter) return;    sprintf(s, "%s/%s", index_dir, FILTER_FILE);    filterfile = fopen(s, "r");    if(filterfile == NULL) {	/* fprintf(stderr, "can't open filter file %s\n", s); -- no need */	num_filter = 0;    }    else if (fstat(fileno(filterfile), &filstbuf) == -1) {	num_filter = 0;    }    else {	while((num_filter < MAX_FILTER) && fgets(filterbuf, MAX_LINE_LEN, filterfile)) {		if ((len = strlen(filterbuf)) < 1) continue;		filterbuf[len-1] = '\0';		commandpos = 0;		while ((commandpos < len) && ((filterbuf[commandpos] == ' ') || (filterbuf[commandpos] == '\t'))) commandpos ++;	/* leading spaces */		if (commandpos >= len) continue;		if (filterbuf[commandpos] == '\'') {			commandpos ++;			patpos = commandpos;			patlen = 0;			while (commandpos < len) {				if (filterbuf[commandpos] == '\\') {					commandpos += 2;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -