-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
155 lines (134 loc) Β· 3.75 KB
/
utils.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "cell.h"
/**
* Getcwd - Gets current working directory with error handling
* @buf: Buffer to store path
* @size: Size of buffer
* Return: Pointer to buf on success, NULL on failure
* Corner cases:
* - NULL buffer with zero size: allocates buffer
* - Buffer too small: prints error
* - Permission denied: prints error
*/
void Getcwd(char *buf, size_t size){
if(!NULL == getcwd(buf, size)){ //this will get the current working directory
perror(RED"getcwd failed"RST);
}
}
pid_t Fork(void)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror(RED"Fork failed"RST);
exit(EX_OSERR);
}
return (pid);
}
void Execvp(const char *file, char *const argv[])
{
if (!file || !argv)
{
fprintf(stderr, RED"Execvp: invalid arguments\n"RST);
exit(EXIT_FAILURE);
}
if (execvp(file, argv) == -1)
{
perror(RED"π₯CELL_Jr failedπ₯"RST);
exit(EX_UNAVAILABLE);
}
}
pid_t Wait(int *status)
{
pid_t result;
if (!status)
{
fprintf(stderr, RED"Wait: status argument required\n"RST);
return (-1);
}
result = wait(status);
if (result == -1)
perror(RED"Wait failed"RST);
if (WIFEXITED(*status))
*status = WEXITSTATUS(*status);
return (result);
}
/**
* Malloc - Allocates memory with error handling
* @size: Number of bytes to allocate
* Return: Pointer to allocated memory
* Corner cases:
* - Zero size: returns NULL
* - Allocation failure: prints error and exits
*/
void *Malloc(size_t size){
void *ptr;
if(size == 0){
return (NULL);
}
ptr = malloc(size);
if(!ptr){
perror(RED"malloc failed"RST);
exit(EXIT_FAILURE);
}
return (ptr);
}
/**
* Realloc - Reallocates memory with error handling
* @ptr: Pointer to previously allocated memory
* @size: New size in bytes
* Return: Pointer to reallocated memory
* Corner cases:
* - NULL ptr: acts as malloc
* - Zero size: acts as free
* - Allocation failure: prints error and exits
*/
void *Realloc(void *ptr, size_t size)
{
void *new_ptr;
new_ptr = realloc(ptr, size);
if (!new_ptr && size != 0)
{
perror(RED"Realloc failed"RST);
exit(EXIT_FAILURE);
}
return (new_ptr);
}
void dbzSpinnerLoading()
{
const char *charging[] = {
"[ ]",
"[= ]",
"[== ]",
"[=== ]",
"[==== ]",
"[===== ]",
"[====== ]",
"[======= ]",
"[======== ]",
"[========= ]",
"[========== ]",
"[=========== ]",
"[===========π₯]",
"π Byeπ Byeπ Bye!",
};
const int frames = sizeof(charging) / sizeof(charging[0]);
p(RED"Shutting down...\n"RST);
// Loop through the "charging" animation for 3 seconds
for (int i = 0; i < frames; i++) {
p("\r" Y "%s" RST, charging[i]);
fflush(stdout); // Force update the console
usleep(421337);
}
p(C"\nβ
EXIT β
\n"RST);
//exit(EX_OK);
}
void printbanner(void){
p(G" ββββββββββββββββββ βββ βββ βββ βββ\n"
"βββββββββββββββββββ βββ βββ βββ ββββ\n"
"βββ ββββββ βββ βββ βββ βββ ββββ\n"
"βββ ββββββ βββ βββ ββββ ββββ βββ\n"
"ββββββββββββββββββββββββββββββββ βββββββββββββ\n"
" βββββββββββββββββββββββββββββββ βββββ ββββββ\n"
"Made with β€οΈ by Abhilov \n"RST);
}