|
1 | 1 | #include "headers/utils.h" |
| 2 | +#include <limits.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
2 | 5 |
|
3 | 6 | // Expand tilde in path |
4 | | -char* expand_tilde(char *path) { |
5 | | - if (!path) return NULL; |
6 | | - |
7 | | - if (path[0] == '~') { |
8 | | - char *home = getenv("HOME"); |
9 | | - if (!home) { |
10 | | - return strdup(path); // Return original if HOME not set |
11 | | - } |
12 | | - |
13 | | - int home_len = strlen(home); |
14 | | - int path_len = strlen(path); |
15 | | - char *expanded = malloc(home_len + path_len); |
16 | | - |
17 | | - if (!expanded) { |
18 | | - return NULL; |
19 | | - } |
20 | | - |
21 | | - strcpy(expanded, home); |
22 | | - strcat(expanded, path + 1); // Skip the '~' |
23 | | - |
24 | | - return expanded; |
| 7 | +char *expand_tilde(char *path) { |
| 8 | + if (!path) |
| 9 | + return NULL; |
| 10 | + |
| 11 | + if (path[0] == '~') { |
| 12 | + char *home = getenv("HOME"); |
| 13 | + if (!home) { |
| 14 | + return strdup(path); // Return original if HOME not set |
25 | 15 | } |
26 | | - |
27 | | - return strdup(path); |
| 16 | + |
| 17 | + int home_len = strlen(home); |
| 18 | + int path_len = strlen(path); |
| 19 | + char *expanded = malloc(home_len + path_len); |
| 20 | + |
| 21 | + if (!expanded) { |
| 22 | + return NULL; |
| 23 | + } |
| 24 | + |
| 25 | + strcpy(expanded, home); |
| 26 | + strcat(expanded, path + 1); // Skip the '~' |
| 27 | + |
| 28 | + return expanded; |
| 29 | + } |
| 30 | + |
| 31 | + return strdup(path); |
28 | 32 | } |
29 | 33 |
|
30 | 34 | // Get absolute path |
31 | | -char* get_absolute_path(char *path) { |
32 | | - if (!path) return NULL; |
33 | | - |
34 | | - char *abs_path = malloc(PATH_MAX); |
35 | | - if (!abs_path) return NULL; |
36 | | - |
37 | | - if (realpath(path, abs_path) == NULL) { |
38 | | - free(abs_path); |
39 | | - return strdup(path); // Return original if realpath fails |
40 | | - } |
41 | | - |
42 | | - return abs_path; |
| 35 | +char *get_absolute_path(char *path) { |
| 36 | + if (!path) |
| 37 | + return NULL; |
| 38 | + |
| 39 | + char *abs_path = malloc(PATH_MAX); |
| 40 | + if (!abs_path) |
| 41 | + return NULL; |
| 42 | + |
| 43 | + if (realpath(path, abs_path) == NULL) { |
| 44 | + free(abs_path); |
| 45 | + return strdup(path); // Return original if realpath fails |
| 46 | + } |
| 47 | + |
| 48 | + return abs_path; |
43 | 49 | } |
44 | 50 |
|
45 | 51 | // Check if file is executable |
46 | 52 | int is_executable(char *path) { |
47 | | - if (!path) return 0; |
48 | | - |
49 | | - struct stat st; |
50 | | - if (stat(path, &st) == 0) { |
51 | | - return (st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH); |
52 | | - } |
53 | | - |
| 53 | + if (!path) |
54 | 54 | return 0; |
| 55 | + |
| 56 | + struct stat st; |
| 57 | + if (stat(path, &st) == 0) { |
| 58 | + return (st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || |
| 59 | + (st.st_mode & S_IXOTH); |
| 60 | + } |
| 61 | + |
| 62 | + return 0; |
55 | 63 | } |
56 | 64 |
|
57 | 65 | // Print error message |
58 | | -void print_error(char *msg) { |
59 | | - fprintf(stderr, "mini-bash: %s\n", msg); |
60 | | -} |
| 66 | +void print_error(char *msg) { fprintf(stderr, "mini-bash: %s\n", msg); } |
61 | 67 |
|
62 | 68 | // Print error message with errno |
63 | 69 | void print_error_with_errno(char *msg) { |
64 | | - fprintf(stderr, "mini-bash: %s: %s\n", msg, strerror(errno)); |
| 70 | + fprintf(stderr, "mini-bash: %s: %s\n", msg, strerror(errno)); |
65 | 71 | } |
66 | 72 |
|
67 | 73 | // String duplicate (for compatibility with older systems) |
68 | 74 | #ifndef _POSIX_C_SOURCE |
69 | | -char* strdup(const char *s) { |
70 | | - if (!s) return NULL; |
71 | | - |
72 | | - size_t len = strlen(s) + 1; |
73 | | - char *dup = malloc(len); |
74 | | - if (dup) { |
75 | | - memcpy(dup, s, len); |
76 | | - } |
77 | | - return dup; |
| 75 | +char *strdup(const char *s) { |
| 76 | + if (!s) |
| 77 | + return NULL; |
| 78 | + |
| 79 | + size_t len = strlen(s) + 1; |
| 80 | + char *dup = malloc(len); |
| 81 | + if (dup) { |
| 82 | + memcpy(dup, s, len); |
| 83 | + } |
| 84 | + return dup; |
78 | 85 | } |
79 | 86 | #endif |
80 | 87 |
|
81 | 88 | // Free string array |
82 | 89 | void free_string_array(char **arr, int count) { |
83 | | - if (!arr) return; |
84 | | - |
85 | | - for (int i = 0; i < count; i++) { |
86 | | - if (arr[i]) { |
87 | | - free(arr[i]); |
88 | | - } |
| 90 | + if (!arr) |
| 91 | + return; |
| 92 | + |
| 93 | + for (int i = 0; i < count; i++) { |
| 94 | + if (arr[i]) { |
| 95 | + free(arr[i]); |
89 | 96 | } |
90 | | - free(arr); |
| 97 | + } |
| 98 | + free(arr); |
91 | 99 | } |
0 commit comments