-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_func2.c
More file actions
146 lines (118 loc) · 2.25 KB
/
b_func2.c
File metadata and controls
146 lines (118 loc) · 2.25 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
#include "main.h"
/**
*_calloc -allocated memoria for nmeb elemn de zise bytes
*@nmemb: number of element in the array
*@size: bytes for each position in the array
*Return: pointer void
*/
void *_calloc(unsigned int nmemb, unsigned int size)
{
char *p;
unsigned int i;
if (nmemb == 0 || size == 0)
return (NULL);
p = malloc(nmemb * size);
if (p == NULL)
return (NULL);
for (i = 0; i < nmemb * size; i++)
p[i] = 0;
return (p);
}
/**
*_puts - prints a string
*@str: A to be printed
*
*Return: void
*/
void _puts(char *str)
{
int i;
for (i = 0 ; str[i] != '\0' ; i++)
{
_putchar(str[i]);
}
_putchar('\n');
}
/**
* search - gets the path to execute commands
* @environ: Environment variable
* Return: kalat_path (array of directories containing the command)
* or NULL on failure
**/
char **search(char **environ)
{
int position = 0;
char **kalat_path;
for (; environ[position] != NULL ; position++)
{
if (environ[position][0] == 'P' && environ[position][2] == 'T')
{
kalat_path = _which(environ[position]);
}
}
return (kalat_path);
}
/**
* _itoa - converts an integer to ASCII
* @num: number
* @base: base
* reference: geeksforgeeks
* Return: character string
**/
char *_itoa(int num, int base)
{
static char *digits = "0123456789abcdef";
static char buffer[50];
char sign = 0;
char *ptr;
unsigned long n = (unsigned long)num;
if (num < 0)
{
n = (unsigned long)(-num);
sign = '-';
}
ptr = &buffer[49];
*ptr = '\0';
do {
*--ptr = digits[n % base];
n /= base;
} while (n != 0);
if (sign)
*--ptr = sign;
return (ptr);
}
/**
* str_concat - concatenate two strings
* @s1: first string
* @s2: second string
*
* Return: pointer to the concatenated string
*/
char *str_concat(char *s1, char *s2)
{
char *dest;
unsigned int i, j, size;
/* If the array is empty */
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
/* Count total size */
size = (_strlen(s1) + _strlen(s2) + 1);
/* Allocate memory */
dest = (char *) malloc(size * sizeof(char));
if (dest == 0)
{
return (NULL);
}
/* Concatenate arrays */
for (i = 0; *(s1 + i) != '\0'; i++)
*(dest + i) = *(s1 + i);
for (j = 0; *(s2 + j) != '\0'; j++)
{
*(dest + i) = *(s2 + j);
i++;
}
dest[i] = '\0';
return (dest);
}