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