-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetls.c
More file actions
53 lines (41 loc) · 956 Bytes
/
Copy pathgetls.c
File metadata and controls
53 lines (41 loc) · 956 Bytes
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
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
int main(int argc, char** argv){
char* str1;
FILE * fp;
struct dirent *ptr;
char helper1[100];
int st;
char *p;
str1 = argv[1];
DIR *dr;
if (strcmp(str1, "--help")==0){ // --help flag
fp = fopen ("ls.txt", "r");
if(fp == NULL) {
perror("Error opening file\n"); // error1 reported
return(-1);
}
while(fgets (helper1, 600, fp)!=NULL) {
puts(helper1);
}
fclose(fp);
}
else if (strcmp(str1, "-1")==0){
dr = opendir(argv[2]);
if (dr == NULL) {
printf("Error: The directory cannot be opened\n" );
return 0;}
while ((ptr = readdir(dr)) != NULL){printf("%s \n", ptr->d_name); }
}
else{
dr = opendir(str1);
if (dr == NULL){ printf("Error: The directory cannot be opened\n"); //error2 reported
return 0;}
while ((ptr = readdir(dr)) != NULL){printf("%s ", ptr->d_name);}
printf("\n");
}
closedir(dr);
return 0;
}