rjf@canon.co.uk (Robin Faichney) (06/13/90)
SunOS 4.0.1c on a SPARCstation 1. I want the names of rasterfiles in a given directory and scandir seems ideal, but I can't get it to do it. Going by debugging statements (which are omitted here for clarity) however many appropriate files are found, the list always has only one entry, in which the d_name field is "\0". (BTW, I know I should be using dirent -- but this doesn't work with that either. I also know that I could do it using a script with file, grep and awk -- that's what I'm doing at the moment, but I need more speed and flexibility.) Thanks in advance. Have a nice day. ;-) Code follows ---- /* findfiles.c -- takes directory name on command line, scans it for files of the appropriate types (presently only rasterfiles) and outputs their names */ #include <stdio.h> #include <sys/types.h> #include <sys/param.h> #include <sys/dir.h> #include <rasterfile.h> #define MAXNAMES 200 int approp(d) struct direct *d; { FILE *fp; int magic; if ((fp = fopen(d->d_name, "r")) == NULL) { return 0; } if (fread((char *) &magic, sizeof (magic), 1, fp) != 1) { close(fp); return 0; } close(fp); if (magic != RAS_MAGIC) { return 0; } return 1; } main(argc, argv) int argc; char *argv[]; { struct direct *namelist[MAXNAMES]; int i; if (argc != 2) { exit(-1); } if (chdir(argv[1]) == -1) { exit(-1); } for (i = 0; i < MAXNAMES; i++) { namelist[i] = NULL; } if (scandir(".", namelist, approp, NULL) == -1) { exit(-1); } for (i = 0; namelist[i]; i++) { printf("%s\n", namelist[i]->d_name); } } -- Robin Faichney, Canon Research Centre Europe Ltd, NRS: rjf@uk.co.canon 17-20 Frederick Sanger Road, ARPA: rjf@canon.co.uk Surrey Research Park, Guildford. GU2 5YD UUCP: rjf@canon.uucp Tel (+44)||(0) 483 574325 Fax (+44)||(0) 483 574360