overwrite.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * \file overwrite.c
  3. *
  4. * \brief GIS Library - Check for overwrite.
  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, Martin Landa <landa.martin gmail.com>
  12. *
  13. * \date 2007-2008
  14. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <grass/gis.h>
  18. /**
  19. * \brief Check for overwrite mode
  20. *
  21. * Check variables OVERWRITE, GRASS_OVERWRITE and '--o' flag.
  22. *
  23. * The parser G_parser() checks if the map already exists in current mapset,
  24. * we can switch out the check and do it
  25. * in the module after the parser.
  26. *
  27. * \param[in] argc number of arguments
  28. * \param[in] argv array of arguments
  29. *
  30. * \return 1 if overwrite
  31. * \return 0 if not overwrite
  32. */
  33. int G_check_overwrite(int argc, char **argv)
  34. {
  35. char *overstr;
  36. int overwrite;
  37. overwrite = 0;
  38. if ((overstr = G__getenv("OVERWRITE"))) {
  39. overwrite = atoi(overstr);
  40. }
  41. /* check if inherited GRASS_OVERWRITE is 1 */
  42. if (!overwrite && (overstr = getenv("GRASS_OVERWRITE"))) {
  43. overwrite = atoi(overstr);
  44. }
  45. /* check for --o or --overwrite option */
  46. if (!overwrite) {
  47. int i;
  48. for (i = 0; i < argc; i++) {
  49. if (strcmp(argv[i], "--o") == 0 ||
  50. strcmp(argv[i], "--overwrite") == 0) {
  51. overwrite = 1;
  52. break;
  53. }
  54. }
  55. }
  56. G__setenv("OVERWRITE", "1");
  57. return overwrite;
  58. }