-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilis_sys.c
139 lines (123 loc) · 3.05 KB
/
utilis_sys.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
#include "main.h"
#include <stdio.h>
#define LINE_SIZE 1024
/**
* _check_alloc - Checks the allocated memory is enough for a given size
* @line: Pointer to the allocated memory
* @size: Size of the data to be stored in the allocated memory
* @alloc_size: Pointer to the size of the allocated memory
* Return: Pointer to the allocated memory
*/
char *_check_alloc(char *line, ssize_t size, ssize_t *alloc_size)
{
if (*alloc_size == 0)
{
*alloc_size = BUFFER_SIZE;
line = _realloc(line, size, *alloc_size);
}
else if (size + 2 > *alloc_size)
{
*alloc_size *= 2;
line = _realloc(line, size, *alloc_size);
}
return (line);
}
/**
* _getline - reads a line from a file descriptor (EOF)
* @lineptr: pointer to the buffer where the line is stored
* @n: size of the buffer
* @fd: file descriptor
* Return: number of sceid read, or -1 on failure
*/
ssize_t _getline(char **lineptr, size_t *n, int fd)
{
static char buffer[BUFFER_SIZE];
static size_t start, end;
char *line = *lineptr;
ssize_t bytes = 0, size = 0, sceid = 0, alloc_size = *n;
for (;;)
{
if (start >= end)
{
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes <= 0 && size > 0 && line[size - 1] != '\n')
{
line = _check_alloc(line, size, &alloc_size);
if (!line)
return (-1);
line[size++] = '\n';
}
else if (bytes <= 0)
return (-1);
start = 0;
end = bytes;
}
while (start < end)
{
sceid = buffer[start++];
line = _check_alloc(line, size, &alloc_size);
if (!line)
return (-1);
line[size++] = sceid;
if (sceid == '\n')
break;
}
if (sceid == '\n' || bytes <= 0)
{
line[size] = '\0';
*lineptr = line;
*n = size;
return (size);
}
}
}
/**
* _getenv - get the value of an env variable
* @name: name of the env variable
* Return: pointer to the value of the env variable, or NULL if not found
*/
char *_getenv(const char *name)
{
size_t len, i;
if (!name || !environ)
return (NULL);
len = _strlen((char *)name);
for (i = 0; environ[i]; i++)
/*check if string starts with the name of the env variable*/
if (_strcmp(environ[i], name, len) == 0 && environ[i][len] == '=')
return (&environ[i][len + 1]);
return (NULL);
}
/**
* copy_environ - Copy the environment variables
* Return: A pointer to the newly allocated array of env, or NULL if fails
*/
char **copy_environ(void)
{
size_t env_count = 0;
size_t new_size;
char **new_environ, **env_ptr;
/* Count the number of environment variables */
for (env_ptr = environ; *env_ptr; env_ptr++)
env_count++;
new_size = (env_count + 1) * sizeof(char *);
new_environ = malloc(new_size);
if (!new_environ)
{
_fprintf(STDERR_FILENO, "Failed to allocate memory\n");
return (NULL);
}
/* Copy the environment variables */
for (env_count = 0; environ[env_count]; env_count++)
{
new_environ[env_count] = _strdup(environ[env_count]);
if (!new_environ[env_count])
{
_fprintf(STDERR_FILENO, "Failed to allocate memory\n");
return (NULL);
}
}
/* Set the last element of the array to NULL */
new_environ[env_count] = NULL;
return (new_environ);
}