-
Notifications
You must be signed in to change notification settings - Fork 0
/
opener.cc
68 lines (58 loc) · 1.29 KB
/
opener.cc
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
60
61
62
63
64
65
66
67
68
//
// opener.c
// Open URLS in C
//
// MIT licensed.
// Copyright (c) Abraham Hernandez <[email protected]>
//
#include <cstring>
#include <cstdlib>
static const char * operating_system()
{
#ifdef _WIN32
return "win32";
#elif _WIN64
return "win64";
#elif __unix || __unix__
return "unix";
#elif __APPLE__ || __MACH__
return "macOS";
#elif __linux__
return "linux";
#elif __FreeBSD__
return "freeBSD";
#else
return "other";
#endif
}
char *
create_cmd(const char * cmd, const char * link) {
char * url = (char *)malloc(strlen(cmd) + strlen(link) + 1);
strcpy(url, cmd);
strcat(url, " ");
strcat(url, link);
return url;
}
int
opener(const char *url)
{
const char *platform = operating_system();
const char *cmd = NULL;
// Hanlde macOS
if (!strcmp(platform, "macOS")) {
cmd = "open";
// Handle Windows
} else if (!strcmp(platform, "win32") || !strcmp(platform, "win64")) {
cmd = "start";
// Handle Linux, Unix, etc
} else if (!strcmp(platform, "unix")
|| !strcmp(platform, "linux")
|| !strcmp(platform, "freeBSD")
|| !strcmp(platform, "other")) {
cmd = "xdg-open";
}
char *script = create_cmd(cmd, url);
system(script);
free(script);
return 0;
}