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:%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"
52 int filter (const struct dirent
*dir
)
54 char *p
= (char*) &dir
->d_name
;
58 return !strcmp(p
, ".desktop");
61 int main(int argc
, char *argv
[])
67 char *dir
= realpath( "/proc/self/exe", NULL
);
70 die( "Could not access /proc/self/exe\n" );
73 sprintf( path
, "%s", dirname(dir
) );
75 // printf( "Moving to %s ...\n", path );
81 die( "Could not cd into %s\n", path
);
84 struct dirent
**namelist
;
86 ret
= scandir( ".", &namelist
, filter
, NULL
);
90 die( "No .desktop files found\n" );
94 die( "Could not scan directory %s\n", path
);
98 /* Extract executable from .desktop file
99 printf( "Extracting executable name from '%s' ...\n",
100 namelist[0]->d_name ); */
102 f
= fopen(namelist
[0]->d_name
, "r");
104 char *line
= malloc(LINE_SIZE
);
105 unsigned int n
= LINE_SIZE
;
108 while (getline( &line
, &n
, f
) != -1)
110 if (!strncmp(line
,"Exec=",5))
113 while (*++p
&& *p
!= ' ' && *p
!= '%' && *p
!= '\n' );
124 die( "Executable not found, make sure there is a line starting with 'Exec='\n" );
128 char *executable
= basename(line
+5);
129 // printf( "Executing '%s' ...\n", executable );
134 die( "Could not cd into %s\n", "usr" );
137 /* Build environment */
138 char *env
, *new_env
[2];
140 env
= getenv("LD_LIBRARY_PATH") ?: "";
141 new_env
[0] = malloc( strlen(NEW_LD_LIBRARY_PATH
) + strlen(env
) );
142 sprintf( new_env
[0], NEW_LD_LIBRARY_PATH
, env
);
143 // printf( " using %s\n", new_env[0] );
144 putenv( new_env
[0] );
146 env
= getenv("PATH") ?: "";
147 new_env
[1] = malloc( strlen(NEW_PATH
) + strlen(env
) );
148 sprintf( new_env
[1], NEW_PATH
, env
);
149 // printf( " using %s\n", new_env[1] );
150 putenv( new_env
[1] );
152 env
= getenv("PYTHONPATH") ?: "";
153 new_env
[2] = malloc( strlen(NEW_PYTHONPATH
) + strlen(env
) );
154 sprintf( new_env
[2], NEW_PYTHONPATH
, env
);
155 // printf( " using %s\n", new_env[2] );
156 putenv( new_env
[2] );
158 env
= getenv("XDG_DATA_DIRS") ?: "";
159 new_env
[3] = malloc( strlen(NEW_XDG_DATA_DIRS
) + strlen(env
) );
160 sprintf( new_env
[3], NEW_XDG_DATA_DIRS
, env
);
161 // printf( " using %s\n", new_env[3] );
162 putenv( new_env
[3] );
164 env
= getenv("QT_PLUGIN_PATH") ?: "";
165 new_env
[4] = malloc( strlen(NEW_QT_PLUGIN_PATH
) + strlen(env
) );
166 sprintf( new_env
[4], NEW_QT_PLUGIN_PATH
, env
);
167 // printf( " using %s\n", new_env[4] );
168 putenv( new_env
[4] );
170 env
= getenv("PERLLIB") ?: "";
171 new_env
[5] = malloc( strlen(NEW_PERLLIB
) + strlen(env
) );
172 sprintf( new_env
[5], NEW_PERLLIB
, env
);
173 // printf( " using %s\n", new_env[5] );
174 putenv( new_env
[5] );
177 ret
= execvp(executable
, argv
);
181 die( "Error executing '%s'\n", line
+5 );