ofer has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/env perl use 5.012; use strict; use warnings; my $str = &listfiles('<Directory>'); print $str; use Inline C => <<'END_OF_C_CODE'; #include <dirent.h> /* Defines DT_* constants */ #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/syscall.h> #define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } whil +e (0) struct linux_dirent { long d_ino; off_t d_off; unsigned short d_reclen; char d_name[]; }; #define BUF_SIZE 1024*1024*5 int listfiles(int argc, char *argv[]) { int fd, nread; char buf[BUF_SIZE]; struct linux_dirent *d; int bpos; char d_type; fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY); if (fd == -1) handle_error("open"); for ( ; ; ) { nread = syscall(SYS_getdents, fd, buf, BUF_SIZE); if (nread == -1) handle_error("getdents"); if (nread == 0) break; for (bpos = 0; bpos < nread;) { d = (struct linux_dirent *) (buf + bpos); if (d->d_ino != 0) printf("%s\n", (char *) d->d_name); bpos += d->d_reclen; } } exit(EXIT_SUCCESS); } END_OF_C_CODE
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using syscalls in perl through inline c
by afoken (Chancellor) on Jan 16, 2017 at 14:17 UTC | |
by ofer (Novice) on Jan 16, 2017 at 14:21 UTC | |
by stevieb (Canon) on Jan 16, 2017 at 14:41 UTC | |
by glasswalk3r (Friar) on Apr 23, 2017 at 18:23 UTC | |
|
Re: using syscalls in perl through inline c
by stevieb (Canon) on Jan 16, 2017 at 15:09 UTC | |
by ofer (Novice) on Jan 16, 2017 at 15:46 UTC | |
by ofer (Novice) on Jan 17, 2017 at 14:43 UTC | |
by ofer (Novice) on Jan 24, 2017 at 15:54 UTC |