int main(int argc, char * argv[], char * envp[]) { char * path = NULL; int ch, fildes, count; struct dirent dbuf[10]; argv0 = argv[0]; while ((ch = getopt(argc, argv, "la")) != EOF) { switch (ch) { case 'l': flags.l = 1; break; case 'a': flags.a = 1; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (*argv) path = *argv; if (path == NULL || !strcmp(path, "")) path = "./"; fildes = open(path, O_DIRECTORY | O_RDONLY | O_SEARCH); if (fildes < 0) { printf("Open failed\n"); return 1; } while ((count = getdents(fildes, (char *)dbuf, sizeof(dbuf))) > 0) { for (int i = 0; i < count; i++) { if (!flags.a && dbuf[i].d_name[0] == '.') continue; if (flags.l) { struct stat stat; char mode[12]; fstatat(fildes, dbuf[i].d_name, &stat, 0); strmode(stat.st_mode, mode); printf("% 7u %s %u:%u %s\n", (unsigned)dbuf[i].d_ino, mode, (unsigned)stat.st_uid, (unsigned)stat.st_gid, dbuf[i].d_name); } else { printf("%s ", dbuf[i].d_name); } } } printf("\n"); close(fildes); return 0; }
static void ls(char ** args) { char * path = kstrtok(0, DELIMS, args); int fildes, count; struct dirent dbuf[10]; if (path == 0 || !strcmp(path, "")) path = "./"; fildes = open(path, O_DIRECTORY | O_RDONLY | O_SEARCH); if (fildes < 0) { puts("Open failed\n"); return; } count = getdents(fildes, (char *)dbuf, sizeof(dbuf)); if (count < 0) { puts("Reading directory entries failed\n"); } for (int i = 0; i < count; i++) { char buf[80]; struct stat stat; fstatat(fildes, dbuf[i].d_name, &stat, 0); ksprintf(buf, sizeof(buf), "%u %o %u:%u %s\n", (uint32_t)dbuf[i].d_ino, (uint32_t)stat.st_mode, (uint32_t)stat.st_uid, (uint32_t)stat.st_gid, dbuf[i].d_name); puts(buf); } puts("\n"); close(fildes); }
/* * get next entry in a directory. */ struct dirent * _readdir_unlocked(DIR *dirp, int skipdeleted) { struct dirent *dp; for (;;) { if (dirp->dd_loc >= dirp->dd_size) { if (dirp->dd_flags & __DTF_READALL) return (NULL); dirp->dd_loc = 0; } if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) { dirp->dd_seek = lseek(dirp->dd_fd, (off_t)0, SEEK_CUR); dirp->dd_size = getdents(dirp->dd_fd, dirp->dd_buf, (size_t)dirp->dd_len); if (dirp->dd_size <= 0) return (NULL); } dp = (struct dirent *) (void *)(dirp->dd_buf + (size_t)dirp->dd_loc); if ((intptr_t)dp & _DIRENT_ALIGN(dp))/* bogus pointer check */ return (NULL); /* d_reclen is unsigned; no need to compare it <= 0 */ if (dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) return (NULL); dirp->dd_loc += dp->d_reclen; if (dp->d_ino == 0 && skipdeleted) continue; #ifndef __minix if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW)) continue; #endif return (dp); } }
/* * Get directory entries info and return in the buffer. Cookie * will keep the state of each call */ static int fs_getdents(int fildes, struct dirent *buf, size_t *nbyte, char *pn_path, long *dpos, longlong_t *cookie, long *n_entries, dent_arg_t *darg) { struct dirent *ptr; char file_path[PATH_MAX + 1]; fs_fhandle_t fh; struct stat64 st; char *p; int len; int rv; if (*nbyte == 0) { (void) memset((char *)buf, 0, MAX_DENT_BUF_SIZE); *nbyte = rv = getdents(fildes, buf, darg->da_size); *cookie = 0LL; if (rv <= 0) return (rv); } p = (char *)buf + *cookie; len = *nbyte; do { /* LINTED improper alignment */ ptr = (struct dirent *)p; *dpos = ptr->d_off; if (rootfs_dot_or_dotdot(ptr->d_name)) goto skip_entry; (void) snprintf(file_path, PATH_MAX, "%s/", pn_path); (void) strlcat(file_path, ptr->d_name, PATH_MAX + 1); (void) memset(&fh, 0, sizeof (fs_fhandle_t)); if (lstat64(file_path, &st) != 0) { rv = -1; break; } fh.fh_fid = st.st_ino; if (S_ISDIR(st.st_mode)) goto skip_entry; if (fs_populate_dents(darg, strlen(ptr->d_name), (char *)ptr->d_name, n_entries, &st, &fh) != 0) break; skip_entry: p = p + ptr->d_reclen; len -= ptr->d_reclen; } while (len); *cookie = (longlong_t)(p - (char *)buf); *nbyte = len; return (rv); }
int getents (uint fd, uint8_t *buffer, uint buf_size) { int rc; rc = getdents(fd, (struct dirent *) buffer, buf_size); return rc; }
struct dirent* readdir(DIR *d) { if (!d->num || (d->cur += ((struct dirent*)(d->buf+d->cur))->d_reclen)>=d->num) { int res=getdents(d->fd,(struct dirent*)d->buf,sizeof (d->buf)-1); if (res<=0) return 0; d->num=res; d->cur=0; } return (struct dirent*)(d->buf+d->cur); }
int main(int ac, char **av) { int lc; char *msg; int rval, fd; struct linux_dirent64 dir64; struct linux_dirent dir; if ((msg = parse_opts(ac, av, options, &help)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; if ((fd = open("test", O_CREAT | O_RDWR, 0777)) == -1) tst_brkm(TBROK | TERRNO, cleanup, "open of file failed"); if (longsyscall) rval = getdents64(fd, &dir64, sizeof(dir64)); else rval = getdents(fd, &dir, sizeof(dir)); /* * Calling with a non directory file descriptor should give * an ENOTDIR error. */ if (rval < 0) { TEST_ERROR_LOG(errno); switch (errno) { case ENOTDIR: tst_resm(TPASS, "getdents failed as expected with ENOTDIR"); break; case ENOSYS: tst_resm(TCONF, "syscall not implemented"); break; break; default: tst_resm(TFAIL | TERRNO, "getdents failed unexpectedly"); break; } } else { tst_resm(TFAIL, "getdents call succeeded unexpectedly"); } if (close(fd) == -1) tst_brkm(TBROK, cleanup, "fd close failed"); } cleanup(); tst_exit(); }
static bool __fill_DIR(DIR* d) { int rc = TEMP_FAILURE_RETRY(getdents(d->fd_, d->buff_, sizeof(d->buff_))); if (rc <= 0) { return false; } d->available_bytes_ = rc; d->next_ = d->buff_; return true; }
int _main(void) { // Pass address of a syscall gadget in rcx register f rcx asm("rcx"); directSyscall = rcx; // Init and resolve libraries initLibc(); initNetwork(); // Connect to server and send message char socketName[] = "debug"; struct sockaddr_in server; server.sin_family = sceNetHtons(AF_INET); server.sin_addr.s_addr = IP(192, 168, 0, 4); server.sin_port = sceNetHtons(9023); memset(server.sin_zero, 0, sizeof(server.sin_zero)); int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0); sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server)); char buffer[512]; struct dirent *dent; int dfd = open("/", O_RDONLY, 0); if(dfd < 0) { debug(sock, "Invalid directory.\n"); return 1; } while(getdents(dfd, buffer, sizeof(buffer)) != 0) { dent = (struct dirent *)buffer; while(dent->d_fileno) { debug(sock, "%s\n", dent->d_name); dent = (struct dirent *)((void *)dent + dent->d_reclen); } memset(buffer, 0, sizeof(buffer)); } // Return to browser close(dfd); sceNetSocketClose(sock); return 0; }
int ls(char *path) { int fd,result; char buf[BUF_SIZE]; int nread; int offset; struct vmwos_dirent *d; struct stat stat_buf; result=stat(path,&stat_buf); if (result<0) { printf("Cannot access %s: no such file or directory!\n",path); return -1; } // printf("Mode: %x\n",stat_buf.st_mode); /* handle if it's not a directory */ if ( (stat_buf.st_mode&S_IFMT)!=S_IFDIR) { list_file(path); return 0; } /* handle if it's a directory */ fd=open(path,O_RDONLY,0); if (fd<0) { printf("Error! %s\n",strerror(errno)); } while(1) { nread = getdents (fd, (struct vmwos_dirent *)buf, BUF_SIZE); if (nread<0) { printf("Error! %s\n",strerror(errno)); break; } if (nread==0) break; offset=0; while(offset<nread) { d=(struct vmwos_dirent *)(buf+offset); // printf("Inode: %ld\n",d->d_ino); list_file(d->d_name); offset+=d->d_reclen; } } close(fd); return 0; }
static void test_ebadf(void) { int fd = -5; struct linux_dirent64 dirp64; struct linux_dirent dirp; if (longsyscall) getdents64(fd, &dirp64, sizeof(dirp64)); else getdents(fd, &dirp, sizeof(dirp)); print_test_result(errno, EBADF); }
int _main(void) { // Init and resolve libraries initLibc(); initNetwork(); // Connect to server char socketName[] = "debug"; struct sockaddr_in server; server.sin_len = sizeof(server); server.sin_family = AF_INET; server.sin_addr.s_addr = IP(192, 168, 0, 4); server.sin_port = sceNetHtons(9023); memset(server.sin_zero, 0, sizeof(server.sin_zero)); int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0); sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server)); // Read / directory char buffer[512]; struct dirent *dent; int dfd = open("/", O_RDONLY, 0); if(dfd < 0) { debug(sock, "Invalid directory.\n"); return 1; } while(getdents(dfd, buffer, sizeof(buffer)) != 0) { dent = (struct dirent *)buffer; while(dent->d_fileno) { debug(sock, "[%s]: %s\n", entryName(dent->d_type), dent->d_name); dent = (struct dirent *)((void *)dent + dent->d_reclen); } memset(buffer, 0, sizeof(buffer)); } // Return to browser close(dfd); sceNetSocketClose(sock); return 0; }
struct dirent *readdir(struct e_dirent *de) { if(de->nbytes==0) { de->nbytes=getdents(de->fd,(struct dirent *)de->buf,1024); de->bp=de->buf; if(de->nbytes==0) return(NULL); } de->dp=(struct dirent *)de->bp; de->nbytes-=de->dp->d_reclen; de->bp+=de->dp->d_reclen; return(de->dp); }
struct dirent* readdir( DIR* dir ) { int error; if ( dir == NULL ) { return NULL; } error = getdents( dir->fd, &dir->entry, sizeof( struct dirent ) ); if ( error == 0 ) { return NULL; } return &dir->entry; }
static void test_enotdir(void) { int fd; struct linux_dirent64 dir64; struct linux_dirent dir; fd = SAFE_OPEN(cleanup, "test", O_CREAT | O_RDWR); if (longsyscall) getdents64(fd, &dir64, sizeof(dir64)); else getdents(fd, &dir, sizeof(dir)); print_test_result(errno, ENOTDIR); SAFE_CLOSE(cleanup, fd); }
static void test_einval(void) { int fd; char buf[1]; fd = SAFE_OPEN(cleanup, ".", O_RDONLY); /* Pass one byte long buffer. The result should be EINVAL */ if (longsyscall) getdents64(fd, (void *)buf, sizeof(buf)); else getdents(fd, (void *)buf, sizeof(buf)); print_test_result(errno, EINVAL); SAFE_CLOSE(cleanup, fd); }
/* * get next entry in a directory. */ struct dirent * readdir (register DIR *dirp) { register struct dirent *dp; #ifdef HAVE_DD_LOCK __lock_acquire_recursive(dirp->dd_lock); #endif for (;;) { if (dirp->dd_loc == 0) { dirp->dd_size = getdents (dirp->dd_fd, dirp->dd_buf, dirp->dd_len); if (dirp->dd_size <= 0) { #ifdef HAVE_DD_LOCK __lock_release_recursive(dirp->dd_lock); #endif return NULL; } } if (dirp->dd_loc >= dirp->dd_size) { dirp->dd_loc = 0; continue; } dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc); if (dp->d_reclen <= 0 || dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) { #ifdef HAVE_DD_LOCK __lock_release_recursive(dirp->dd_lock); #endif return NULL; } dirp->dd_loc += dp->d_reclen; if (dp->d_ino == 0) continue; #ifdef HAVE_DD_LOCK __lock_release_recursive(dirp->dd_lock); #endif return (dp); } }
struct dirent *readdir(DIR *dir) { struct dirent *dent; int rv; if (!dir->bytes_left) { rv = getdents(dir->__fd, dir->buffer, sizeof(dir->buffer)); if (rv <= 0) return NULL; dir->bytes_left = rv; dir->next = dir->buffer; } dent = dir->next; dir->next = (struct dirent *)((char *)dir->next + dent->d_reclen); dir->bytes_left -= dent->d_reclen; return dent; }
int readdir_r( DIR* dir, struct dirent* entry, struct dirent** result ) { int error; if ( ( dir == NULL ) || ( entry == NULL ) ) { errno = -EINVAL; return -1; } error = getdents( dir->fd, entry, sizeof( struct dirent ) ); if ( error == 0 ) { *result = NULL; } else { *result = entry; } return 0; }
struct dirent64* readdir64(DIR *d) { #ifdef __NR_getdents64 static int trygetdents64=1; #endif static struct dirent64 d64; #ifdef __NR_getdents64 again: if (!trygetdents64) { #endif struct linux_dirent *ld = d->num ? (struct linux_dirent*)(d->buf+d->cur) : NULL; if (!d->num || (d->cur += ld->d_reclen)>=d->num) { int res=getdents(d->fd,(struct linux_dirent*)d->buf, __DIRSTREAM_BUF_SIZE-1); if (res<=0) return 0; d->num=res; d->cur=0;d->is_64=0; ld=(struct linux_dirent*)(d->buf+d->cur); } d64.d_ino=ld->d_ino; d64.d_off=ld->d_off; d64.d_reclen=ld->d_reclen; strcpy(d64.d_name,ld->d_name); d64.d_type=0; /* is this correct? */ return &d64; #ifdef __NR_getdents64 } { struct linux_dirent64 *ld = d->num ? (struct linux_dirent64*)(d->buf+d->cur) : NULL; if (!d->num || (d->cur += ld->d_reclen)>=d->num) { int res=getdents64(d->fd,(struct linux_dirent64*)d->buf,__DIRSTREAM_BUF_SIZE); if (res<=0) { if (errno==ENOSYS) { trygetdents64=0; goto again; } return 0; } d->num=res; d->cur=0; d->is_64=1; } } /* \todo: ensure that dirent64 and linux_dirent64 are compatible */ return (struct dirent64*)(d->buf+d->cur); #endif }
static int getdiffdirentries(int fd, char *buf, int nbytes) { char *read_de; int dentsbytes_actual; int i, readlen = nbytes * 11 / 16; /* worst growth */ int written = 0; lseek(fd, (off_t)0, SEEK_CUR); if(!(read_de = malloc(readlen))) errx(1, "getdiffdirentries: can't malloc"); dentsbytes_actual = getdents(fd, (struct dirent *) read_de, readlen); if(dentsbytes_actual <= 0) return dentsbytes_actual; while(dentsbytes_actual > 0) { int namelen; struct diffdirent *dde = (struct diffdirent *) buf; struct dirent *de = (struct dirent *) read_de; dde->d_status = 0; dde->d_fileno = de->d_ino; namelen = strlen(de->d_name); dde->d_reclen = namelen + 1 + sizeof(*dde) - sizeof(dde->d_name); strcpy(dde->d_name, de->d_name); dde->d_status = 0; assert(dentsbytes_actual >= de->d_reclen); dentsbytes_actual -= de->d_reclen; buf += dde->d_reclen; read_de += de->d_reclen; written += dde->d_reclen; assert(written <= nbytes); #if 0 fprintf(stderr, "orig: inode %d len %d; made: inode %d len %d\n", de->d_ino, de->d_reclen, dde->d_fileno, dde->d_reclen); #endif } return written; }
void index_generate( int fd, void *void_st, struct reqinfo *req, lx_s *body) { struct stat *st = (struct stat *)void_st; int bsiz = st->st_blksize > 8192 ? st->st_blksize : 8192; char dbuf[bsiz]; int dbytes; #ifndef USE_GETDENTS long base = 0; #endif if (fchdir(fd)) { die_html(HTERR_FORBID|ERRNO, req->location.s, "error changing directory", err_perm_denied); } entry_prefix(body); for (;;) { #ifndef USE_GETDENTS dbytes = getdirentries(fd, dbuf, bsiz, &base); #else dbytes = getdents(fd, dbuf, bsiz); #endif if (dbytes < 0) { die_html(HTERR_SERVERR|errno, req->location.s, "error reading directory entries", err_dir_index); } if (!dbytes) break; process_dents(body, (struct dirent *)dbuf, dbytes); } entry_suffix(body); }
struct dirent* readdir(register DIR* dirp) { register struct dirent* dp; for (;;) { if (dirp->dd_loc >= dirp->dd_size) { if (dirp->dd_size < 0) return(0); dirp->dd_loc = 0; if ((dirp->dd_size = getdents(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0) return(0); } dp = (struct dirent*)((char*)dirp->dd_buf + dirp->dd_loc); if (dp->d_reclen <= 0) return(0); dirp->dd_loc += dp->d_reclen; if (dp->d_fileno) return(dp); } /*NOTREACHED*/ }
/* ** Mmm... chunky. */ void read_chunk() { #if PLATFORM_KERNEL == PLATFORM_KERNEL_LINUX m_read = getdents(m_fd, m_buf.get(), m_bufsz); #elif PLATFORM_KERNEL == PLATFORM_KERNEL_XNU auto off = ::off_t{}; m_read = getdirentries64(m_fd, m_buf.get(), m_bufsz, &off); #endif if (m_read > 0) { m_buf_cur = m_buf.get(); m_buf_end = m_buf.get() + m_read; } else if (m_read == 0) { m_pos = -1; } else if (m_read == -1) { throw std::system_error{errno, std::system_category(), "failed to read directory entries"}; } }
static void check_getdents(const char *testname, const char *path, int isdir, const int *modes, int modes_count) { int fd, i, mode; char buf[8192]; /* * getdents() should always work on directories and never on files, * assuming directories are always opened for read (which they are). */ for (i = 0; i < modes_count; i++) { mode = modes[i]; fd = open(path, mode); if (fd < 0) { notok_mode(testname, "open", mode); continue; } if (getdents(fd, buf, sizeof(buf)) >= 0) { if (isdir && ((mode & O_ACCMODE) == O_RDONLY)) ok_mode(testname, "directory succeeded", mode); else if (isdir) notok_mode(testname, "directory succeeded", mode); else notok_mode(testname, "file succeeded", mode); } else { if (isdir && ((mode & O_ACCMODE) == O_RDONLY)) notok_mode(testname, "directory failed", mode); else if (isdir) ok_mode(testname, "directory failed", mode); else ok_mode(testname, "file failed", mode); } close(fd); } }
int main(int argc, char **argv, char **envp) { int devdir, ii; dirent_t d; int status; for (ii = 0; ii < NFILES; ii++) close(ii); ii = ii; if (-1 == open_tty("/dev/tty0")) { exit(1); } chdir("/dev"); devdir = open("/dev", O_RDONLY, 0); while(getdents(devdir, &d, sizeof(d)) > 0) { if (0 == strncmp(d.d_name, ttystr, strlen(ttystr))) { spawn_shell_on(d.d_name); } } close(devdir); int pid; while (0 <= (pid = wait(&status))) { if (EFAULT == status) { printf("process %i faulted\n", pid); } } if (ECHILD != errno) { printf("error: wait: %s\n", strerror(errno)); return 1; } else { printf(alldone, NULL); return 0; } }
struct dirent64* readdir64(DIR *d) { #ifdef __NR_getdents64 static int trygetdents64=1; #endif struct dirent* o; static struct dirent64 d64; #ifdef __NR_getdents64 again: if (!trygetdents64) { #endif if (!d->num || (d->cur += ((struct dirent*)(d->buf+d->cur))->d_reclen)>=d->num) { int res=getdents(d->fd,(struct dirent*)d->buf, sizeof (d->buf)-1); if (res<=0) return 0; d->num=res; d->cur=0; } o=(struct dirent*)(d->buf+d->cur); d64.d_ino=o->d_ino; d64.d_off=o->d_off; d64.d_reclen=o->d_reclen; strcpy(d64.d_name,o->d_name); d64.d_type=0; /* is this correct? */ return &d64; #ifdef __NR_getdents64 } if (!d->num || (d->cur += ((struct dirent64*)(d->buf+d->cur))->d_reclen)>=d->num) { int res=getdents64(d->fd,(struct dirent64*)d->buf,sizeof (d->buf)); if (res<=0) { if (errno==ENOSYS) { trygetdents64=0; goto again; } return 0; } d->num=res; d->cur=0; } return (struct dirent64*)(d->buf+d->cur); #endif }
static void test_enoent(void) { int fd; struct linux_dirent64 dir64; struct linux_dirent dir; SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE); fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY); if (rmdir(TEST_DIR) == -1) { tst_brkm(TBROK | TERRNO, cleanup, "rmdir(%s) failed", TEST_DIR); } if (longsyscall) getdents64(fd, &dir64, sizeof(dir64)); else getdents(fd, &dir, sizeof(dir)); print_test_result(errno, ENOENT); SAFE_CLOSE(cleanup, fd); }
static int getdent(const char *dir, dirent_t *dirent) { int ret, fd = -1; if (0 > (fd = open(dir, O_RDONLY, 0777))) { return -1; } ret = 1; while (ret != 0) { if (0 > (ret = getdents(fd, dirent, sizeof(*dirent)))) { return -1; } if (0 != strcmp(".", dirent->d_name) && 0 != strcmp("..", dirent->d_name)) { close(fd); return 1; } } close(fd); return 0; }
int init_module( void ) { int dir; dirent_t entry; int error; kprintf( INFO, "Detecting partitions ...\n" ); error = mkdir( "/device/storage/partition", 0777 ); if ( error < 0 ) { kprintf( ERROR, "Failed to create /device/storage/partition!\n" ); return error; } dir = open( "/device/storage", O_RDONLY ); if ( dir < 0 ) { return dir; } while ( getdents( dir, &entry, sizeof( dirent_t ) ) == 1 ) { if ( ( strcmp( entry.name, "." ) == 0 ) || ( strcmp( entry.name, ".." ) == 0 ) ) { continue; } error = check_device_partitions( entry.name ); if ( error < 0 ) { kprintf( ERROR, "Failed to detect partitions on: /device/storage/%s\n", entry.name ); } } close( dir ); return 0; }