You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

196 lines
4.5 KiB

  1. #include <memory.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. struct Directory;
  5. struct File;
  6. struct File {
  7. int id;
  8. unsigned long size;
  9. char data[2048];
  10. char name[64];
  11. struct Directory *directory; // The parent directory
  12. };
  13. struct Directory {
  14. int nf;
  15. int nd;
  16. char path[4096];
  17. struct File *files[256];
  18. struct Directory *directories[8];
  19. };
  20. typedef struct Directory Directory;
  21. typedef struct File File;
  22. // Operations on files
  23. void add_to_file(File *file, const char *content);
  24. void append_to_file(File *file, const char *content);
  25. void pwd_file(File *file);
  26. // Operations on directories
  27. void add_file(File *file, Directory *dir);
  28. void add_dir(Directory *dir1, Directory *dir2); // given to you
  29. // Helper functions
  30. void show_dir(Directory *dir);
  31. void show_file(File *file);
  32. void show_file_detailed(File *file);
  33. void init_dir(Directory *dir, char *name);
  34. void init_file(File *file, char *name);
  35. int cur_id = 0;
  36. int main() {
  37. char content1[] = "int printf(const char * format, ...);";
  38. char content2[] = "int main(){printf('Hello World');}";
  39. char content3[] = "//This is a comment in C language";
  40. char content4[] = "Bourne Again Shell!";
  41. Directory home, bin, root;
  42. init_dir(&root, "/");
  43. init_dir(&home, "home");
  44. init_dir(&bin, "bin");
  45. // Example: the path of the folder home is /home
  46. // Add subdirectories
  47. add_dir(&home, &root);
  48. add_dir(&bin, &root);
  49. File bash, ex31, ex32;
  50. init_file(&bash, "bash");
  51. init_file(&ex31, "ex3_1.c");
  52. init_file(&ex32, "ex3_2.c");
  53. //<WRITE YOUR CODE HERE>
  54. add_file(&bash, &bin);
  55. add_file(&ex31, &home);
  56. add_file(&ex32, &home);
  57. add_to_file(&bash, content4);
  58. add_to_file(&ex31, content1);
  59. add_to_file(&ex32, content3);
  60. append_to_file(&ex31, content2);
  61. show_dir(&root);
  62. show_file_detailed(&bash);
  63. show_file_detailed(&ex31);
  64. show_file_detailed(&ex32);
  65. pwd_file(&bash);
  66. pwd_file(&ex31);
  67. pwd_file(&ex32);
  68. return EXIT_SUCCESS;
  69. }
  70. // Helper functions
  71. // Displays the content of the Directory dir
  72. void show_dir(Directory *dir) {
  73. printf("\nDIRECTORY\n");
  74. printf(" path: %s\n", dir->path);
  75. printf(" files:\n");
  76. printf(" [ ");
  77. for (int i = 0; i < dir->nf; i++) {
  78. show_file(dir->files[i]);
  79. }
  80. printf("]\n");
  81. printf(" directories:\n");
  82. printf(" { ");
  83. for (int i = 0; i < dir->nd; i++) {
  84. show_dir(dir->directories[i]);
  85. }
  86. printf("}\n");
  87. }
  88. // Prints the name of the File file
  89. void show_file(File *file) { printf("%s ", file->name); }
  90. // Shows details of the File file
  91. void show_file_detailed(File *file) {
  92. printf("\nFILE\n");
  93. printf(" id: %d\n", file->id);
  94. printf(" name: %s\n", file->name);
  95. printf(" size: %lu\n", file->size);
  96. printf(" data:\n");
  97. printf(" [ %s ]\n", file->data);
  98. }
  99. // Implementation: Operations on files
  100. // Adds the content to the File file
  101. void add_to_file(File *file, const char *content) {
  102. if (file && content) {
  103. strcpy(file->data, content);
  104. file->size = strlen(file->data) + 1;
  105. }
  106. }
  107. // Appends the content to the File file
  108. void append_to_file(File *file, const char *content) {
  109. if (file && content) {
  110. int sz = strlen(file->data);
  111. strcpy(file->data + sz, content);
  112. file->size = strlen(file->data) + 1;
  113. }
  114. }
  115. // Prints the path of the File file
  116. void pwd_file(File *file) {
  117. if (file) {
  118. printf("%s/%s\n", file->directory->path, file->name);
  119. }
  120. }
  121. // Implementation: Operations on directories
  122. // Adds the File file to the Directory dir
  123. void add_file(File *file, Directory *dir) {
  124. if (file && dir) {
  125. dir->files[dir->nf] = file;
  126. dir->nf++;
  127. file->directory = dir;
  128. }
  129. }
  130. // Given to you
  131. // Adds the subdirectory dir1 to the directory dir2
  132. void add_dir(Directory *dir1, Directory *dir2) {
  133. if (dir1 && dir2) {
  134. dir2->directories[dir2->nd] = dir1;
  135. dir2->nd++;
  136. int sz1 = strlen(dir1->path), sz2 = strlen(dir2->path);
  137. if (dir2->path[sz2 - 1] == '/') {
  138. memmove(dir1->path + sz2, dir1->path, sz1 + 1);
  139. } else {
  140. memmove(dir1->path + sz2 + 1, dir1->path, sz1 + 1);
  141. *(dir1->path + sz2) = '/';
  142. }
  143. memcpy(dir1->path, dir2->path, sz2);
  144. }
  145. }
  146. void init_dir(Directory *dir, char *name) {
  147. dir->nd = dir->nf = 0;
  148. strcpy(dir->path, name);
  149. }
  150. void init_file(File *file, char *name) {
  151. file->data[0] = '\0';
  152. file->id = cur_id++;
  153. strcpy(file->name, name);
  154. file->size = 0;
  155. }