Advertisement
Guest User

Zem

a guest
Oct 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <locale.h>
  7.  
  8. const rmConst = 9999;
  9.  
  10. char nome[8][1023];
  11. char data[10][1023];
  12. char hora[8][1023];
  13. int pai[1023];
  14.  
  15. int posicaoAtual = 0;
  16. int codDir = 0;
  17.  
  18. void setup() {
  19. setlocale(NULL, "Portuguese");
  20.  
  21. nome[0][0] = '/';
  22. data[0][0] = 'XX';
  23. hora[0][0] = 'XX';
  24. pai[0] = 0;
  25.  
  26. printf("========================> Trabalho Especial S.O <========================\n\n");
  27. printf("Bem vindo ao trabalho especial da matéria de\nSistemas Operacionais, Fatec Americana - 2º Semestre 2019.\n");
  28. printf("\nPara saber as funcionalidades e os comandos disponíveis digite 'help'.\n");
  29. printf("\n========================> Trabalho Especial S.O <========================\n");
  30. }
  31.  
  32. void pwd() {
  33. for (int i = 0; i < posicaoAtual + 1; i++) {
  34. printf("%s", nome[i]);
  35.  
  36. if (i > 0)
  37. printf("/");
  38. }
  39. printf("\n");
  40. }
  41.  
  42. void mkdir(char nomeP[8]) {
  43.  
  44. for (int i = 1; i <= codDir; i++) {
  45. if (strcmp(nomeP, nome[codDir]) == 0 && pai[i] == posicaoAtual + 1) {
  46. printf("(!) Não é possível criar o diretório indicado. Causa: O diretório já existe.\n");
  47. return;
  48. }
  49. }
  50.  
  51. codDir++;
  52.  
  53. for (int i = 0; i < 8; i++) {
  54. nome[codDir][i] = nomeP[i];
  55. }
  56.  
  57. struct tm * tm;
  58. time_t t;
  59.  
  60. time(&t);
  61. tm = localtime(&t);
  62. strftime(data[codDir], 10, "%d-%m-%y", tm);
  63.  
  64. time(&t);
  65. tm = localtime(&t);
  66. strftime(hora[codDir], 100, "%H:%M:%S", tm);
  67.  
  68. pai[codDir] = codDir;
  69. }
  70.  
  71. void rmdir(char param[8]) {
  72. if (codDir == 0) {
  73. printf("(!) Diretório informado não encontrado.\n");
  74. return;
  75. }
  76.  
  77. bool found = false;
  78.  
  79. for (int i = 1; i <= codDir; i++) {
  80. if (strcmp(nome[i], param) == 0 && pai[i] == posicaoAtual + 1) {
  81. bool hasSon = false;
  82.  
  83. for (int j = 1; j <= codDir; j++) {
  84. if ((pai[i]+1) == pai[j]) {
  85. hasSon = true;
  86. }
  87. }
  88.  
  89. if (hasSon) {
  90. printf("(!) Não é possível remover o diretório indicado. Causa: O diretório não está vazio.\n");
  91. return;
  92. }
  93.  
  94. pai[i] = rmConst;
  95. found = true;
  96. }
  97. }
  98.  
  99. if (!found) {
  100. printf("(!) Diretório informado não encontrado.\n");
  101. }
  102. codDir--;
  103. }
  104.  
  105. void ls(bool lParam) {
  106. if (lParam) {
  107. for (int i = 1; i <= codDir; i++) {
  108. if (pai[i] != rmConst && pai[i] == posicaoAtual + 1) {
  109. printf("%s %s %s\n", nome[i], data[i], hora[i]);
  110. }
  111. }
  112. } else {
  113. for (int i = 1; i <= codDir; i++) {
  114. if (pai[i] != rmConst && pai[i] == posicaoAtual + 1) {
  115. printf("%s\n", nome[i]);
  116. }
  117. }
  118. }
  119. }
  120.  
  121. void cd(char param[8]) {
  122. if (strcmp(param, "..") == 0) {
  123. if (posicaoAtual > 0) {
  124. posicaoAtual--;
  125. }
  126. } else if (strcmp(param, "/") == 0) {
  127. posicaoAtual = 0;
  128. } else {
  129. if (codDir == 0) {
  130. printf("(!) Diretório informado não encontrado.\n");
  131. return;
  132. }
  133.  
  134. bool found = false;
  135.  
  136. for (int i = 1; i <= codDir; i++) {
  137. if (strcmp(nome[i], param) == 0 && pai[i] == posicaoAtual + 1) {
  138. if (pai[i] != rmConst) {
  139. posicaoAtual = pai[i];
  140. found = true;
  141. }
  142. }
  143. }
  144.  
  145. if (!found) {
  146. printf("(!) Diretório informado não encontrado.\n");
  147. }
  148. }
  149. }
  150.  
  151. void clear() {
  152. system("cls");
  153. }
  154.  
  155. void copyright () {
  156. printf("\n=-=-=-=-= Trabalho Especial S.O =-=-=-=-=\n");
  157. printf("\nTrabalho da matéria de S.0 I,\nda Fatec Americana - 2º Semestre 2019.");
  158. printf("\n\nEquipe: \n");
  159. printf("- André Nicola Nunes \n");
  160. printf("- Gustavo Rolim dos Santos \n");
  161. printf("- Maicon Gabriel de Sá \n");
  162. printf("- Vinicius Penachioni Tenomaro \n\n");
  163. printf("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
  164. }
  165.  
  166. void help() {
  167. printf("\n===============================> AJUDA <===============================\n\n");
  168. printf("pwd - Exibe o caminho de diretórios atual.\n");
  169. printf("mkdir - Cria um diretório. <Use: mkdir nome_do_diretório>\n");
  170. printf("ls - Lista os diretórios no caminho atual.\n");
  171. printf("rmdir - Remove um diretório. <Use: rmdir nome_do_diretório>\n");
  172. printf("ls -l - Lista os diretórios do caminho atual com informações de data e hora.\n");
  173. printf("cd - Vai para o diretório indicado. <Use: cd nome_do_diretório>.\n");
  174. printf("cd .. - Volta para o diretório anterior.\n");
  175. printf("cd / - Volta para o diretório raiz.\n");
  176. printf("clear - Limpa o prompt.\n");
  177. printf("copyright - Exibe os créditos dos desenvolvedores.\n");
  178. printf("date - Exibe data e hora.\n");
  179. printf("poweroff - Desliga a máquina.\n");
  180. printf("\n===========================> COMANDOS DO S.O <===========================\n");
  181. }
  182.  
  183. void date() {
  184. char data5[100];
  185.  
  186. struct tm * tm;
  187. time_t t;
  188.  
  189. time(&t);
  190. tm = localtime(&t);
  191. strftime(data5, 100, "%m/%d/%Y %H:%M:%S", tm);
  192.  
  193. printf("\n%s", data5);
  194. }
  195.  
  196. int main() {
  197. setup();
  198.  
  199. char line[80];
  200. char *command, *param;
  201.  
  202. bool off = false;
  203.  
  204. while(!off) {
  205. printf("\n$ ");
  206. gets(line);
  207. command = strtok(line, " ");
  208. param = strtok(NULL, "\0");
  209.  
  210. if (command == NULL) {
  211. printf("\nOpção inválida! Digite help para ver os comandos.");
  212. continue;
  213. }
  214.  
  215. if (strcmp(command, "pwd") == 0) {
  216. pwd();
  217. } else if (strcmp(command, "pwd") == 0) {
  218. pwd();
  219. } else if (strcmp(command, "mkdir") == 0 && param == NULL) {
  220. printf("\n(!) Use: mkdir <nome_do_diretório>");
  221. } else if (strcmp(command, "rmdir") == 0 && param == NULL) {
  222. printf("\n(!) Use: rmdir <nome_do_diretório>");
  223. } else if (strcmp(command, "mkdir") == 0 && strlen(param) > 8) {
  224. printf("(!) Nome muito grande. (Máximo 8 caracteres)\n");
  225. } else if (strcmp(command, "mkdir") == 0) {
  226. mkdir(param);
  227. } else if (strcmp(command, "ls") == 0 && param == NULL) {
  228. ls(false);
  229. } else if (strcmp(command, "ls") == 0 && param != NULL) {
  230. if (strcmp(param, "-l") == 0) {
  231. ls(true);
  232. }
  233. } else if (strcmp(command, "cd") == 0 && param == NULL) {
  234. printf("\n(!) Use: cd <nome_do_diretório>");
  235. } else if (strcmp(command, "cd") == 0 && param != NULL) {
  236. cd(param);
  237. } else if (strcmp(command, "rmdir") == 0) {
  238. rmdir(param);
  239. } else if (strcmp(command, "clear") == 0) {
  240. clear();
  241. } else if (strcmp(command, "copyright") == 0) {
  242. copyright();
  243. } else if (strcmp(command, "help") == 0) {
  244. help();
  245. } else if (strcmp(command, "date") == 0) {
  246. date();
  247. } else if (strcmp(command, "poweroff") == 0) {
  248. printf("\nPrograma finalizado com sucesso!\n");
  249. off = true;
  250. } else {
  251. printf("\nOpção inválida! Digite help para ver os comandos.");
  252. }
  253. }
  254.  
  255. return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement