Update AppRun.c
[appimagekit/gsi.git] / AppRun.c
blob9d393b5165955b61ce8077c296c94eb6f484fecd
1 /**************************************************************************
3 Copyright (c) 2010 RazZziel
4 Copyright (c) 2005-15 Simon Peter
6 All Rights Reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
26 **************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <libgen.h>
32 #include <dirent.h>
33 #include <string.h>
35 #define die(...) \
36 do { \
37 fprintf( stderr, "Error: " __VA_ARGS__ ); \
38 exit( 1 ); \
39 } while (0);
41 #define NEW_LD_LIBRARY_PATH "LD_LIBRARY_PATH=./lib/:./lib/i386-linux-gnu/:./lib/x86_64-linux-gnu/:./lib32/:./lib64/%s"
42 #define NEW_PATH "PATH=./bin/:./sbin/:./games/:%s"
43 #define NEW_PYTHONPATH "PYTHONPATH=./share/pyshared/:%s"
44 #define NEW_XDG_DATA_DIRS "XDG_DATA_DIRS=./share/:%s"
45 #define NEW_QT_PLUGIN_PATH "QT_PLUGIN_PATH=./lib/qt4/plugins/:./lib/qt5/plugins/:%s"
46 #define NEW_PERLLIB "PERLLIB=./share/perl5/:./lib/perl5/:%s"
48 // http://askubuntu.com/questions/251712/how-can-i-install-a-gsettings-schema-without-root-privileges
49 #define NEW_GSETTINGS_SCHEMA_DIR "GSETTINGS_SCHEMA_DIR=./share/glib-2.0/schemas/:%s"
51 #define LINE_SIZE 255
53 int filter (const struct dirent *dir)
55 char *p = (char*) &dir->d_name;
56 while (*++p);
57 while (*--p != '.');
59 return !strcmp(p, ".desktop");
62 int main(int argc, char *argv[])
64 char path[LINE_SIZE];
65 int ret;
66 FILE *f;
68 char *dir = realpath( "/proc/self/exe", NULL );
69 if (!dir)
71 die( "Could not access /proc/self/exe\n" );
74 sprintf( path, "%s", dirname(dir) );
76 // printf( "Moving to %s ...\n", path );
78 ret = chdir(path);
80 if ( ret != 0 )
82 die( "Could not cd into %s\n", path );
85 struct dirent **namelist;
87 ret = scandir( ".", &namelist, filter, NULL );
89 if ( ret == 0 )
91 die( "No .desktop files found\n" );
93 else if ( ret == -1 )
95 die( "Could not scan directory %s\n", path );
99 /* Extract executable from .desktop file
100 printf( "Extracting executable name from '%s' ...\n",
101 namelist[0]->d_name ); */
103 f = fopen(namelist[0]->d_name, "r");
105 char *line = malloc(LINE_SIZE);
106 unsigned int n = LINE_SIZE;
107 int found = 0;
109 while (getline( &line, &n, f ) != -1)
111 if (!strncmp(line,"Exec=",5))
113 char *p = line+5;
114 while (*++p && *p != ' ' && *p != '%' && *p != '\n' );
115 *p = 0;
116 found = 1;
117 break;
121 fclose( f );
123 if (!found)
125 die( "Executable not found, make sure there is a line starting with 'Exec='\n" );
128 /* Execution */
129 char *executable = basename(line+5);
130 // printf( "Executing '%s' ...\n", executable );
132 ret = chdir("usr");
133 if ( ret != 0 )
135 die( "Could not cd into %s\n", "usr" );
138 /* Build environment */
139 char *env, *new_env[2];
141 env = getenv("LD_LIBRARY_PATH") ?: "";
142 new_env[0] = malloc( strlen(NEW_LD_LIBRARY_PATH) + strlen(env) );
143 sprintf( new_env[0], NEW_LD_LIBRARY_PATH, env );
144 // printf( " using %s\n", new_env[0] );
145 putenv( new_env[0] );
147 env = getenv("PATH") ?: "";
148 new_env[1] = malloc( strlen(NEW_PATH) + strlen(env) );
149 sprintf( new_env[1], NEW_PATH, env );
150 // printf( " using %s\n", new_env[1] );
151 putenv( new_env[1] );
153 env = getenv("PYTHONPATH") ?: "";
154 new_env[2] = malloc( strlen(NEW_PYTHONPATH) + strlen(env) );
155 sprintf( new_env[2], NEW_PYTHONPATH, env );
156 // printf( " using %s\n", new_env[2] );
157 putenv( new_env[2] );
159 env = getenv("XDG_DATA_DIRS") ?: "";
160 new_env[3] = malloc( strlen(NEW_XDG_DATA_DIRS) + strlen(env) );
161 sprintf( new_env[3], NEW_XDG_DATA_DIRS, env );
162 // printf( " using %s\n", new_env[3] );
163 putenv( new_env[3] );
165 env = getenv("QT_PLUGIN_PATH") ?: "";
166 new_env[4] = malloc( strlen(NEW_QT_PLUGIN_PATH) + strlen(env) );
167 sprintf( new_env[4], NEW_QT_PLUGIN_PATH, env );
168 // printf( " using %s\n", new_env[4] );
169 putenv( new_env[4] );
171 env = getenv("PERLLIB") ?: "";
172 new_env[5] = malloc( strlen(NEW_PERLLIB) + strlen(env) );
173 sprintf( new_env[5], NEW_PERLLIB, env );
174 // printf( " using %s\n", new_env[5] );
175 putenv( new_env[5] );
177 env = getenv("GSETTINGS_SCHEMA_DIR") ?: "";
178 new_env[6] = malloc( strlen(NEW_GSETTINGS_SCHEMA_DIR) + strlen(env) );
179 sprintf( new_env[6], NEW_GSETTINGS_SCHEMA_DIR, env );
180 // printf( " using %s\n", new_env[6] );
181 putenv( new_env[6] );
183 /* Run */
184 ret = execvp(executable, argv);
186 if (ret == -1)
188 die( "Error executing '%s'\n", line+5 );
191 free(new_env[0]);
192 free(new_env[1]);
193 free(line);
194 return 0;