-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_func3.c
More file actions
92 lines (86 loc) · 1.74 KB
/
Copy path_func3.c
File metadata and controls
92 lines (86 loc) · 1.74 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
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
92
#include "main.h"
/**
* take_only_cmd - Extract the first command from a string
* @buffer: Input string containing one or more commands
* @no_exc: Number of commands
* @n_err: Number of commands
* @argc: num of args
* @argv: vector agrs
* Return: Pointer to the first command
*/
char *take_only_cmd(char **buffer, int *no_exc, int argc,
char *argv[], int *n_err)
{
int i = 0;
char str_cmd[50], *cmd;
if (_strlen(*buffer) >= 50)
{
perror("command to long\n");
free(*buffer), exit(1);
}
while ((*buffer)[i])
{
if ((*buffer)[i] == ' ')
break;
str_cmd[i] = (*buffer)[i];
i++;
}
str_cmd[i] = '\0';
cmd = malloc(_strlen(str_cmd) + 1);
if (!cmd)
{
perror("fail to allocate");
exit(1);
}
_strcpy(cmd, str_cmd);
if (_strcmp(cmd, "cd") == 0)
{
*no_exc = 0;
change_dir(*buffer, cmd, argc, argv, n_err);
}
return (cmd);
}
/**
* ls_check - Check if the command is 'ls' and validate directories
* @ave: Array of command-line arguments
* @buf: Buffer for storing command-line input
* @only: The name of the command
*/
void ls_check(char *ave[], char *buf, char *only)
{
const char *dir_path = NULL;
DIR *dir;
int index = 1;
if (_strcmp(only, "ls") == 0 || _strcmp(ave[0], "/usr/bin/ls") == 0 ||
_strcmp(ave[0], "/bin/ls") == 0)
{
while (ave[index])
{
if (ave[index][0] != '-')
{
dir_path = ave[index];
dir = opendir(dir_path);
if (!dir)
{
fprintf(stderr, "%s: cannot access '%s': No such file or directory\n",
only, dir_path);
if (ave[index + 1] == NULL)
free(buf), free(only), exit(2);
}
if (dir)
closedir(dir);
}
index++;
}
}
}
/**
* shell_exit - exit program
*@status: status num to exit
*/
void shell_exit(int status)
{
if (status == 512)
exit(status / 256);
exit(0);
}