-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions-0.c
70 lines (65 loc) · 1.17 KB
/
functions-0.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
#include "monty.h"
/**
* command_builder - addnode_end singly linked list of parsed commands.
* @head: head of the linked list
* @s: command to be parsed
* @i: line number of the script
* Return: new node, NULL if it fails.
*/
cmds *command_builder(cmds **head, char *s, int i)
{
cmds *tmp = *head, *new = NULL;
char *str;
new = malloc(sizeof(cmds));
if (!new)
return (NULL);
new->cmd[0] = strdup(strtok(s, " \t"));
if (!(new->cmd[0]))
{
free(new);
return (NULL);
}
str = strtok(NULL, " \t");
if (str)
{
new->cmd[1] = strdup(str);
if (!(new->cmd[1]))
{
free(new->cmd[1]);
free(new);
return (NULL);
}
}
else
new->cmd[1] = NULL;
new->line_number = i;
new->next = NULL;
new->mode = 0;
if (!(*head))
{
*head = new;
return (new);
}
for (; tmp->next; tmp = tmp->next)
continue;
tmp->next = new;
return (new);
}
/**
* m_com - Function that find # in the line.
* @s: line to read
*
* Return: 1 if find # and 0 if not find.
*/
int m_com(char **s)
{
int i = 0;
for (; (*s)[i] && ((*s)[i] == ' ' || (*s)[i] == '\t'); i++)
;
if ((*s)[i] == '#')
return (1);
for (i = 0; (*s)[i] && (*s)[i] != '#'; i++)
;
(*s)[i] = '\0';
return (0);
}