View difference between Paste ID: MWbBXULA and Fa0XvAg5
SHOW: | | - or go back to the newest paste.
1
lab3 : implementing ls command
2
3
4
system calls:
5
6
-mkdir
7
-opendir
8
-readdir
9
-rmdir
10
-closedir
11
-getcwd
12
13
14
aim: print pwd
15
16
pwd.c
17
18
#include<stdio.h>
19
#include<stdlib.h>
20
#include<unistd.h>
21
#define BUFF 1024
22
23
int main()
24
{
25
	char buf[BUFF];
26
27
	if(getcwd(buf,sizeof(buf))!=NULL)
28
	{
29
		printf("%s\n",buf);
30
	}
31
	else
32
	{
33
		printf("Error.");
34
	}
35
	return 0;
36
}
37
38
39
aim:ls without hidden
40
41
#include<stdio.h>
42
#include<stdlib.h>
43
#include<unistd.h>
44
#include<dirent.h>
45
#include<sys/types.h>
46
#define BUFF 1024
47
48
int main()
49
{
50
	char buf[BUFF];
51
	DIR *dirstream;
52
	struct dirent *d;
53
54
	if(getcwd(buf,sizeof(buf))!=NULL)
55
	{
56
		if((dirstream=opendir(buf))!=NULL)
57
		{
58
			while((d=readdir(dirstream))!=NULL)
59
			{
60
				if((d->d_name)[0]!='.')
61
				printf("%s\n",d->d_name);
62
			}
63
		}
64
		else
65
		{
66
			printf("Error.\n");
67
			exit(1);
68
		}
69
	}
70
	else
71
	{
72
		printf("Error.\n");
73
		exit(1);
74
	}
75
	
76
	
77
	
78
	
79
	return 0;
80
}
81
82
83
84
85
*/recursive
86
87
88
89
#include<stdio.h>
90
#include<stdlib.h>
91
#include<unistd.h>
92
#include<dirent.h>
93
#include<sys/types.h>
94
#include<string.h>
95
#define BUFF 4096
96
void print(char* buf)
97
{
98
	char buf2[BUFF];
99
	DIR *dirstream,*dirstream2;
100
	struct dirent *d;
101
	printf("%s\t:\n",buf);
102
103
		if((dirstream=opendir(buf))!=NULL)
104
		{
105
			while((d=readdir(dirstream))!=NULL)
106
			{
107
				if((d->d_name)[0]!='.')
108
				{
109
					printf("%s\n",d->d_name);
110
				}
111
			}
112
			
113
			
114
			if((dirstream2=opendir(buf))!=NULL)
115
			{
116
				while((d=readdir(dirstream2))!=NULL)
117
				{
118
					if(d->d_type == DT_DIR)
119
					{
120
						if(d->d_name[0]!='.')
121
						{
122
							if((chdir(d->d_name))==0 )
123
							{
124
								getcwd(buf2,sizeof(buf2));
125
								print(buf2);
126
							}
127
						}
128
					}	
129
				}
130
			}
131
			
132
				
133
			
134
		}
135
		else
136
		{
137
			printf("Error.1\n");
138
			exit(1);
139
		}
140
}
141
142
int main()
143
{
144
	char buf[BUFF];
145
	
146
	if(getcwd(buf,sizeof(buf))!=NULL)
147
	
148
	{
149
		
150
		print(buf);
151
	}
152
	else
153
	{
154
		printf("Error.\n");
155
		exit(1);
156
	}
157
	
158
	
159
	
160
	
161
	return 0;
162
}