Working version.
This commit is contained in:
@@ -20,38 +20,77 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
|
||||
char *expand_home_directory(const char *path) {
|
||||
if (path == NULL)
|
||||
return NULL;
|
||||
|
||||
if (path[0] == '~' && path[1] == '/') {
|
||||
const char *home_dir = getenv("HOME");
|
||||
if (home_dir == NULL) {
|
||||
#include <limits.h>
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (pw == NULL)
|
||||
return NULL;
|
||||
home_dir = pw->pw_dir;
|
||||
#endif
|
||||
|
||||
void get_current_directory() {
|
||||
char buffer[PATH_MAX];
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD length = GetCurrentDirectory(PATH_MAX, buffer);
|
||||
if (length > 0 && length < PATH_MAX) {
|
||||
printf("Current Directory: %s\n", buffer);
|
||||
} else {
|
||||
printf("Error getting current directory.\n");
|
||||
}
|
||||
#else
|
||||
if (getcwd(buffer, sizeof(buffer)) != NULL) {
|
||||
printf("Current Directory: %s\n", buffer);
|
||||
} else {
|
||||
perror("Error getting current directory");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
char* expand_home_directory(const char* path) {
|
||||
if (path[0] != '~') {
|
||||
return strdup(path); // Return the original path if it doesn't start with ~
|
||||
}
|
||||
|
||||
size_t home_len = strlen(home_dir);
|
||||
size_t path_len = strlen(path) - 1;
|
||||
char *expanded_path = malloc(home_len + path_len + 1);
|
||||
char* home_dir;
|
||||
|
||||
if (expanded_path == NULL)
|
||||
return NULL;
|
||||
#ifdef _WIN32
|
||||
home_dir = getenv("USERPROFILE"); // Get home directory on Windows
|
||||
#else
|
||||
struct passwd* pw = getpwuid(getuid());
|
||||
home_dir = pw->pw_dir; // Get home directory on Linux
|
||||
#endif
|
||||
|
||||
strcpy(expanded_path, home_dir);
|
||||
strcat(expanded_path, path + 1);
|
||||
if (home_dir == NULL) {
|
||||
return NULL; // Error getting home directory
|
||||
}
|
||||
|
||||
// Allocate memory for the expanded path
|
||||
size_t expanded_size = strlen(home_dir) + strlen(path);
|
||||
char* expanded_path = (char *)malloc(expanded_size);
|
||||
if (expanded_path == NULL) {
|
||||
return NULL; // Memory allocation error
|
||||
}
|
||||
|
||||
// Construct the expanded path
|
||||
snprintf(expanded_path, expanded_size, "%s%s", home_dir, path + 1);
|
||||
return expanded_path;
|
||||
} else {
|
||||
return strdup(path);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long hash(const char *str) {
|
||||
unsigned long hash = 5381; // Starting value
|
||||
int c;
|
||||
|
||||
while ((c = *str++)) {
|
||||
hash = ((hash << 5) + hash) + c; // hash * 33 + c
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
char *read_file(const char *path) {
|
||||
char *expanded_path = expand_home_directory(path);
|
||||
FILE *file = fopen(expanded_path, "r");
|
||||
|
||||
Reference in New Issue
Block a user