1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
/*
* u-libfile.c -- file operations for UNIX versions of translators.
*
* UnixOpenLibFile () - Open a library file. Looks in the following
* directories:
* - Looks in current directory.
* - if TROFFCVT_LIB_DIR environment variable is set, looks in directory
* named by it.
* - Looks in executable program's directory, if UnixSetProgPath() has
* been called.
* - Looks in compiled-in library directory, LIBDIR.
*
* Exception: if file is an absolute pathname, look only for file as named.
*
* Returns NULL if file cannot be found and opened.
*/
#include <stdio.h>
#include "memmgr.h"
#include "tcgen.h"
#include "tcunix.h"
extern char *getenv ();
static char *progPath = (char *) NULL;
void
UnixSetProgPath (char *path)
{
int i, j, n;
n = strlen (path);
for (j = -1, i = 0; i < n; i++)
{
if (path[i] == '/')
j = i;
}
if (j < 0) /* no slash found */
{
path = ".";
j = 1;
}
if ((progPath = Alloc (j + 1)) != (char *) NULL)
{
(void) strncpy (progPath, path, j);
progPath[j] = '\0';
}
}
FILE *
UnixOpenLibFile (char *file, char *mode)
{
FILE *f;
char buf[bufSiz];
char *p;
if ((f = fopen (file, mode)) != (FILE *) NULL)
{
return (f);
}
/* if abolute pathname, give up, else look in library */
if (file[0] == '/')
{
return ((FILE *) NULL);
}
if ((p = getenv ("TROFFCVT_LIB_DIR")) != (char *) NULL)
{
sprintf (buf, "%s/%s", p, file);
if ((f = fopen (buf, mode)) != (FILE *) NULL)
return (f);
}
if (progPath != (char *) NULL)
{
sprintf (buf, "%s/%s", progPath, file);
if ((f = fopen (buf, mode)) != (FILE *) NULL)
return (f);
}
sprintf (buf, "%s/%s", PROJLIBDIR, file);
f = fopen (buf, mode); /* NULL if it fails */
return (f);
}
|