-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
111 lines (85 loc) · 1.72 KB
/
commands.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "commands.h"
#include "main.h"
void change_directory(char *newdirectory)
{
char temp[1000],relpath[1000];
strcpy(temp,newdirectory);
char * temp1 = strtok(temp, " \t\r\n");
int val;
int val1;
newdirectory = strtok(NULL, " \t\r\n");
if(newdirectory == NULL || strlen(newdirectory)==0 || (strcmp(newdirectory,"")==0) )
{
chdir(homepath);
return;
}
char first = newdirectory[0];
char second = newdirectory[1];
if(strlen(newdirectory)>2){
strcpy(relpath,homepath);
strcat(relpath, "/");
strcat(relpath,&newdirectory[2]);
}
if(strcmp(newdirectory,"~")==0)
{
chdir(homepath);
}
else if(strcmp(newdirectory,"~/")==0)
chdir(homepath);
else if(first != '~' && second != '/')
{
val = chdir(newdirectory);
if(val < 0)
perror("Error");
}
else if(first == '~' && second == '/')
{
val1 = chdir(relpath);
if(val1 < 0)
perror("Error");
}
return;
}
//----------------------------------------------------------------------
int present_working_directory() {
char cwd[1000];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
return 0;
}
//----------------------------------------------------------------------
void echo(char * input)
{
char temp[1000];
strcpy(temp,input);
char * token = strtok(NULL, " \t\r\n");
//printf("%s\n",token);
char temp1[1000];
int i;
char a = '\'';
char b = '\"';
int it=0;
if(token == NULL)
printf("\n");
else{
int s = strlen(temp);
for(i=5;i<strlen(temp);i++)
{
if(temp[i]!= '\'' && temp[i]!= '\"' )
temp1[it++]= temp[i];
}
for(i=0;i<it;i++)
printf("%c",temp1[i]);
printf("\n");
}
return;
}