/*- * Copyright (c) 2015 The FreeBSD Foundation * All rights reserved. * * This software was developed by Edward Tomasz Napierala under sponsorship * from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ /* * Simple test program for playing with different getdents(2) buffer sizes. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #define BUF_SIZE 16 * 1024 //#define BUF_SIZE 1724 static void usage(void) { fprintf(stderr, "usage: dents bufsize [path]\n"); exit(1); } static const char * type2str(uint8_t type) { switch (type) { case DT_REG: return("reg"); case DT_DIR: return("dir"); case DT_FIFO: return("fifo"); case DT_SOCK: return("sock"); case DT_LNK: return("lnk"); case DT_BLK: return("blk"); case DT_CHR: return("chr"); default: return("unknown"); } } int main(int argc, char *argv[]) { int fd, off, nread; char buf[BUF_SIZE]; const char *path; struct dirent *d; size_t bufsize; if (argc < 2 || argc > 3) usage(); bufsize = atoi(argv[1]); if (bufsize <= 0) err(1, "bufsize must be > 0"); if (bufsize >= sizeof(buf)) err(1, "bufsize must be < %zd", sizeof(buf)); if (argc == 3) path = argv[2]; else path = "."; fd = open(path, O_RDONLY | O_DIRECTORY); if (fd == -1) err(1, "%s", path); printf("fileno reclen type namlen name\n"); printf("--------------------------------------------\n"); for (;;) { nread = getdents(fd, buf, bufsize); if (nread == -1) err(1, "getdents"); if (nread == 0) break; printf("\nnread = %d:\n", nread); for (off = 0; off < nread; off += d->d_reclen) { d = (struct dirent *)(buf + off); printf("%-10u %d %s %u %s\n", d->d_fileno, d->d_reclen, type2str(d->d_type), d->d_namlen, d->d_name); } } return (0); }