From david@cs.uow.edu.au Mon Dec 7 16:41:31 1992 Received: from calvin.sfasu.edu by umaxc.weeg.uiowa.edu (5.61.jnf/920629) on Mon, 7 Dec 92 16:41:26 -0600 id AA11718 with SMTP Received: from wraith.cs.uow.edu.au by calvin.sfasu.EDU with SMTP (5.59/25-eef) id AA06260; Mon, 7 Dec 92 16:34:15 CST Return-Path: Received: by wraith.cs.uow.edu.au (5.65c/IDA-1.4.4); id AA21553; Tue, 8 Dec 1992 09:31:32 +1100 (from david@cs.uow.edu.au for hyperC-l@calvin.sfasu.edu) From: David E A Wilson Message-Id: <199212072231.AA21553@wraith.cs.uow.edu.au> Subject: findfirst & findnext - why opendir & friends. To: hyperC-l@calvin.sfasu.edu (Hyper C Mailing List) Date: Tue, 8 Dec 92 9:31:31 EST Status: R Forwarded message: > As I'm Unix illiterate, please tell me whether the preference is one of > substance or environmental familiarity. Yes - I must admit up front to a bias in favour of UNIX. If I were an MS-DOS programmer I would probably have stopped using my Apple //s many years ago. > Perhaps, some reasons for the choice. As we add more functions we'll soon > come to point when we will have to decide if what we want HyperC to be is > a Unix clone or some unrecognizable hybrid or ..... I would vote for a UNIX clone :-) but the following is sort of a defense. Most C compilers provide a set of routines to emulate UNIX's open/read/write & close interface so providing the opendir/readdir/closedir is in that spirit. Most if not all C code I pull off Usenet is written to run under UNIX. (I am not sure if any MS-DOS compilers provide this interface in addition to the findnext style of interface). > > > struct dirent *readdir(dirp) > > DIR *dirp; > > > I can make an educated guess at what the other functions do and I > _think_ I know what this one does, but can you elaborate? It simply reads directory entries in sequence and returns them one at a time. Here is an example from K&R 2. Dirent *readdir(DIR *dp) { static direct dirbuf; /* local directory structure */ static Dirent d; /* return: portable structure */ while (read(dp->fd, (char *) &dirbuf, sizeof(dirbuf)) == sizeof(dirbuf)) { if (dirbuf.d_ino == 0) /* slot not in use */ continue; d.ino = dirbuf.d_ino; strncpy(d.name, dirbuf.d_name, DIRSIZ); d.name[DIRSIZ] = '\0'; /* terminate name */ return &d; } return NULL; } Obviously this exact code will only work on a UNIX file system but a similar routine for ProDOS should be quite easy. Note: The dirent vs direct is sysV vs BSD. I would lean towards dirent (but this is probably because Sun has moved to dirent. Sample code which searchs a directory for entry ``name'' is: dirp = opendir("."); /* pity Apple did not provide . & .. entries */ for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) if (!strcmp(dp->d_name, name)) { closedir (dirp); return FOUND; } closedir (dirp); return NOT_FOUND; > > >Under ProDOS they should not be very difficult at all to write. I might have > >a go myself. > Please do. The approach looks elegant to this novice. OK. Yet another project (fortunately this fits in nicely with another one). -- David Wilson +61 42 213802 voice, +61 42 213262 fax Dept Comp Sci, Uni of Wollongong david@cs.uow.edu.au