legal_name.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <stdio.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  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. */
  32. int G_legal_filename(const char *s)
  33. {
  34. const char *name = s;
  35. if (*s == '.' || *s == 0) {
  36. G_warning(_("Illegal filename <%s>. Cannot be '.' or 'NULL'."), name);
  37. return -1;
  38. }
  39. for (; *s; s++)
  40. if (*s == '/' || *s == '"' || *s == '\'' || *s <= ' ' ||
  41. *s == '@' || *s == ',' || *s == '=' || *s == '*' || *s > 0176) {
  42. G_warning(_("Illegal filename <%s>. Character <%c> not allowed.\n"), name, *s);
  43. return -1;
  44. }
  45. return 1;
  46. }
  47. /*!
  48. * \brief Check input and output file names.
  49. *
  50. * Check:
  51. * 1) output is legal map name,
  52. * 2) if can find input map, and
  53. * 3) if input was found in current mapset, check if input != output.
  54. *
  55. * \param input input map name
  56. * \param output output map name
  57. * \param error error type: G_FATAL_EXIT, G_FATAL_PRINT, G_FATAL_RETURN
  58. *
  59. * \return 0 OK
  60. * \return 1 error
  61. */
  62. int G_check_input_output_name(const char *input, const char *output,
  63. int error)
  64. {
  65. const char *mapset;
  66. if (output == NULL)
  67. return 0; /* don't die on undefined parameters */
  68. if (G_legal_filename(output) == -1) {
  69. if (error == G_FATAL_EXIT) {
  70. G_fatal_error(_("Output raster map name <%s> is not valid map name"),
  71. output);
  72. }
  73. else if (error == G_FATAL_PRINT) {
  74. G_warning(_("Output raster map name <%s> is not valid map name"),
  75. output);
  76. return 1;
  77. }
  78. else { /* G_FATAL_RETURN */
  79. return 1;
  80. }
  81. }
  82. mapset = G_find_raster2(input, "");
  83. if (mapset == NULL) {
  84. if (error == G_FATAL_EXIT) {
  85. G_fatal_error(_("Raster map <%s> not found"), input);
  86. }
  87. else if (error == G_FATAL_PRINT) {
  88. G_warning(_("Raster map <%s> not found"), input);
  89. return 1;
  90. }
  91. else { /* G_FATAL_RETURN */
  92. return 1;
  93. }
  94. }
  95. if (strcmp(mapset, G_mapset()) == 0) {
  96. char nm[1000], ms[1000];
  97. const char *in;
  98. if (G_name_is_fully_qualified(input, nm, ms)) {
  99. in = nm;
  100. }
  101. else {
  102. in = input;
  103. }
  104. if (strcmp(in, output) == 0) {
  105. if (error == G_FATAL_EXIT) {
  106. G_fatal_error(_("Output raster map <%s> is used as input"),
  107. output);
  108. }
  109. else if (error == G_FATAL_PRINT) {
  110. G_warning(_("Output raster map <%s> is used as input"),
  111. output);
  112. return 1;
  113. }
  114. else { /* G_FATAL_RETURN */
  115. return 1;
  116. }
  117. }
  118. }
  119. return 0;
  120. }