-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathbuiltin_command.c
61 lines (59 loc) · 1.84 KB
/
builtin_command.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
/*
* =====================================================================================
* Filename: builtin_command.c
* Description:
* Version: 1.0
* Created: 2013.11.01 15h31m28s
* Author: wuyue (wy), [email protected]
* Company: UESTC
* =====================================================================================
*/
#include "wshell.h"
int builtin_command(char *command, char **parameters)
{
extern struct passwd *pwd;
if(strcmp(command,"exit")==0 || strcmp(command,"quit")==0)
exit(0);
else if(strcmp(command,"about") == 0)
{
printf("This is a simulation of shell (bash) in Linux.\n");
return 1;
}
else if(strcmp(command,"cd")==0)
{
char *cd_path = NULL;
if(parameters[1] == NULL)
//make "cd" to "cd .." as in bash
{
parameters[1] = malloc(3 * sizeof(char));
parameters[1][0]= '.';
parameters[1][1]= '.';
parameters[1][2]= '\0';
}
if(parameters[1][0] == '~')
{
cd_path = malloc(strlen(pwd->pw_dir)+strlen(parameters[1]));
//'~' makes length 1 more,but instead of '\0'
if(cd_path == NULL)
{
printf("cd:malloc failed.\n");
}
strcpy(cd_path,pwd->pw_dir);
strncpy(cd_path+strlen(pwd->pw_dir),parameters[1]+1,strlen(parameters[1]));
//printf("path with ~:\n%s\n",cd_path);
}
else
{
cd_path = malloc(strlen(parameters[1]+1));
if(cd_path == NULL)
{
printf("cd:malloc failed.\n");
}
strcpy(cd_path,parameters[1]);
}
if(chdir(cd_path)!= 0)
printf("-wshell: cd: %s:%s\n",cd_path,strerror(errno));
free(cd_path);
}
return 0;
}