Category : Utilities for DOS and Windows Machines
Archive   : CRCDOS3.ZIP
Filename : FNSTUFF.C

 
Output of file : FNSTUFF.C contained in archive : CRCDOS3.ZIP

/*********************************************************************/
/* Module name: fnstuff.c */
/* Date: 27 Dec 87 */
/* Environment: Turbo C 1.0 */
/* Author: R. E. Faith */
/* Notice: Public Domain: The following conditions apply to use: */
/* 1) No fee shall be charged for distribution. */
/* 2) Modifications may be made, but authorship information */
/* for all contributing authors shall be retained. */
/* 3) This code may not be included as part of a commercial */
/* package. */
/* This program is provided AS IS without any warranty, expressed or */
/* implied, including, but not limited to, fitness for a particular */
/* purpose. */
/*********************************************************************/
static char *__ATH__ =
"@(#)fnstuff.c v1.0 (27Dec87): Public Domain (P) 1987 R. E. Faith";

#include

#include
#include
#include
#include
#include
#include
#include
#include

/* isroot() returns TRUE if path is a root directory */
int isroot( char *path )
{
struct stat buff;
struct ffblk ffblk;
register FILE *str;
register char *filename;

/* If stat will function, the directory is not the root */
if (!stat( path, &buff ))
return 0;

/* Look for the self-referential directory -- it won't be in the root */
if (!findfirst( fnappend( path, "." ), &ffblk, FA_DIREC ))
return 0;

/* A file can be opened in the root, but not in an invalid directory */
filename = fnappend( path, "tmp.$$$" );
if ((str = fopen( filename, "r" )) != NULL) {
/* Since the test file already exists, this must be the root */
fclose( str );
return 1;
}
/* The test file does not exist */
if ((str = fopen( filename, "w" )) != NULL) {
/* Since file was created, this is the root */
fclose( str );
unlink( filename );
return 1;
}
return 0;
}

/* hasdotname() returns true if path is a "." or ".." file */
int hasdotname( char *path )
{
register int length = strlen( path );
register char *last;

if (!length)
return 0;

last = &path[ length - 1 ];

/* The last character must be a dot */
if (*last != '.')
return 0;
/* If the second to last character is a slash, path is "." */
if (length == 1 || last[-1] == '/' || last[-1] == '\\')
return 1;
/* If the second and third to last characters are dot and */
/* slash, then path is ".." */
if (last[-1] != '.')
return 0;
if (length == 2 || last[-2] == '/' || last[-2] == '\\')
return 1;
return 0;
}

/* fnappend() returns a pointer to a static area, with file appended to path. */
/* The arguments are not changed. */
char *fnappend( char *path, char *file )
{
static char result[MAXPATH];
register int path_len = strlen( path );
register char last = path[path_len - 1];

if (path_len) {
/* If path ends in a slash, adjust path_len */
if (last == '\\' || last == '/')
--path_len;

/* If the created path is too long, return a NULL result */
if (path_len + strlen( file ) + 1 /* for NULL */ >= MAXPATH)
return NULL;

/* Create the new path in result[] and return a pointer to it */
strncpy( result, path, path_len );
result[path_len] = '\0'; /* Because strncpy won't do it */
strcat( result, "\\" );
strcat( result, file );
} else {
/* If the path is of zero length, then return a copy of file */
if (strlen( file ) + 1 /* for NULL */ >= MAXPATH)
return NULL;
strcpy( result, file );
}
return result;
}

/* fntomsdos() returns a pointer to path, which is modified and MUST be */
/* at least MAXPATH bytes long. */
char *fntomsdos( char *path )
{
/* Make these static so they won't be on the stack */
static char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE];
static char ext[MAXEXT];
register int flags;
register char *pt;

/* Be sure path has a terminating null */
path[MAXPATH - 1] = '\0';

/* First convert to upper case and back-slashes */
for (pt = path; *pt; ++pt) {
*pt = toupper( *pt );
if (*pt == '/')
*pt = '\\';
/* Fix possible newlines from fgets() */
if (*pt == '\n')
*pt = '\0';
}

/* The parse the pathname */
strnset( drive, '\0', MAXDRIVE );
strnset( dir, '\0', MAXDIR );
strnset( file, '\0', MAXFILE );
strnset( ext, '\0', MAXEXT );
flags = fnsplit( path, drive, dir, file, ext );

/* In MS-DOS, the drive letter with no directory refers to the */
/* default directory on that drive. */
if ( (flags & DRIVE) && !(flags & DIRECTORY) ) {
dir[0] = '\\';
getcurdir( drive[0] - 'A' + 1, dir+1 );
}

/* Put the new pathname back together */
fnmerge( path, drive, dir, file, ext );
/* stat() can't deal with a directory ending in \\ */
if (path[strlen( path ) - 1] == '\\' && strlen( path ) > 1)
path[strlen( path ) - 1] = '\0';
return path;
}

/* fntounix() returns a pointer to path, which is modified and MUST be */
/* at least MAXPATH bytes long. */
char *fntounix( char *path )
{
register char *pt;

fntomsdos( path ); /* All the fancy fix up is in fntomsdos() */
/* Convert to lower case and make all \\'s into /'s */
for (pt = path; *pt; ++pt) {
*pt = tolower( *pt );
if (*pt == '\\')
*pt = '/';
}
return path;
}