Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2. Podaj różnice między dowiązaniem symbolicznym, a zwykłym.
- Zadanie - rozszerzyć interpreter ls z materiałów przykładowych o opcje -a (nie pomija ukrytych) i -R (rekurencyjnie wyświetla zawartości podkatalogów) korzystając z getopt. W razie nie podania ścieżki do katalogu ma operować na bieżącym. Miało działać zarówno ls -a -R jak i ls -aR.
- Kod:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/param.h>
- #include <dirent.h>
- #include <errno.h>
- #include <string.h>
- int pokazkat(char *kat, int af, int rf)
- {
- DIR *dir;
- struct dirent *pozycja;
- struct stat info;
- int i;
- if(chdir(kat) == -1) {
- perror("Blad chdir");
- return 1;
- }
- if((dir = opendir(".")) == NULL) {
- perror("Blad opendir");
- return 1;
- }
- errno = 0;
- while((pozycja = readdir(dir)) != NULL)
- {
- if(!af) if(pozycja->d_name[0] == '.') continue;
- if(rf) for(i=0; i<rf-1; i++) printf("+");
- printf("%s\n", pozycja->d_name);
- if(rf) {
- if(lstat(pozycja->d_name, &info) == -1) {
- perror("Blad lstat");
- return 1;
- }
- if(S_ISDIR(info.st_mode) &&
- pozycja->d_name[1] != '.' &&
- strcmp(pozycja->d_name, "."))
- pokazkat(pozycja->d_name, af, rf+1);
- }
- }
- if(rf>1) {
- if(chdir("..") == -1)
- {
- perror("Blad chdir");
- return 1;
- }
- }
- if(errno) {
- printf("%s ", pozycja->d_name);
- perror("Blad readdir");
- return 1;
- }
- if(closedir(dir) == -1) {
- perror("Blad closedir");
- return 1;
- }
- return 0;
- }
- int main(int argc, char *argv[])
- {
- int aflag = 0, rflag = 0, c;
- while((c = getopt(argc, argv, "aR")) != -1) {
- switch(c) {
- case 'a': aflag = 1; break;
- case 'R': rflag = 1; break;
- case '?': printf("Nieprawidlowy parametr: %c\n", optopt);
- }
- }
- if(argv[optind] != NULL) {
- return pokazkat(argv[optind], aflag, rflag);
- } else {
- return pokazkat(".", aflag, rflag);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement