-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathdirect.c
116 lines (96 loc) · 2.31 KB
/
direct.c
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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 ExoSkye
// Part of Microsoft CRT
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <windows.h>
// Made referencing https://www.digitalmars.com/rtl/direct.html, retrieved 2023-06-10, copyrighted 1999-2018 by Digital Mars
static int convert_error(DWORD winerror)
{
switch (winerror) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
case ERROR_FILENAME_EXCED_RANGE:
case ERROR_BAD_PATHNAME:
return ENOENT;
break;
case ERROR_ALREADY_EXISTS:
case ERROR_FILE_EXISTS:
return EEXIST;
break;
case ERROR_NOT_LOCKED:
case ERROR_ACCESS_DENIED:
return EACCES;
break;
case ERROR_INVAID_PARAMETER:
case ERROR_INVALID_FUNCTION:
return EINVAL;
break;
case ERROR_NOT_ENOUGH_MEMORY:
return ENOMEM;
break;
case ERROR_DIR_NOT_EMPTY:
return ENOTEMPTY;
break;
}
}
/*
* TODO: Make a working directory system?
*/
int _chdir(char* path) {
assert(0);
return -1; // Unreachable
}
int _chdrive(int drive) {
assert(0);
return -1; // Unreachable
}
char* _getcwd(char* buffer, size_t length) {
assert(0);
return NULL; // Unreachable
}
char* _getwd(char* path_name) {
assert(0);
return NULL; // Unreachable
}
int _getdrive(void) {
assert(0);
return -1; // Unreachable
}
/*
* Below are the only things that can work
*/
int _mkdir(const char* pathname) {
BOOL result = CreateDirectoryA(
pathname,
NULL
);
if (result == true) {
return 0;
} else {
DWORD err = GetLastError();
if (err == ERROR_ALREADY_EXISTS) {
errno = EACCES;
} else if (err == ERROR_PATH_NOT_FOUND) {
errno = ENOENT;
}
return -1;
}
}
int _rmdir(const char* pathname) {
BOOL result = RemoveDirectoryA(
pathname
);
if (result == true) {
return 0;
} else {
DWORD err = GetLastError();
if (err == ERROR_ALREADY_EXISTS) {
errno = EACCES;
} else if (err == ERROR_PATH_NOT_FOUND) {
errno = ENOENT;
}
return -1;
}
}