[Gc] Interior pointers
Michael Talbot-Wilson
mtw at view.net.au
Fri Dec 28 21:21:10 PST 2012
Guys, thanks. Here is the full program. (Assuming I can send an
attachment to this mailing list.)
Hope you can put me straight.
--Mike
-------------- next part --------------
/* Copied from the getdents(2) manpage and modified. */
/*
* $(CC) -o $(PROGNAME) \
* -I./gc-7.2d/include $(PROGNAME).c ./gc-7.2d/.libs/libgc.a
*/
#define _GNU_SOURCE
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#define GC_DEBUG
#include <gc.h>
#define handle_error(msg) \
do { perror(msg);exit(EXIT_FAILURE); } while (0)
struct linux_dirent {
long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[];
};
int off_to_name = sizeof(long) + sizeof(off_t) + sizeof(short);
#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
int fd, nread, bpos, k;
/* char buf[BUF_SIZE]; */
char *buf;
struct linux_dirent *d;
GC_INIT();
/* array holding pointers to 5000 names */
char **allnames = (char **) GC_MALLOC(5000);
int array_subscript=0;
fd = open(argc>1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
if (fd==-1)
handle_error("open");
for (;;) {
buf = (char *) GC_MALLOC(BUF_SIZE);
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);
allnames[array_subscript++] = (char *)d + off_to_name;
printf("& allnames[0]=%ld\n", (long) &(allnames[0]));
printf("1 allnames[0]=%s\n", allnames[0]);
printf("2 allnames[%d]=%s\n",
array_subscript-1, allnames[array_subscript-1]);
bpos+=d->d_reclen;
}
}
close(fd);
exit(EXIT_SUCCESS);
}
More information about the Gc
mailing list