aboutsummaryrefslogtreecommitdiff
path: root/compiler/platform.c
blob: 9b814b944711c497212fe4adb1e046251ee56257 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

/*

  platform.c -- Platform-specific functions

  Copyright © 2012-2013 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#ifndef _WIN32
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE 1
#define _BSD_SOURCE 1
#endif

#include "platform.h"
#include <string.h>


char *join_paths(const char *a, const char *b)
{
    char *path = malloc(strlen(a)+1+strlen(b)+1);
    sprintf(path, "%s" PATHSEP "%s", a, b);
    return path;
}


/* * * * Windows/Wine/ReactOS * * * */
#ifdef _WIN32 /* also 64-bit */

#include <shlobj.h>
#include <string.h>
#include "misc.h"

const LRLPath *const lrl_platform_includes;

static char *get_sys_path(int pathid, const char *subdir)
{
    /* TODO use wide-char functions and convert to UTF-8 (char*). Then convert
       back to wide-char when loading files, starting processes, etc. */
    char path[MAX_PATH];
    
    HRESULT res = SHGetFolderPath(NULL, pathid, NULL, 0, path);

    if (!SUCCEEDED(res) || strlen(path) >= MAX_PATH-strlen(subdir)-1) return NULL;
    
    strcat(path, subdir);
    return lrl_strdup(path);
}

static LRLPath sys_include = { NULL, NULL };
const LRLPath *const lrl_platform_includes = &sys_include;

static LRLPath dist_config = { NULL, NULL };
static LRLPath sys_config = { &dist_config, NULL };
static LRLPath user_config = { &sys_config, NULL };
const LRLPath *const lrl_platform_config = &user_config;

void lrl_platform_init_paths(void)
{
    sys_include.path = get_sys_path(CSIDL_PROGRAM_FILES_COMMON, "\\LRL Includes");
    
    dist_config.path = get_sys_path(CSIDL_PROGRAM_FILES_COMMON, "\\lrlc\\conf");
    sys_config.path = get_sys_path(CSIDL_COMMON_APPDATA, "\\lrlc");
    user_config.path = get_sys_path(CSIDL_APPDATA, "\\lrlc");
}


int check_not_dir(FILE *file)
{
    (void)file;
    return 1; /* Directories can't be "opened" under Windows */
}

int mark_executable(FILE *file, const char *filename)
{
    (void)file;
    (void)filename;
    return 1;
}

int pclose_exit_ok(int status)
{
    /* pclose returns the exit status directly under Windows. But it
       seems that the exit status is not propagated reliably. E.g.
       when I tested this function with GCC under Wine the status code
       was always zero.  -- SLB 2013-08-31 */
    return status == 0;
}

#else
/* * * * Others. Assume POSIX compliant * * * */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>


/* TODO should include the include dir specified in the Makefile, if its
        neither /usr/share/lrl-includes nor /usr/share/local/lrl-includes */
static const LRLPath usr_include = { NULL, "/usr/share/lrl-includes" };
static const LRLPath local_include = { &usr_include, "/usr/local/share/lrl-includes" };
const LRLPath *const lrl_platform_includes = &local_include;

static const LRLPath dist_config = { NULL, "/usr/share/lrlc/conf" }; /* TODO should use install prefix */
static const LRLPath sys_config = { &dist_config, "/etc/lrlc" };
static LRLPath user_config = { &sys_config, NULL };
const LRLPath *const lrl_platform_config = &user_config;

void lrl_platform_init_paths(void)
{
    const char *xdgconf = getenv("XDG_CONFIG_HOME");
    
    if (xdgconf) {
        user_config.path = join_paths(xdgconf, "lrlc");
    } else {
        const char *home = getenv("HOME");
        if (home) {
            user_config.path = join_paths(home, ".config/lrlc");
        }
    }
    
    /* TODO should use XDG_CONFIG_DIRS also (before XDG_CONFIG_HOME) */
}

int check_not_dir(FILE *file)
{
    struct stat st;
    int fd = fileno(file);
    
    if (fd == -1 || fstat(fd, &st) == -1) return 1;
    
    if (S_ISDIR(st.st_mode)) {
        errno = EISDIR;
        return 0;
    }
    
    return 1;
}

int mark_executable(FILE *file, const char *filename)
{
    struct stat st;
    mode_t mode;
    int fd = fileno(file);
    
    if (fd == -1 || fstat(fd, &st) == -1) goto error;
    
    mode = st.st_mode;
    if (mode & S_IRUSR) mode |= S_IXUSR;
    if (mode & S_IRGRP) mode |= S_IXGRP;
    if (mode & S_IROTH) mode |= S_IXOTH;
    
    if (fchmod(fd, mode) == -1) goto error;
    
    return 1;
    
  error:
    perror(filename);
    return 0;
}

int pclose_exit_ok(int status)
{
    return WIFEXITED(status) && WEXITSTATUS(status) == 0;
}

#endif