forked from Teslothorcha/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_maker.c
75 lines (68 loc) · 1.26 KB
/
list_maker.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
#include "holberton.h"
/**
* list_maker - add node initialize it and give it a string
*@head: head node of linked list
*@token: token to be insterted on list
*@l: length of token + command + \n + '\0'
* Return: node with string on it
*/
listint_t *list_maker(listint_t **head, char *token, unsigned int l)
{
listint_t *new;
unsigned int count;
new = malloc(sizeof(listint_t));
if (!new)
return (NULL);
new->dir = malloc(sizeof(char) * l);
if (!new->dir)
return (NULL);
for (count = 0; count < l; count++)
{
new->dir[count] = 0;
}
for (count = 0; token[count] != '\0'; count++)
{
new->dir[count] = token[count];
}
new->next = *head;
*head = new;
return (*head);
}
/**
*free_list - free list and sets head to NULL
*@head: - pointer to the head of the list
*/
void free_list(listint_t **head)
{
listint_t *temp;
while (head && *head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp->dir);
free(temp);
}
}
/**
*listint_len - list nodes of a list
*@h: - pointer to the head of the list
*Return: the number of nodes in the list
*/
size_t listint_len(const listint_t *h)
{
unsigned int counter;
counter = 0;
if (h == NULL)
{
return (0);
}
else
{
while (h)
{
h = h->next;
counter++;
}
}
return (counter);
}