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