AtlatestRepositorysigil-ui

sigil-ui / tree / demo / macoslauncher.c

1/* SDK-free Launch Services entry. Keep the demo's receipts and resources
2 * beside its development bundle before replacing this process. */
3#include <limits.h>
4#include <stdint.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9extern int _NSGetExecutablePath(char *, uint32_t *);
11static int parent(char *path)
13 char *slash = strrchr(path, '/');
14 if (!slash || slash == path) return 0;
15 *slash = 0;
16 return 1;
19int main(int argc, char **argv)
21 (void)argc;
22 char unresolved[PATH_MAX], contents[PATH_MAX], root[PATH_MAX], file[PATH_MAX];
23 uint32_t capacity = sizeof(unresolved);
24 if (_NSGetExecutablePath(unresolved, &capacity) || !realpath(unresolved, contents) ||
25 !parent(contents) || !parent(contents)) return 1;
26 if (snprintf(root, sizeof(root), "%s", contents) >= (int)sizeof(root) ||
27 !parent(root) || !parent(root)) return 1;
28#define PATH(base, suffix) \
29 (snprintf(file, sizeof(file), "%s/%s", base, suffix) < (int)sizeof(file))
30 if (!PATH(contents, "Resources") || chdir(file)) return 1;
31 if (!PATH(root, "state") || setenv("CFFIXED_USER_HOME", file, 1)) return 1;
32 if (!PATH(root, "tmp/") || setenv("TMPDIR", file, 1)) return 1;
33 if (!PATH(root, "app.log") || !freopen(file, "a", stdout) ||
34 dup2(fileno(stdout), STDERR_FILENO) < 0) return 1;
35 if (!PATH(root, "app.exit")) return 1;
36 unlink(file);
37 if (setenv("SIGIL_UI_DEMO_EXIT_FILE", file, 1) || !PATH(root, "app.pid")) return 1;
38 FILE *receipt = fopen(file, "w");
39 if (!receipt) return 1;
40 int written = fprintf(receipt, "%ld\n", (long)getpid());
41 int closed = fclose(receipt);
42 if (written < 0 || closed) return 1;
43 if (!PATH(contents, "Resources/sigil-ui-demo")) return 1;
44 argv[0] = file;
45 execv(file, argv);
46 perror("exec demo");
47 return 1;