-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory.c
More file actions
68 lines (58 loc) · 1.45 KB
/
directory.c
File metadata and controls
68 lines (58 loc) · 1.45 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include "main.h"
//for ls and we have to find how back we have to go to home
void find_how_back(char * h_str,char * c_str)
{
char * ret = malloc(MAX_LENGTH*sizeof(char));
strcpy(ret,"");
int a =strlen(h_str),b=strlen(c_str);
for(int i = a;i<b;i++)
if (c_str[i] == '/')
strcat(ret,"../");
if (chdir(ret) < 0)
printf("directory not changed\n");
free(ret);
return ;
}
//storing the list of files in current directory
char ** list_all(char * cur,int * listsize )
{
struct dirent *de;
char ** list = malloc(MAX_LENGTH*sizeof(char *));
int l = MAX_LENGTH;
DIR *dr = opendir(cur);
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
int pos = 0;
while ((de = readdir(dr)) != NULL)
{
if (pos >= l)
{
l += MAX_LENGTH;
list = realloc(list,l*sizeof(char *));
}
list[pos] = de->d_name;
pos++;
}
closedir(dr);
*listsize = pos;
return list ;
}
//print the list out
char ** list_out_ls(char * cur,int * listsize)
{
char ** list = list_all(cur,listsize);
int l = *listsize;
for(int i = 0;i<l;i++)
if (list[i][0] != '.')
printf("%s ",list[i]);
printf("\n");
return list;
}