GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
is.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/is.c
3  *
4  * \brief GIS Library - Tests for file existence.
5  *
6  * (C) 2001-2014 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author GRASS GIS Development Team
12  *
13  * \date 2001-2014
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <grass/gis.h>
20 
21 static int test_path_file(const char *, const char *);
22 
23 static int test_path_file(const char *path, const char *file)
24 {
25  int ret;
26  char *buf;
27 
28  buf = (char *)G_malloc(strlen(path) + strlen(file) + 2);
29  sprintf(buf, "%s/%s", path, file);
30 
31  ret = access(buf, F_OK);
32  G_free(buf);
33 
34  if (ret == 0)
35  return 1;
36 
37  return 0;
38 }
39 
40 /**
41 
42  * \brief Test if specified directory is GISBASE.
43  *
44  * \param[in] path Path to directory
45  * \return 1 The directory is GISBASE
46  * \return 0 The directory is not GISBASE
47  */
48 
49 int G_is_gisbase(const char *path)
50 {
51  return test_path_file(path, "etc/element_list");
52 }
53 
54 /**
55  * \brief Test if specified directory is location.
56  *
57  * \param[in] path Path to directory
58  * \return 1 The directory is location
59  * \return 0 The directory is not location
60  */
61 
62 int G_is_location(const char *path)
63 {
64  return test_path_file(path, "PERMANENT/DEFAULT_WIND");
65 }
66 
67 /**
68  * \brief Test if specified directory is mapset.
69  *
70  * \param[in] path Path to directory
71  * \return 1 The directory is mapset
72  * \return 0 The directory is not mapset
73  */
74 
75 int G_is_mapset(const char *path)
76 {
77  return test_path_file(path, "WIND");
78 }
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_malloc(n)
Definition: defs/gis.h:94
int G_is_mapset(const char *path)
Test if specified directory is mapset.
Definition: is.c:75
int G_is_gisbase(const char *path)
Test if specified directory is GISBASE.
Definition: is.c:49
int G_is_location(const char *path)
Test if specified directory is location.
Definition: is.c:62
#define file
Definition: path.h:15