140 lines
4.7 KiB
C
140 lines
4.7 KiB
C
#include <errno.h>
|
|
#include <sys/types.h>
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
// strlen
|
|
#include <string.h>
|
|
// malloc
|
|
#include <stdlib.h>
|
|
//DIR *opendir(const char *name);
|
|
//DIR *fdopendir(int fd);
|
|
//int fstatat(int dirfd, const char *pathname, struct stat *buf,
|
|
// int flags);
|
|
|
|
|
|
int list(int dirfd, char* curdir,int length){
|
|
char* next_curdirnext = NULL;
|
|
int next_length = 0;
|
|
// putchar
|
|
|
|
DIR* pdir = fdopendir(dirfd);
|
|
if(pdir==NULL){
|
|
perror("fehler fopendir");
|
|
return 1;
|
|
}
|
|
struct dirent * pdirent = NULL;
|
|
struct stat statbuf;
|
|
while(1)
|
|
{
|
|
errno = 0;
|
|
pdirent = readdir(pdir);
|
|
if(pdirent==NULL){
|
|
if(errno==0)
|
|
{
|
|
// printf("end reached\n");
|
|
closedir(pdir);
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr," readdir errno = %d\n",errno);
|
|
return 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(pdirent->d_name[0]=='.'){
|
|
if(pdirent->d_name[1]==0){
|
|
//printf("ignore SELF\n");
|
|
continue;
|
|
}
|
|
if(pdirent->d_name[1]=='.'){
|
|
if(pdirent->d_name[2]==0){
|
|
// printf("ignore PARENT\n");
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
if( 0== fstatat(dirfd,pdirent->d_name,&statbuf,AT_SYMLINK_NOFOLLOW) )
|
|
{
|
|
printf("%s/%s\n",curdir,pdirent->d_name);
|
|
if(S_ISDIR(statbuf.st_mode)){
|
|
// printf("%s\n",pdirent->d_name);
|
|
int subdirfd = openat(dirfd,pdirent->d_name,O_RDONLY);
|
|
if(-1 == subdirfd)
|
|
{
|
|
perror("subdirfd");
|
|
} else {
|
|
next_length=(length+1+strlen(pdirent->d_name));
|
|
next_curdirnext = malloc(next_length);
|
|
if(next_curdirnext==NULL)
|
|
{
|
|
perror("malloc");
|
|
}
|
|
else
|
|
{
|
|
sprintf(next_curdirnext,"%s/%s",curdir,pdirent->d_name);
|
|
list(subdirfd,next_curdirnext,next_length);
|
|
free(next_curdirnext);
|
|
close(subdirfd);
|
|
}
|
|
}
|
|
}
|
|
//else
|
|
//{
|
|
//// printf("%s/%s\n",curdir,pdirent->d_name);
|
|
//}
|
|
//struct stat {
|
|
// dev_t st_dev; /* ID of device containing file */
|
|
// ino_t st_ino; /* Inode number */
|
|
// mode_t st_mode; /* File type and mode */
|
|
// nlink_t st_nlink; /* Number of hard links */
|
|
// uid_t st_uid; /* User ID of owner */
|
|
// gid_t st_gid; /* Group ID of owner */
|
|
// dev_t st_rdev; /* Device ID (if special file) */
|
|
// off_t st_size; /* Total size, in bytes */
|
|
// blksize_t st_blksize; /* Block size for filesystem I/O */
|
|
// blkcnt_t st_blocks; /* Number of 512 B blocks allocated */
|
|
|
|
// /* Since POSIX.1-2008, this structure supports nanosecond
|
|
// precision for the following timestamp fields.
|
|
// For the details before POSIX.1-2008, see VERSIONS. */
|
|
|
|
// struct timespec st_atim; /* Time of last access */
|
|
// struct timespec st_mtim; /* Time of last modification */
|
|
// struct timespec st_ctim; /* Time of last status change */
|
|
|
|
//#define st_atime st_atim.tv_sec /* Backward compatibility */
|
|
//#define st_mtime st_mtim.tv_sec
|
|
//#define st_ctime st_ctim.tv_sec
|
|
//};
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr,"fstatat errno = %d\n",errno);
|
|
return 2;
|
|
}
|
|
//printf("%d %s\n",pdirent->d_type ,pdirent->d_name);
|
|
}
|
|
}
|
|
// printf("dirfd = %d %p\n",dirfd,pdir);
|
|
return 0;
|
|
}
|
|
|
|
|
|
int main(int argc, char* argv[]){
|
|
// DIR current;
|
|
int dirfd;
|
|
char* curdir= ".";
|
|
puts(".");
|
|
dirfd = openat(AT_FDCWD,".",O_RDONLY);
|
|
if (dirfd > -1){
|
|
list(dirfd,curdir,1+strlen(curdir));
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|