0% found this document useful (0 votes)
33 views7 pages

Unit 2 Directories

The document provides an overview of system calls in Linux for managing directories, including mkdir for creating directories, rmdir for removing empty directories, and chdir for changing the current working directory. It also covers obtaining the current working directory with getcwd and listing directory contents using opendir, readdir, and closedir. Additionally, it explains how to scan directories using opendir, readdir, rewinddir, and closedir functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views7 pages

Unit 2 Directories

The document provides an overview of system calls in Linux for managing directories, including mkdir for creating directories, rmdir for removing empty directories, and chdir for changing the current working directory. It also covers obtaining the current working directory with getcwd and listing directory contents using opendir, readdir, and closedir. Additionally, it explains how to scan directories using opendir, readdir, rewinddir, and closedir functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Directories: creating, removing, changing directories – mkdir, rmdir,

chdir

In Linux, the system calls mkdir, rmdir, and chdir are used to perform operations on
directories at a low level, typically within a C program or other system-level
programming environments. Here’s a brief overview of each:

1. Creating Directories (mkdir)

The mkdir system call is used to create a new directory.

 Prototype: int mkdir(const char *pathname, mode_t mode);


 Parameters:
o pathname: The path to the directory you want to create.
o mode: The permissions for the new directory (e.g., 0755).
 Return Value:
o Returns 0 on success.
o Returns -1 on failure, and errno is set to indicate the error.
 Example:

#include <sys/stat.h>
#include <sys/types.h>

int result = mkdir("/path/to/directory", 0755);


if (result == -1) {
// Handle error
}

2. Removing Directories (rmdir)

The rmdir system call is used to remove an empty directory.

 Prototype: int rmdir(const char *pathname);


 Parameters:
o pathname: The path to the directory you want to remove.
 Return Value:
o Returns 0 on success.
o Returns -1 on failure, and errno is set to indicate the error.
 Example:

#include <unistd.h>

int result = rmdir("/path/to/directory");


if (result == -1) {
// Handle error
}

3. Changing Directories (chdir)


The chdir system call changes the current working directory of the calling process.

 Prototype: int chdir(const char *path);


 Parameters:
o path: The path to the directory you want to change to.
 Return Value:
o Returns 0 on success.
o Returns -1 on failure, and errno is set to indicate the error.
 Example:

#include <unistd.h>

int result = chdir("/path/to/directory");


if (result == -1) {
// Handle error
}

Summary:

 mkdir: Creates a new directory.


 rmdir: Removes an empty directory.
 chdir: Changes the current working directory.

These system calls are fundamental for directory management in low-level


programming on Linux, providing direct interaction with the file system.
Obtaining Current Working Directory – getcwd, directory
contents
In Linux, you can obtain the current working directory using the getcwd system call
and list the contents of a directory using system calls like opendir, readdir, and
closedir. Here's how these work:
1. Obtaining the Current Working Directory (getcwd)
The getcwd system call is used to get the current working directory of the calling
process.
 Prototype: char *getcwd(char *buf, size_t size);
 Parameters:
o buf: A pointer to a buffer where the current working directory path will
be stored.
o size: The size of the buffer.
 Return Value:
o Returns buf on success.
o Returns NULL on failure, and errno is set to indicate the error.
 Example:
#include <unistd.h>
#include <stdio.h>
#include <limits.h>

int main() {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working directory: %s\n", cwd);
} else {
perror("getcwd() error");
}
return 0;
}
2. Listing Directory Contents Using System Calls
To list the contents of a directory, you typically use the following system calls:
 opendir: Opens a directory for reading.
 readdir: Reads the next directory entry.
 closedir: Closes the directory stream.
opendir: Opening a Directory
 Prototype: DIR *opendir(const char *name);
 Parameters:
o name: The path of the directory you want to open.
 Return Value:
o Returns a pointer to a DIR structure on success.
o Returns NULL on failure, and errno is set to indicate the error.
readdir: Reading Directory Entries
 Prototype: struct dirent *readdir(DIR *dirp);
 Parameters:
o dirp: A pointer to the DIR structure returned by opendir.
 Return Value:
o Returns a pointer to a struct dirent on success.
o Returns NULL when the end of the directory is reached or on error.
 struct dirent: This structure typically contains fields like:
o d_name: Name of the directory entry (file or subdirectory).
o d_ino: Inode number.
o (Note: struct dirent may have additional fields depending on the
system.)
closedir: Closing a Directory
 Prototype: int closedir(DIR *dirp);
 Parameters:
o dirp: A pointer to the DIR structure returned by opendir.
 Return Value:
o Returns 0 on success.
o Returns -1 on failure, and errno is set to indicate the error.
Example: Listing Directory Contents
#include <stdio.h>
#include <dirent.h>

int main() {
DIR *d;
struct dirent *dir;
d = opendir("/path/to/directory");
if (d) {
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
}
closedir(d);
} else {
perror("opendir() error");
}
return 0;
}
Summary:
 getcwd: Retrieves the current working directory.
 opendir: Opens a directory for reading.
 readdir: Reads the contents of the directory.
 closedir: Closes the directory stream.
These system calls are essential for navigating and managing directories
programmatically in Linux at a low level.
Scanning Directories – opendir, readdir, closedir, rewinddir
functions

In Linux, scanning directories involves using a combination of system calls to open,


read, and close directory streams, as well as resetting the directory stream to the
beginning. Here’s a detailed overview of the functions opendir, readdir, closedir,
and rewinddir:
1. Opening a Directory (opendir)
The opendir function opens a directory for reading and returns a pointer to a DIR
structure that represents the directory stream.
 Prototype: DIR *opendir(const char *name);
 Parameters:
o name: The path of the directory to be opened.
 Return Value:
o Returns a pointer to a DIR structure on success.
o Returns NULL on failure, and errno is set to indicate the error.
 Example:
DIR *dir = opendir("/path/to/directory");
if (dir == NULL) {
perror("opendir");
return 1;
}
2. Reading Directory Entries (readdir)
The readdir function reads the next entry in the directory stream opened by
opendir.
 Prototype: struct dirent *readdir(DIR *dirp);
 Parameters:
o dirp: A pointer to the DIR structure returned by opendir.
 Return Value:
o Returns a pointer to a struct dirent on success.
o Returns NULL when the end of the directory is reached or on error.
 struct dirent: This structure typically contains fields like:
o d_name: Name of the directory entry (file or subdirectory).
o d_ino: Inode number.
o (Other fields may be present depending on the system.)
 Example:
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
3. Rewinding the Directory Stream (rewinddir)
The rewinddir function resets the position of the directory stream to the beginning,
allowing you to re-read the directory entries from the start.
 Prototype: void rewinddir(DIR *dirp);
 Parameters:
o dirp: A pointer to the DIR structure returned by opendir.
 Return Value:
o This function does not return a value.
 Example:
rewinddir(dir);
4. Closing a Directory (closedir)
The closedir function closes the directory stream and frees the resources associated
with it.
 Prototype: int closedir(DIR *dirp);
 Parameters:
o dirp: A pointer to the DIR structure returned by opendir.
 Return Value:
o Returns 0 on success.
o Returns -1 on failure, and errno is set to indicate the error.
 Example:
if (closedir(dir) == -1) {
perror("closedir");
return 1;
}
Example: Scanning a Directory
Here’s a complete example that demonstrates opening a directory, reading its
contents, rewinding the directory stream, and closing it:
#include <stdio.h>
#include <dirent.h>

int main() {
DIR *dir;
struct dirent *entry;

// Open the directory


dir = opendir("/path/to/directory");
if (dir == NULL) {
perror("opendir");
return 1;
}

// Read and print directory entries


while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

// Rewind and read again


rewinddir(dir);
printf("Re-reading directory entries:\n");
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

// Close the directory


if (closedir(dir) == -1) {
perror("closedir");
return 1;
}

return 0;
}
Summary:
 opendir: Opens a directory stream.
 readdir: Reads the next entry from the directory stream.
 rewinddir: Resets the directory stream to the beginning.
 closedir: Closes the directory stream.
These functions are essential for directory manipulation in C programs on Linux,
providing the means to efficiently scan and manage directory contents.

You might also like