-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.c
More file actions
65 lines (57 loc) · 1.01 KB
/
Copy pathcommand.c
File metadata and controls
65 lines (57 loc) · 1.01 KB
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
#include "main.h"
/**
* command_line - function that handle the command and its arguments.
* @s: string of command and arguments
* Return: array
*/
char **command_line(char *s)
{
int index = 0, i = 0;
char **argv;
char *tok, *tok1;
int count = 0;
tok1 = strtok(s, "\n");
tok = tok1;
while (tok != NULL)
{
count++;
tok = strtok(NULL, " ");
}
argv = malloc(sizeof(char *) * (count + 2));
tok = strtok(tok1, " ");
while (tok != NULL)
{
if (tok[0] == '#')
{
argv[index] = NULL;
return (argv);
}
argv[index] = malloc(stringlen(tok) + 1);
for (i = 0; i < stringlen(tok); i++)
argv[index][i] = tok[i];
argv[index][i] = '\0';
index++;
tok = strtok(NULL, " ");
}
argv[index] = NULL;
if (!index)
return (argv);
/* free(s);*/
return (argv);
}
/**
* free_argv - function to free argv and its elements
* @argv: array to be freed
* Return: void
*/
void free_argv(char **argv)
{
int i;
if (argv == NULL)
return;
for (i = 0; argv[i] != NULL; i++)
{
free(argv[i]);
}
free(argv);
}