Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void normalize_path(char *buf) {
  5. char *wr = buf + 1;
  6. char *ch = buf + 1;
  7. while (*ch != '\0') {
  8. printf("%c", *ch);
  9. if (*ch == '.' && ch[1] == '/') {
  10. ch += 2;
  11. continue;
  12. }
  13. if (*ch == '.' && ch[1] == '.' && ch[2] == '/') {
  14. if (wr > buf + 1) {
  15. wr -= 2;
  16. }
  17. while (wr > buf + 1 && *wr != '/') {
  18. wr--;
  19. }
  20. wr++;
  21. ch += 3;
  22. continue;
  23. }
  24. while (*ch != '\0' && *ch != '/') {
  25. *wr = *ch;
  26. wr++;
  27. ch++;
  28. }
  29. if (ch[1] != '\0') {
  30. *wr = *ch;
  31. wr++;
  32. ch++;
  33. } else {
  34. ch++;
  35. }
  36. }
  37. *wr = *ch;
  38. printf("%s\n", buf);
  39. }
  40.  
  41. int main(void) {
  42. char *buf = "/a/b/c/";
  43. normalize_path(buf);
  44. buf[6] = 0;
  45. printf("%s\n", buf);
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement