GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 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 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 int G_is_location(const char *path)
62 {
63  return test_path_file(path, "PERMANENT/DEFAULT_WIND");
64 }
65 
66 /**
67  * \brief Test if specified directory is mapset.
68  *
69  * \param[in] path Path to directory
70  * \return 1 The directory is mapset
71  * \return 0 The directory is not mapset
72  */
73 int G_is_mapset(const char *path)
74 {
75  return test_path_file(path, "WIND");
76 }
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:147
#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:73
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:61
#define file
Definition: path.h:15
#define access
Definition: unistd.h:7
#define F_OK
Definition: unistd.h:22