-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
92 lines (87 loc) · 1.53 KB
/
memory.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
#include "main.h"
/**
* _realloc - reallocates memory block
* @ptr: pointer to the previous memory
* @old_size: the old size
* @new_size: the new size
*
* Return: a pointer to the newly allocated memory
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *result;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr)
{
free(ptr);
return (NULL);
}
result = malloc(new_size);
if (result == NULL)
return (NULL);
if (ptr == NULL)
{
fill_an_array(result, '\0', new_size);
free(ptr);
}
else
{
_memcpy(result, ptr, old_size);
free(ptr);
}
return (result);
}
/**
* _memset - fills a memory with constant byte
* @s: pointer to memory area
* @c: first n bytes
* @n: constant byte
*
* Return: A pointer to a character
*/
void *_memset(void *s, int c, unsigned int n)
{
unsigned char* p=s;
while(n--)
{
*p++ = (unsigned char) c;
}
return (s);
}
/**
* free_data - frees data
* @data: the data structure
*
* Return: (Success) positive number
* ------- (Fail) negative number
*/
int free_data(sh_t *data)
{
free(data->line);
data->line = NULL;
free(data->args);
data->args = NULL;
free(data->cmd);
data->cmd = NULL;
free(data->env);
data->env = NULL;
return (0);
}
/**
* _memcpy - cpies memory area
* @dest: Destination memory area
* @src: Source memory area
* @n: Amount of memory byte
*
* Return: A pointer to dest
*/
char *_memcpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
{
dest[i] = src[i];
}
return (dest);
}