main.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /****************************************************************************
  2. *
  3. * MODULE: g.tempfile
  4. * AUTHOR(S): Michael Shapiro CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Roberto Flor <flor itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Jan-Oliver Wagner <jan intevation.de>,
  9. * Martin Landa <landa.martin gmail.com>
  10. * PURPOSE:
  11. * COPYRIGHT: (C) 1999-2006, 2011 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General
  14. * Public License (>=v2). Read the file COPYING that
  15. * comes with GRASS for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <grass/gis.h>
  24. #include <grass/glocale.h>
  25. int main(int argc, char *argv[])
  26. {
  27. struct GModule *module;
  28. struct Option *pid;
  29. struct Flag *dry_run;
  30. char *tempfile;
  31. int p;
  32. G_gisinit(argv[0]);
  33. module = G_define_module();
  34. G_add_keyword(_("general"));
  35. G_add_keyword(_("support"));
  36. G_add_keyword(_("scripts"));
  37. module->description = _("Creates a temporary file and prints it's file name.");
  38. pid = G_define_option();
  39. pid->key = "pid";
  40. pid->type = TYPE_INTEGER;
  41. pid->required = YES;
  42. pid->description = _("Process id to use when naming the tempfile");
  43. dry_run = G_define_flag();
  44. dry_run->key = 'd';
  45. dry_run->description = _("Dry run - don't create a file, just prints it's file name");
  46. G_disable_interactive();
  47. if (G_parser(argc, argv))
  48. exit(EXIT_FAILURE);
  49. if (sscanf(pid->answer, "%d", &p) != 1) {
  50. G_usage();
  51. exit(EXIT_FAILURE);
  52. }
  53. tempfile = G_tempfile_pid(p);
  54. /* create tempfile so next run of this program will create a unique name */
  55. if (!dry_run->answer)
  56. close(creat(tempfile, 0666));
  57. fprintf(stdout, "%s\n", tempfile);
  58. exit(EXIT_SUCCESS);
  59. }