Advertisement
Guest User

Untitled

a guest
May 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define ALPHABET_SIZE (27)
  6.  
  7. char *convertToAbsolute(char currentFile[], char currentDirectory[])
  8. {
  9. int i;
  10. while (1)
  11. {
  12. int check = 0;
  13. while (currentFile[0] == '.' && currentFile[1] != '.')
  14. {
  15. currentFile = currentFile + 2;
  16. check = 1;
  17. }
  18. while (currentFile[0] == '.' && currentFile[1] == '.')
  19. {
  20. char *aux = strrchr(currentDirectory, '/');
  21. char *auxDir = malloc(sizeof(char) * strlen(currentDirectory) + 1 - sizeof(char) * strlen(aux));
  22. for (i = 0; i < strlen(currentDirectory) - strlen(aux); i++)
  23. {
  24. auxDir[i] = currentDirectory[i];
  25. }
  26. auxDir[i] = '\0';
  27. currentFile = currentFile + 3;
  28. currentDirectory = auxDir;
  29. check = 1;
  30. }
  31. if (check == 0)
  32. {
  33. break;
  34. }
  35. }
  36. char *auxFile = malloc(sizeof(char) * strlen(currentFile) + sizeof(char) * strlen(currentDirectory) + 1);
  37. if (strlen(currentDirectory) != 1)
  38. {
  39. strcpy(auxFile, currentDirectory);
  40. strcat(auxFile, "/");
  41. }
  42. else
  43. {
  44. strcpy(auxFile, "/");
  45. }
  46. strcat(auxFile, currentFile);
  47. currentFile = auxFile;
  48. return currentFile;
  49. }
  50.  
  51. int convertToIndex(int currentLetter)
  52. {
  53. if (currentLetter - (int)'/' == 0)
  54. {
  55. return 0;
  56. }
  57. return currentLetter - (int)'a' + 1;
  58. }
  59.  
  60. typedef struct Trie
  61. {
  62. struct Trie *children[123];
  63. int wordEnd;
  64. }TrieNode;
  65.  
  66. TrieNode* createNode()
  67. {
  68. TrieNode *pNode = NULL;
  69.  
  70. pNode = malloc(sizeof(TrieNode));
  71.  
  72. if (pNode != NULL)
  73. {
  74. int i;
  75. pNode->wordEnd = 0;
  76. for (i = 0; i < 123; i++)
  77. {
  78. pNode->children[i] = NULL;
  79. }
  80. }
  81. else
  82. {
  83. exit(-1);
  84. }
  85.  
  86. return pNode;
  87. }
  88.  
  89. void insert(TrieNode *root, const char file[])
  90. {
  91. int level;
  92. int length = strlen(file);
  93. int index;
  94.  
  95. TrieNode *pCrawl = root;
  96.  
  97. for (level = 0; level < length; level++)
  98. {
  99. index = (int)(file[level]);
  100. if (pCrawl->children[index] == NULL)
  101. {
  102. pCrawl->children[index] = createNode();
  103. }
  104. pCrawl = pCrawl->children[index];
  105. }
  106. pCrawl->wordEnd = 1;
  107. }
  108.  
  109. int search(TrieNode *root, const char file[])
  110. {
  111. int level;
  112. int length = strlen(file);
  113. int index;
  114. TrieNode *pCrawl = root;
  115.  
  116. for (level = 0; level < length; level++)
  117. {
  118. index = (int)(file[level]);
  119.  
  120. if (pCrawl->children[index] == NULL)
  121. {
  122. return 0;
  123. }
  124. pCrawl = pCrawl->children[index];
  125. }
  126. if (pCrawl != NULL && pCrawl->wordEnd == 1)
  127. {
  128. return 1;
  129. }
  130. return 0;
  131. }
  132.  
  133. TrieNode *find(TrieNode *root, const char file[])
  134. {
  135. int level;
  136. int length = strlen(file);
  137. int index;
  138. TrieNode *pCrawl = root;
  139.  
  140. for (level = 0; level < length; level++)
  141. {
  142. index = (int)(file[level]);
  143.  
  144. if (pCrawl->children[index] == NULL)
  145. {
  146. return NULL;
  147. }
  148. pCrawl = pCrawl->children[index];
  149. }
  150. if (pCrawl != NULL && pCrawl->wordEnd == 1)
  151. {
  152. return pCrawl;
  153. }
  154. return NULL;
  155. }
  156.  
  157. void ls(TrieNode* root, char str[], int level, int type)
  158. {
  159. if (root->wordEnd == 1)
  160. {
  161. str[level] = '\0';
  162. if (str[0] != '\0')
  163. {
  164. char *p;
  165. char aux[100];
  166. strcpy(aux, str);
  167. aux[strlen(aux) - 1] = '\0';
  168. p = strchr(aux, '/');
  169. if (p == NULL)
  170. {
  171. if (type == 0)
  172. {
  173. printf("%s\n", aux);
  174. }
  175. else
  176. {
  177. printf("%s\n", str);
  178. }
  179. }
  180. }
  181. }
  182. int i;
  183. for (i = 0; i < 123; i++)
  184. {
  185. if (root->children[i])
  186. {
  187. str[level] = i;
  188. ls(root->children[i], str, level + 1, type);
  189. }
  190. }
  191. }
  192.  
  193. int isEmpty(TrieNode* root)
  194. {
  195. for (int i = 0; i < 123; i++)
  196. {
  197. if (root->children[i] != NULL)
  198. {
  199. return 0;
  200. }
  201. }
  202. return 1;
  203. }
  204.  
  205. TrieNode* rm(TrieNode* root, char* file, int depth)
  206. {
  207. if (root == NULL)
  208. {
  209. return NULL;
  210. }
  211.  
  212. if (depth == strlen(file))
  213. {
  214. if (root->wordEnd == 1)
  215. {
  216. root->wordEnd = 0;
  217. }
  218. if (isEmpty(root))
  219. {
  220. free(root);
  221. root = NULL;
  222. }
  223. return root;
  224. }
  225. int index = (int)(file[depth]);
  226. root->children[index] = rm(root->children[index], file, depth + 1);
  227. if (isEmpty(root) && root->wordEnd == 0)
  228. {
  229. free(root);
  230. root = NULL;
  231. }
  232. return root;
  233. }
  234.  
  235. int main()
  236. {
  237. int i;
  238. TrieNode *root = createNode();
  239. char *currentDirectory = malloc(1000);
  240. strcpy(currentDirectory, "/");
  241. insert(root, "/");
  242. char currentFile[100];
  243. char a[100], b[100], c[100], *p, *p2;
  244. insert(root, "/");
  245. while (fgets(a, sizeof(a), stdin) != NULL)
  246. {
  247. strcpy(b, a);
  248. p = strtok(a, " \n");
  249. if (strcmp(p, "pwd") == 0)
  250. {
  251. printf("%s\n", currentDirectory);
  252. p = strtok(NULL, " \n");
  253. }
  254. else if (strcmp(p, "mkdir") == 0)
  255. {
  256. int i = 0;
  257. while (p != NULL)
  258. {
  259. i++;
  260. p = strtok(NULL, " \n");
  261. if (p != NULL)
  262. {
  263. if (p[0] != '/' && p[0] == '.')
  264. {
  265. p = convertToAbsolute(p, currentDirectory);
  266. }
  267. else
  268. {
  269. strcpy(b, "./");
  270. strcat(b, p);
  271. p = b;
  272. p = convertToAbsolute(p, currentDirectory);
  273. }
  274. p = strcat(p, "/");
  275. insert(root, p);
  276. }
  277. }
  278. if (i < 2)
  279. {
  280. fprintf(stderr, "mkdir: missing operand\n");
  281. }
  282. }
  283. else if (strcmp(p, "touch") == 0)
  284. {
  285. int i = 0;
  286. while (p != NULL)
  287. {
  288. i++;
  289. p = strtok(NULL, " \n");
  290. if (p != NULL)
  291. {
  292. p = convertToAbsolute(p, currentDirectory);
  293. strcat(p, "*");
  294. insert(root, p);
  295. }
  296. }
  297. if (i < 2)
  298. {
  299. fprintf(stderr, "mkdir: missing operand\n");
  300. }
  301. }
  302. else if (strcmp(p, "ls") == 0)
  303. {
  304. if (strlen(currentDirectory) != 1)
  305. {
  306. currentDirectory = strcat(currentDirectory, "/");
  307. }
  308. p = strtok(NULL, " \n");
  309. if (p == NULL)
  310. {
  311. p = find(root, currentDirectory);
  312. ls(p, b, 0, 0);
  313. currentDirectory[strlen(currentDirectory) - 1] = '\0';
  314. continue;
  315. }
  316. else if (strcmp(p, "-F") == 0)
  317. {
  318. p = strtok(NULL, " \n");
  319. if (p == NULL)
  320. {
  321. p = find(root, currentDirectory);
  322. ls(p, b, 0, 1);
  323. }
  324. else
  325. {
  326. if (p[0] != '/' && p[0] == '.')
  327. {
  328. p = convertToAbsolute(p, currentDirectory);
  329. }
  330. else
  331. {
  332. strcpy(b, "./");
  333. strcat(b, p);
  334. p = b;
  335. p = convertToAbsolute(p, currentDirectory);
  336. }
  337. p = strcat(p, "/");
  338. p = find(root, p);
  339. if (p == NULL)
  340. {
  341. fprintf(stderr, "PATH: No such file or directory\n");
  342. currentDirectory[strlen(currentDirectory) - 1] = '\0';
  343. break;
  344. }
  345. else
  346. {
  347. p2 = strtok(NULL, " \n");
  348. if (p2 != NULL)
  349. {
  350. fprintf(stderr, "ls: too many arguments\n");
  351. currentDirectory[strlen(currentDirectory) - 1] = '\0';
  352. break;
  353. }
  354. ls(p, b, 0, 1);
  355. }
  356. }
  357. }
  358. else
  359. {
  360. if (p[0] != '/' && p[0] == '.')
  361. {
  362. p = convertToAbsolute(p, currentDirectory);
  363. }
  364. else
  365. {
  366. strcpy(b, "./");
  367. strcat(b, p);
  368. p = b;
  369. p = convertToAbsolute(p, currentDirectory);
  370. }
  371. if (strlen(p) != 1)
  372. {
  373. p = strcat(p, "/");
  374. }
  375. p = find(root, p);
  376. if (p == NULL)
  377. {
  378. fprintf(stderr, "PATH: No such file or directory\n");
  379. currentDirectory[strlen(currentDirectory) - 1] = '\0';
  380. break;
  381. }
  382. else
  383. {
  384. p2 = strtok(NULL, " \n");
  385. if (p2 != NULL)
  386. {
  387. fprintf(stderr, "ls: too many arguments\n");
  388. currentDirectory[strlen(currentDirectory) - 1] = '\0';
  389. break;
  390. }
  391. ls(p, b, 0, 0);
  392. }
  393. }
  394. currentDirectory[strlen(currentDirectory) - 1] = '\0';
  395. }
  396. else if (strcmp(p, "cp") == 0)
  397. {
  398. p = strtok(NULL, " \n");
  399. p2 = strrchr(b, ' ');
  400. if (p2 == NULL)
  401. {
  402. fprintf(stderr, "cp: missing operand\n");
  403. break;
  404. }
  405. p2 = p2 + 1;
  406. p2[strlen(p2) - 1] = '\0';
  407. if (strcmp(p, p2) == 0)
  408. {
  409. fprintf(stderr, "cp: missing operand\n");
  410. break;
  411. }
  412. char c[100];
  413. strcpy(c, p2);
  414. if (p2[0] != '/' && p2[0] == '.')
  415. {
  416. p2 = convertToAbsolute(p2, currentDirectory);
  417. }
  418. else
  419. {
  420. strcpy(b, "./");
  421. strcat(b, p2);
  422. p2 = b;
  423. p2 = convertToAbsolute(p2, currentDirectory);
  424. }
  425. p2 = strcat(p2, "/");
  426. if (search(root, p2) == 0)
  427. {
  428. fprintf(stderr, "PATH: No such file or directory\n");
  429. break;
  430. }
  431. int i = 0;
  432. while (strcmp(p, c) != 0)
  433. {
  434. i++;
  435. if (p[0] != '/' && p[0] == '.')
  436. {
  437. p = convertToAbsolute(p, currentDirectory);
  438. }
  439. else
  440. {
  441. strcpy(b, "./");
  442. strcat(b, p);
  443. p = b;
  444. p = convertToAbsolute(p, currentDirectory);
  445. }
  446. if (strlen(p) != 1)
  447. {
  448. p = strcat(p, "*");
  449. }
  450. if (search(root, p) == 0)
  451. {
  452. fprintf(stderr, "PATH: No such file or directory\n");
  453. }
  454. else
  455. {
  456. char *p3 = strrchr(p, '/');
  457. p = p + 1;
  458. strcat(p2, p);
  459. insert(root, p2);
  460. }
  461. p = strtok(NULL, " \n");
  462. }
  463. if (i < 1)
  464. {
  465. fprintf(stderr, "cp: missing operand\n");
  466. }
  467. }
  468. else if (strcmp(p, "cd") == 0)
  469. {
  470. p = strtok(NULL, " \n");
  471. if (p == NULL)
  472. {
  473. currentDirectory = "/";
  474. break;
  475. }
  476. else
  477. {
  478. if (p[0] != '/' && p[0] == '.')
  479. {
  480. p = convertToAbsolute(p, currentDirectory);
  481. }
  482. else
  483. {
  484. strcpy(b, "./");
  485. strcat(b, p);
  486. p = b;
  487. p = convertToAbsolute(p, currentDirectory);
  488. }
  489. if (strlen(p) != 1)
  490. {
  491. int j = strlen(p) + 1;
  492. p[strlen(p)] = '/';
  493. p[j] = '\0';
  494. }
  495. p2 = strtok(NULL, " \n");
  496. if (p2 != NULL)
  497. {
  498. fprintf(stderr, "cd: too many arguments\n");
  499. break;
  500. }
  501. else
  502. {
  503. if (search(root, p) == 0)
  504. {
  505. fprintf(stderr, "PATH: No such file or directory\n");
  506. }
  507. else
  508. {
  509. p[strlen(p) - 1] = '\0';
  510. strcpy(currentDirectory, p);
  511. }
  512. }
  513. }
  514. }
  515. else if (strcmp(p, "rmdir") == 0)
  516. {
  517. p = strtok(NULL, " \n");
  518. if (p == NULL)
  519. {
  520. fprintf(stderr, "rmdir: missing operand\n");
  521. break;
  522. }
  523. while (p != NULL)
  524. {
  525. if (p[0] != '/' && p[0] == '.')
  526. {
  527. p = convertToAbsolute(p, currentDirectory);
  528. }
  529. else
  530. {
  531. strcpy(b, "./");
  532. strcat(b, p);
  533. p = b;
  534. p = convertToAbsolute(p, currentDirectory);
  535. }
  536. if (strlen(p) != 1)
  537. {
  538. p = strcat(p, "/");
  539. }
  540. if (search(root, p) == 0)
  541. {
  542. fprintf(stderr, "PATH: No such file or directory\n");
  543. }
  544. else
  545. {
  546. if (isEmpty(find(root, p)) == 1)
  547. {
  548. root = rm(root, p, 0);
  549. }
  550. else
  551. {
  552. fprintf(stderr, "PATH: Directory not empty\n");
  553. }
  554. }
  555. p = strtok(NULL, " \n");
  556. }
  557. }
  558. else if (strcmp(p, "rm") == 0)
  559. {
  560. p = strtok(NULL, " \n");
  561. if (p == NULL)
  562. {
  563. fprintf(stderr, "rm: missing operand\n");
  564. break;
  565. }
  566. while (p != NULL)
  567. {
  568. if (p[0] != '/' && p[0] == '.')
  569. {
  570. p = convertToAbsolute(p, currentDirectory);
  571. }
  572. else
  573. {
  574. strcpy(b, "./");
  575. strcat(b, p);
  576. p = b;
  577. p = convertToAbsolute(p, currentDirectory);
  578. }
  579. if (strlen(p) != 1)
  580. {
  581. p = strcat(p, "/");
  582. }
  583. if (search(root, p) == 0)
  584. {
  585. fprintf(stderr, "PATH: No such file or directory\n");
  586. }
  587. else
  588. {
  589. root = rm(root, p, 0);
  590. }
  591. p = strtok(NULL, " \n");
  592. }
  593. }
  594. else if (strcmp(p, "mv") == 0)
  595. {
  596. p = strtok(NULL, " \n");
  597. p2 = strrchr(b, ' ');
  598. if (p2 == NULL)
  599. {
  600. fprintf(stderr, "mv: missing operand\n");
  601. break;
  602. }
  603. p2 = p2 + 1;
  604. p2[strlen(p2) - 1] = '\0';
  605. if (strcmp(p, p2) == 0)
  606. {
  607. fprintf(stderr, "mv: missing operand\n");
  608. break;
  609. }
  610. char c[100];
  611. strcpy(c, p2);
  612. if (p2[0] != '/' && p2[0] == '.')
  613. {
  614. p2 = convertToAbsolute(p2, currentDirectory);
  615. }
  616. else
  617. {
  618. strcpy(b, "./");
  619. strcat(b, p2);
  620. p2 = b;
  621. p2 = convertToAbsolute(p2, currentDirectory);
  622. }
  623. p2 = strcat(p2, "/");
  624. if (search(root, p2) == 0)
  625. {
  626. fprintf(stderr, "PATH: No such file or directory\n");
  627. break;
  628. }
  629. int i = 0;
  630. while (strcmp(p, c) != 0)
  631. {
  632. i++;
  633. if (p[0] != '/' && p[0] == '.')
  634. {
  635. p = convertToAbsolute(p, currentDirectory);
  636. }
  637. else
  638. {
  639. strcpy(b, "./");
  640. strcat(b, p);
  641. p = b;
  642. p = convertToAbsolute(p, currentDirectory);
  643. }
  644. if (strlen(p) != 1)
  645. {
  646. p = strcat(p, "*");
  647. }
  648. if (search(root, p) == 0)
  649. {
  650. fprintf(stderr, "PATH: No such file or directory\n");
  651. }
  652. else
  653. {
  654. root = rm(root, p, 0);
  655. char *p3 = strrchr(p, '/');
  656. p = p + 1;
  657. strcat(p2, p);
  658. insert(root, p2);
  659. }
  660. p = strtok(NULL, " \n");
  661. }
  662. if (i < 1)
  663. {
  664. fprintf(stderr, "mv: missing operand\n");
  665. }
  666. }
  667. else
  668. {
  669. fprintf(stderr, "%s: command not found", p);
  670. }
  671. }
  672.  
  673. return 0;
  674. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement