From 6d236f6ad3348089805c72174ef1f42c46467abf Mon Sep 17 00:00:00 2001 From: Alexander Mahr Date: Sun, 2 Feb 2025 15:58:56 +0100 Subject: [PATCH] add nn.find --- README.md | 7 +-- source/Makefile | 4 +- source/find.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 source/find.c diff --git a/README.md b/README.md index 5459105..990c789 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,5 @@ -# Building stuff in containers +# "nn.find" (similar to `find`) -## Rationale / sought for Benefits - -1. "cleanliness", all dependencies can be setup / installed in container and do not needlessly remain after build -2. "less/no missing dependecies" -3. "hightened" reproducibility ## Usage: diff --git a/source/Makefile b/source/Makefile index 82daf9e..6143414 100644 --- a/source/Makefile +++ b/source/Makefile @@ -1,2 +1,2 @@ -all: - echo hallo +find: find.c + gcc "$<" -o "$@" diff --git a/source/find.c b/source/find.c new file mode 100644 index 0000000..895cd50 --- /dev/null +++ b/source/find.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include +// strlen +#include +// malloc +#include +//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; +} + +