-
Notifications
You must be signed in to change notification settings - Fork 1
/
getCdMod.c
90 lines (82 loc) · 1.76 KB
/
getCdMod.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "shell.h"
/**
*__cd_error -a function to handling the failure.
* @argv: the file name.
* @count: the number of errors we did.
* @av: the input string we read
* Return: nothing.
*/
void __cd_error(char *argv, unsigned int count, char *av)
{
char *prompt = string_concat("can't cd to ", av);
prompt = string_concat(prompt, "\n");
_putstr(argv);
_putchar(':'), _putchar(' ');
_putnum(count);
_putchar(':'), _putchar(' ');
_putstr("cd");
_putchar(':'), _putchar(' ');
_putstr(prompt);
if (prompt)
free(prompt);
}
/**
* __cd_success - a function to handling the success.
* @cwd: to read the old path.
* Return: nothing.
*/
void __cd_success(char *cwd)
{
getcwd(cwd, sizeof(cwd));
setenv("OLDPWD", getenv("PWD"), 1);
setenv("PWD", cwd, 1);
}
/**
* getCdMod - a function to handling the success.
* @argv: the file name.
* @numCount: the number of errors we did.
* @strRead_cp: read the path we want to cd to
* Return: nothing.
*/
int getCdMod(char **strRead_cp, char *argv, unsigned int numCount)
{
char *dir = NULL, *cwd = getcwd(NULL, 0);
int ret = -1, err = 0; /*returned 0 sucss or -1 error*/
if (strRead_cp[1] != NULL)
{
if (_strcmp(strRead_cp[1], "-") == 0)
{
dir = _getenv("OLDPWD");
if (dir == NULL)
{
free(dir);
__cd_error(argv, numCount, strRead_cp[1]);
err = errno = -1;
return (err);
}
ret = chdir(dir);
if (!ret)
printf("%s\n", dir);
}
else
ret = chdir(strRead_cp[1]);
}
else
{
dir = _getenv("HOME");
if (dir == NULL)
{
free(dir);
__cd_error(argv, numCount, strRead_cp[1]), err = errno = -1;
return (err);
}
ret = chdir(dir);
}
if (ret != -1)
__cd_success(cwd);
else
__cd_error(argv, numCount, strRead_cp[1]), err = errno = -1;
free(dir);
free(cwd);
return (err);
}