-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
43 lines (40 loc) · 757 Bytes
/
ls.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
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include <string.h>
void ls1(char input[1000]){
char comm[256] = {};
int i=0;
int j=1;
int flag = 0;
comm[0] = '-';
while(i < 1000 && input[i]!='\0' && input[i]!='\n'){
if(flag == 0 && input[i]=='l' && input[i+1] == 's'){
flag = 1;
}
else if(flag == 1 ){
if(input[i+1]!='\n' && input[i+1]!=' ' && input[i+1]!='-'){
comm[j] = input[i+1];
++j;
}
}
else;
++i;
}
pid_t pid;
pid = fork();
if(pid<0){
printf("%s\n", "Some error occurred");
}
else if(pid==0 && comm[1]!='\0'){
execl("/bin/ls","ls",comm,(char *)0);
}
else if(pid==0 && comm[1]=='\0'){
execl("/bin/ls","ls",(char *)0);
}
else{
wait(NULL);
}
}