int main(int argc, char *argv[]) { struct GModule *module; struct Option *pid; char *tempfile, *G__tempfile(); int p; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("general, map management"); module->description = "Creates a temporary file and prints the file name."; pid = G_define_option(); pid->key = "pid"; pid->type = TYPE_INTEGER; pid->required = YES; pid->description = "Process id to use when naming the tempfile"; G_disable_interactive(); if (G_parser(argc, argv)) exit(1); if (sscanf(pid->answer, "%d", &p) != 1) { G_usage(); exit(EXIT_FAILURE); } tempfile = G__tempfile(p); /* create tempfile so next run of this program will create a unique name */ close(creat(tempfile, 0666)); fprintf(stdout, "%s\n", tempfile); exit(EXIT_SUCCESS); }
/*! * \brief Returns a temporary file name. * * This routine returns a pointer to a string containing a unique * temporary file name that can be used as a temporary file within the * module. Successive calls to G_tempfile() will generate new * names. Only the file name is generated. The file itself is not * created. To create the file, the module must use standard UNIX * functions which create and open files, e.g., <i>creat()</i> or * <i>fopen()</i>. * * Successive calls will generate different names the names are of the * form pid.n where pid is the programs process id number and n is a * unique identifier. * * <b>Note:</b> It is recommended to <i>unlink()</i> (remove) the * temp file on exit/error. Only if GRASS is left with 'exit', the GIS * mapset management will clean up the temp directory (ETC/clean_temp). * * \return pointer to a character string containing the name. The name * is copied to allocated memory and may be released by the unix free() * routine. */ char *G_tempfile(void) { return G__tempfile(getpid()); }