#include <stdio.h>
#include <unistd.h>

int main() {
    char filename[256];

    // Prompt the user to enter the file name
    printf("Enter the name of the file to check: ");
    scanf("%s", filename);

    // Check if the file exists
    if (access(filename, F_OK) != -1) {
        printf("The file '%s' is present in the current directory.\n", filename);
    } else {
        printf("The file '%s' is NOT present in the current directory.\n", filename);
    }

    return 0;
}
----------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

void list_files_with_prefix(const char *prefix) {
    DIR *dir;
    struct dirent *entry;

    // Open the current directory
    dir = opendir(".");
    if (dir == NULL) {
        perror("opendir");
        return;
    }

    // Iterate through the directory entries
    while ((entry = readdir(dir)) != NULL) {
        // Check if the file name starts with the given prefix
        if (strncmp(entry->d_name, prefix, strlen(prefix)) == 0) {
            printf("%s\n", entry->d_name);
        }
    }

    // Close the directory
    closedir(dir);
}

int main(int argc, char *argv[]) {
    // Check for the correct number of arguments
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <prefix>\n", argv[0]);
        return EXIT_FAILURE;
    }

    // Call the function to list files
    list_files_with_prefix(argv[1]);

    return EXIT_SUCCESS;
}
------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main() {
    DIR *dir;
    struct dirent *entry;
    int file_count = 0;

    // Open the current directory
    dir = opendir(".");
    if (dir == NULL) {
        perror("opendir");
        return EXIT_FAILURE;
    }

    printf("Files in the current directory:\n");

    // Iterate through the directory entries
    while ((entry = readdir(dir)) != NULL) {
        // Check if the entry is a regular file (not a directory)
        if (entry->d_type == DT_REG) {
            printf("%s\n", entry->d_name);
            file_count++;
        }
    }

    // Close the directory
    closedir(dir);

    // Display the total number of files
    printf("\nTotal number of files: %d\n", file_count);

    return EXIT_SUCCESS;
}
------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#define MAX_FILES 100

// Structure to hold filename and its size
typedef struct {
    char *filename;
    off_t size; // Size of the file
} FileInfo;

// Comparison function for qsort
int compare(const void *a, const void *b) {
    FileInfo *fileA = (FileInfo *)a;
    FileInfo *fileB = (FileInfo *)b;
    return (fileA->size - fileB->size);
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <file1> <file2> ... <fileN>\n", argv[0]);
        return EXIT_FAILURE;
    }

    FileInfo files[MAX_FILES];
    int file_count = 0;

    // Iterate through the command-line arguments
    for (int i = 1; i < argc; i++) {
        struct stat file_stat;

        // Get the file status
        if (stat(argv[i], &file_stat) == 0) {
            files[file_count].filename = argv[i];
            files[file_count].size = file_stat.st_size;
            file_count++;
        } else {
            perror("stat");
        }
    }

    // Sort the files based on size
    qsort(files, file_count, sizeof(FileInfo), compare);

    // Display the sorted filenames with their sizes
    printf("Files sorted by size:\n");
    for (int i = 0; i < file_count; i++) {
        printf("%s: %ld bytes\n", files[i].filename, files[i].size);
    }

    return EXIT_SUCCESS;
}
-----------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>

void list_files_created_in_month(int target_month) {
    DIR *dir;
    struct dirent *entry;
    struct stat file_stat;
    char *month_names[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    // Open the current directory
    dir = opendir(".");
    if (dir == NULL) {
        perror("opendir");
        return;
    }

    printf("Files created in the month %s:\n", month_names[target_month - 1]);

    // Iterate through the directory entries
    while ((entry = readdir(dir)) != NULL) {
        // Get the file status
        if (stat(entry->d_name, &file_stat) == 0) {
            // Get the creation time
            struct tm *time_info = localtime(&file_stat.st_ctime);
            if (time_info->tm_mon + 1 == target_month) {  // tm_mon is 0-indexed
                printf("%s\n", entry->d_name);
            }
        } else {
            perror("stat");
        }
    }

    // Close the directory
    closedir(dir);
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <month (1-12)>\n", argv[0]);
        return EXIT_FAILURE;
    }

    int month = atoi(argv[1]);

    if (month < 1 || month > 12) {
        fprintf(stderr, "Please enter a valid month (1-12).\n");
        return EXIT_FAILURE;
    }

    // Call the function to list files
    list_files_created_in_month(month);

    return EXIT_SUCCESS;
}
-----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

void list_files_greater_than_n_bytes(off_t size_threshold) {
    DIR *dir;
    struct dirent *entry;
    struct stat file_stat;

    // Open the current directory
    dir = opendir(".");
    if (dir == NULL) {
        perror("opendir");
        return;
    }

    printf("Files greater than %ld bytes:\n", size_threshold);

    // Iterate through the directory entries
    while ((entry = readdir(dir)) != NULL) {
        // Get the file status
        if (stat(entry->d_name, &file_stat) == 0) {
            // Check if it's a regular file and its size is greater than the threshold
            if (S_ISREG(file_stat.st_mode) && file_stat.st_size > size_threshold) {
                printf("%s (%ld bytes)\n", entry->d_name, file_stat.st_size);
            }
        } else {
            perror("stat");
        }
    }

    // Close the directory
    closedir(dir);
}

int main() {
    off_t size_threshold;

    // Ask the user for the size threshold
    printf("Enter the size threshold in bytes: ");
    if (scanf("%ld", &size_threshold) != 1) {
        fprintf(stderr, "Invalid input. Please enter a valid number.\n");
        return EXIT_FAILURE;
    }

    // Call the function to list files
    list_files_greater_than_n_bytes(size_threshold);

    return EXIT_SUCCESS;
}
----------------------------------------
#include <stdio.h>

int main() {
    // Open a file for writing (this will overwrite the file if it already exists)
    FILE *file = freopen("output.txt", "w", stdout);
    
    // Check if the file was successfully opened
    if (file == NULL) {
        perror("Failed to redirect stdout");
        return 1;
    }

    // Output that will be written to the file
    printf("This output will be redirected to the file 'output.txt'.\n");
    printf("You can write multiple lines like this:\n");
    printf("Line 1: Hello, World!\n");
    printf("Line 2: Redirection is useful!\n");

    // Close the file
    fclose(file);

    // Optionally, restore stdout to the console (not necessary in this simple case)
    freopen("/dev/tty", "w", stdout);
    printf("Standard output has been restored to the console.\n");

    return 0;
}
---------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>

#define MAX_DIRS 100 // Maximum number of subdirectories

// Function to compare two strings for qsort
int compare(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

int main() {
    DIR *dir;
    struct dirent *entry;
    struct stat file_stat;
    char *subdirs[MAX_DIRS];
    int dir_count = 0;

    // Open the current directory
    dir = opendir(".");
    if (dir == NULL) {
        perror("opendir");
        return EXIT_FAILURE;
    }

    // Iterate through the directory entries
    while ((entry = readdir(dir)) != NULL) {
        // Get the file status
        if (stat(entry->d_name, &file_stat) == 0) {
            // Check if the entry is a directory and not "." or ".."
            if (S_ISDIR(file_stat.st_mode) && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
                // Store the directory name
                if (dir_count < MAX_DIRS) {
                    subdirs[dir_count] = strdup(entry->d_name); // Duplicate the string
                    dir_count++;
                } else {
                    fprintf(stderr, "Warning: Too many subdirectories (maximum is %d).\n", MAX_DIRS);
                    break;
                }
            }
        } else {
            perror("stat");
        }
    }

    // Close the directory
    closedir(dir);

    // Sort the subdirectory names
    qsort(subdirs, dir_count, sizeof(char *), compare);

    // Print the sorted subdirectory names
    printf("Subdirectories in alphabetical order:\n");
    for (int i = 0; i < dir_count; i++) {
        printf("%s\n", subdirs[i]);
        free(subdirs[i]); // Free the duplicated string
    }

    return EXIT_SUCCESS;
}
--------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

void identify_file_type(const char *filename) {
    struct stat file_stat;

    // Get the file status
    if (stat(filename, &file_stat) == -1) {
        perror("stat");
        return;
    }

    // Identify the type of file
    printf("File: %s\n", filename);
    if (S_ISREG(file_stat.st_mode)) {
        printf("Type: Regular File\n");
    } else if (S_ISDIR(file_stat.st_mode)) {
        printf("Type: Directory\n");
    } else if (S_ISCHR(file_stat.st_mode)) {
        printf("Type: Character Device\n");
    } else if (S_ISBLK(file_stat.st_mode)) {
        printf("Type: Block Device\n");
    } else if (S_ISFIFO(file_stat.st_mode)) {
        printf("Type: FIFO or Pipe\n");
    } else if (S_ISLNK(file_stat.st_mode)) {
        printf("Type: Symbolic Link\n");
    } else if (S_ISSOCK(file_stat.st_mode)) {
        printf("Type: Socket\n");
    } else {
        printf("Type: Unknown\n");
    }
}

int main(int argc, char *argv[]) {
    // Check if the user provided a filename
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        return EXIT_FAILURE;
    }

    // Identify the file type
    identify_file_type(argv[1]);

    return EXIT_SUCCESS;
}
----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main() {
    int fd[2]; // Array to hold the pipe file descriptors
    pid_t pid1, pid2; // Process IDs for the two child processes

    // Create a pipe
    if (pipe(fd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    // First child process for the 'ls' command
    if ((pid1 = fork()) == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid1 == 0) { // Child process
        // Close the read end of the pipe
        close(fd[0]);
        
        // Redirect stdout to the write end of the pipe
        dup2(fd[1], STDOUT_FILENO);
        
        // Close the write end after duplicating
        close(fd[1]);
        
        // Execute the 'ls' command
        execlp("ls", "ls", NULL);
        
        // If exec fails
        perror("execlp");
        exit(EXIT_FAILURE);
    }

    // Second child process for the 'grep' command
    if ((pid2 = fork()) == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid2 == 0) { // Child process
        // Close the write end of the pipe
        close(fd[1]);
        
        // Redirect stdin to the read end of the pipe
        dup2(fd[0], STDIN_FILENO);
        
        // Close the read end after duplicating
        close(fd[0]);
        
        // Execute the 'grep' command to filter results (for example, files starting with 'a')
        execlp("grep", "grep", "a*", NULL);
        
        // If exec fails
        perror("execlp");
        exit(EXIT_FAILURE);
    }

    // Parent process
    // Close both ends of the pipe
    close(fd[0]);
    close(fd[1]);

    // Wait for both child processes to finish
    wait(NULL);
    wait(NULL);

    return EXIT_SUCCESS;
}
--------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

// Signal handler for the child process
void signal_handler(int signo) {
    if (signo == SIGHUP) {
        printf("Child received SIGHUP signal\n");
    } else if (signo == SIGINT) {
        printf("Child received SIGINT signal\n");
    } else if (signo == SIGQUIT) {
        printf("My Papa has Killed me!!!\n");
        exit(0); // Terminate the child process
    }
}

int main() {
    pid_t pid;

    // Create a child process
    pid = fork();

    if (pid < 0) {
        perror("fork failed");
        exit(EXIT_FAILURE);
    }

    if (pid == 0) { // Child process
        // Set up signal handlers
        signal(SIGHUP, signal_handler);
        signal(SIGINT, signal_handler);
        signal(SIGQUIT, signal_handler);

        // Keep the child process running to catch signals
        while (1) {
            pause(); // Wait for signals
        }
    } else { // Parent process
        // Send signals to the child process
        for (int i = 0; i < 5; i++) {
            sleep(3); // Wait for 3 seconds
            if (i < 4) {
                // Send SIGHUP or SIGINT alternately
                if (i % 2 == 0) {
                    kill(pid, SIGHUP);
                } else {
                    kill(pid, SIGINT);
                }
            }
        }

        // Send SIGQUIT signal to terminate the child process
        kill(pid, SIGQUIT);

        // Optionally, wait for the child process to terminate
        wait(NULL);
    }

    return EXIT_SUCCESS;
}
--------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

void block_signals() {
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);  // Block Ctrl-C (SIGINT)
    sigaddset(&set, SIGQUIT); // Block Ctrl-\ (SIGQUIT)
    sigprocmask(SIG_BLOCK, &set, NULL); // Block the signals
}

int main() {
    int fd[2]; // Array to hold the pipe file descriptors
    pid_t pid1, pid2; // Process IDs for the two child processes

    // Create a pipe
    if (pipe(fd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    // Block signals
    block_signals();

    // First child process for the 'ls -l' command
    if ((pid1 = fork()) == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid1 == 0) { // Child process 1
        // Close the read end of the pipe
        close(fd[0]);
        
        // Redirect stdout to the write end of the pipe
        dup2(fd[1], STDOUT_FILENO);
        
        // Close the write end after duplicating
        close(fd[1]);
        
        // Execute the 'ls -l' command
        execlp("ls", "ls", "-l", NULL);
        
        // If exec fails
        perror("execlp");
        exit(EXIT_FAILURE);
    }

    // Second child process for the 'wc -l' command
    if ((pid2 = fork()) == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid2 == 0) { // Child process 2
        // Close the write end of the pipe
        close(fd[1]);
        
        // Redirect stdin to the read end of the pipe
        dup2(fd[0], STDIN_FILENO);
        
        // Close the read end after duplicating
        close(fd[0]);
        
        // Execute the 'wc -l' command
        execlp("wc", "wc", "-l", NULL);
        
        // If exec fails
        perror("execlp");
        exit(EXIT_FAILURE);
    }

    // Parent process
    // Close both ends of the pipe
    close(fd[0]);
    close(fd[1]);

    // Wait for both child processes to finish
    wait(NULL);
    wait(NULL);

    // Unblock signals after execution (if necessary)
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);
    sigaddset(&set, SIGQUIT);
    sigprocmask(SIG_UNBLOCK, &set, NULL);

    return EXIT_SUCCESS;
}
