-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomRepeat.c
97 lines (87 loc) · 2.02 KB
/
customRepeat.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
#include "Header.h"
#include "Execute.h"
#include "customRepeat.h"
void customRepeat(char * token, char * cwd, char * home_dir, char * prev_directory){
int start = 6;
int end = strlen(token);
for (int i = 6; i < strlen(token); i++){
if( (token[i]==' ') || (token[i]=='\t'))
start = i;
else
break;
}
for (int i = strlen(token)-1; i >= 0; i--){
if( (token[i]==' ') || (token[i]=='\t'))
end = i;
else
break;
}
// printf("start:%d;end:%d", start, end);
for( int i=start+1; i<end; i++ ){
token[i-start-1] = token[i];
}
token[end-start-1] = '\0';
if( start>=end ){
printf("repeat: Enter 2 Arguments.\n");
return;
}
// printf("%s\n", token);
// check if there's only 1 arg
int flag = 0;
for (int i = 0; i < strlen(token); i++){
if( token[i]==' ' || token[i]=='\t' ){
flag = 1;
}
}
if(flag==0){
printf("repeat: Too few arguments.\n");
return;
}
// EXTRACT THE NUMBER
char arg1[15];
int arg1_int = 0;
int last_pointer = -1;
for (int i = 0; i < strlen(token); i++){
if( token[i]==' ' || token[i]=='\t' ){
break;
}
else{
if( (token[i]>='0') && (token[i]<='9')){
arg1[arg1_int++] = token[i];
arg1[arg1_int] = '\0';
last_pointer = i;
}
else{
printf("repeat: The first argument has to be an positive integer.\n");
return;
}
}
}
// REMOVE ARG_1 AND ANY GAPS AFTER THAT
start = last_pointer + 1;
end = strlen(token);
for (int i = last_pointer + 1; i < strlen(token); i++){
if( (token[i]==' ') || (token[i]=='\t'))
start = i;
else
break;
}
for (int i = strlen(token)-1; i >= 0; i--){
if( (token[i]==' ') || (token[i]=='\t'))
end = i;
else
break;
}
// printf("start:%d;end:%d", start, end);
for( int i=start+1; i<end; i++ ){
token[i-start-1] = token[i];
}
token[end-start-1] = '\0';
// printf("%s\n", token);
arg1_int = atoi(arg1);
// printf("%d\n",arg1_int);
for(int i=0; i<arg1_int;i++ ){
Execute(token, cwd, home_dir, prev_directory);
}
return;
}