Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!decvax!ittatc!dcdwest!sdcsvax!ucbvax!PAVEPAWS.BERKELEY.EDU!dillon From: dillon@PAVEPAWS.BERKELEY.EDU (Matt Dillon) Newsgroups: net.micro.amiga Subject: Re: Does anyone know how to get directory info within a prog? Message-ID: <8606241017.AA10938@pavepaws> Date: Tue, 24-Jun-86 06:17:57 EDT Article-I.D.: pavepaws.8606241017.AA10938 Posted: Tue Jun 24 06:17:57 1986 Date-Received: Wed, 25-Jun-86 07:33:53 EDT Sender: daemon@ucbvax.BERKELEY.EDU Organization: University of California at Berkeley Lines: 171 I took this out of the code for my SHELL. It should give you some idea of how to get a directory. /* * Disk directory routines * * dptr = dopen(name, stat) * struct DPTR *dptr; * char *name; * int *stat; * * dnext(dptr, name, stat) * struct DPTR *dptr; * char **name; * int *stat; * * dclose(dptr) -may be called with NULL without harm * * dopen() returns a struct DPTR, or NULL if the given file does not * exist. stat will be set to 1 if the file is a directory. If the * name is "", then the current directory is openned. * * dnext() returns 1 until there are no more entries. The **name and * *stat are set. *stat = 1 if the file is a directory. * * dclose() closes a directory channel. * */ struct DPTR { /* Format of directory fetch pointer */ struct FileLock *lock; /* lock on directory */ struct FileInfoBlock *fib; /* mod'd fib for entry */ }; struct DPTR * dopen(name, stat) char *name; int *stat; { struct DPTR *dp; int namelen, endslash = 0; namelen = strlen(name); if (namelen && name[namelen - 1] == '/') { name[namelen - 1] = '\0'; endslash = 1; } *stat = 0; dp = (struct DPTR *)malloc(sizeof(struct DPTR)); if (*name == '\0') dp->lock = DupLock (Clock); else dp->lock = Lock (name, ACCESS_READ); if (endslash) name[namelen - 1] = '/'; if (dp->lock == NULL) { free (dp); return (NULL); } dp->fib = (struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC); if (!Examine (dp->lock, dp->fib)) { perror (name); dclose (dp); return (NULL); } if (dp->fib->fib_DirEntryType >= 0) *stat = 1; return (dp); } dnext(dp, pname, stat) struct DPTR *dp; char **pname; int *stat; { if (dp == NULL) return (0); if (ExNext (dp->lock, dp->fib)) { *stat = (dp->fib->fib_DirEntryType < 0) ? 0 : 1; *pname = dp->fib->fib_FileName; return (1); } return (0); } dclose(dp) struct DPTR *dp; { if (dp == NULL) return (1); if (dp->fib) FreeMem (dp->fib, sizeof(*dp->fib)); if (dp->lock) UnLock (dp->lock); free (dp); return (1); } ----------------------------------------------------------------------------- do_dir(garbage, com) char *garbage; { struct DPTR *dp; struct InfoData *info; char *name; int i, stat; long total = 0; if (ac == 1) { ++ac; av[1] = ""; } for (i = 1; i < ac; ++i) { if ((dp = dopen (av[i], &stat)) == NULL) continue; if (com < 0) { info = (struct InfoData *)AllocMem(sizeof(struct InfoData), MEMF_PUBLIC); if (Info (dp->lock, info)) { int bpb = info->id_BytesPerBlock; printf ("Unit:%2d Errs:%3d Bytes: %-7d Free: %-7d\n", info->id_UnitNumber, info->id_NumSoftErrors, bpb * info->id_NumBlocks, bpb * (info->id_NumBlocks - info->id_NumBlocksUsed)); } else { perror (av[i]); } FreeMem (info, sizeof(*info)); } else { if (stat) { while (dnext (dp, &name, &stat)) { total += disp_entry (dp->fib); if (CHECKBREAK()) break; } } else { total += disp_entry(dp->fib); } } dclose (dp); } printf ("TOTAL: %ld\n", total); return (1); } long disp_entry(fib) register struct FileInfoBlock *fib; { char str[5]; register char *dirstr; str[4] = '\0'; str[0] = (fib->fib_Protection & FIBF_READ) ? '-' : 'r'; str[1] = (fib->fib_Protection & FIBF_WRITE) ? '-' : 'w'; str[2] = (fib->fib_Protection & FIBF_EXECUTE) ? '-' : 'x'; str[3] = (fib->fib_Protection & FIBF_DELETE) ? '-' : 'd'; dirstr = (fib->fib_DirEntryType < 0) ? " " : "DIR"; printf ("%s %6ld %s %s\n", str, (long)fib->fib_Size, dirstr, fib->fib_FileName); return ((long)fib->fib_Size); }