home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Public domain code from the FidoNet C_Echo by Martin Maney
- */
-
- #include <dos.h>
-
- /* isDirectory -- is the path a valid directory?
- **
- ** returns non-zero if yes, zero if no (including errors)
- **
- ** This relies upon DOS's behavior in resolving a pathname: the path part is
- ** parsed and checked, then the filename part is checked against the device
- ** list before the final directory is actually searched. This is crufty but
- ** seems to be deeply ingrained in MS-DOS, and works better on more DOS
- ** versions than any alternative I've found.
- **
- ** If you don't trust this, using "*.*" and _A_SUBDIR to search for any file
- ** or subdirectory should work for all cases except an empty root directory.
- */
-
- int isDirectory(char const *path)
- {
- struct find_t ft;
- char buf[MAX_PATH];
-
- if (*path == 0 || splicePath(buf, MAX_PATH, path, "nul") < 0)
- return 0;
- return _dos_findfirst(buf, 0, &ft) == 0;
- }
-
- #include <string.h>
-
- /* splicePath -- splice a pathname and a filename together
- **
- ** returns the length of the result, or negative on error (too long)
- **
- ** n is the maximum allowable length for the spliced result including NUL
- **
- ** NB: it's prototyped for non-overlapping sources and destination; it WILL
- ** blow up for non-trivial cases if buf == filename; this implementation
- ** will work with buf == path, and this is waranteed (it proved useful).
- */
-
- int splicePath(char *buf, size_t n, char const *path, char const *filename)
- {
- int pl = strlen(path);
- int tl = pl + strlen(filename);
- int addSep = 0;
-
- if (pl != 0 && path[pl-1] != ':' && path[pl-1] != '/' &&
- path[pl-1] != '\\')
- addSep = 1;
-
- if (n < tl + addSep + 1)
- return -1;
- if (buf != path) /* otherwise copy is redundant! */
- strcpy(buf, path);
- if (addSep)
- strcat(buf, "/");
- strcat(buf, filename);
- return tl + addSep;
- }
-