2013-06-10 06:14:09 -04:00
|
|
|
/* flook.c -- implements flook.h */
|
|
|
|
#include "flook.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-19 23:44:18 -04:00
|
|
|
#include "defines.h"
|
2013-06-10 06:14:09 -04:00
|
|
|
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* possible names and paths of help files under different OSes */
|
2013-06-10 06:14:09 -04:00
|
|
|
const char *pathname[] = {
|
2021-08-13 23:45:41 -04:00
|
|
|
#if BSD | USG
|
|
|
|
".emacsrc",
|
|
|
|
"emacs.hlp",
|
|
|
|
# if PKCODE
|
|
|
|
"/usr/global/lib/", "/usr/local/bin/", "/usr/local/lib/",
|
|
|
|
# endif
|
|
|
|
"/usr/local/", "/usr/lib/", ""
|
2013-06-10 06:14:09 -04:00
|
|
|
#endif
|
2019-07-03 20:06:13 -04:00
|
|
|
} ;
|
2013-06-10 06:14:09 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
#define PATHTABLE_SIZE (sizeof pathname / sizeof pathname[ 0])
|
2013-06-10 06:14:09 -04:00
|
|
|
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* does <fname> exist on disk?
|
2013-06-10 06:14:09 -04:00
|
|
|
*
|
|
|
|
* char *fname; file to check for existance
|
|
|
|
*/
|
2021-08-13 23:45:41 -04:00
|
|
|
boolean fexist( const char *fname) {
|
|
|
|
FILE *fp = fopen( fname, "r") ;
|
|
|
|
if( fp == NULL)
|
|
|
|
return FALSE ;
|
|
|
|
else
|
|
|
|
fclose( fp) ;
|
|
|
|
|
|
|
|
return TRUE ;
|
2013-06-10 06:14:09 -04:00
|
|
|
}
|
|
|
|
|
2015-03-26 03:56:33 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* Look up the existence of a file along the normal or PATH environment
|
|
|
|
variable. Look first in the HOME directory if asked and possible.
|
2013-06-10 06:14:09 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
char *fname; base file name to search for
|
|
|
|
boolean hflag; look in the HOME environment variable first?
|
|
|
|
*/
|
|
|
|
char *flook( const char *fname, boolean hflag) {
|
|
|
|
static char fspec[ NSTRING] ; /* full path spec to search */
|
|
|
|
|
|
|
|
int len = sizeof fspec - strlen( fname) - 1 ;
|
|
|
|
if( len < 0)
|
|
|
|
return NULL ;
|
|
|
|
|
|
|
|
#if ENVFUNC
|
|
|
|
if( hflag) {
|
|
|
|
char *home = getenv( "HOME") ; /* path to home directory */
|
|
|
|
if( home != NULL) {
|
|
|
|
if( len > (int) strlen( home) + 1) {
|
|
|
|
/* build home dir file spec */
|
|
|
|
strcpy( fspec, home) ;
|
|
|
|
strcat( fspec, "/") ;
|
|
|
|
strcat( fspec, fname) ;
|
|
|
|
|
|
|
|
/* and try it out */
|
|
|
|
if( fexist( fspec))
|
|
|
|
return fspec ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-19 23:44:18 -04:00
|
|
|
#endif
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* always try the current directory first */
|
|
|
|
if( len >= 0) {
|
|
|
|
strcpy( fspec, fname) ;
|
|
|
|
if( fexist( fspec))
|
|
|
|
return fspec ;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENVFUNC
|
|
|
|
# if USG | BSD
|
|
|
|
# define PATHCHR ':'
|
|
|
|
# else
|
|
|
|
# define PATHCHR ';'
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/* get the PATH variable */
|
|
|
|
char *path = getenv( "PATH") ; /* environmental PATH variable */
|
|
|
|
if( path != NULL)
|
|
|
|
while( *path) {
|
|
|
|
int cnt = len ;
|
|
|
|
/* build next possible file spec */
|
|
|
|
char *sp = fspec ; /* pointer into path spec */
|
|
|
|
while( *path && (*path != PATHCHR)) {
|
|
|
|
if( cnt-- > 0)
|
|
|
|
*sp++ = *path ;
|
|
|
|
|
|
|
|
path += 1 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cnt >= 0) {
|
|
|
|
/* add a terminating dir separator if we need it */
|
|
|
|
if (sp != fspec)
|
|
|
|
*sp++ = '/';
|
|
|
|
*sp = 0;
|
|
|
|
strcat(fspec, fname);
|
|
|
|
|
|
|
|
/* and try it out */
|
|
|
|
if( fexist( fspec))
|
|
|
|
return fspec ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *path == PATHCHR)
|
|
|
|
++path ;
|
|
|
|
}
|
2013-06-10 06:14:09 -04:00
|
|
|
#endif
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* look it up via the old table method */
|
|
|
|
for( unsigned i = 2 ; i < PATHTABLE_SIZE ; i++)
|
|
|
|
if( len >= (int) strlen( pathname[ i])) {
|
|
|
|
strcpy( fspec, pathname[ i]) ;
|
|
|
|
strcat( fspec, fname);
|
2013-06-10 06:14:09 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* and try it out */
|
|
|
|
if( fexist( fspec))
|
|
|
|
return fspec ;
|
|
|
|
}
|
2013-06-10 06:14:09 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
return NULL ; /* no such luck */
|
2013-06-10 06:14:09 -04:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* end of flook.c */
|