1
/****************************************************************************
5
This file contains the C code that implements the directory
6
iteration and file information subsystem.
8
This code is intended to be used as a convenient, machine
9
independent interface to iterate through the contents of a
12
****************************************************************************/
17
* Department of Computer Science
18
* University Of Illinois at Urbana-Champaign
19
* 1304 West Springfield Avenue
26
#include "Directory.h"
29
/*--------------------------------------------------------------------------*
31
L O W L E V E L D I R E C T O R Y I N T E R F A C E
33
*--------------------------------------------------------------------------*/
35
int DirectoryOpen(dir_name,dp)
39
if (DirectoryPathExpand(dir_name,DirectoryPath(dp)) == NULL)
43
DirectoryDir(dp) = opendir(DirectoryPath(dp));
44
if (DirectoryDir(dp) == NULL) return(FALSE);
46
} /* End DirectoryOpen */
49
void DirectoryRestart(dp)
52
rewinddir(DirectoryDir(dp));
53
} /* End DirectoryRestart */
56
void DirectoryClose(dp)
59
closedir(DirectoryDir(dp));
60
} /* End DirectoryClose */
63
long DirectoryTellPosition(dp)
66
return(telldir(DirectoryDir(dp)));
67
} /* End DirectoryTellPosition */
70
void DirectorySetPosition(dp,pos)
74
seekdir(dp->filep,pos);
75
} /* End DirectorySetPosition */
78
int DirectoryReadNextEntry(dp,de)
82
u_short orig_file_type;
83
static struct dirent *_ep;
84
static struct stat _lstats,_stats;
85
char full_path[MAXPATHLEN + 2];
87
_ep = readdir(DirectoryDir(dp));
88
if (_ep == NULL) return(FALSE);
89
strcpy(DirEntryFileName(de),_ep->d_name);
90
strcpy(full_path,DirectoryPath(dp));
91
strcat(full_path,DirEntryFileName(de));
93
if (lstat(full_path,&_lstats) != 0) return(FALSE);
95
orig_file_type = _lstats.st_mode & S_IFMT;
96
switch (orig_file_type)
99
DirEntryType(de) = F_TYPE_DIR;
102
DirEntryType(de) = F_TYPE_FILE;
105
DirEntryType(de) = F_TYPE_CHAR_SPECIAL;
108
DirEntryType(de) = F_TYPE_BLOCK_SPECIAL;
111
DirEntryType(de) = F_TYPE_SYM_LINK;
114
DirEntryType(de) = F_TYPE_SOCKET;
118
DirEntryType(de) = F_TYPE_FIFO;
122
DirEntryType(de) = orig_file_type;
126
DirEntryIsBrokenLink(de) = FALSE;
127
DirEntryIsDirectoryLink(de) = FALSE;
128
if (DirEntryIsSymLink(de)) /* Symbolic Link */
130
if (stat(full_path,&_stats) != 0) /* Can't Stat File */
132
DirEntryIsBrokenLink(de) = TRUE;
135
else /* Link Not Broken */
137
#ifdef SLOW_DIRLINK_TEST
138
char temp_path[MAXPATHLEN + 2];
140
if (DirectoryPathExpand(full_path,temp_path) != NULL)
143
if ((_stats.st_mode & S_IFMT) == S_IFDIR)
146
DirEntryIsDirectoryLink(de) = TRUE;
151
else /* Not Symbolic Link */
156
FileInfoOrigMode(DirEntrySelfInfo(de)) = _lstats.st_mode;
157
FileInfoProt(DirEntrySelfInfo(de)) = _lstats.st_mode & 0777;
158
FileInfoUserID(DirEntrySelfInfo(de)) = _lstats.st_uid;
159
FileInfoGroupID(DirEntrySelfInfo(de)) = _lstats.st_gid;
160
FileInfoFileSize(DirEntrySelfInfo(de)) = _lstats.st_size;
161
FileInfoLastAccess(DirEntrySelfInfo(de)) = _lstats.st_atime;
162
FileInfoLastModify(DirEntrySelfInfo(de)) = _lstats.st_mtime;
163
FileInfoLastStatusChange(DirEntrySelfInfo(de)) = _lstats.st_ctime;
165
FileInfoOrigMode(DirEntryActualInfo(de)) = _stats.st_mode;
166
FileInfoProt(DirEntryActualInfo(de)) = _stats.st_mode & 0777;
167
FileInfoUserID(DirEntryActualInfo(de)) = _stats.st_uid;
168
FileInfoGroupID(DirEntryActualInfo(de)) = _stats.st_gid;
169
FileInfoFileSize(DirEntryActualInfo(de)) = _stats.st_size;
170
FileInfoLastAccess(DirEntryActualInfo(de)) = _stats.st_atime;
171
FileInfoLastModify(DirEntryActualInfo(de)) = _stats.st_mtime;
172
FileInfoLastStatusChange(DirEntryActualInfo(de)) = _stats.st_ctime;
175
} /* End DirectoryReadNextEntry */
178
char *DirectoryPathExpand(old_path,new_path)
179
char *old_path,*new_path;
182
char path[MAXPATHLEN + 2];
184
if (getwd(path) == NULL) return(NULL);
185
if (chdir(old_path) != 0) return(NULL);
186
if (getwd(new_path) == NULL) strcpy(new_path,old_path);
187
if (chdir(path) != 0) return(NULL);
188
for (p = new_path; *p != '\0'; p++);
189
/* append trailing slash if not already in place... */
190
if ((p != new_path) && *(p - 1) != '/')
196
} /* End DirectoryPathExpand */
199
/*---------------------------------------------------------------------------*
201
D I R E C T O R Y E N T R Y R O U T I N E S
203
*---------------------------------------------------------------------------*/
205
void DirEntryDump(fp,de)
209
fprintf(fp,"%20s, Size %7d, Prot %3o\n",
210
DirEntryFileName(de),DirEntryFileSize(de),DirEntryProt(de));
211
} /* End DirEntryDump */