f9663a50bb11faecefc0b0b7546d4f770d938d3a
[profile/ivi/ecore.git] / src / lib / ecore / ecore_app.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6
7 #ifndef _MSC_VER
8 # include <unistd.h>
9 #else
10 # include <process.h>
11 #endif
12
13 #ifdef HAVE_EVIL
14 # include <Evil.h>
15 #endif
16
17 #include "Ecore.h"
18 #include "ecore_private.h"
19
20 static int app_argc = 0;
21 static char **app_argv = NULL;
22
23 /**
24  * Set up the programs command-line arguments.
25  * @param argc The same as passed as argc to the programs main() function
26  * @param argv The same as passed as argv to the programs main() function
27  *
28  * A call to this function will store the programs command-line arguments
29  * for later use by ecore_app_restart() or ecore_app_args_get().
30  */
31 EAPI void
32 ecore_app_args_set(int argc, const char **argv)
33 {
34    if ((argc < 1) ||
35        (!argv)) return;
36    app_argc = argc;
37    app_argv = (char **)argv;
38 }
39
40 /**
41  * Return the programs stored command-line arguments.
42  * @param argc A pointer to the return value to hold argc
43  * @param argv A pointer to the return value to hold argv
44  *
45  * When called, this funciton returns the arguments for the program stored by
46  * ecore_app_args_set(). The integer pointed to by @p argc will be filled, if
47  * the pointer is not NULL, and the string array pointer @p argv will be filled
48  * also if the pointer is not NULL. The values they are filled with will be the
49  * same set by ecore_app_args_set().
50  */
51 EAPI void
52 ecore_app_args_get(int *argc, char ***argv)
53 {
54    if (argc) *argc = app_argc;
55    if (argv) *argv = app_argv;
56 }
57
58 /**
59  * Restart the program executable with the command-line arguments stored.
60  *
61  * This function will restart & re-execute this program in place of itself
62  * using the command-line arguments stored by ecore_app_args_set(). This is
63  * an easy way for a program to restart itself for cleanup purposes,
64  * configuration reasons or in the event of a crash.
65  */
66 EAPI void
67 ecore_app_restart(void)
68 {
69    char *args[4096];
70    int i;
71
72    if ((app_argc < 1) || (!app_argv)) return;
73    if (app_argc >= 4096) return;
74    for (i = 0; i < app_argc; i++) args[i] = app_argv[i];
75    args[i] = NULL;
76    execvp(app_argv[0], args);
77 }