1 /**************************************************************************
5 Copyright (c) 2010 RazZziel
6 Copyright (c) 2005-11 Simon Peter
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 **************************************************************************/
39 fprintf( stderr, "Error: " __VA_ARGS__ ); \
43 #define NEW_LD_LIBRARY_PATH "LD_LIBRARY_PATH=./lib/:./lib/i386-linux-gnu/:./lib/x86_64-linux-gnu/:%s"
44 #define NEW_PATH "PATH=./bin/:./sbin/:./games/:%s"
45 #define NEW_PYTHONPATH "PYTHONPATH=./share/pyshared/:%s"
46 #define NEW_XDG_DATA_DIRS "XDG_DATA_DIRS=./share/:%s"
47 #define NEW_QT_PLUGIN_PATH "QT_PLUGIN_PATH=./lib/qt4/plugins/:%s"
48 #define NEW_PERLLIB "PERLLIB=./share/perl5/:./lib/perl5/:%s"
50 // http://askubuntu.com/questions/251712/how-can-i-install-a-gsettings-schema-without-root-privileges
51 #define NEW_GSETTINGS_SCHEMA_DIR "GSETTINGS_SCHEMA_DIR=./share/glib-2.0/schemas/:%s"
55 int filter (const struct dirent
*dir
)
57 char *p
= (char*) &dir
->d_name
;
61 return !strcmp(p
, ".desktop");
64 int main(int argc
, char *argv
[])
70 char *dir
= realpath( "/proc/self/exe", NULL
);
73 die( "Could not access /proc/self/exe\n" );
76 sprintf( path
, "%s", dirname(dir
) );
78 // printf( "Moving to %s ...\n", path );
84 die( "Could not cd into %s\n", path
);
87 struct dirent
**namelist
;
89 ret
= scandir( ".", &namelist
, filter
, NULL
);
93 die( "No .desktop files found\n" );
97 die( "Could not scan directory %s\n", path
);
101 /* Extract executable from .desktop file
102 printf( "Extracting executable name from '%s' ...\n",
103 namelist[0]->d_name ); */
105 f
= fopen(namelist
[0]->d_name
, "r");
107 char *line
= malloc(LINE_SIZE
);
108 unsigned int n
= LINE_SIZE
;
111 while (getline( &line
, &n
, f
) != -1)
113 if (!strncmp(line
,"Exec=",5))
116 while (*++p
&& *p
!= ' ' && *p
!= '%' && *p
!= '\n' );
127 die( "Executable not found, make sure there is a line starting with 'Exec='\n" );
131 char *executable
= basename(line
+5);
132 // printf( "Executing '%s' ...\n", executable );
137 die( "Could not cd into %s\n", "usr" );
140 /* Build environment */
141 char *env
, *new_env
[2];
143 env
= getenv("LD_LIBRARY_PATH") ?: "";
144 new_env
[0] = malloc( strlen(NEW_LD_LIBRARY_PATH
) + strlen(env
) );
145 sprintf( new_env
[0], NEW_LD_LIBRARY_PATH
, env
);
146 // printf( " using %s\n", new_env[0] );
147 putenv( new_env
[0] );
149 env
= getenv("PATH") ?: "";
150 new_env
[1] = malloc( strlen(NEW_PATH
) + strlen(env
) );
151 sprintf( new_env
[1], NEW_PATH
, env
);
152 // printf( " using %s\n", new_env[1] );
153 putenv( new_env
[1] );
155 env
= getenv("PYTHONPATH") ?: "";
156 new_env
[2] = malloc( strlen(NEW_PYTHONPATH
) + strlen(env
) );
157 sprintf( new_env
[2], NEW_PYTHONPATH
, env
);
158 // printf( " using %s\n", new_env[2] );
159 putenv( new_env
[2] );
161 env
= getenv("XDG_DATA_DIRS") ?: "";
162 new_env
[3] = malloc( strlen(NEW_XDG_DATA_DIRS
) + strlen(env
) );
163 sprintf( new_env
[3], NEW_XDG_DATA_DIRS
, env
);
164 // printf( " using %s\n", new_env[3] );
165 putenv( new_env
[3] );
167 env
= getenv("QT_PLUGIN_PATH") ?: "";
168 new_env
[4] = malloc( strlen(NEW_QT_PLUGIN_PATH
) + strlen(env
) );
169 sprintf( new_env
[4], NEW_QT_PLUGIN_PATH
, env
);
170 // printf( " using %s\n", new_env[4] );
171 putenv( new_env
[4] );
173 env
= getenv("PERLLIB") ?: "";
174 new_env
[5] = malloc( strlen(NEW_PERLLIB
) + strlen(env
) );
175 sprintf( new_env
[5], NEW_PERLLIB
, env
);
176 // printf( " using %s\n", new_env[5] );
177 putenv( new_env
[5] );
179 env
= getenv("GSETTINGS_SCHEMA_DIR") ?: "";
180 new_env
[6] = malloc( strlen(NEW_GSETTINGS_SCHEMA_DIR
) + strlen(env
) );
181 sprintf( new_env
[6], NEW_GSETTINGS_SCHEMA_DIR
, env
);
182 // printf( " using %s\n", new_env[6] );
183 putenv( new_env
[6] );
186 ret
= execvp(executable
, argv
);
190 die( "Error executing '%s'\n", line
+5 );