GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
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 * COPYRIGHT: (C) 2007 Glynn Clements and the GRASS Development Team
6 *
7 * NOTE: Based upon open.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 *****************************************************************************/
20
21#include <grass/config.h>
22#include <errno.h>
23#include <string.h>
24
25#include <unistd.h>
26#include <fcntl.h>
27
28#include <grass/gis.h>
29#include <grass/glocale.h>
30
31#include "gis_local_proto.h"
32
33static int G__open_misc(const char *dir, const char *element, const char *name,
34 const char *mapset, int mode)
35{
36 int fd;
37 char path[GPATH_MAX];
39
41
42 /* READ */
43 if (mode == 0) {
45 if (*mapset && strcmp(xmapset, mapset) != 0) {
46 G_warning(_("G__open_misc(read): mapset <%s> doesn't match "
47 "xmapset <%s>"),
48 mapset, xmapset);
49 return -1;
50 }
51 name = xname;
52 mapset = xmapset;
53 }
54
55 mapset = G_find_file2_misc(dir, element, name, mapset);
56
57 if (!mapset)
58 return -1;
59
60 G_file_name_misc(path, dir, element, name, mapset);
61
62 if ((fd = open(path, 0)) < 0)
63 G_warning("G__open_misc(read): Unable to open '%s': %s", path,
65 return fd;
66 }
67 /* WRITE */
68 if (mode == 1 || mode == 2) {
69 mapset = G_mapset();
71 if (strcmp(xmapset, mapset) != 0) {
73 _("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"),
74 xmapset, mapset);
75 return -1;
76 }
77 name = xname;
78 }
79
80 if (G_legal_filename(name) == -1)
81 return -1;
82
83 G_file_name_misc(path, dir, element, name, mapset);
84 if (mode == 1 || access(path, 0) != 0) {
86 close(creat(path, 0666));
87 }
88
89 if ((fd = open(path, mode)) < 0)
90 G_warning("G__open_misc(write): Unable to open '%s': %s", path,
92 return fd;
93 }
94 return -1;
95}
96
97/*!
98 * \brief open a new database misc file
99 *
100 * The database file <b>element</b> under <b>dir/name</b> in the
101 * current mapset is created and opened for writing (but not reading).
102 * The UNIX open( ) routine is used to open the file. If the file does not
103 * exist, -1 is returned. Otherwise the file is positioned at the end of the
104 * file and the file descriptor from the open( ) is returned.
105 *
106 * \param dir
107 * \param element
108 * \param name
109 * \return int
110 */
111int G_open_new_misc(const char *dir, const char *element, const char *name)
112{
113 return G__open_misc(dir, element, name, G_mapset(), 1);
114}
115
116/*!
117 * \brief open a database misc file for reading
118 *
119 * The database file <b>element</b> under
120 * <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
121 * not for writing).
122 * The UNIX open( ) routine is used to open the file. If the file does not
123 * exist, -1 is returned. Otherwise the file descriptor from the open( ) is
124 * returned.
125 *
126 * \param dir
127 * \param element
128 * \param name
129 * \param mapset
130 * \return int
131 */
132int G_open_old_misc(const char *dir, const char *element, const char *name,
133 const char *mapset)
134{
135 return G__open_misc(dir, element, name, mapset, 0);
136}
137
138/*!
139 * \brief open a database misc file for update
140 *
141 * The database file <b>element</b> under <b>dir/name</b> in the
142 * current mapset is opened for reading and writing.
143 * The UNIX open( ) routine is used to open the file. If the file does not
144 * exist, -1 is returned. Otherwise the file is positioned at the end of the
145 * file and the file descriptor from the open( ) is returned.
146 *
147 * \param dir
148 * \param element
149 * \param name
150 * \return int
151 */
152int G_open_update_misc(const char *dir, const char *element, const char *name)
153{
154 int fd;
155
156 fd = G__open_misc(dir, element, name, G_mapset(), 2);
157 if (fd >= 0)
158 if (lseek(fd, 0L, SEEK_END) == -1) {
159 int err = errno;
160 G_warning(_("File read/write operation failed: %s (%d)"),
161 strerror(err), err);
162 return -1;
163 }
164
165 return fd;
166}
167
168/*!
169 * \brief open a new database misc file
170 *
171 * The database file <b>element</b> under <b>dir/name</b> in the
172 * current mapset is created and opened for writing (but not reading).
173 * The UNIX fopen( ) routine, with "w" write mode, is used to open the file. If
174 * the file does not exist, the NULL pointer is returned. Otherwise the file is
175 * positioned at the end of the file and the file descriptor from the fopen( )
176 * is returned.
177 *
178 * \param dir
179 * \param element
180 * \param name
181 * \return FILE *
182 */
183FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name)
184{
185 int fd;
186
187 fd = G__open_misc(dir, element, name, G_mapset(), 1);
188 if (fd < 0)
189 return (FILE *)0;
190
191 return fdopen(fd, "w");
192}
193
194/*!
195 * \brief open a database misc file for reading
196 *
197 * The database file <b>element</b> under
198 * <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
199 * not for writing).
200 * The UNIX fopen( ) routine, with "r" read mode, is used to open the file. If
201 * the file does not exist, the NULL pointer is returned. Otherwise the file
202 * descriptor from the fopen( ) is returned.
203 *
204 * \param dir
205 * \param element
206 * \param name
207 * \param mapset
208 * \return FILE *
209 */
210FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name,
211 const char *mapset)
212{
213 int fd;
214
215 fd = G__open_misc(dir, element, name, mapset, 0);
216 if (fd < 0)
217 return (FILE *)0;
218
219 return fdopen(fd, "r");
220}
221
222FILE *G_fopen_append_misc(const char *dir, const char *element,
223 const char *name)
224{
225 int fd;
226
227 fd = G__open_misc(dir, element, name, G_mapset(), 2);
228 if (fd < 0)
229 return (FILE *)0;
230 if (lseek(fd, 0L, SEEK_END) == -1) {
231 int err = errno;
232 G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
233 err);
234 return (FILE *)-1;
235 }
236
237 return fdopen(fd, "a");
238}
239
240FILE *G_fopen_modify_misc(const char *dir, const char *element,
241 const char *name)
242{
243 int fd;
244
245 fd = G__open_misc(dir, element, name, G_mapset(), 2);
246 if (fd < 0)
247 return (FILE *)0;
248 if (lseek(fd, 0L, SEEK_END) == -1) {
249 int err = errno;
250 G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
251 err);
252 return (FILE *)-1;
253 }
254
255 return fdopen(fd, "r+");
256}
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition nme_in_mps.c:36
int G__make_mapset_element_misc(const char *, const char *)
Create misc element in the current mapset.
Definition mapset_msc.c:261
void G_warning(const char *,...) __attribute__((format(printf
int G_legal_filename(const char *)
Check for legal database file name.
Definition legal_name.c:34
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:101
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:257
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:33
Header file for msvc/fcntl.c.
#define creat
Definition fcntl.h:34
#define open
Definition fcntl.h:33
#define GMAPSET_MAX
Definition gis.h:197
#define GPATH_MAX
Definition gis.h:199
#define GNAME_MAX
Definition gis.h:196
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition gisinit.c:148
#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:210
int G_open_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition open_misc.c:111
FILE * G_fopen_modify_misc(const char *dir, const char *element, const char *name)
Definition open_misc.c:240
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:152
FILE * G_fopen_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition open_misc.c:183
FILE * G_fopen_append_misc(const char *dir, const char *element, const char *name)
Definition open_misc.c:222
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:132
#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