home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / SQDEV200.ZIP / SRC / FEXIST.C < prev    next >
C/C++ Source or Header  |  1994-05-23  |  4KB  |  160 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Squish Developers Kit Source, Version 2.00                             *
  4.  *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
  5.  *                                                                         *
  6.  *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
  7.  *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
  8.  *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
  9.  *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
  10.  *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
  11.  *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
  12.  *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
  13.  *  ABLE TO REACH WITH THE AUTHOR.                                         *
  14.  *                                                                         *
  15.  *  You can contact the author at one of the address listed below:         *
  16.  *                                                                         *
  17.  *  Scott Dudley       FidoNet     1:249/106                               *
  18.  *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
  19.  *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
  20.  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
  21.  *                                                                         *
  22.  ***************************************************************************/
  23.  
  24. /*# name=File-exist and directory-searching routines
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #if (defined(__MSDOS__) || defined(OS_2) || defined(NT)) && !defined(__IBMC__)
  33. #include <dos.h>
  34. #endif
  35.  
  36. #include "ffind.h"
  37. #include "prog.h"
  38.  
  39.  
  40. /*
  41. main()
  42. {
  43.   printf("asdfe=%d\n",direxist("c:\\asdfe"));
  44.   printf("blank=%d\n",direxist("c:\\blank"));
  45.   printf("tc=%d\n",direxist("c:\\tc"));
  46.   printf("c:\\=%d\n",direxist("c:\\"));
  47.   printf("d:\\=%d\n",direxist("d:\\"));
  48.   printf("e:\\=%d\n",direxist("e:\\"));
  49.   printf("f:\\=%d\n",direxist("f:\\"));
  50. }
  51. */
  52.  
  53.  
  54. int _fast fexist(char *filename)
  55. {
  56.   FFIND *ff;
  57.  
  58.   ff=FindOpen(filename,0);
  59.  
  60.   if (ff)
  61.   {
  62.     FindClose(ff);
  63.     return TRUE;
  64.   }
  65.   else return FALSE;
  66. }
  67.  
  68. long _fast fsize(char *filename)
  69. {
  70.   FFIND *ff;
  71.   long ret=-1L;
  72.  
  73.   ff=FindOpen(filename, 0);
  74.  
  75.   if (ff)
  76.   {
  77.     ret=ff->ulSize;
  78.     FindClose(ff);
  79.   }
  80.  
  81.   return ret;
  82. }
  83.  
  84. #if defined(__MSDOS__)
  85.   int _fast direxist(char *directory)
  86.   {
  87.     FFIND *ff;
  88.     char *tempstr;
  89.     int ret;
  90.  
  91.     if ((tempstr=(char *)malloc(strlen(directory)+5))==NULL)
  92.       return FALSE;
  93.  
  94.     strcpy(tempstr, directory);
  95.  
  96.     Add_Trailing(tempstr,'\\');
  97.  
  98.     /* Root directory of any drive always exists! */
  99.  
  100.     if ((isalpha(tempstr[0]) && tempstr[1]==':' &&
  101.         ((tempstr[2]=='\0') ||
  102.          (tempstr[2]=='\\' || tempstr[2]=='/') && tempstr[3]=='\0')) ||
  103.         eqstri(tempstr, "\\"))
  104.     {
  105.       ret=TRUE;
  106.     }
  107.     else
  108.     {
  109.       Strip_Trailing(tempstr, '\\');
  110.  
  111.       ff=FindOpen(tempstr, ATTR_SUBDIR | ATTR_HIDDEN | ATTR_READONLY);
  112.  
  113.       ret=(ff != NULL && (ff->usAttr & ATTR_SUBDIR));
  114.  
  115.       if (ff)
  116.         FindClose(ff);
  117.     }
  118.  
  119.     free(tempstr);
  120.     return ret;
  121.  
  122.   }
  123. #else
  124.   #include "uni.h"
  125.  
  126.   int _fast direxist(char *directory)
  127.   {
  128.     int ret;
  129.     char *tempstr;
  130.     size_t l;
  131.  
  132.     if (NULL == (tempstr=(char *)strdup(directory)))
  133.       return FALSE;
  134.  
  135.     /* Root directory of any drive always exists! */
  136.  
  137. #ifdef UNIX
  138.     if (eqstr(tempstr, "/"))
  139. #else
  140.     if ((isalpha(tempstr[0]) && tempstr[1]==':' &&
  141.         (tempstr[2]=='\\' || tempstr[2]=='/') && !tempstr[3]) ||
  142.         eqstr(tempstr, "\\"))
  143. #endif
  144.     {
  145.       free(tempstr);
  146.       return TRUE;
  147.     }
  148.  
  149.     l = strlen(tempstr);
  150.     if( tempstr[l-1] == '\\' || tempstr[l-1] == '/')
  151.       tempstr[l-1] = 0;           /* remove trailing backslash */
  152.  
  153.     ret = !access(tempstr, 0);
  154.  
  155.     free(tempstr);
  156.     return ret;
  157.   }
  158. #endif
  159.  
  160.