aboutsummaryrefslogtreecommitdiff
path: root/src/os.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/os.cpp')
-rw-r--r--src/os.cpp53
1 files changed, 18 insertions, 35 deletions
diff --git a/src/os.cpp b/src/os.cpp
index 470d222307..83c67d5818 100644
--- a/src/os.cpp
+++ b/src/os.cpp
@@ -751,39 +751,15 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {
#endif
}
-Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
+Error os_fetch_file(FILE *f, Buf *out_buf) {
static const ssize_t buf_size = 0x2000;
buf_resize(out_buf, buf_size);
ssize_t actual_buf_len = 0;
- bool first_read = true;
-
for (;;) {
size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f);
actual_buf_len += amt_read;
- if (skip_shebang && first_read && buf_starts_with_str(out_buf, "#!")) {
- size_t i = 0;
- while (true) {
- if (i > buf_len(out_buf)) {
- zig_panic("shebang line exceeded %zd characters", buf_size);
- }
-
- size_t current_pos = i;
- i += 1;
-
- if (out_buf->list.at(current_pos) == '\n') {
- break;
- }
- }
-
- ZigList<char> *list = &out_buf->list;
- memmove(list->items, list->items + i, list->length - i);
- list->length -= i;
-
- actual_buf_len -= i;
- }
-
if (amt_read != buf_size) {
if (feof(f)) {
buf_resize(out_buf, actual_buf_len);
@@ -794,7 +770,6 @@ Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
}
buf_resize(out_buf, actual_buf_len + buf_size);
- first_read = false;
}
zig_unreachable();
}
@@ -864,8 +839,8 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
FILE *stdout_f = fdopen(stdout_pipe[0], "rb");
FILE *stderr_f = fdopen(stderr_pipe[0], "rb");
- Error err1 = os_fetch_file(stdout_f, out_stdout, false);
- Error err2 = os_fetch_file(stderr_f, out_stderr, false);
+ Error err1 = os_fetch_file(stdout_f, out_stdout);
+ Error err2 = os_fetch_file(stderr_f, out_stderr);
fclose(stdout_f);
fclose(stderr_f);
@@ -1097,7 +1072,7 @@ Error os_copy_file(Buf *src_path, Buf *dest_path) {
}
}
-Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
+Error os_fetch_file_path(Buf *full_path, Buf *out_contents) {
FILE *f = fopen(buf_ptr(full_path), "rb");
if (!f) {
switch (errno) {
@@ -1116,7 +1091,7 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
return ErrorFileSystem;
}
}
- Error result = os_fetch_file(f, out_contents, skip_shebang);
+ Error result = os_fetch_file(f, out_contents);
fclose(f);
return result;
}
@@ -1772,8 +1747,14 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) {
// TODO use /etc/passwd
return ErrorFileNotFound;
}
- buf_resize(out_path, 0);
- buf_appendf(out_path, "%s/.local/share/%s", home_dir, appname);
+ if (home_dir[0] == 0) {
+ return ErrorFileNotFound;
+ }
+ buf_init_from_str(out_path, home_dir);
+ if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {
+ buf_append_char(out_path, '/');
+ }
+ buf_appendf(out_path, ".local/share/%s", appname);
return ErrorNone;
#endif
}
@@ -2081,11 +2062,13 @@ Error os_file_overwrite(OsFile file, Buf *contents) {
#endif
}
-void os_file_close(OsFile file) {
+void os_file_close(OsFile *file) {
#if defined(ZIG_OS_WINDOWS)
- CloseHandle(file);
+ CloseHandle(*file);
+ *file = NULL;
#else
- close(file);
+ close(*file);
+ *file = -1;
#endif
}