Advertisement
Delta_Sierra

Lab4

Feb 13th, 2025
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6.  
  7. void createFile(const char *filename) {
  8.     int fd = open(filename, O_CREAT | O_WRONLY, 0644);
  9.     if (fd == -1) {
  10.         perror("Error creating file");
  11.         return;
  12.     }
  13.     printf("File '%s' created successfully!\n", filename);
  14.     close(fd);
  15. }
  16.  
  17. void deleteFile(const char *filename) {
  18.     if (unlink(filename) == 0) {
  19.         printf("File '%s' deleted successfully!\n", filename);
  20.     } else {
  21.         perror("Error deleting file");
  22.     }
  23. }
  24.  
  25. void renameFile(const char *oldname, const char *newname) {
  26.     if (rename(oldname, newname) == 0) {
  27.         printf("File renamed from '%s' to '%s' successfully!\n", oldname, newname);
  28.     } else {
  29.         perror("Error renaming file");
  30.     }
  31. }
  32.  
  33. void changePermissions(const char *filename, mode_t mode) {
  34.     if (chmod(filename, mode) == 0) {
  35.         printf("Permissions of '%s' changed successfully!\n", filename);
  36.     } else {
  37.         perror("Error changing file permissions");
  38.     }
  39. }
  40.  
  41. int main() {
  42.     int choice;
  43.     char filename[100], newname[100];
  44.     mode_t mode;
  45.  
  46.     while (1) {
  47.         printf("\nFile Operations Menu:\n");
  48.         printf("1. Create File\n");
  49.         printf("2. Delete File\n");
  50.         printf("3. Rename File\n");
  51.         printf("4. Change File Permissions\n");
  52.         printf("5. Exit\n");
  53.         printf("Enter your choice: ");
  54.         scanf("%d", &choice);
  55.  
  56.         switch (choice) {
  57.             case 1:
  58.                 printf("Enter filename to create: ");
  59.                 scanf("%s", filename);
  60.                 createFile(filename);
  61.                 break;
  62.             case 2:
  63.                 printf("Enter filename to delete: ");
  64.                 scanf("%s", filename);
  65.                 deleteFile(filename);
  66.                 break;
  67.             case 3:
  68.                 printf("Enter current filename: ");
  69.                 scanf("%s", filename);
  70.                 printf("Enter new filename: ");
  71.                 scanf("%s", newname);
  72.                 renameFile(filename, newname);
  73.                 break;
  74.             case 4:
  75.                 printf("Enter filename to change permissions: ");
  76.                 scanf("%s", filename);
  77.                 printf("Enter new permissions (octal, e.g., 0644): ");
  78.                 scanf("%o", &mode);
  79.                 changePermissions(filename, mode);
  80.                 break;
  81.             case 5:
  82.                 printf("Exiting...\n");
  83.                 exit(0);
  84.             default:
  85.                 printf("Invalid choice! Please try again.\n");
  86.         }
  87.     }
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement