#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);
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];
};
#includeA program to scan the current directory, printing its contents is#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); }
Program Development using Unix Home