}
static FILE *find_file_dir(char *file, char *dirname) {    char path[PATH_MAX+1];    char fullpath[PATH_MAX + 1];    size_t len;    int num_magics = sizeof(magics) / sizeof(*magics);    int i;    struct dirent *dp;    DIR *dfd;    char fname[MAXNAMLEN + 1], filename[MAXNAMLEN + 1], *sptr;    /* combine dirname and file parameters into a single full file path */    /* (Note: the file parameter itself may contain path information too) */    len = strlen(dirname);    if (len == 0 || strcmp(dirname, "./") == 0)	strcpy(fullpath, file);    else {	if (dirname[len - 1] == '/')	    sprintf(fullpath, "%s%s", dirname, file);	else	    sprintf(fullpath, "%s/%s", dirname, file);    }    /* see if the file exists under that exact name (with no additional extension) */    if (file_exists(fullpath)) {	/* yes, try to open it, and return the resulting file pointer */        return fopen_compressed(fullpath, NULL);    }	    /* now separate the full path into path and filename components */    sptr = strrchr(fullpath, '/');    if (sptr != NULL) {	/* get the path information */	len = (size_t)(sptr - fullpath);	strncpy(path, fullpath, len);	path[len] = '\0';	/* special case for "./" */	if (!strcmp(path, "."))	    strcpy(path, "./");	/* filename is everything following the final '/' */	strcpy(filename, sptr + 1);    }    else {	/* there is no path information */	strcpy(path, "./");	strcpy(filename, fullpath);    }    /* save length of file name that must be matched */    len = strlen(filename);    /* open the directory */    if ((dfd = opendir(path)) == NULL) {	/* error -- can't open directory path */	return NULL;    }    /* read directory entries until we find a match */    strcpy(fname, "");    while (strlen(fname) == 0 && (dp = readdir(dfd)) != NULL) {	/* does name from directory match the caller's name in the first len characters? */	if ((strncmp(filename, dp->d_name, len)) == 0) {	    /* yes -- file name must be an exact match, or followed by "_" or "." */	    if (strlen(dp->d_name) == len) {		/* exact match -- point to string terminator for search of magics array */		sptr = dp->d_name + len;	    }	    else if (dp->d_name[len] == '_' || dp->d_name[len] == '.') {		/* find the last "." (if any) in full file name after the required match */		sptr = strrchr(dp->d_name + len, '.');		if (sptr == NULL) {		    /* no ".", point to string terminator */		    sptr = dp->d_name + strlen(dp->d_name);		}	    }	    else {		/* this file is not a candidate */		continue;	    }	}        /* see if the extension matches something we recognize */        for (i = 0; i < num_magics; i++) {            if (!strcmp(sptr, magics[i])) {                /* found a matching extension */                strcpy(fname, dp->d_name);                break;	    }	}    }    /* close the directory */    closedir(dfd);    /* did we find a matching file? */    if (strlen(fname) > 0) {	/* yes, build the complete path */	if (!strcmp(path, "./"))	    strcpy(fullpath, fname);	else	    sprintf(fullpath, "%s/%s", path, fname);		/* open the file and return its file pointer */	return fopen_compressed(fullpath, NULL);    }    /* no match found */    return NULL;}