legal_name.c 3.0 KB

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