stat.c

Go to the documentation of this file.
00001 /*                         S T A T . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 /** @addtogroup bu_log */
00022 /*@{*/
00023 /** @file stat.c
00024  *
00025  *  Support routine for identifying whether files and directories
00026  *  exist or not.
00027  *
00028  *  @author
00029  *      Christopher Sean Morrison
00030  *
00031  * @par  Source -
00032  *      The U. S. Army Research Laboratory
00033  * @n   Aberdeen Proving Ground, Maryland  21005-5068  USA
00034  *
00035  */
00036 static const char RCS_stat[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/stat.c,v 14.8 2006/08/31 23:16:39 lbutler Exp $";
00037 
00038 #include "common.h"
00039 
00040 #include <stdio.h>
00041 
00042 #ifdef HAVE_UNISTD_H
00043 #  include <unistd.h>
00044 #endif
00045 #ifdef HAVE_SYS_STAT_H
00046 #  include <sys/stat.h>
00047 #endif
00048 
00049 #include "machine.h"
00050 #include "bu.h"
00051 
00052 
00053 /**
00054  *                      B U _ F I L E _ E X I S T S
00055  *
00056  *  @return     1       The given filename exists.
00057  *  @return     0       The given filename does not exist.
00058  */
00059 int
00060 bu_file_exists(const char *path)
00061 {
00062     struct stat sbuf;
00063 
00064     if (bu_debug) {
00065         printf("Does %s exist? ", path);
00066     }
00067 
00068     if( path == NULL )  return 0;                       /* FAIL */
00069 
00070     /* defined in unistd.h */
00071 #if defined(HAVE_ACCESS) && defined(F_OK)
00072 #  define bu_file_exists_method 1
00073     if( access( path, F_OK )  == 0 ) {
00074         if (bu_debug) {
00075             printf("YES\n");
00076         }
00077         return 1;       /* OK */
00078     }
00079 #endif
00080 
00081     /* does it exist as a filesystem entity? */
00082 #if defined(HAVE_STAT)
00083 #  define bu_file_exists_method 1
00084     if( stat( path, &sbuf ) == 0 ) {
00085         if (bu_debug) {
00086             printf("YES\n");
00087         }
00088         return 1;       /* OK */
00089     }
00090 #endif
00091 
00092 #ifndef bu_file_exists_method
00093 #  error "Do not know how to check whether a file exists on this system"
00094 #endif
00095 
00096     if (bu_debug) {
00097         printf("NO\n");
00098     }
00099     return 0;                                   /* FAIL */
00100 }
00101 /*@}*/
00102 /*
00103  * Local Variables:
00104  * mode: C
00105  * tab-width: 8
00106  * c-basic-offset: 4
00107  * indent-tabs-mode: t
00108  * End:
00109  * ex: shiftwidth=4 tabstop=8
00110  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6