forked from AdamBurakowski/ExecutableRunningPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch.c
More file actions
Latest commit
59 lines (50 loc) · 1.36 KB
/
Copy pathlaunch.c
File metadata and controls
59 lines (50 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifdef_WIN32
#include<windows.h>
#definePATH_SEPARATOR "\\"
#else
#include<unistd.h>
#definePATH_SEPARATOR "/"
#endif
intcommand_exists(constchar*cmd){
charcheck_cmd[256];
#ifdef_WIN32
snprintf(check_cmd, sizeof(check_cmd), "where %s >nul 2>nul", cmd);
#else
snprintf(check_cmd, sizeof(check_cmd), "command -v %s >/dev/null 2>&1", cmd);
#endif
return(system(check_cmd) ==0);
}
intmain(){
charexe_path[1024];
chardir_path[1024];
charcommand[2048];
#ifdef_WIN32
GetModuleFileNameA(NULL, exe_path, sizeof(exe_path));
#else
ssize_tlen=readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
if(len==-1){
perror("readlink");
return1;
}
exe_path[len] ='\0';
#endif
strcpy(dir_path, exe_path);
char*last_sep=strrchr(dir_path, PATH_SEPARATOR[0]);
if(last_sep) *last_sep='\0';
constchar*python_cmd="python3";
if(!command_exists("python3")){
if(command_exists("python")){
python_cmd="python";
} else {
fprintf(stderr, "Error: Python is not installed or not in PATH\n");
return1;
}
}
snprintf(command, sizeof(command), "%s \"%s%s%s\"",
python_cmd, dir_path, PATH_SEPARATOR, "main.py");
intret=system(command);
returnret;
}