Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lab3 : implementing ls command
- system calls:
- -mkdir
- -opendir
- -readdir
- -rmdir
- -closedir
- -getcwd
- aim: print pwd
- pwd.c
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #define BUFF 1024
- int main()
- {
- char buf[BUFF];
- if(getcwd(buf,sizeof(buf))!=NULL)
- {
- printf("%s\n",buf);
- }
- else
- {
- printf("Error.");
- }
- return 0;
- }
- aim:ls without hidden
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<dirent.h>
- #include<sys/types.h>
- #define BUFF 1024
- int main()
- {
- char buf[BUFF];
- DIR *dirstream;
- struct dirent *d;
- if(getcwd(buf,sizeof(buf))!=NULL)
- {
- if((dirstream=opendir(buf))!=NULL)
- {
- while((d=readdir(dirstream))!=NULL)
- {
- if((d->d_name)[0]!='.')
- printf("%s\n",d->d_name);
- }
- }
- else
- {
- printf("Error.\n");
- exit(1);
- }
- }
- else
- {
- printf("Error.\n");
- exit(1);
- }
- return 0;
- }
- */recursive
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<dirent.h>
- #include<sys/types.h>
- #include<string.h>
- #define BUFF 4096
- void print(char* buf)
- {
- char buf2[BUFF];
- DIR *dirstream,*dirstream2;
- struct dirent *d;
- printf("%s\t:\n",buf);
- if((dirstream=opendir(buf))!=NULL)
- {
- while((d=readdir(dirstream))!=NULL)
- {
- if((d->d_name)[0]!='.')
- {
- printf("%s\n",d->d_name);
- }
- }
- if((dirstream2=opendir(buf))!=NULL)
- {
- while((d=readdir(dirstream2))!=NULL)
- {
- if(d->d_type == DT_DIR)
- {
- if(d->d_name[0]!='.')
- {
- if((chdir(d->d_name))==0 )
- {
- getcwd(buf2,sizeof(buf2));
- print(buf2);
- }
- }
- }
- }
- }
- }
- else
- {
- printf("Error.1\n");
- exit(1);
- }
- }
- int main()
- {
- char buf[BUFF];
- if(getcwd(buf,sizeof(buf))!=NULL)
- {
- print(buf);
- }
- else
- {
- printf("Error.\n");
- exit(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement