This code example demonstrates how to create a directory with specific permissions using the mkdir
function. The permissions are specified using symbolic constants provided by the system headers. The program checks if the directory creation was successful and provides appropriate feedback.
In Unix-like operating systems, file and directory permissions are represented by specific constants that are defined in system headers. These permissions control the ability of users to read, write, and execute files or directories. Here is a detailed explanation of these constants:
0777
(rwxrwxrwx). Grants read, write, and execute permissions to user, group, and others.0666
(rw-rw-rw-). Grants read and write permissions to user, group, and others, but no execute permissions.Here is the detailed C++ code with explanations:
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int main() {
// Define the directory path to be created
char* dir = "/path/directory-name";
// Create the directory with read, write, and execute permissions for user, group, and others
int created = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
// The above line is equivalent to: int created = mkdir(dir, 0777);
// Check if the directory was created successfully
if (!created) {
// If the return value is 0, the directory was created successfully
printf("Directory created successfully\n");
} else {
// If the return value is not 0, directory creation failed
printf("Directory creation failed\n");
exit(1);
}
return 0;
}
int main() {
// Define the directory path to be created
char* dir = "/path/directory-name";
// Create the directory with read, write, and execute permissions for user, group, and others
int created = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
// The above line is equivalent to: int created = mkdir(dir, 0777);
int main() {
: The main function where the program execution begins.char* dir = "/path/directory-name";
: Defines a character pointer dir
to hold the path of the directory to be created.int created = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
: Calls the mkdir
function to create a directory with specified permissions. S_IRWXU
, S_IRWXG
, and S_IRWXO
are constants that grant read, write, and execute permissions to the user, group, and others, respectively. // Check if the directory was created successfully
if (!created) {
// If the return value is 0, the directory was created successfully
printf("Directory created successfully\n");
} else {
// If the return value is not 0, directory creation failed
printf("Directory creation failed\n");
exit(1);
}
return 0;
}
if (!created) {
: Checks if the mkdir
function returned 0, indicating success.printf("Directory created successfully\n");
: Prints a success message if the directory was created.} else {
: Executes if mkdir
did not return 0, indicating failure.printf("Directory creation failed\n");
: Prints a failure message.exit(1);
: Exits the program with a non-zero status indicating an error.When the program is executed, the output will be one of the following based on the success or failure of the directory creation:
Success:
Directory created successfully
Failure:
Directory creation failed