-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrealpath.c
37 lines (31 loc) · 897 Bytes
/
realpath.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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <windows.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
// Function to emulate the basic functionality of realpath on Linux in a Windows environment
char* realpath(const char* name, char* resolved) {
char* fullPath = NULL;
// If 'resolved' is not provided, allocate a buffer
if (resolved == NULL) {
fullPath = (char*)malloc(PATH_MAX);
if (fullPath == NULL) {
perror("malloc");
return NULL;
}
} else {
fullPath = resolved;
}
// Get the absolute path using GetFullPathNameA
DWORD result = GetFullPathNameA(name, PATH_MAX, fullPath, NULL);
if (result == 0 || result >= PATH_MAX) {
perror("GetFullPathNameA");
if (resolved == NULL) {
free(fullPath);
}
return NULL;
}
return fullPath;
}