home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CRCDOS.ARJ / FNSTUFF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-12  |  5.5 KB  |  180 lines

  1. /*********************************************************************/
  2. /* Module name: fnstuff.c                                            */
  3. /* Date: 27 Dec 87                                                   */
  4. /* Environment: Turbo C 1.0                                          */
  5. /* Author: R. E. Faith                                               */
  6. /* Notice: Public Domain: The following conditions apply to use:     */
  7. /*         1) No fee shall be charged for distribution.              */
  8. /*         2) Modifications may be made, but authorship information  */
  9. /*            for all contributing authors shall be retained.        */
  10. /*         3) This code may not be included as part of a commercial  */
  11. /*            package.                                               */
  12. /* This program is provided AS IS without any warranty, expressed or */
  13. /* implied, including, but not limited to, fitness for a particular  */
  14. /* purpose.                                                          */
  15. /*********************************************************************/
  16. static char    *__ATH__ =
  17. "@(#)fnstuff.c v1.0 (27Dec87): Public Domain (P) 1987 R. E. Faith";
  18.  
  19. #include <genlib.h>
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <sys/stat.h>
  25. #include <dir.h>
  26. #include <string.h>
  27. #include <dos.h>
  28. #include <ctype.h>
  29.  
  30. /* isroot() returns TRUE if path is a root directory */
  31. int isroot( char *path )
  32. {
  33.     struct stat    buff;
  34.     struct ffblk    ffblk;
  35.     register FILE    *str;
  36.     register char    *filename;
  37.  
  38.     /* If stat will function, the directory is not the root */
  39.     if (!stat( path, &buff ))
  40.         return 0;
  41.  
  42.     /* Look for the self-referential directory -- it won't be in the root */
  43.     if (!findfirst( fnappend( path, "." ), &ffblk, FA_DIREC ))
  44.         return 0;
  45.  
  46.     /* A file can be opened in the root, but not in an invalid directory */
  47.     filename = fnappend( path, "tmp.$$$" );
  48.     if ((str = fopen( filename, "r" )) != NULL) {
  49.         /* Since the test file already exists, this must be the root */
  50.         fclose( str );
  51.         return 1;
  52.     }
  53.     /* The test file does not exist */
  54.     if ((str = fopen( filename, "w" )) != NULL) {
  55.         /* Since file was created, this is the root */
  56.         fclose( str );
  57.         unlink( filename );
  58.         return 1;
  59.     }
  60.     return 0;
  61. }
  62.  
  63. /* hasdotname() returns true if path is a "." or ".." file */
  64. int hasdotname( char *path )
  65. {
  66.     register int    length = strlen( path );
  67.     register char    *last;
  68.  
  69.     if (!length)
  70.         return 0;
  71.  
  72.     last = &path[ length - 1 ];
  73.  
  74.     /* The last character must be a dot */
  75.     if (*last != '.')
  76.         return 0;
  77.     /* If the second to last character is a slash, path is "." */
  78.     if (length == 1 || last[-1] == '/' || last[-1] == '\\')
  79.         return 1;
  80.     /* If the second and third to last characters are dot and */
  81.     /* slash, then path is ".." */
  82.     if (last[-1] != '.')
  83.         return 0;
  84.     if (length == 2 || last[-2] == '/' || last[-2] == '\\')
  85.         return 1;
  86.     return 0;
  87. }
  88.     
  89. /* fnappend() returns a pointer to a static area, with file appended to path. */
  90. /* The arguments are not changed. */
  91. char *fnappend( char *path, char *file )
  92. {
  93.     static char    result[MAXPATH];
  94.     register int    path_len = strlen( path );
  95.     register char    last = path[path_len - 1];
  96.  
  97.     if (path_len) {
  98.         /* If path ends in a slash, adjust path_len */
  99.         if (last == '\\' || last == '/')
  100.             --path_len;
  101.  
  102.         /* If the created path is too long, return a NULL result */
  103.         if (path_len + strlen( file ) + 1 /* for NULL */ >= MAXPATH)
  104.             return NULL;
  105.  
  106.         /* Create the new path in result[] and return a pointer to it */
  107.         strncpy( result, path, path_len );
  108.         result[path_len] = '\0';       /* Because strncpy won't do it */
  109.         strcat( result, "\\" );
  110.         strcat( result, file );
  111.     } else {
  112.         /* If the path is of zero length, then return a copy of file */
  113.         if (strlen( file ) + 1 /* for NULL */ >= MAXPATH)
  114.             return NULL;
  115.         strcpy( result, file );
  116.     }
  117.     return result;
  118. }
  119.  
  120. /* fntomsdos() returns a pointer to path, which is modified and MUST be */
  121. /* at least MAXPATH bytes long. */
  122. char *fntomsdos( char *path )
  123. {
  124.     /* Make these static so they won't be on the stack */
  125.     static char    drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE];
  126.     static char    ext[MAXEXT];
  127.     register int    flags;
  128.     register char    *pt;
  129.  
  130.     /* Be sure path has a terminating null */
  131.     path[MAXPATH - 1] = '\0';
  132.     
  133.     /* First convert to upper case and back-slashes */
  134.     for (pt = path; *pt; ++pt) {
  135.         *pt = toupper( *pt );
  136.         if (*pt == '/')
  137.             *pt = '\\';
  138.         /* Fix possible newlines from fgets() */
  139.         if (*pt == '\n')
  140.             *pt = '\0';
  141.     }
  142.  
  143.     /* The parse the pathname */
  144.     strnset( drive, '\0', MAXDRIVE );
  145.     strnset( dir, '\0', MAXDIR );
  146.     strnset( file, '\0', MAXFILE );
  147.     strnset( ext, '\0', MAXEXT );
  148.     flags = fnsplit( path, drive, dir, file, ext );
  149.  
  150.     /* In MS-DOS, the drive letter with no directory refers to the */
  151.     /* default directory on that drive. */
  152.     if ( (flags & DRIVE) && !(flags & DIRECTORY) ) {
  153.         dir[0] = '\\';
  154.         getcurdir( drive[0] - 'A' + 1, dir+1 );
  155.     }
  156.  
  157.     /* Put the new pathname back together */
  158.     fnmerge( path, drive, dir, file, ext );
  159.     /* stat() can't deal with a directory ending in \\ */
  160.     if (path[strlen( path ) - 1] == '\\' && strlen( path ) > 1)
  161.         path[strlen( path ) - 1] = '\0';
  162.     return path;
  163. }
  164.  
  165. /* fntounix() returns a pointer to path, which is modified and MUST be */
  166. /* at least MAXPATH bytes long. */
  167. char *fntounix( char *path )
  168. {
  169.     register char    *pt;
  170.  
  171.     fntomsdos( path );    /* All the fancy fix up is in fntomsdos() */
  172.     /* Convert to lower case and make all \\'s into /'s */
  173.     for (pt = path; *pt; ++pt) {
  174.         *pt = tolower( *pt );
  175.         if (*pt == '\\')
  176.             *pt = '/';
  177.     }
  178.     return path;
  179. }
  180.