GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
legal_name.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/legal_name.c
3 *
4 * \brief GIS Library - Functions to handle file name legality.
5 *
6 * SPDX-FileCopyrightText: 2001-2009, 2013 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <stdio.h>
13#include <string.h>
14#include <grass/gis.h>
15#include <grass/glocale.h>
16
17/*!
18 * \brief Check for legal database file name.
19 *
20 * Legal file names will <b>not</b> begin with '.' or NULL and must
21 * not contain the characters, ' ' (space), '/', '"'. '\'' (single
22 * quote), '@', ',', '=', '*', and all other non-alphanumeric
23 * characters within.
24 *
25 * The function prints a warning on error.
26 *
27 * \param s file name to check
28 *
29 * \return 1 success
30 * \return -1 failure
31 */
32int G_legal_filename(const char *s)
33{
34 const char *name = s;
35
36 if (*s == '.' || *s == 0) {
38 _("Illegal filename <%s>. Cannot start with '.' or be 'NULL'."),
39 name);
40 return -1;
41 }
42
43 for (; *s; s++)
44 if (*s == '/' || *s == '"' || *s == '\'' || *s <= ' ' || *s == '@' ||
45 *s == ',' || *s == '=' || *s == '*' || *s > 0176) {
46 G_warning(_("Illegal filename <%s>. Character <%c> not allowed.\n"),
47 name, *s);
48 return -1;
49 }
50
51 return 1;
52}
53
54/*!
55 * \brief Check input and output file names.
56 *
57 * Check:
58 * 1) output is legal map name,
59 * 2) if can find input map, and
60 * 3) if input was found in current mapset, check if input != output.
61 *
62 * \param input input map name
63 * \param output output map name
64 * \param error error type: G_FATAL_EXIT, G_FATAL_PRINT, G_FATAL_RETURN
65 *
66 * \return 0 OK
67 * \return 1 error
68 */
69int G_check_input_output_name(const char *input, const char *output, int error)
70{
71 const char *mapset;
72
73 if (output == NULL)
74 return 0; /* don't die on undefined parameters */
75 if (G_legal_filename(output) == -1) {
76 if (error == G_FATAL_EXIT) {
78 _("Output raster map name <%s> is not valid map name"), output);
79 }
80 else if (error == G_FATAL_PRINT) {
81 G_warning(_("Output raster map name <%s> is not valid map name"),
82 output);
83 return 1;
84 }
85 else { /* G_FATAL_RETURN */
86 return 1;
87 }
88 }
89
90 mapset = G_find_raster2(input, "");
91
92 if (mapset == NULL) {
93 if (error == G_FATAL_EXIT) {
94 G_fatal_error(_("Raster map <%s> not found"), input);
95 }
96 else if (error == G_FATAL_PRINT) {
97 G_warning(_("Raster map <%s> not found"), input);
98 return 1;
99 }
100 else { /* G_FATAL_RETURN */
101 return 1;
102 }
103 }
104
105 if (strcmp(mapset, G_mapset()) == 0) {
106 char nm[1000], ms[1000];
107 const char *in;
108
109 if (G_name_is_fully_qualified(input, nm, ms)) {
110 in = nm;
111 }
112 else {
113 in = input;
114 }
115
116 if (strcmp(in, output) == 0) {
117 if (error == G_FATAL_EXIT) {
118 G_fatal_error(_("Output raster map <%s> is used as input"),
119 output);
120 }
121 else if (error == G_FATAL_PRINT) {
122 G_warning(_("Output raster map <%s> is used as input"), output);
123 return 1;
124 }
125 else { /* G_FATAL_RETURN */
126 return 1;
127 }
128 }
129 }
130
131 return 0;
132}
#define NULL
Definition ccmath.h:32
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
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
const char * G_find_raster2(const char *, const char *)
Find a raster map (look but don't touch)
Definition find_rast.c:73
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
#define G_FATAL_PRINT
Definition gis.h:408
#define G_FATAL_EXIT
Definition gis.h:407
#define _(str)
Definition glocale.h:10
int G_legal_filename(const char *s)
Check for legal database file name.
Definition legal_name.c:32
int G_check_input_output_name(const char *input, const char *output, int error)
Check input and output file names.
Definition legal_name.c:69
const char * name
Definition named_colr.c:6
void output(const char *fmt,...)