-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
99 lines (79 loc) · 1.83 KB
/
utils.c
File metadata and controls
99 lines (79 loc) · 1.83 KB
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
#include "headers/utils.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
// Expand tilde in path
char *expand_tilde(char *path) {
if (!path)
return NULL;
if (path[0] == '~') {
char *home = getenv("HOME");
if (!home) {
return strdup(path); // Return original if HOME not set
}
int home_len = strlen(home);
int path_len = strlen(path);
char *expanded = malloc(home_len + path_len);
if (!expanded) {
return NULL;
}
strcpy(expanded, home);
strcat(expanded, path + 1); // Skip the '~'
return expanded;
}
return strdup(path);
}
// Get absolute path
char *get_absolute_path(char *path) {
if (!path)
return NULL;
char *abs_path = malloc(PATH_MAX);
if (!abs_path)
return NULL;
if (realpath(path, abs_path) == NULL) {
free(abs_path);
return strdup(path); // Return original if realpath fails
}
return abs_path;
}
// Check if file is executable
int is_executable(char *path) {
if (!path)
return 0;
struct stat st;
if (stat(path, &st) == 0) {
return (st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) ||
(st.st_mode & S_IXOTH);
}
return 0;
}
// Print error message
void print_error(char *msg) { fprintf(stderr, "mini-bash: %s\n", msg); }
// Print error message with errno
void print_error_with_errno(char *msg) {
fprintf(stderr, "mini-bash: %s: %s\n", msg, strerror(errno));
}
// String duplicate (for compatibility with older systems)
#ifndef _POSIX_C_SOURCE
char *strdup(const char *s) {
if (!s)
return NULL;
size_t len = strlen(s) + 1;
char *dup = malloc(len);
if (dup) {
memcpy(dup, s, len);
}
return dup;
}
#endif
// Free string array
void free_string_array(char **arr, int count) {
if (!arr)
return;
for (int i = 0; i < count; i++) {
if (arr[i]) {
free(arr[i]);
}
}
free(arr);
}