This is search.c in view mode; [Download] [Up]
/*
* The search routine takes a directory list, separated by PATHSEP, and
* tries to open a file. Null directory components indicate current
* directory. if the file SUBDIR exists and the file is a font file,
* it checks for the file in a subdirectory named the same as the font name.
* Returns the open file descriptor if ok, else NULL.
*/
#include "structures.h" /* The copyright notice in that file is included too! */
#define DIRSEP '/'
#define PATHSEP ':'
#include <stdio.h> /* for FILE and fopen */
#ifdef SYSV
#include <string.h>
#define MAXPATHLEN (128)
#else /* ~SYSV */
#include <sys/param.h> /* for MAXPATHLEN */
#include <strings.h> /* for strlen */
#endif /* ~SYSV */
#include <pwd.h>
/*
*
* We hope MAXPATHLEN is enough -- only rudimentary checking is done!
*/
extern void error() ;
#ifdef DEBUG
extern integer debug_flag;
#endif /* DEBUG */
char *mfmode ;
extern int actualdpi ;
#define mode "r"
FILE *
search(path, file)
char *path, *file ;
{
extern char *getenv(), *newstring() ;
register char *nam ; /* index into fname */
register FILE *fd ; /* file desc of file */
char fname[MAXPATHLEN] ; /* to store file name */
static char *home = 0 ; /* home is where the heart is */
if (*file == DIRSEP) { /* if full path name */
if ((fd=fopen(file,mode)) != NULL)
return(fd) ;
else
return(NULL) ;
}
do {
/* copy the current directory into fname */
nam = fname;
/* copy till PATHSEP */
if (*path == '~') {
char *p = nam ;
path++ ;
while (*path && *path != PATHSEP && *path != DIRSEP)
*p++ = *path++ ;
*p = 0 ;
if (*nam == 0) {
if (home == 0) {
if (home = getenv("HOME"))
home = newstring(home) ;
else
home = "." ;
}
strcpy(fname, home) ;
} else {
struct passwd *pw = getpwnam(fname) ;
if (pw)
strcpy(fname, pw->pw_dir) ;
else
error("no such user") ;
}
nam = fname + strlen(fname) ;
}
while (*path != PATHSEP && *path) *nam++ = *path++;
*nam = 0 ;
if (nam == fname) *nam++ = '.'; /* null component is current dir */
if (*file != '\0') {
*nam++ = DIRSEP; /* add separator */
(void)strcpy(nam,file); /* tack the file on */
}
else
*nam = '\0' ;
/* belated check -- bah! */
if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
error("! overran allocated storage in search()");
if ((fd=fopen(fname,mode)) != NULL)
return(fd);
/* skip over PATHSEP and try again */
} while (*(path++));
return(NULL);
} /* end search */
FILE *
pksearch(path, file, n, dpi)
char *path, *file ;
char *n ;
halfword dpi ;
{
extern char *getenv(), *newstring() ;
register char *nam ; /* index into fname */
register FILE *fd ; /* file desc of file */
char fname[MAXPATHLEN] ; /* to store file name */
static char *home = 0 ; /* home is where the heart is */
int sub ;
if (*file == DIRSEP) { /* if full path name */
if ((fd=fopen(file,mode)) != NULL)
return(fd) ;
else
return(NULL) ;
}
do {
/* copy the current directory into fname */
nam = fname;
sub = 0 ;
/* copy till PATHSEP */
if (*path == '~') {
char *p = nam ;
path++ ;
while (*path && *path != PATHSEP && *path != DIRSEP)
*p++ = *path++ ;
*p = 0 ;
if (*nam == 0) {
if (home == 0) {
if (home = getenv("HOME"))
home = newstring(home) ;
else
home = "." ;
}
strcpy(fname, home) ;
} else {
struct passwd *pw = getpwnam(fname) ;
if (pw)
strcpy(fname, pw->pw_dir) ;
else
error("no such user") ;
}
nam = fname + strlen(fname) ;
}
/* copy till PATHSEP */
while (*path != PATHSEP && *path) {
if (*path == '%') {
sub = 1 ;
path++ ;
switch(*path) {
case 'd': sprintf(nam, "%d", dpi) ; break ;
case 'f': strcpy(nam, n) ; break ;
case 'm': if (mfmode == 0)
if (actualdpi == 300) mfmode = "imagen" ;
else if (actualdpi == 400) mfmode = "nexthi" ;
else if (actualdpi == 635) mfmode = "linolo" ;
else if (actualdpi == 1270) mfmode = "linohi" ;
else if (actualdpi == 2540) mfmode = "linosuper" ;
if (mfmode == 0)
error("! MF mode not set, but used in pk path") ;
strcpy(nam, mfmode) ;
break ;
case 'p': strcpy(nam, "pk") ; break ;
case '%': strcpy(nam, "%") ; break ;
default: error("! bad format character in pk path") ;
}
nam = fname + strlen(fname) ;
if (*path)
path++ ;
} else
*nam++ = *path++;
}
if (nam == fname) *nam++ = '.'; /* null component is current dir */
if (sub == 0 && *file) {
*nam++ = DIRSEP ;
strcpy(nam, file) ;
} else
*nam = '\0' ;
/* belated check -- bah! */
if (strlen(fname) + 1 > MAXPATHLEN)
error("! overran allocated storage in search()");
if ((fd=fopen(fname,mode)) != NULL)
return(fd);
/* skip over PATHSEP and try again */
} while (*(path++));
return(NULL);
} /* end search */
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.