Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Directory Attribute Extraction and Half-Copy Image Operations with Parent-Child Processes

Tech Apr 15 10

Extracting File Attributes to a Text File

A program that reads all non-hidden files in a specified directory, extracts their permissions and last access times, and writes this information to file.txt.

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>

int main() {
    FILE *output_file = fopen("file.txt", "w");
    if (output_file == NULL) {
        perror("fopen");
        return -1;
    }
    
    char base_path[256] = "/home/gec/gec6818/temp/";
    char user_path[256] = {0};
    
    printf("Enter directory path (format: name/.../.../):\n");
    printf("Default base path is /home/gec/gec6818/temp/\n");
    scanf("%s", user_path);
    getchar();
    
    strcat(base_path, user_path);
    chdir(base_path);
    
    DIR *directory = opendir(base_path);
    if (directory == NULL) {
        perror("opendir");
        return -1;
    }
    
    struct dirent *entry;
    struct stat file_info;
    struct tm *time_info;
    int line_number = 1;
    
    fprintf(output_file, "%2d   %20s  %6s  %s\n", line_number, "Filename", "Mode", "Last Access Time");
    line_number++;
    
    while ((entry = readdir(directory)) != NULL) {
        if (entry->d_name[0] == '.') {
            continue;
        }
        
        if (stat(entry->d_name, &file_info) == -1) {
            perror("stat");
            continue;
        }
        
        time_info = localtime(&file_info.st_atime);
        fprintf(output_file, "%2d %20s  %o  %d-%02d-%02d %02d:%02d:%02d\n",
                line_number,
                entry->d_name,
                file_info.st_mode,
                time_info->tm_year + 1900,
                time_info->tm_mon + 1,
                time_info->tm_mday,
                time_info->tm_hour,
                time_info->tm_min,
                time_info->tm_sec);
        line_number++;
    }
    
    if (errno != 0) {
        perror("readdir");
    }
    
    closedir(directory);
    fclose(output_file);
    return 0;
}

Half-Copy Image File Using Parent-Child Processes

A program that splits an image file copy oepration betwean parent and child proecsses, with the parent copying the first half and the child copying the second half.

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
    pid_t process_id = fork();
    
    int source_file = open("/home/gec/Downloads/629e6b7c99237e335dd98f06a6cd51ab.jpeg", O_RDONLY);
    if (source_file < 0) {
        perror("open source");
        return -1;
    }
    
    off_t file_size = lseek(source_file, 0, SEEK_END);
    int odd_size_adjustment = (file_size % 2 == 0) ? 0 : 1;
    lseek(source_file, 0, SEEK_SET);
    
    int destination_file = open("copy.jpeg", O_WRONLY | O_CREAT | O_TRUNC, 0777);
    if (destination_file < 0) {
        perror("open destination");
        return -1;
    }
    
    char buffer[1];
    
    if (process_id > 0) {
        for (off_t i = 0; i < file_size / 2 + odd_size_adjustment; i++) {
            if (read(source_file, buffer, 1) < 0) {
                perror("parent read");
                break;
            }
            write(destination_file, buffer, 1);
        }
    }
    
    if (process_id == 0) {
        sleep(1);
        lseek(source_file, file_size / 2, SEEK_SET);
        lseek(destination_file, file_size / 2, SEEK_SET);
        
        for (off_t i = 0; i < file_size / 2 + odd_size_adjustment; i++) {
            if (read(source_file, buffer, 1) < 0) {
                perror("child read");
                break;
            }
            write(destination_file, buffer, 1);
        }
    }
    
    close(source_file);
    close(destination_file);
    return 0;
}

File comparision with diff shows identical content, and visual inspection confirms the copied image matches the original.

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.