-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtin.c
50 lines (49 loc) · 1.11 KB
/
builtin.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
#include "shell.h"
/**
* Builtin - Evaluate Buitins
* @command: line of command
* @env: var enviroments
* @status: status process
* Return: 1 CD DONE, 2 EXIT DONE, 0 NO ONE
**/
int Builtin(char *command, char **env, int status)
{
char **cdCommand = NULL;
size_t sizeDirBuf = 512;
static char *currDir;
static char currDirector[512];
static int countAlloc;
if (_strncmp(command, "cd", 2) == 0)
{ cdCommand = ParseCommand(command, " ");
if (_strncmp(cdCommand[0], "cd", 2) == 0)
{
if ((cdCommand[1] == NULL) || (_strncmp(cdCommand[1], "-", 1) == 0))
{
if (cdCommand[1] == NULL)
cdCommand[1] = _GetEnv("HOME", env);
else
{
if (_strncmp(cdCommand[1], "-", 1) == 0)
{
if (chdir(currDirector))
perror("Error:<chdir>");
free(cdCommand);
_prompt();
return (1);
}
}
}
currDir = getcwd(currDirector, sizeDirBuf);
if (currDir == NULL)
perror("Error <getcwd>");
else
countAlloc++;
if (chdir(cdCommand[1]))
perror("Error:<chdir>");
}
free(cdCommand);
_prompt();
return (1);
} /*CD LOGICAL*/
return (BuiltExit(command, status));
}