os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/compat/opendir.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* 
sl@0
     2
 * opendir.c --
sl@0
     3
 *
sl@0
     4
 *	This file provides dirent-style directory-reading procedures
sl@0
     5
 *	for V7 Unix systems that don't have such procedures.  The
sl@0
     6
 *	origin of this code is unclear, but it seems to have come
sl@0
     7
 *	originally from Larry Wall.
sl@0
     8
 *
sl@0
     9
 *
sl@0
    10
 * RCS: @(#) $Id: opendir.c,v 1.2 1998/09/14 18:39:44 stanton Exp $
sl@0
    11
 */
sl@0
    12
sl@0
    13
#include "tclInt.h"
sl@0
    14
#include "tclPort.h"
sl@0
    15
sl@0
    16
#undef DIRSIZ
sl@0
    17
#define DIRSIZ(dp) \
sl@0
    18
    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
sl@0
    19
sl@0
    20
/*
sl@0
    21
 * open a directory.
sl@0
    22
 */
sl@0
    23
DIR *
sl@0
    24
opendir(name)
sl@0
    25
char *name;
sl@0
    26
{
sl@0
    27
	register DIR *dirp;
sl@0
    28
	register int fd;
sl@0
    29
	char *myname;
sl@0
    30
sl@0
    31
	myname = ((*name == '\0') ? "." : name);
sl@0
    32
	if ((fd = open(myname, 0, 0)) == -1)
sl@0
    33
		return NULL;
sl@0
    34
	if ((dirp = (DIR *)ckalloc(sizeof(DIR))) == NULL) {
sl@0
    35
		close (fd);
sl@0
    36
		return NULL;
sl@0
    37
	}
sl@0
    38
	dirp->dd_fd = fd;
sl@0
    39
	dirp->dd_loc = 0;
sl@0
    40
	return dirp;
sl@0
    41
}
sl@0
    42
sl@0
    43
/*
sl@0
    44
 * read an old style directory entry and present it as a new one
sl@0
    45
 */
sl@0
    46
#ifndef pyr
sl@0
    47
#define	ODIRSIZ	14
sl@0
    48
sl@0
    49
struct	olddirect {
sl@0
    50
	ino_t	od_ino;
sl@0
    51
	char	od_name[ODIRSIZ];
sl@0
    52
};
sl@0
    53
#else	/* a Pyramid in the ATT universe */
sl@0
    54
#define	ODIRSIZ	248
sl@0
    55
sl@0
    56
struct	olddirect {
sl@0
    57
	long	od_ino;
sl@0
    58
	short	od_fill1, od_fill2;
sl@0
    59
	char	od_name[ODIRSIZ];
sl@0
    60
};
sl@0
    61
#endif
sl@0
    62
sl@0
    63
/*
sl@0
    64
 * get next entry in a directory.
sl@0
    65
 */
sl@0
    66
struct dirent *
sl@0
    67
readdir(dirp)
sl@0
    68
register DIR *dirp;
sl@0
    69
{
sl@0
    70
	register struct olddirect *dp;
sl@0
    71
	static struct dirent dir;
sl@0
    72
sl@0
    73
	for (;;) {
sl@0
    74
		if (dirp->dd_loc == 0) {
sl@0
    75
			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
sl@0
    76
			    DIRBLKSIZ);
sl@0
    77
			if (dirp->dd_size <= 0)
sl@0
    78
				return NULL;
sl@0
    79
		}
sl@0
    80
		if (dirp->dd_loc >= dirp->dd_size) {
sl@0
    81
			dirp->dd_loc = 0;
sl@0
    82
			continue;
sl@0
    83
		}
sl@0
    84
		dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
sl@0
    85
		dirp->dd_loc += sizeof(struct olddirect);
sl@0
    86
		if (dp->od_ino == 0)
sl@0
    87
			continue;
sl@0
    88
		dir.d_ino = dp->od_ino;
sl@0
    89
		strncpy(dir.d_name, dp->od_name, ODIRSIZ);
sl@0
    90
		dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
sl@0
    91
		dir.d_namlen = strlen(dir.d_name);
sl@0
    92
		dir.d_reclen = DIRSIZ(&dir);
sl@0
    93
		return (&dir);
sl@0
    94
	}
sl@0
    95
}
sl@0
    96
sl@0
    97
/*
sl@0
    98
 * close a directory.
sl@0
    99
 */
sl@0
   100
void
sl@0
   101
closedir(dirp)
sl@0
   102
register DIR *dirp;
sl@0
   103
{
sl@0
   104
	close(dirp->dd_fd);
sl@0
   105
	dirp->dd_fd = -1;
sl@0
   106
	dirp->dd_loc = 0;
sl@0
   107
	ckfree((char *) dirp);
sl@0
   108
}