Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба_1_Док_Файл.docx
Скачиваний:
0
Добавлен:
14.12.2022
Размер:
52.76 Кб
Скачать
  1. Написать программу вывода на экран содержимого текущего и заданного первым параметром вызова программы каталогов.

#include <stdio.h>

#include <dirent.h>

 

int main(int argc, char *argv[])

{

    // Open the current directory

    DIR *dir = opendir(".");

    if (dir == NULL)

    {

        printf("Error: unable to open current directory.\n");

        return 1;

    }

 

    // Print the contents of the current directory

    struct dirent *entry;

    printf("Current directory:\n");

    while ((entry = readdir(dir)) != NULL)

    {

        printf("%s\n", entry->d_name);

    }

 

    // Close the current directory

    if (closedir(dir) != 0)

    {

        printf("Error: unable to close current directory.\n");

        return 1;

    }

 

    // Check if a directory name was provided as an argument

    if (argc < 2)

    {

        printf("\nError: please provide a directory name as an argument.\n");

        return 1;

    }

 

    // Open the specified directory

    dir = opendir(argv[1]);

    if (dir == NULL)

    {

        printf("\nError: unable to open specified directory.\n");

        return 1;

    }

 

    // Print the contents of the specified directory

    printf("\nSpecified directory:\n");

    while ((entry = readdir(dir)) != NULL)

    {

        printf("%s\n", entry->d_name);

    }

 

    // Close the specified directory

    if (closedir(dir) != 0)

    {

        printf("Error: unable to close specified directory.\n");

        return 1;

    }

 

    return 0;

}

  1. Индивидуальное задание

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <dirent.h>

#include <sys/stat.h>

// Function to compare two files based on their size

int compare_by_size(const void *a, const void *b) {

const struct stat *fa = a;

const struct stat *fb = b;

return (fa->st_size - fb->st_size);

}

// Function to compare two files based on their name

int compare_by_name(const void *a, const void *b) {

const struct dirent *da = a;

const struct dirent *db = b;

return strcmp(da->d_name, db->d_name);

}

// Function to sort files in the specified directory and all its subdirectories

void sort_files(const char *dirname, int criterion, const char *output_dirname) {

DIR *dir = opendir(dirname);

if (dir == NULL) {

perror("Error opening directory");

return;

}

// Create the output directory if it doesn't exist

mkdir(output_dirname, 0777);

// Read the files in the current directory

struct dirent **files;

int num_files = scandir(dirname, &files, NULL, criterion == 1 ? compare_by_size : compare_by_name);

if (num_files < 0) {

perror("Error reading directory");

return;

}

// Iterate over the files and sort them according to the specified criterion

for (int i = 0; i < num_files; i++) {

// Skip the current and parent directories

if (strcmp(files[i]->d_name, ".") == 0 || strcmp(files[i]->d_name, "..") == 0) {

continue;

}

// Construct the full path of the file

char path[256];

snprintf(path, sizeof(path), "%s/%s", dirname, files[i]->d_name);

// Get the file stats

struct stat st;

stat(path, &st);

// If the file is a directory, sort the files in the subdirectory

if (S_ISDIR(st.st_mode)) {

sort_files(path, criterion, output_dirname);

} else {

// Copy the file to the output directory

char output_path[256];

snprintf(output_path, sizeof(output_path), "%s/%s", output_dirname, files[i]->d_name);

FILE *input_file = fopen(path, "r");

FILE *output_file = fopen(output_path, "w");

if (input_file == NULL || output_file == NULL) {

perror("Error copying file");

return;

}

char a;

do {

a = fgetc(input_file);

fputc(a, output_file);

} while (a != EOF);

// Close the input and output files

fclose(input_file);

fclose(output_file);

// Print the file info

printf("%s\n", output_path);

printf("Name: %s\n", files[i]->d_name);

printf("Size: %ld bytes\n", st.st_size);

}

}

// Free the memory allocated for the file names

for (int i = 0; i < num_files; i++) {

free(files[i]);

}

free(files);

// Close the directory

closedir(dir);

}

int main(int argc, char *argv[]) {

if (argc != 4) {

printf("Usage: sort_files <dirname> <criterion> <output_dirname>\n");

printf("<dirname> - the name of the directory to sort files in\n");

printf("<criterion> - the sorting criterion (1 - by size, 2 - by name)\n");

printf("<output_dirname> - the name of the directory to write the sorted files to\n");

return 1;

}

// Read the command line arguments

const char *dirname = argv[1];

int criterion = atoi(argv[2]);

const char *output_dirname = argv[3];

// Sort the files in the specified directory

sort_files(dirname, criterion, output_dirname);

return 0;

}