os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/compat/opendir.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/compat/opendir.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,108 @@
     1.4 +/* 
     1.5 + * opendir.c --
     1.6 + *
     1.7 + *	This file provides dirent-style directory-reading procedures
     1.8 + *	for V7 Unix systems that don't have such procedures.  The
     1.9 + *	origin of this code is unclear, but it seems to have come
    1.10 + *	originally from Larry Wall.
    1.11 + *
    1.12 + *
    1.13 + * RCS: @(#) $Id: opendir.c,v 1.2 1998/09/14 18:39:44 stanton Exp $
    1.14 + */
    1.15 +
    1.16 +#include "tclInt.h"
    1.17 +#include "tclPort.h"
    1.18 +
    1.19 +#undef DIRSIZ
    1.20 +#define DIRSIZ(dp) \
    1.21 +    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
    1.22 +
    1.23 +/*
    1.24 + * open a directory.
    1.25 + */
    1.26 +DIR *
    1.27 +opendir(name)
    1.28 +char *name;
    1.29 +{
    1.30 +	register DIR *dirp;
    1.31 +	register int fd;
    1.32 +	char *myname;
    1.33 +
    1.34 +	myname = ((*name == '\0') ? "." : name);
    1.35 +	if ((fd = open(myname, 0, 0)) == -1)
    1.36 +		return NULL;
    1.37 +	if ((dirp = (DIR *)ckalloc(sizeof(DIR))) == NULL) {
    1.38 +		close (fd);
    1.39 +		return NULL;
    1.40 +	}
    1.41 +	dirp->dd_fd = fd;
    1.42 +	dirp->dd_loc = 0;
    1.43 +	return dirp;
    1.44 +}
    1.45 +
    1.46 +/*
    1.47 + * read an old style directory entry and present it as a new one
    1.48 + */
    1.49 +#ifndef pyr
    1.50 +#define	ODIRSIZ	14
    1.51 +
    1.52 +struct	olddirect {
    1.53 +	ino_t	od_ino;
    1.54 +	char	od_name[ODIRSIZ];
    1.55 +};
    1.56 +#else	/* a Pyramid in the ATT universe */
    1.57 +#define	ODIRSIZ	248
    1.58 +
    1.59 +struct	olddirect {
    1.60 +	long	od_ino;
    1.61 +	short	od_fill1, od_fill2;
    1.62 +	char	od_name[ODIRSIZ];
    1.63 +};
    1.64 +#endif
    1.65 +
    1.66 +/*
    1.67 + * get next entry in a directory.
    1.68 + */
    1.69 +struct dirent *
    1.70 +readdir(dirp)
    1.71 +register DIR *dirp;
    1.72 +{
    1.73 +	register struct olddirect *dp;
    1.74 +	static struct dirent dir;
    1.75 +
    1.76 +	for (;;) {
    1.77 +		if (dirp->dd_loc == 0) {
    1.78 +			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
    1.79 +			    DIRBLKSIZ);
    1.80 +			if (dirp->dd_size <= 0)
    1.81 +				return NULL;
    1.82 +		}
    1.83 +		if (dirp->dd_loc >= dirp->dd_size) {
    1.84 +			dirp->dd_loc = 0;
    1.85 +			continue;
    1.86 +		}
    1.87 +		dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
    1.88 +		dirp->dd_loc += sizeof(struct olddirect);
    1.89 +		if (dp->od_ino == 0)
    1.90 +			continue;
    1.91 +		dir.d_ino = dp->od_ino;
    1.92 +		strncpy(dir.d_name, dp->od_name, ODIRSIZ);
    1.93 +		dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
    1.94 +		dir.d_namlen = strlen(dir.d_name);
    1.95 +		dir.d_reclen = DIRSIZ(&dir);
    1.96 +		return (&dir);
    1.97 +	}
    1.98 +}
    1.99 +
   1.100 +/*
   1.101 + * close a directory.
   1.102 + */
   1.103 +void
   1.104 +closedir(dirp)
   1.105 +register DIR *dirp;
   1.106 +{
   1.107 +	close(dirp->dd_fd);
   1.108 +	dirp->dd_fd = -1;
   1.109 +	dirp->dd_loc = 0;
   1.110 +	ckfree((char *) dirp);
   1.111 +}