147 lines
3.4 KiB
C
Raw Normal View History

2025-03-16 07:36:13 +01:00
// Written by retoor@molodetz.nl
2025-03-28 06:56:36 +01:00
// This header file contains utility functions for manipulating file system
// paths, focusing primarily on expanding paths starting with '~' to the home
// directory.
2025-03-16 07:36:13 +01:00
2025-03-28 06:56:36 +01:00
// This code uses standard libraries: stdio.h, stdlib.h, and string.h, and
// conditionally includes the posix libraries pwd.h and unistd.h when expanding
// the home directory manually.
2025-03-16 07:36:13 +01:00
// MIT License
//
2025-03-28 06:56:36 +01:00
// Permission is granted to use, copy, modify, merge, distribute, sublicense,
// and/or sell copies of the Software. The license includes conditions about
// providing a copy of the license and the limitation of liability and warranty.
2025-03-16 07:36:13 +01:00
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2025-03-30 00:13:42 +01:00
#ifdef _WIN32
#include <windows.h>
#else
#include <limits.h>
#include <pwd.h>
2025-05-05 14:33:08 +02:00
#include <unistd.h>
2025-03-30 00:13:42 +01:00
#endif
void get_current_directory() {
2025-05-05 14:33:08 +02:00
char buffer[PATH_MAX];
2025-03-30 00:13:42 +01:00
#ifdef _WIN32
2025-05-05 14:33:08 +02:00
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");
}
2025-03-30 00:13:42 +01:00
#else
2025-05-05 14:33:08 +02:00
if (getcwd(buffer, sizeof(buffer)) != NULL) {
printf("Current Directory: %s\n", buffer);
} else {
perror("Error getting current directory");
}
2025-03-30 00:13:42 +01:00
#endif
}
2025-05-05 14:33:08 +02:00
char *expand_home_directory(const char *path) {
if (path[0] != '~') {
return strdup(path); // Return the original path if it doesn't start with ~
}
2025-03-16 07:36:13 +01:00
2025-05-05 14:33:08 +02:00
char *home_dir;
2025-03-16 07:36:13 +01:00
2025-03-30 00:13:42 +01:00
#ifdef _WIN32
2025-05-05 14:33:08 +02:00
home_dir = getenv("USERPROFILE"); // Get home directory on Windows
2025-03-30 00:13:42 +01:00
#else
2025-05-05 14:33:08 +02:00
struct passwd *pw = getpwuid(getuid());
home_dir = pw->pw_dir; // Get home directory on Linux
2025-03-30 00:13:42 +01:00
#endif
2025-03-16 07:36:13 +01:00
2025-05-05 14:33:08 +02:00
if (home_dir == NULL) {
return NULL; // Error getting home directory
}
2025-03-16 07:36:13 +01:00
2025-12-18 01:08:38 +01:00
size_t home_len = strlen(home_dir);
size_t path_len = strlen(path + 1);
size_t expanded_size = home_len + path_len + 1;
2025-05-05 14:33:08 +02:00
char *expanded_path = (char *)malloc(expanded_size);
if (expanded_path == NULL) {
2025-12-18 01:08:38 +01:00
return NULL;
2025-05-05 14:33:08 +02:00
}
2025-03-30 00:13:42 +01:00
2025-12-18 01:08:38 +01:00
memcpy(expanded_path, home_dir, home_len);
memcpy(expanded_path + home_len, path + 1, path_len);
expanded_path[home_len + path_len] = '\0';
2025-05-05 14:33:08 +02:00
return expanded_path;
2025-03-16 07:36:13 +01:00
}
2025-03-30 00:13:42 +01:00
unsigned long hash(const char *str) {
2025-05-05 14:33:08 +02:00
unsigned long hash = 5381; // Starting value
int c;
2025-03-30 00:13:42 +01:00
2025-05-05 14:33:08 +02:00
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c; // hash * 33 + c
}
2025-03-30 00:13:42 +01:00
2025-05-05 14:33:08 +02:00
return hash;
2025-03-30 00:13:42 +01:00
}
2025-05-05 14:33:08 +02:00
char *joinpath(const char *base_url, const char *path) {
2025-12-18 01:08:38 +01:00
static char result[4096];
size_t base_len = strlen(base_url);
size_t pos = 0;
if (base_len >= sizeof(result) - 2) {
base_len = sizeof(result) - 2;
}
memcpy(result, base_url, base_len);
pos = base_len;
if (pos > 0 && result[pos - 1] != '/') {
result[pos++] = '/';
2025-05-05 14:33:08 +02:00
}
if (path[0] == '/') {
path++;
}
2025-12-18 01:08:38 +01:00
size_t path_len = strlen(path);
if (pos + path_len >= sizeof(result)) {
path_len = sizeof(result) - pos - 1;
}
memcpy(result + pos, path, path_len);
result[pos + path_len] = '\0';
2025-05-05 14:33:08 +02:00
return result;
2025-04-03 02:18:41 +02:00
}
2025-03-30 00:13:42 +01:00
2025-03-28 06:56:36 +01:00
char *read_file(const char *path) {
char *expanded_path = expand_home_directory(path);
2025-12-18 01:08:38 +01:00
if (expanded_path == NULL) {
return NULL;
}
2025-03-28 06:56:36 +01:00
FILE *file = fopen(expanded_path, "r");
free(expanded_path);
if (file == NULL) {
return NULL;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
2025-03-16 22:46:09 +01:00
2025-03-28 06:56:36 +01:00
char *buffer = (char *)malloc(size + 1);
size_t read = fread(buffer, 1, size, file);
if (read == 0) {
free(buffer);
return NULL;
}
2025-03-16 07:36:13 +01:00
2025-03-28 06:56:36 +01:00
fclose(file);
buffer[read] = '\0';
return buffer;
2025-03-16 22:46:09 +01:00
}
#endif