Problem z rekurencja..

0

Oto moj kod:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
#include <grp.h>
#include <string.h>

static char permissions_buf[30];
const char *perms(mode_t tryb)
{
	char ftype = '?';
	if (S_ISDIR(tryb)) ftype = 'd';
	if (S_ISREG(tryb)) ftype = '-';
	if (S_ISLNK(tryb)) ftype = 'l';
	if (S_ISFIFO(tryb)) ftype = 'p';
	if (S_ISBLK(tryb)) ftype = 'b';
	if (S_ISSOCK(tryb)) ftype = 's';
	if (S_ISCHR(tryb)) ftype = 'c';
	sprintf(permissions_buf, "%c%c%c%c%c%c%c%c%c%c", ftype,
	tryb & S_IRUSR ? 'r' : '-',
	tryb & S_IWUSR ? 'w' : '-',
	tryb & S_IXUSR ? 'x' : '-',
	tryb & S_IRGRP ? 'r' : '-',
	tryb & S_IWGRP ? 'w' : '-',
	tryb & S_IXGRP ? 'x' : '-',
	tryb & S_IROTH ? 'r' : '-',
	tryb & S_IWOTH ? 'w' : '-',
	tryb & S_IXOTH ? 'x' : '-');
	return (const char *)permissions_buf;
}
void err()
{
	fprintf(stderr, "Podano niepoprawne argumenty..\n");
	return;
}
void rec(char *path)
{
	DIR *dp;
	struct dirent *wpis;
	char newdir[1024];
	
	dp = opendir(path);
	printf("%s :\n", path);
	while((wpis = readdir(dp))) {
		if (!( (strcmp(wpis->d_name,".") == 0 ) || (strcmp(wpis->d_name,"..") == 0) ))
			printf("\t%s\n", wpis->d_name);
	}

	printf("\n");	
	rewinddir(dp);
	while((wpis = readdir(dp)))
	{
	  	if (!( (strcmp(wpis->d_name,".") == 0 ) || (strcmp(wpis->d_name,"..") == 0) )) {			
			if (wpis->d_type == DT_DIR) {
				sprintf(newdir,"%s/%s", path, wpis->d_name);
				rec(newdir);
			}
		} 
	}
	closedir(dp);
	}
void info(char *nazwa)
{
	int tryb;
	struct stat buf;
	time_t czas;
	char bufor[1024];
	struct tm czasTM;
	struct passwd *struser;
	struct group *strgroup;
		 lstat(nazwa, &buf);
         	 tryb = buf.st_mode; 	   
		 printf("%s  ", perms(tryb));	
		 printf("%2d  ", (unsigned int)buf.st_nlink);
	         struser = getpwuid(buf.st_uid);
		 printf("%2s\t", struser->pw_name);
		 strgroup = getgrgid(buf.st_gid);
		 printf("%5s  ", strgroup->gr_name);
		 printf("%d\t ", (unsigned int)buf.st_size);
		 czas = buf.st_mtime;
		 czasTM = *localtime(&czas);
		 strftime(bufor, sizeof(bufor), "%b %d %H:%M ", &czasTM);
		 printf("%s\t", bufor);
		 printf("%s\n", nazwa);
}
void extract(char *name)
{
	struct dirent **wpis;
	DIR *dp;
	int count, i;
	int tryb;
	struct stat buf;
	time_t czas;
	char bufor[1024];
	struct tm czasTM;
	struct passwd *struser;
	struct group *strgroup;
	count = scandir(name, &wpis, 0, alphasort);
	if (count > 0)
	{
		printf("total %d\n", count);
		for(i = 0; i < count; ++i)
		{
				lstat(wpis[i]->d_name, &buf);
		 		tryb = buf.st_mode; 	   
		 		printf("%s  ", perms(tryb));	
		 		printf("%2d  ", (unsigned int)buf.st_nlink);
	         		struser = getpwuid(buf.st_uid);
		 		printf("%2s\t", struser->pw_name);
		 		strgroup = getgrgid(buf.st_gid);
		 		printf("%5s  ", strgroup->gr_name);
		 		printf("%d\t ", (unsigned int)buf.st_size);
		 		czas = buf.st_mtime;
		 		czasTM = *localtime(&czas);
		 		strftime(bufor, sizeof(bufor), "%b %d %H:%M ", &czasTM);
		 		printf("%s\t", bufor);
				printf("%s\n", wpis[i]->d_name);
			free(wpis[i]);
		}
		free(wpis);
	}
}
int main(int argc, char **argv) {
	DIR *dp;
	struct dirent *wpis;
	struct stat buf;
	int tryb;
	if (argc < 2)
	{
		printf("Bledna ilosc argumentow.\n");
	}
	else if (argc == 1) {
		dp = opendir(argv[1]);
		while ((wpis = readdir(dp)) != NULL){
			printf("%s\n", wpis->d_name);
			printf("%d\n", wpis->d_type);
		}
		closedir(dp);
	}
	else if (argc == 2) {	
		if (strcmp(argv[1], "-R") == 0) err();
		else {
			lstat(argv[1], &buf);
			if(S_ISDIR(buf.st_mode)) extract(argv[1]);
			else info(argv[1]);
			} 
	}
	else if (argc == 3)
	{
		if (strcmp(argv[1], "-R") == 0)  rec(argv[2]);
		else err();
	}
	else err();
	return 0;
}

Otoz mam problem z rekurencja.. Po wywolaniu programu, np. ./my_ls -R katalog listuje mi wszystkie pliki/ katalogi zawarte w katalogu o nazwie "katalog". Problem polega na tym, ze nie wiem co mam zle w kodzie. Chcialbym, aby do kazdego nastepnego katalogu rowniez wchodzilo i wypisywalo mi liste tego co sie tam znajduje itd.. Pomoze mi ktos?

0

staw kod w znaczniki < code >. Czy uzywales debuggera?

0

Nawiazujac do rekurencji w linijce if (wpis->d_type == DT_DIR) moglbym dac if (wpis->d_type == S_ISDIR(buf.st_mode)) , ale w tym wypadku akurat wywali mi segmenta w pewnym momencie..

1 użytkowników online, w tym zalogowanych: 0, gości: 1