Merge pull request #475 from th-otto/PR-2
[appimagekit/gsi.git] / AppRun.c
blobea23805fc2cd592d13a6fe102aef363b2ba7759f
1 /**************************************************************************
3 Copyright (c) 2004-17 Simon Peter
4 Portions Copyright (c) 2010 RazZziel
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);
40 #define MAX(a,b) (a > b ? a : b)
41 #define bool int
42 #define false 0
43 #define true -1
45 #define LINE_SIZE 255
47 int filter(const struct dirent *dir) {
48 char *p = (char*) &dir->d_name;
49 p = strrchr(p, '.');
50 return p && !strcmp(p, ".desktop");
53 int main(int argc, char *argv[]) {
54 char *appdir = dirname(realpath("/proc/self/exe", NULL));
55 if (!appdir)
56 die("Could not access /proc/self/exe\n");
58 int ret;
59 struct dirent **namelist;
60 ret = scandir(appdir, &namelist, filter, NULL);
62 if (ret == 0) {
63 die("No .desktop files found\n");
64 } else if(ret == -1) {
65 die("Could not scan directory %s\n", appdir);
68 /* Extract executable from .desktop file */
69 char *desktop_file = malloc(LINE_SIZE);
70 snprintf(desktop_file, LINE_SIZE-1, "%s/%s", appdir, namelist[0]->d_name);
71 FILE *f = fopen(desktop_file, "r");
72 char *line = malloc(LINE_SIZE);
73 char *exe = line+5;
74 size_t n = LINE_SIZE;
76 do {
77 if (getline(&line, &n, f) == -1)
78 die("Executable not found, make sure there is a line starting with 'Exec='\n");
79 } while(strncmp(line, "Exec=", 5));
80 fclose(f);
82 // parse arguments
83 bool in_quotes = 0;
84 for (n = 0; n < LINE_SIZE; n++) {
85 if (!line[n]) // end of string
86 break;
87 else if (line[n] == 10 || line[n] == 13) {
88 line[n] = '\0';
89 line[n+1] = '\0';
90 line[n+2] = '\0';
91 break;
92 } else if (line[n] == '"') {
93 in_quotes = !in_quotes;
94 } else if (line[n] == ' ' && !in_quotes)
95 line[n] = '\0';
98 // count arguments
99 char* arg = exe;
100 int argcount = 0;
101 while ((arg += (strlen(arg)+1)) && *arg)
102 argcount += 1;
104 // merge args
105 char* outargptrs[argcount + argc + 1];
106 outargptrs[0] = exe;
107 int outargindex = 1;
108 arg = exe;
109 int argc_ = argc - 1; // argv[0] is the filename
110 char** argv_ = argv + 1;
111 while ((arg += (strlen(arg)+1)) && *arg) {
112 if (arg[0] == '%' || (arg[0] == '"' && arg[1] == '%')) { // handle desktop file field codes
113 char code = arg[arg[0] == '%' ? 1 : 2];
114 switch(code) {
115 case 'f':
116 case 'u':
117 if (argc_ > 0) {
118 outargptrs[outargindex++] = *argv_++;
119 argc_--;
121 break;
122 case 'F':
123 case 'U':
124 while (argc_ > 0) {
125 outargptrs[outargindex++] = *argv_++;
126 argc_--;
128 break;
129 case 'i':
130 case 'c':
131 case 'k':
132 fprintf(stderr, "WARNING: Desktop file field code %%%c is not currently supported\n", code);
133 break;
134 default:
135 fprintf(stderr, "WARNING: Invalid desktop file field code %%%c\n", code);
136 break;
138 } else {
139 outargptrs[outargindex++] = arg;
142 while (argc_ > 0) {
143 outargptrs[outargindex++] = *argv_++;
144 argc_--;
146 outargptrs[outargindex] = '\0'; // trailing null argument required by execvp()
148 // change directory
149 char usr_in_appdir[1024];
150 sprintf(usr_in_appdir, "%s/usr", appdir);
151 ret = chdir(usr_in_appdir);
152 if (ret != 0)
153 die("Could not cd into %s\n", usr_in_appdir);
155 // set environment variables
156 char *old_env;
157 const int length = 2047;
158 char new_env[8][length+1];
160 /* https://docs.python.org/2/using/cmdline.html#envvar-PYTHONHOME */
161 snprintf(new_env[0], length, "PYTHONHOME=%s/usr/", appdir);
163 old_env = getenv("PATH") ?: "";
164 snprintf(new_env[1], length, "PATH=%s/usr/bin/:%s/usr/sbin/:%s/usr/games/:%s/bin/:%s/sbin/:%s", appdir, appdir, appdir, appdir, appdir, old_env);
166 old_env = getenv("LD_LIBRARY_PATH") ?: "";
167 snprintf(new_env[2], length, "LD_LIBRARY_PATH=%s/usr/lib/:%s/usr/lib/i386-linux-gnu/:%s/usr/lib/x86_64-linux-gnu/:%s/usr/lib32/:%s/usr/lib64/:%s/lib/:%s/lib/i386-linux-gnu/:%s/lib/x86_64-linux-gnu/:%s/lib32/:%s/lib64/:%s", appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, old_env);
169 old_env = getenv("PYTHONPATH") ?: "";
170 snprintf(new_env[3], length, "PYTHONPATH=%s/usr/share/pyshared/:%s", appdir, old_env);
172 old_env = getenv("XDG_DATA_DIRS") ?: "";
173 snprintf(new_env[4], length, "XDG_DATA_DIRS=%s/usr/share/:%s", appdir, old_env);
175 old_env = getenv("PERLLIB") ?: "";
176 snprintf(new_env[5], length, "PERLLIB=%s/usr/share/perl5/:%s/usr/lib/perl5/:%s", appdir, appdir, old_env);
178 /* http://askubuntu.com/questions/251712/how-can-i-install-a-gsettings-schema-without-root-privileges */
179 old_env = getenv("GSETTINGS_SCHEMA_DIR") ?: "";
180 snprintf(new_env[6], length, "GSETTINGS_SCHEMA_DIR=%s/usr/share/glib-2.0/schemas/:%s", appdir, old_env);
182 old_env = getenv("QT_PLUGIN_PATH") ?: "";
183 snprintf(new_env[7], length, "QT_PLUGIN_PATH=%s/usr/lib/qt4/plugins/:%s/usr/lib/i386-linux-gnu/qt4/plugins/:%s/usr/lib/x86_64-linux-gnu/qt4/plugins/:%s/usr/lib32/qt4/plugins/:%s/usr/lib64/qt4/plugins/:%s/usr/lib/qt5/plugins/:%s/usr/lib/i386-linux-gnu/qt5/plugins/:%s/usr/lib/x86_64-linux-gnu/qt5/plugins/:%s/usr/lib32/qt5/plugins/:%s/usr/lib64/qt5/plugins/:%s", appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, old_env);
185 for (n = 0; n < 8; n++)
186 putenv(new_env[n]);
188 /* Otherwise may get errors because Python cannot write __pycache__ bytecode cache */
189 putenv("PYTHONDONTWRITEBYTECODE=1");
191 /* Run */
192 ret = execvp(exe, outargptrs);
194 if (ret == -1)
195 die("Error executing '%s'; return code: %d\n", exe, ret);
197 free(line);
198 free(desktop_file);
199 return 0;