Errno Explorer

Errno Explorer

Hard C Error Handling 34 views
Explanation Complexity

Problem Statement

Make system calls (write file operations) fail deliberately. Print the value of errno and the message from strerror() on each failure. Understand the pattern.

Input Format

A program that performs file system calls (like file open/write) in situations where failure is guaranteed.

Output Format

For each failure:

• The numeric value of errno

• The corresponding error message from strerror(errno)

Example

Attempt to open a file in a non-existent directory.
errno = 2
Error message = No such file or directory

Constraints

• Program runs in C

• Uses system calls that set errno on failure

Concept Explanation

When a system call fails in C, it sets a global variable errno to indicate what went wrong.
The function strerror(errno) converts this error number into a human-readable message.

Step-by-Step Explanation

1.Intentionally perform a file operation that will fail
(example: opening a file from a directory that does not exist).

2.Check the return value of the system call.

3.If the return value indicates failure:

•Read the value of errno.

4.Print the numeric value of errno.

5.Pass errno to strerror() to get the descriptive error message.

6.Print the error message returned by strerror().

7.Repeat with different failing operations to observe different errno values.

Concept Explanation

When a system call fails in C, it sets a global variable errno to indicate what went wrong.
The function strerror(errno) converts this error number into a human-readable message.

Step-by-Step Explanation

1.Intentionally perform a file operation that will fail
(example: opening a file from a directory that does not exist).

2.Check the return value of the system call.

3.If the return value indicates failure:

•Read the value of errno.

4.Print the numeric value of errno.

5.Pass errno to strerror() to get the descriptive error message.

6.Print the error message returned by strerror().

7.Repeat with different failing operations to observe different errno values.

Input / Output Format

Input Format
A program that performs file system calls (like file open/write) in situations where failure is guaranteed.
Output Format
For each failure:

• The numeric value of errno

• The corresponding error message from strerror(errno)
Constraints
• Program runs in C

• Uses system calls that set errno on failure

Examples

Input:
Attempt to open a file in a non-existent directory.
Output:
errno = 2 Error message = No such file or directory

Example Solution (Public)

C
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

void print_error(const char *msg)
{
    printf("%sn", msg);
    printf("  errno = %dn", errno);
    printf("  strerror = %snn", strerror(errno));
}

int main(void)
{
    int fd;
    ssize_t bytes;

    /* 1. open() failure: file does not exist */
    fd = open("this_file_does_not_exist.txt", O_RDONLY);
    if (fd == -1) {
        print_error("open() failed (nonexistent file)");
    }

    /* 2. write() failure: invalid file descriptor */
    bytes = write(-1, "hello", 5);
    if (bytes == -1) {
        print_error("write() failed (invalid file descriptor)");
    }

    /* 3. open() failure: permission denied (likely) */
    fd = open("/root/secret.txt", O_WRONLY);
    if (fd == -1) {
        print_error("open() failed (permission denied)");
    }

    /* 4. write() failure: writing to read-only file */
    fd = open("readonly.txt", O_RDONLY | O_CREAT, 0444);
    if (fd != -1) {
        bytes = write(fd, "test", 4);
        if (bytes == -1) {
            print_error("write() failed (read-only file)");
        }
        close(fd);
    }

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

void print_error(const char *msg)
{
    printf("%sn", msg);
    printf("  errno = %dn", errno);
    printf("  strerror = %snn", strerror(errno));
}

int main(void)
{
    int fd;
    ssize_t bytes;

    /* 1. open() failure: file does not exist */
    fd = open("this_file_does_not_exist.txt", O_RDONLY);
    if (fd == -1) {
        print_error("open() failed (nonexistent file)");
    }

    /* 2. write() failure: invalid file descriptor */
    bytes = write(-1, "hello", 5);
    if (bytes == -1) {
        print_error("write() failed (invalid file descriptor)");
    }

    /* 3. open() failure: permission denied (likely) */
    fd = open("/root/secret.txt", O_WRONLY);
    if (fd == -1) {
        print_error("open() failed (permission denied)");
    }

    /* 4. write() failure: writing to read-only file */
    fd = open("readonly.txt", O_RDONLY | O_CREAT, 0444);
    if (fd != -1) {
        bytes = write(fd, "test", 4);
        if (bytes == -1) {
            print_error("write() failed (read-only file)");
        }
        close(fd);
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.