Advertisement
Guest User

Untitled

a guest
May 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. /**
  2. * SO, 2016
  3. * Lab #12
  4. *
  5. * Task 01 - mini.c
  6. * Implementing a minimal comand line file utility
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14.  
  15. #include "utils.h"
  16.  
  17. #define MAX_LINE_SIZE 256
  18.  
  19. const char *delim = " \t\n";
  20. char *prompt = "so-lab12";
  21.  
  22. #define TODO2
  23. //#define TODO3
  24. //#define TODO4
  25. //#define TODO5
  26. //#define TODO6
  27. //#define TODO7
  28.  
  29. int main(void)
  30. {
  31. char line[MAX_LINE_SIZE];
  32. char *cmd, *arg1, *arg2, *arg3;
  33. int ret; /* to be used for function calls return code */
  34.  
  35. while (1) {
  36. printf("<%s>", prompt);
  37. fflush(stdout);
  38.  
  39. memset(line, 0, MAX_LINE_SIZE);
  40.  
  41. if (fgets(line, sizeof(line), stdin) == NULL)
  42. exit(EXIT_SUCCESS);
  43.  
  44. cmd = strtok(line, delim);
  45. if (!cmd)
  46. continue;
  47. #ifdef DEBUG
  48. printf("Executing command: %s\n", cmd);
  49. #endif
  50. if (strncmp(cmd, "quit", 4) == 0)
  51. break;
  52. #ifdef TODO2
  53. /* TODO2: implement list <device_node>
  54. * Output: <c|b> <major>:<minor>
  55. * e.g: list /dev/zero
  56. * Output: /dev/zero: <c> 1:5
  57. */
  58.  
  59. if (strncmp(cmd, "list", 4) == 0) {
  60.  
  61. char type;
  62. struct stat statbuf;
  63. arg1 = strtok(NULL, delim); /* device_node name */
  64.  
  65. if (!arg1)
  66. continue;
  67.  
  68.  
  69. stat (arg1, &statbuf);
  70.  
  71. if(S_ISCHR(statbuf.st_mode)) {
  72. type = 'c';
  73. }
  74.  
  75. else
  76. if(S_ISBLK(statbuf.st_mode)) {
  77. type = 'b';
  78. }
  79.  
  80. printf("%s: <%c> %d:%d\n",
  81. arg1, type, major(statbuf.st_rdev), minor(statbuf.st_rdev));
  82. }
  83. #endif
  84.  
  85. #ifdef TODO3
  86. /* TODO3: implement mount source target fs_type
  87. * e.g: mount /dev/sda1 /mnt/disk2 ext3
  88. */
  89. if (strncmp(cmd, "mount", 5) == 0) {
  90. arg1 = strtok(NULL, delim); /* source */
  91. arg2 = strtok(NULL, delim); /* target */
  92. arg3 = strtok(NULL, delim);/* fs_type (e.g: ext2) */
  93. }
  94. if (strncmp(cmd, "umount", 6) == 0) {
  95. /* TODO3: implement umount */
  96. arg1 = strtok(NULL, delim); /* target */
  97. }
  98. #endif
  99.  
  100. #ifdef TODO4
  101. /* TODO4: implement symlink oldpath newpath
  102. * e.g: symlink a.txt b.txt
  103. */
  104. if (strncmp(cmd, "symlink", 7) == 0) {
  105. arg1 = strtok(NULL, delim); /* oldpath */
  106. arg2 = strtok(NULL, delim); /* newpath */
  107. }
  108. if (strncmp(cmd, "unlink", 6) == 0) {
  109. /* TODO4: implement unlink */
  110. arg1 = strtok(NULL, delim); /* pathname */
  111. }
  112. #endif
  113.  
  114. #ifdef TODO5
  115. /* TODO5: implement mkdir pathname mode
  116. * e.g: mkdir food 0777
  117. */
  118. if (strncmp(cmd, "mkdir", 5) == 0) {
  119. arg1 = strtok(NULL, delim); /* pathname */
  120. }
  121. if (strncmp(cmd, "rmdir", 5) == 0) {
  122. /* TODO5: implement rmdir pathname */
  123. arg1 = strtok(NULL, delim); /* pathname */
  124. }
  125. #endif
  126.  
  127. #ifdef TODO6
  128. /* TODO6: implement ls dirname
  129. * e.g: ls ..
  130. */
  131. if (strncmp(cmd, "ls", 2) == 0) {
  132. /* recursively print files starting with arg1 */
  133. arg1 = strtok(NULL, delim);
  134. }
  135. #endif
  136.  
  137. #ifdef TODO7
  138. if (strncmp(cmd, "chdir", 5) == 0) {
  139. /* TODO7: implement chdir <dir>
  140. * e.g: chdir bar
  141. */
  142. arg1 = strtok(NULL, delim); /* pathname */
  143. }
  144.  
  145. if (strncmp(cmd, "pwd", 3) == 0) {
  146. /* TODO7: implement pwdir
  147. * e.g: pwd
  148. */
  149. /* print workding directory */
  150. }
  151. #endif
  152. }
  153.  
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement