cs00wsc@unccvax.UUCP (Shiang Chin) (11/15/89)
I am trying to the file list of a file directory on
Sun station running Unix. Can someone read the following
C code for me; I don't know what's wrong on my code.
#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
main(){
dir(".");
exit(0);
}
int dir(path)
char* path;
{
int fd;
struct direct nbuf;
if ((fd = open(path,0)) == -1)
return (0);
while (read(fd,&nbuf, DIRSIZ(&nbuf)) {
if (nbuf.d_fileno != 0)
printf("%u %s\n",nbuf.d_namlen,nbuf.d_name);
else
printf("error\n");
}
return 1;
}
------------------------------------------------------------------
If anyone knows the problem, please tell me how to solve this.
Thanks a lot.cpcahil@virtech.uucp (Conor P. Cahill) (11/15/89)
In article <1724@unccvax.UUCP>, cs00wsc@unccvax.UUCP (Shiang Chin) writes: > I am trying to the file list of a file directory on > Sun station running Unix. Can someone read the following > C code for me; I don't know what's wrong on my code. > [ example of reading fixed length directory entries deleted ] > If anyone knows the problem, please tell me how to solve this. > Thanks a lot. The problem is that Sun uses a BSD style directory that does not have fixed length directory entries. You should use the opendir(), readdir(), etc library calls to process the directory entries. Doug gwyn posted a portable version of these directory utilities (that work for both system V and BSD file systems) to comp.sources.unix (I think). You can either use the standard BSD functions that are included in the sun libc or you can download the portable versions. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+
gwyn@smoke.BRL.MIL (Doug Gwyn) (11/16/89)
In article <1724@unccvax.UUCP> cs00wsc@unccvax.UUCP (Shiang Chin) writes: >... I don't know what's wrong on my code. The main thing wrong is that you're attempting to read a directory as though it were an ordinary data file. These days, that's a poor assumption, and you should always use the portable <dirent.h> interface to read directories.
chris@mimsy.umd.edu (Chris Torek) (11/20/89)
>In article <1724@unccvax.UUCP> cs00wsc@unccvax.UUCP (Shiang Chin) writes: >>... I don't know what's wrong on my code. (Well, for one thing, it is in the wrong newsgroup: it is a Unix-specific question. Merely because the directory reading is written in C does not make directory-reading a C operation, and questions about reading Unix directories belong in a Unix newsgroup---just as questions about reading MS-DOS directories belong in an MS-DOS newsgroup.) In article <11602@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn) writes: >The main thing wrong is that you're attempting to read a directory >as though it were an ordinary data file. These days, that's a poor >assumption, and you should always use the portable <dirent.h> >interface to read directories. Actually, the directory still *is* (almost) an ordinary data file; but it is a data file with a complex format, not a simple collection of fixed-length records. Once you Reach Out over NFS (or are we allowed to use the words `Reach Out' with NFS, that being a Sun system? :-) ), however, directories are indeed not anything at all like ordinary data files. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@cs.umd.edu Path: uunet!mimsy!chris