-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_func2.c
More file actions
171 lines (157 loc) · 3.07 KB
/
_func2.c
File metadata and controls
171 lines (157 loc) · 3.07 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "main.h"
/**
* _strcpy - Prints a string to stdout
* @destin: buffer to set
* @source: buffer to get from
* Return: char pointer if success or NULL
*/
char *_strcpy(char *destin, const char *source)
{
char *ptr;
if (!destin)
return (NULL);
ptr = destin;
while (*source != '\0')
{
*destin = *source;
destin++;
source++;
}
*destin = '\0';
return (ptr);
}
/**
* _tokenize - Splits a string into tokens.
* @buf: The input string to tokenize.
* @args: The array to store the tokens.
* @del: The delimiter to use for tokenization.
* @str: The string to store the first token separately.
* @env: The array of environment variables.
*/
void tok_buf(char *buf, char *args[],
const char *del, char *str, char *env[])
{
int i = 0, j = 0;
char *token, *find_path = NULL, get_cmd[50], new_buf[512];
if (buf[i] != '/' && buf[i] != '.')
{
while (buf[i])
{
if (buf[i] == ' ')
break;
get_cmd[i] = buf[i];
i++;
}
get_cmd[i] = '\0';
find_path = full_path(env, get_cmd);
while (find_path[j] != '\0')
{
new_buf[j] = find_path[j];
j++;
}
while (buf[i])
new_buf[j++] = buf[i++];
new_buf[j] = '\0';
_strcpy(buf, new_buf), free(find_path);
}
else if (buf[i] == '.' && buf[i + 1] == '/')
{
j = 0;
i += 2;
if (getcwd(new_buf, sizeof(new_buf)) == NULL)
perror("getcwd"), exit(EXIT_FAILURE);
while (new_buf[j])
j++;
new_buf[j++] = '/';
while (buf[i])
new_buf[j++] = buf[i++];
new_buf[j] = '\0';
_strcpy(buf, new_buf);
}
tok(&buf, &del, &token, &str, args);
}
/**
*_strdup - Duplicates a string
*@str: The string to duplicate
*Return: The pointer to the duplicated string, or NULL if it fails
*/
char *_strdup(const char *str)
{
size_t len = 0;
const char *s = str;
char *c, *copy;
while (*s++)
len++;
copy = malloc((len + 1) * sizeof(char));
if (copy != NULL)
{
c = copy;
while (*str)
*c++ = *str++;
*c = '\0';
}
return (copy);
}
/**
* tostring - Convert an integer to a string and print it
* @num: The integer to convert and print
*
* This function takes an integer `num` and converts it to
* a string representation. The resulting string is then printed
* using the `write` function. The string is printed to the standard
* output with a length equal to the number of digits in the integer.
*/
void tostring(int num)
{
char str[20];
int i, rem, len = 0, n, dec;
n = num;
while (n != 0)
{
len++;
n /= 10;
}
dec = len - 1;
for (i = 0; i < len; i++)
{
rem = num % 10;
num = num / 10;
str[dec] = rem + '0';
dec--;
}
str[len] = '\0';
write(1, str, len);
}
/**
* _printf - Custom implementation of printf
* @format: Format string
* @...: Variable number of arguments
*/
void _printf(const char *format, ...)
{
char *str;
int num;
va_list args;
va_start(args, format);
while (*format != '\0')
{
if (*format == '%')
{
format++;
if (*format == 's')
{
str = va_arg(args, char*);
write(1, str, _strlen(str));
} else if (*format == 'd')
{
num = va_arg(args, int);
tostring(num);
}
} else
{
write(1, format, 1);
}
format++;
}
va_end(args);
}