Program Development using Unix DIrectories - Unix API

Contents

mkdir
creat
link
unlink
rmdir
chdir
getcwd
Reading dirs
Examples
">

Unix API

Some of the Unix API for directory operations is
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int mkdir(char *path,
          mode_t mode);
int creat(char *path, 
          mode_t mode);
int link(char *path1, char *path2);
int unlink(char *path);
int rmdir(char *path);
int chdir(char *path);
char *getcwd(char *buf, int size);
and
#include <sys/types.h>
#include <dirent.h>

DIR *opendir(char *path);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);

mkdir

This creates a new directory with no initial contents (apart from `.' and `..'). It returns -1 on failure

creat

This creates a new file, using a new inode. It also creates a new entry in the directory. Since this changes the directory (as a file), the last modified times of the directory are updated.

link

link adds another name to an existing file. It does not make a new inode entry, but does make a new directory entry. The link count in the inode is increased.

unlink

This removes an entry from a directory. The link count in the inode is decreased. If the count falls to zero, the file can be removed once no process has it open. Removing a file from a directory changes the last modified time of the directory (it is a file with changed contents).

rmdir

Removing a directory will fail unless the directory is empty.

chdir

This will attempt to change directory. The types of patterns that will be accepted for directory names is not specified.

getcwd

This will fill in the buffer with the current directory. This call can actually fail: if another process has succeeded in removing the current directory of a process.

Reading dirs

The call opendir() opens a directory for reading. The returned DIR value is opaque and is just passed to other functions. It is NULL on error.

Under Linux, the DIR structure is

typedef struct DIR
{
  /* file descriptor */
  int dd_fd;

  /* offset of the next dir entry in buffer */
  off_t dd_loc;

  /* bytes of valid entries in buffer */
  size_t dd_size;

  /* -> directory buffer */
  struct dirent *dd_buf;
} DIR;

The call readdir() returns a structure that you can use. It is specified to have one entry char *d_name. Under Linux it is defined as

struct dirent {
        long            d_ino;
        __kernel_off_t  d_off;
        unsigned short  d_reclen;
        char            d_name[256]; 
};

Examples

This creates a directory, puts a file into it and then removes it.
#include 
#include 
#include 

int main(void)
{  int fd;

  if (mkdir("mydir", 0777)
           == -1)
    exit(1);
  if ((fd = creat("mydir/f1",
                0777)) == -1)
    exit(2);
  close(fd);

  unlink("mydir/f1");
  exit(0);
}
A program to scan the current directory, printing its contents is
Home Program Development using Unix Home
Jan Newmarch (http://jan.newmarch.name) <jan@newmarch.name>

Copyright © Jan Newmarch
Last modified: Wed Nov 19 17:55:42 EST 1997