-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathmain.c
83 lines (71 loc) · 2.37 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/****************************************************************************
*
* MODULE: g.tempfile
* AUTHOR(S): Michael Shapiro CERL (original contributor)
* Markus Neteler <neteler itc.it>,
* Roberto Flor <flor itc.it>,
* Bernhard Reiter <bernhard intevation.de>,
* Jan-Oliver Wagner <jan intevation.de>,
* Martin Landa <landa.martin gmail.com>
* PURPOSE:
* COPYRIGHT: (C) 1999-2006, 2011 by the GRASS Development Team
*
* This program is free software under the GNU General
* Public License (>=v2). Read the file COPYING that
* comes with GRASS for details.
*
*****************************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <grass/gis.h>
#include <grass/glocale.h>
int main(int argc, char *argv[])
{
struct GModule *module;
struct Option *pid;
struct Flag *dry_run;
struct Flag *directory;
char *tempfile;
int p;
G_gisinit(argv[0]);
module = G_define_module();
G_add_keyword(_("general"));
G_add_keyword(_("support"));
G_add_keyword(_("scripts"));
module->description =
_("Creates a temporary file and prints it's 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");
dry_run = G_define_flag();
dry_run->key = 'd';
dry_run->description =
_("Dry run - don't create a file, just prints it's file name");
directory = G_define_flag();
directory->key = 'f';
directory->description =
_("Folder mode - create a temporary directory, not a file");
G_option_exclusive(dry_run, directory, NULL);
G_disable_interactive();
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
if (sscanf(pid->answer, "%d", &p) != 1) {
G_usage();
exit(EXIT_FAILURE);
}
tempfile = G_tempfile_pid(p);
/* create tempfile so next run of this program will create a unique name */
if (!dry_run->answer) {
if (directory->answer)
G_mkdir(tempfile);
else
close(creat(tempfile, 0666));
}
fprintf(stdout, "%s\n", tempfile);
exit(EXIT_SUCCESS);
}