Advertisement
Vladislav8653

7.1

Dec 19th, 2023 (edited)
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <dirent.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/wait.h>
  10.  
  11. #define MAX_PATH_LENGTH 4096
  12. #define MAX_FILENAME_LENGTH 256
  13. #define MAX_CHILD_PROCESSES 10
  14.  
  15. typedef struct {
  16.     char filename[MAX_FILENAME_LENGTH];
  17.     off_t size;
  18. } FileInfo;
  19.  
  20. int compareBySize(const void *a, const void *b) {
  21.     return ((FileInfo *)a)->size - ((FileInfo *)b)->size;
  22. }
  23.  
  24. int compareByName(const void *a, const void *b) {
  25.     return strcmp(((FileInfo *)a)->filename, ((FileInfo *)b)->filename);
  26. }
  27.  
  28. void copyFile(const char *sourcePath, const char *destPath) {
  29.     int sourceFile = open(sourcePath, O_RDONLY);
  30.     int destFile = open(destPath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  31.  
  32.     char buffer[4096];
  33.     ssize_t bytesRead, bytesWritten;
  34.  
  35.     while ((bytesRead = read(sourceFile, buffer, sizeof(buffer))) > 0) {
  36.         bytesWritten = write(destFile, buffer, bytesRead);
  37.         if (bytesWritten != bytesRead) {
  38.             perror("Write error");
  39.             exit(EXIT_FAILURE);
  40.         }
  41.     }
  42.  
  43.     close(sourceFile);
  44.     close(destFile);
  45. }
  46.  
  47. void sortAndCopyFiles(const char *dirPath, int sortBy, const char *destPath, int maxProcesses) {
  48.     DIR *dir = opendir(dirPath);
  49.     if (dir == NULL) {
  50.         perror("Error opening directory");
  51.         exit(EXIT_FAILURE);
  52.     }
  53.  
  54.     struct dirent *entry;
  55.     struct stat fileInfo;
  56.  
  57.     int numProcesses = 0;
  58.     int numFiles = 0;
  59.     FileInfo *files = malloc(MAX_CHILD_PROCESSES * sizeof(FileInfo));
  60.  
  61.     while ((entry = readdir(dir)) != NULL) {
  62.         if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  63.             continue;
  64.  
  65.         char filePath[MAX_PATH_LENGTH];
  66.         snprintf(filePath, sizeof(filePath), "%s/%s", dirPath, entry->d_name);
  67.  
  68.         if (lstat(filePath, &fileInfo) == -1) {
  69.             perror("Error getting file info");
  70.             exit(EXIT_FAILURE);
  71.         }
  72.  
  73.         if (S_ISREG(fileInfo.st_mode)) {
  74.             strncpy(files[numFiles].filename, entry->d_name, MAX_FILENAME_LENGTH - 1);
  75.             files[numFiles].size = fileInfo.st_size;
  76.             numFiles++;
  77.  
  78.             if (numFiles >= MAX_CHILD_PROCESSES) {
  79.                 qsort(files, numFiles, sizeof(FileInfo), (sortBy == 1) ? compareBySize : compareByName);
  80.                 for (int i = 0; i < numFiles; i++) {
  81.                     pid_t childPid = fork();
  82.                     if (childPid == -1) {
  83.                         perror("Error forking process");
  84.                         exit(EXIT_FAILURE);
  85.                     }
  86.  
  87.                     if (childPid == 0) {  // Child process
  88.                         printf("PID: %d, Path: %s, Filename: %s, Size: %ld bytes\n", getpid(), filePath, files[i].filename, files[i].size);
  89.  
  90.                         char destFilePath[MAX_PATH_LENGTH];
  91.                         snprintf(destFilePath, sizeof(destFilePath), "%s/%s", destPath, files[i].filename);
  92.  
  93.                         copyFile(filePath, destFilePath);
  94.                         exit(EXIT_SUCCESS);
  95.                     } else {  // Parent process
  96.                         numProcesses++;
  97.                         if (numProcesses >= maxProcesses) {
  98.                             wait(NULL);  // Wait for any child process to finish
  99.                             numProcesses--;
  100.                         }
  101.                     }
  102.                 }
  103.  
  104.                 numFiles = 0;
  105.             }
  106.         }
  107.     }
  108.  
  109.     closedir(dir);
  110.     free(files);
  111. }
  112.  
  113. int main(int argc, char *argv[]) {
  114.     if (argc != 4) {
  115.         fprintf(stderr, "Usage: %s <source_directory> <sort_criteria> <destination_directory>\n", argv[0]);
  116.         exit(EXIT_FAILURE);
  117.     }
  118.  
  119.     const char *dirPath = argv[1];
  120.     int sortBy = atoi(argv[2]);
  121.     const char *destPath = argv[3];
  122.  
  123.     if (sortBy != 1 && sortBy != 2) {
  124.         fprintf(stderr, "Invalid sort criteria. Use 1 for sorting by size or 2 for sorting by name.\n");
  125.         exit(EXIT_FAILURE);
  126.     }
  127.  
  128.     int maxProcesses;
  129.     printf("Enter the maximum number of processes: ");
  130.     scanf("%d", &maxProcesses);
  131.  
  132.     sortAndCopyFiles(dirPath, sortBy, destPath, maxProcesses);
  133.     printf("Ready.");
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement