overwrite.c 1.4 KB

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