files.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright (c) 1994, 1996-1997, 2000, 2003 MEDICAL RESEARCH COUNCIL
  3. All rights reserved
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1 Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2 Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. 3 Neither the name of the MEDICAL RESEARCH COUNCIL, THE LABORATORY OF
  12. MOLECULAR BIOLOGY nor the names of its contributors may be used to endorse or
  13. promote products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "io_lib_config.h"
  28. #endif
  29. #include "cram/misc.h"
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. /* Alliant's Concentrix <sys/stat.h> is hugely deficient */
  33. /* Define things we require in this program */
  34. /* Methinks S_IFMT and S_IFDIR aren't defined in POSIX */
  35. #ifndef S_ISDIR
  36. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  37. #endif /*!S_ISDIR*/
  38. #ifndef S_ISREG
  39. #define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
  40. #endif /*!S_ISREG*/
  41. int is_directory(char * fn)
  42. {
  43. struct stat buf;
  44. if ( stat(fn,&buf) ) return 0;
  45. return S_ISDIR(buf.st_mode);
  46. }
  47. int is_file(char * fn)
  48. {
  49. struct stat buf;
  50. if ( stat(fn,&buf) ) return 0;
  51. return S_ISREG(buf.st_mode);
  52. }
  53. int file_exists(char * fn)
  54. {
  55. struct stat buf;
  56. return ( stat(fn,&buf) == 0);
  57. }
  58. int file_size(char * fn)
  59. {
  60. struct stat buf;
  61. if ( stat(fn,&buf) != 0) return 0;
  62. return buf.st_size;
  63. }