home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
FORTH
/
FLEX.ARC
/
MSDOS.C
< prev
next >
Wrap
Text File
|
1988-10-10
|
4KB
|
185 lines
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define PUBLIC
#define PRIVATE static
PUBLIC convert_path(path)
char *path;
{
for ( ; *path; ++path) {
if (*path == '\\') {
*path = '/';
}
}
}
PUBLIC char *drop_drive(path)
char path[];
{
char *src, *dest;
if ((path[0] != '\0') && (path[1] == ':')) {
for (dest = path, src = &path[2]; *src; ) {
*dest++ = *src++;
}
}
return (path);
}
/*****************************************************************************
*
* drop_extension -- Drop the extension off a path and filename
* This may not always work well with UN*X names.
*
*****************************************************************************/
PUBLIC drop_extenstion(pathname)
char *pathname;
{
int len;
char *s;
char *lastdot;
if ((len = strlen(pathname)) == 0)
return;
if (!strcmp(pathname, ".") || !strcmp(pathname, ".."))
return;
for (s = pathname + len - 1, lastdot = NULL; s >= pathname; --s) {
if ((*s == '\\') || (*s == '/') || (*s == ':'))
break;
if (*s == '.')
lastdot = s;
}
if (s < pathname) {
s = pathname;
} else {
++s; /* move past slash or colon */
}
if (lastdot != NULL && lastdot > pathname) {
*lastdot = '\0'; /* drop extension */
if ((lastdot - s) > 8) {
s[8] = '\0';
}
} else {
if (((pathname + len) - s) > 8) {
s[8] = '\0';
}
}
}
PUBLIC char *drop_path(pathname)
char *pathname;
{
int len;
char *path;
if ((len = strlen(pathname)) > 0) {
for (path = pathname + len - 1; path > pathname; --path) {
if (*path == '\\' || *path == '/')
break;
}
} else {
path = pathname;
}
return (path);
}
/******************************************************************************
******************************************************************************/
#define MAX_FILENAME 20
#define MAX_PATHSIZE 80
#define ENVIRON_SIZE 1000
PUBLIC FILE *localfopen(filename, mode)
char *filename, *mode;
{
return (fopen(drop_path(filename), mode));
}
PUBLIC FILE *variable_fopen(env_var, filename, mode)
char *env_var;
char *filename, *mode;
{
FILE *fp;
char *env, environ[ENVIRON_SIZE];
char name[MAX_FILENAME];
char pathname[MAX_PATHSIZE];
char *dir;
if (env_var == (char *) NULL) {
return ((FILE *) NULL);
}
if (*env_var == '\0') {
return ((FILE *) NULL);
}
if ((env = getenv(env_var)) != NULL) {
strncpy(environ, env, sizeof(environ) - 1);
environ[sizeof(environ) - 1] = '\0';
strncpy(name, drop_path(filename),
sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
for (dir = strtok(environ, ";"); dir != NULL;
dir = strtok((char *) NULL, ";")) {
strncpy(pathname, dir, sizeof(pathname) - 1);
pathname[sizeof(pathname) - 1] = '\0';
strncat(pathname, "/", sizeof(pathname) - 1
- strlen(pathname));
pathname[sizeof(pathname) - 1] = '\0';
strncat(pathname, name, sizeof(pathname) - 1
- strlen(pathname));
pathname[sizeof(pathname) - 1] = '\0';
if ((fp = fopen(pathname, mode)) != (FILE *) NULL) {
return (fp);
}
}
}
return ((FILE *) NULL);
}
FILE *pathfopen(env_var, search_local, filename, mode)
char *env_var;
int search_local;
char *filename, *mode;
{
FILE *fp;
if (tolower(*mode) != 'r') {
return (fopen(filename, mode));
}
if (search_local) {
if ((fp = localfopen(filename, mode)) != NULL) {
return (fp);
}
}
if ((fp = fopen(filename, mode)) != NULL) {
return (fp);
}
if (!search_local) {
if ((fp = localfopen(filename, mode)) != NULL) {
return (fp);
}
}
if ((fp = variable_fopen(env_var, filename, mode)) != NULL) {
return (fp);
}
if ((fp = variable_fopen("PATH", filename, mode)) != NULL) {
return (fp);
}
return ((FILE *) NULL);
}