GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
make_mapset.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/make_mapset.c
3 *
4 * \brief GIS Library - Functions to create a new mapset within an
5 * existing location
6 *
7 * SPDX-FileCopyrightText: 2006-2013 GRASS Development Team
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 *
10 * \author Joel Pitt, joel.pitt@gmail.com
11 */
12
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16#include <sys/stat.h>
17
18#include <grass/gis.h>
19#include <grass/glocale.h>
20
21/*!
22 * \brief Create a new mapset
23 *
24 * This function creates a new mapset in the given location,
25 * initializes default window and the current window.
26 *
27 * Calls G_fatal_error() if location doesn't exist.
28 *
29 * \param gisdbase_name full path of GISDBASE to create mapset in
30 * (NULL for the current GISDBASE)
31 * \param location_name name of location to create mapset in
32 * (NULL for the current location)
33 * \param mapset_name Name of the new mapset. Should not include
34 * the full path, the mapset will be created within
35 * the specified database and location.
36 *
37 * \return 0 on success
38 * \return -1 to indicate a system error (check errno).
39 * \return -2 illegal name
40 */
41int G_make_mapset(const char *gisdbase_name, const char *location_name,
42 const char *mapset_name)
43{
44 char path[GPATH_MAX];
46
47 /* Get location */
48 if (location_name == NULL)
50
51 /* Get GISDBASE */
52 if (gisdbase_name == NULL)
54
55 /* TODO: Should probably check that user specified location and gisdbase are
56 * valid */
57
58 /* check if mapset name is legal */
60 return -2;
61
62 /* Check if location exists */
63 snprintf(path, sizeof(path), "%s/%s", gisdbase_name, location_name);
64 if (access(path, F_OK) == -1)
65 G_fatal_error(_("Location <%s> doesn't exist"), location_name);
66
67 /* Make the mapset */
68 snprintf(path, sizeof(path), "%s/%s/%s", gisdbase_name, location_name,
70 if (G_mkdir(path) != 0) {
71 perror("G_make_mapset");
72 return -1;
73 }
75
76 /* Get PERMANENT default window */
78 G_setenv_nogisrc("LOCATION_NAME", location_name);
79 G_setenv_nogisrc("MAPSET", "PERMANENT");
81
82 /* Change to the new mapset */
84
85 /* Copy default window/regions to new mapset */
87
88 /* And switch back to original environment */
90
91 return 0;
92}
#define NULL
Definition ccmath.h:32
void G_switch_env(void)
Switch environments.
Definition env.c:589
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G_legal_filename(const char *)
Check for legal database file name.
Definition legal_name.c:32
const char * G_gisdbase(void)
Get name of top level database directory.
Definition gisdbase.c:22
void G_get_default_window(struct Cell_head *)
Get the default region.
Definition get_window.c:97
const char * G_location(void)
Get current location name.
Definition location.c:30
void G_create_alt_env(void)
Set up alternative environment variables.
Definition env.c:567
int G_put_element_window(const struct Cell_head *, const char *, const char *)
Write the region.
Definition put_window.c:72
int G_mkdir(const char *)
Creates a new directory.
Definition paths.c:27
void G_setenv_nogisrc(const char *, const char *)
Set environment name to value (doesn't update .gisrc)
Definition env.c:470
#define GPATH_MAX
Definition gis.h:196
#define _(str)
Definition glocale.h:10
int G_make_mapset(const char *gisdbase_name, const char *location_name, const char *mapset_name)
Create a new mapset.
Definition make_mapset.c:41
2D/3D raster map header (used also for region)
Definition gis.h:443
Definition path.h:15
#define access
Definition unistd.h:7
#define F_OK
Definition unistd.h:22