Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.

Download Report

Transcript Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.

Directory
Directory is a file containing list of entries,where each entry
represents inode number and name of the file stored
in that directory
Directory operations are performed using the following
Directory API
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
mkdir
opendir
readdir
closedir
rewinddir
telldir
seekdir
rmdir
chdir
getcwd
MKDIR
Purpose: to create directory files
#include<sys/stat.h>
#include<unistd.h>
int mkdir(const char* path_name,mode_t mode)
Here:
The path_name argument is the path name of a directory file to
be created.
The mode arguments specifies the access permission for the
owner,group,and others to be assigned to the file.
(Note: Mode values is modified by the calling process umask)
The Return value of mkdir is 0 if it success or -1 if it fails
OPENDIR
Purpose: Opens a directory file for read-only
#include<sys/types.h>
#include<dirent.h>
DIR* opendir(const char* path_name)
Here:
The Path_name argument is the path name of a directory file to
be opened.
The function returns a DIR* file handler. The DIR data structure
is defined in the <dirent.h> header if it success
or returns NULL if it fails
READDIR
Purpose: reads the next directory record from a directory
file referenced by the argument . The argument values is the
DIR* return value from an opendir call.
#include<sys/types.h>
#include<dirent.h>
struct dirent* readdir(DIR* f_dir)
The function returns the address of a struct dirent if it
success or NULL if it fails
CLOSEDIR
Purpose: Closes a directory file referenced by f_dir
#include<sys/types.h>
#include<dirent.h>
int closedir(DIR* f_dir)
The function returns 0 if it success or -1 if it fails
REWINDDIR
Purpose: Resets the file pointer to the beginning of the
directory file referenced by f_dir
#include<sys/stat.h>
#include<dirent.h>
void rewinddir(DIR* f_dir)
TELLDIR
Purpose: returns the current location associated with the
specified directory stream.
#include<sys/types.h>
#include<dirent.h>
long int telldir(DIR *DirectoryPointer)
SEEKDIR
Purpose: Changes the file pointer of a given f_dir to a
specified address.
#include<sys/types.h>
#include<dirent.h>
void seekdir(DIR *DirectoryPointer, long Location)
RMDIR
Purpose: removes the directory specified by the path_name
parameter
#include<unistd.h>
int rmdir(const char* path_name)
The function returns 0 if it success or -1 if it fails
CHDIR
Purpose: changes the current directory to the directory
indicated by the Path parameter.
#include<unistd.h>
int chdir(const char* path)
The function returns 0 if it success or -1 if it fails
GETCWD
Purpose: places the absolute path name of the current
working directory in the array pointed to by
the Buffer parameter, and returns that path name.
The size parameter specifies the size in bytes of the
character array pointed to by the Buffer parameter.
#include <unistd.h>
char *getcwd ( char*Buffer,size_t size)
If the getcwd subroutine is unsuccessful, a null value is
returned.
The dirent structure contains the following fields for each
directory entry:
ulong_t d_offset; /* actual offset of this entry */
ino_t d_ino; /* inode number of entry */
ushort_t d_reclen; /* length of this entry */
ushort_t d_namlen; /* length of string in d_name */
char d_name[_D_NAME_MAX+1]; /* name of entry
(filename) *
C program that simulates ls command
#include<stdio.h>
#include<string.h>
#include<dirent.h>
int main(int argc,char* argv[])
{
DIR *dp;
struct dirent *de;
int i;
if(argc==1)//Without arguments printing current directory files
{
argv[1]=".";
argc++;
}
for(i=1;i<argc;++i)
{
dp=opendir(argv[i]);
if(dp==NULL)
{
perror(argv[i]);
exit(1);
}
printf("%s:\n",argv[i]);
while((de=readdir(dp))!=NULL)
{
if(de->d_name[0]!='.')//ignoring file which start with dot(.)
printf("%s\t",de->d_name);
}
printf("\n");
closedir(dp);
}
return 0;
}