]> icculus.org git repositories - taylor/freespace2.git/blob - src/platform/unix.cpp
f1f04988f3ab8e0cec2af9d212de42c2731cd0d3
[taylor/freespace2.git] / src / platform / unix.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #ifdef PLAT_UNIX
10
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <unistd.h>
17
18 #include "SDL.h"
19
20
21 int filelength (int fd)
22 {
23         struct stat buf;
24         if (fstat (fd, &buf) == -1)
25                 return -1;
26                 
27         return buf.st_size;
28 }
29
30 int WSAGetLastError()
31 {
32         return errno;
33 }
34
35 int platform_open_url(const char *url)
36 {
37 #ifdef __APPLE__
38         const char *open_cmd = "open";
39 #else
40         const char *open_cmd = "xdg-open";
41 #endif
42         char s_url[256];
43         int statval = 0;
44
45         // make sure it's a valid www address
46         if ( !SDL_strncasecmp(url, "http://", 7) || !SDL_strncasecmp(url, "https://", 8) ) {
47                 SDL_strlcpy(s_url, url, SDL_arraysize(s_url));
48         } else {
49                 SDL_strlcpy(s_url, "http://", SDL_arraysize(s_url));
50                 SDL_strlcat(s_url, url, SDL_arraysize(s_url));
51         }
52
53         pid_t mpid = fork();
54
55         if (mpid < 0) {
56                 // nothing, will return error
57         } else if (mpid == 0) {
58                 int rv = 0;
59
60                 rv = execlp(open_cmd, open_cmd, s_url, (char *)0);
61
62                 exit(rv);
63         } else {
64                 waitpid(mpid, &statval, 0);
65
66                 if ( WIFEXITED(statval) ) {
67                         if (WEXITSTATUS(statval) == 0) {
68                                 return 0;
69                         } else {
70                                 return -1;
71                         }
72                 }
73         }
74
75         return -1;
76 }
77
78 unsigned int platform_get_kmod()
79 {
80         return 0;
81 }
82
83 void platform_init()
84 {
85         // nothing
86 }
87
88 void platform_close()
89 {
90         // nothing
91 }
92
93 #endif