-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_memo.c
89 lines (70 loc) · 1.32 KB
/
shell_memo.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
#include "shell.h"
/* rayesyounes - spoutnikrs */
/**
* memo_set - fill memo with const byte
* @s: pointer to memo area
* @b: byte to fill
* @n: amount of byte field
* Return: pointer to area memo
*/
char *memo_set(char *s, char b, unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
s[i] = b;
return (s);
}
/**
* free_strn - free string of strings
* @s: string of strings
* Return: void
*/
void free_strn(char **s)
{
char **a = s;
if (!s)
return;
while (*s)
free(*s++);
free(a);
}
/**
* memo_reallocat - reallocation of block memo
* @ptr: pointer to prev malloc
* @old_size: size of prev block
* @new_size: new block size
* Return: pointer
*/
void *memo_reallocat(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *p;
if (!ptr)
return (malloc(new_size));
if (!new_size)
return (free(ptr), NULL);
if (new_size == old_size)
return (ptr);
p = malloc(new_size);
if (!p)
return (NULL);
old_size = old_size < new_size ? old_size : new_size;
while (old_size--)
p[old_size] = ((char *)ptr)[old_size];
free(ptr);
return (p);
}
/**
* free_pointer - frees pointer and put null to the adresse
* @ptr: adresse of pointer
* Return: 1 (Success), 0 (FAIL)
*/
int free_pointer(void **ptr)
{
if (ptr && *ptr)
{
free(*ptr);
*ptr = NULL;
return (1);
}
return (0);
}