GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-299a28a7d0
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  size_t len = strlen(path) + strlen(file) + 2;
29  buf = (char *)G_malloc(len);
30  snprintf(buf, len, "%s/%s", path, file);
31 
32  ret = access(buf, F_OK);
33  G_free(buf);
34 
35  if (ret == 0)
36  return 1;
37 
38  return 0;
39 }
40 
41 /**
42 
43  * \brief Test if specified directory is GISBASE.
44  *
45  * \param[in] path Path to directory
46  * \return 1 The directory is GISBASE
47  * \return 0 The directory is not GISBASE
48  */
49 
50 int G_is_gisbase(const char *path)
51 {
52  return test_path_file(path, "etc/element_list");
53 }
54 
55 /**
56  * \brief Test if specified directory is location.
57  *
58  * \param[in] path Path to directory
59  * \return 1 The directory is location
60  * \return 0 The directory is not location
61  */
62 
63 int G_is_location(const char *path)
64 {
65  return test_path_file(path, "PERMANENT/DEFAULT_WIND");
66 }
67 
68 /**
69  * \brief Test if specified directory is mapset.
70  *
71  * \param[in] path Path to directory
72  * \return 1 The directory is mapset
73  * \return 0 The directory is not mapset
74  */
75 
76 int G_is_mapset(const char *path)
77 {
78  return test_path_file(path, "WIND");
79 }
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:76
int G_is_gisbase(const char *path)
Test if specified directory is GISBASE.
Definition: is.c:50
int G_is_location(const char *path)
Test if specified directory is location.
Definition: is.c:63
#define file
Definition: path.h:15
#define access
Definition: unistd.h:7
#define F_OK
Definition: unistd.h:22