main.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /****************************************************************************
  2. *
  3. * MODULE: g.copy
  4. * AUTHOR(S): CERL (original contributor)
  5. * Radim Blazek <radim.blazek gmail.com>,
  6. * Cedric Shock <cedricgrass shockfamily.net>,
  7. * Huidae Cho <grass4u gmail.com>,
  8. * Glynn Clements <glynn gclements.plus.com>,
  9. * Markus Neteler <neteler itc.it>,
  10. * Martin Landa <landa.martin gmail.com>
  11. * PURPOSE: lets users copy database files
  12. * COPYRIGHT: (C) 2003-2007 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include <grass/manage.h>
  24. int main(int argc, char *argv[])
  25. {
  26. int i, n, nlist;
  27. const char *mapset;
  28. struct GModule *module;
  29. struct Option **parm;
  30. char *from, *to;
  31. int result = EXIT_SUCCESS;
  32. G_gisinit(argv[0]);
  33. M_read_list(FALSE, &nlist);
  34. module = G_define_module();
  35. G_add_keyword(_("general"));
  36. G_add_keyword(_("map management"));
  37. module->description =
  38. _("Copies available data files in the current mapset "
  39. "search path to the user's current mapset.");
  40. module->overwrite = 1;
  41. parm = (struct Option **) G_calloc(nlist, sizeof(struct Option *));
  42. for (n = 0; n < nlist; n++) {
  43. parm[n] = M_define_option(n, _("copied"), NO);
  44. }
  45. if (G_parser(argc, argv))
  46. exit(EXIT_FAILURE);
  47. for (n = 0; n < nlist; n++) {
  48. if (parm[n]->answers == NULL)
  49. continue;
  50. i = 0;
  51. while (parm[n]->answers[i]) {
  52. from = parm[n]->answers[i++];
  53. to = parm[n]->answers[i++];
  54. mapset = M_find(n, from, "");
  55. if (!mapset) {
  56. G_warning(_("<%s> not found"), from);
  57. continue;
  58. }
  59. if (G_strcasecmp(mapset, G_mapset()) == 0 &&
  60. G_strcasecmp(from, to) == 0) {
  61. G_warning(_("%s=%s,%s: files are the same, no copy required"),
  62. parm[n]->key, from, to);
  63. continue;
  64. }
  65. if (M_find(n, to, G_mapset()) && !(module->overwrite)) {
  66. G_warning(_("<%s> already exists"), to);
  67. continue;
  68. }
  69. if (G_legal_filename(to) < 0) {
  70. G_warning(_("<%s> is an illegal file name"), to);
  71. continue;
  72. }
  73. if (M_do_copy(n, from, mapset, to) == 1) {
  74. result = EXIT_FAILURE;
  75. }
  76. G_remove_misc("cell_misc", "reclassed_to", to);
  77. }
  78. }
  79. exit(result);
  80. }