GRASS 8 Programmer's Manual 8.6.0dev(2026)-b0a6d7703c
Loading...
Searching...
No Matches
open_misc.c
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * MODULE: gis library
4 * AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
5 * SPDX-FileCopyrightText: 2007 Glynn Clements
6 * SPDX-FileCopyrightText: GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * NOTE: Based upon open.c
10 *
11 *****************************************************************************/
12
13#include <grass/config.h>
14#include <errno.h>
15#include <string.h>
16
17#include <unistd.h>
18#include <fcntl.h>
19
20#include <grass/gis.h>
21#include <grass/glocale.h>
22
23#include "gis_local_proto.h"
24
25static int G__open_misc(const char *dir, const char *element, const char *name,
26 const char *mapset, int mode)
27{
28 int fd;
29 char path[GPATH_MAX];
31
33
34 /* READ */
35 if (mode == 0) {
37 if (*mapset && strcmp(xmapset, mapset) != 0) {
38 G_warning(_("G__open_misc(read): mapset <%s> doesn't match "
39 "xmapset <%s>"),
40 mapset, xmapset);
41 return -1;
42 }
43 name = xname;
44 mapset = xmapset;
45 }
46
47 mapset = G_find_file2_misc(dir, element, name, mapset);
48
49 if (!mapset)
50 return -1;
51
52 G_file_name_misc(path, dir, element, name, mapset);
53
54 if ((fd = open(path, 0)) < 0)
55 G_warning("G__open_misc(read): Unable to open '%s': %s", path,
57 return fd;
58 }
59 /* WRITE */
60 if (mode == 1 || mode == 2) {
61 mapset = G_mapset();
63 if (strcmp(xmapset, mapset) != 0) {
65 _("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"),
66 xmapset, mapset);
67 return -1;
68 }
69 name = xname;
70 }
71
72 if (G_legal_filename(name) == -1)
73 return -1;
74
75 G_file_name_misc(path, dir, element, name, mapset);
76 if (mode == 1 || access(path, 0) != 0) {
78 close(creat(path, 0666));
79 }
80
81 if ((fd = open(path, mode)) < 0)
82 G_warning("G__open_misc(write): Unable to open '%s': %s", path,
84 return fd;
85 }
86 return -1;
87}
88
89/*!
90 * \brief open a new database misc file
91 *
92 * The database file <b>element</b> under <b>dir/name</b> in the
93 * current mapset is created and opened for writing (but not reading).
94 * The UNIX open( ) routine is used to open the file. If the file does not
95 * exist, -1 is returned. Otherwise the file is positioned at the end of the
96 * file and the file descriptor from the open( ) is returned.
97 *
98 * \param dir
99 * \param element
100 * \param name
101 * \return int
102 */
103int G_open_new_misc(const char *dir, const char *element, const char *name)
104{
105 return G__open_misc(dir, element, name, G_mapset(), 1);
106}
107
108/*!
109 * \brief open a database misc file for reading
110 *
111 * The database file <b>element</b> under
112 * <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
113 * not for writing).
114 * The UNIX open( ) routine is used to open the file. If the file does not
115 * exist, -1 is returned. Otherwise the file descriptor from the open( ) is
116 * returned.
117 *
118 * \param dir
119 * \param element
120 * \param name
121 * \param mapset
122 * \return int
123 */
124int G_open_old_misc(const char *dir, const char *element, const char *name,
125 const char *mapset)
126{
127 return G__open_misc(dir, element, name, mapset, 0);
128}
129
130/*!
131 * \brief open a database misc file for update
132 *
133 * The database file <b>element</b> under <b>dir/name</b> in the
134 * current mapset is opened for reading and writing.
135 * The UNIX open( ) routine is used to open the file. If the file does not
136 * exist, -1 is returned. Otherwise the file is positioned at the end of the
137 * file and the file descriptor from the open( ) is returned.
138 *
139 * \param dir
140 * \param element
141 * \param name
142 * \return int
143 */
144int G_open_update_misc(const char *dir, const char *element, const char *name)
145{
146 int fd;
147
148 fd = G__open_misc(dir, element, name, G_mapset(), 2);
149 if (fd >= 0)
150 if (lseek(fd, 0L, SEEK_END) == -1) {
151 int err = errno;
152 G_warning(_("File read/write operation failed: %s (%d)"),
153 strerror(err), err);
154 return -1;
155 }
156
157 return fd;
158}
159
160/*!
161 * \brief open a new database misc file
162 *
163 * The database file <b>element</b> under <b>dir/name</b> in the
164 * current mapset is created and opened for writing (but not reading).
165 * The UNIX fopen( ) routine, with "w" write mode, is used to open the file. If
166 * the file does not exist, the NULL pointer is returned. Otherwise the file is
167 * positioned at the end of the file and the file descriptor from the fopen( )
168 * is returned.
169 *
170 * \param dir
171 * \param element
172 * \param name
173 * \return FILE *
174 */
175FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name)
176{
177 int fd;
178
179 fd = G__open_misc(dir, element, name, G_mapset(), 1);
180 if (fd < 0)
181 return (FILE *)0;
182
183 return fdopen(fd, "w");
184}
185
186/*!
187 * \brief open a database misc file for reading
188 *
189 * The database file <b>element</b> under
190 * <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
191 * not for writing).
192 * The UNIX fopen( ) routine, with "r" read mode, is used to open the file. If
193 * the file does not exist, the NULL pointer is returned. Otherwise the file
194 * descriptor from the fopen( ) is returned.
195 *
196 * \param dir
197 * \param element
198 * \param name
199 * \param mapset
200 * \return FILE *
201 */
202FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name,
203 const char *mapset)
204{
205 int fd;
206
207 fd = G__open_misc(dir, element, name, mapset, 0);
208 if (fd < 0)
209 return (FILE *)0;
210
211 return fdopen(fd, "r");
212}
213
214FILE *G_fopen_append_misc(const char *dir, const char *element,
215 const char *name)
216{
217 int fd;
218
219 fd = G__open_misc(dir, element, name, G_mapset(), 2);
220 if (fd < 0)
221 return (FILE *)0;
222 if (lseek(fd, 0L, SEEK_END) == -1) {
223 int err = errno;
224 G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
225 err);
226 return (FILE *)-1;
227 }
228
229 return fdopen(fd, "a");
230}
231
232FILE *G_fopen_modify_misc(const char *dir, const char *element,
233 const char *name)
234{
235 int fd;
236
237 fd = G__open_misc(dir, element, name, G_mapset(), 2);
238 if (fd < 0)
239 return (FILE *)0;
240 if (lseek(fd, 0L, SEEK_END) == -1) {
241 int err = errno;
242 G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
243 err);
244 return (FILE *)-1;
245 }
246
247 return fdopen(fd, "r+");
248}
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition nme_in_mps.c:34
int G__make_mapset_element_misc(const char *, const char *)
Create misc element in the current mapset.
Definition mapset_msc.c:259
void G_warning(const char *,...) __attribute__((format(printf
int G_legal_filename(const char *)
Check for legal database file name.
Definition legal_name.c:32
char * G_file_name_misc(char *, const char *, const char *, const char *, const char *)
Builds full path names to GIS misc data files.
Definition file_name.c:99
const char * G_find_file2_misc(const char *, const char *, const char *, const char *)
Searches for a misc file from the mapset search list or in a specified mapset. (look but don't touch)
Definition find_file.c:253
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
Header file for msvc/fcntl.c.
#define creat
Definition fcntl.h:33
#define open
Definition fcntl.h:32
#define GMAPSET_MAX
Definition gis.h:194
#define GPATH_MAX
Definition gis.h:196
#define GNAME_MAX
Definition gis.h:193
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition gisinit.c:146
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database misc file for reading
Definition open_misc.c:202
int G_open_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition open_misc.c:103
FILE * G_fopen_modify_misc(const char *dir, const char *element, const char *name)
Definition open_misc.c:232
int G_open_update_misc(const char *dir, const char *element, const char *name)
open a database misc file for update
Definition open_misc.c:144
FILE * G_fopen_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition open_misc.c:175
FILE * G_fopen_append_misc(const char *dir, const char *element, const char *name)
Definition open_misc.c:214
int G_open_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database misc file for reading
Definition open_misc.c:124
#define fdopen
Definition stdio.h:6
Definition path.h:15
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define access
Definition unistd.h:7
#define close
Definition unistd.h:8