Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- void normalize_path(char *buf) {
- char *wr = buf + 1;
- char *ch = buf + 1;
- while (*ch != '\0') {
- printf("%c", *ch);
- if (*ch == '.' && ch[1] == '/') {
- ch += 2;
- continue;
- }
- if (*ch == '.' && ch[1] == '.' && ch[2] == '/') {
- if (wr > buf + 1) {
- wr -= 2;
- }
- while (wr > buf + 1 && *wr != '/') {
- wr--;
- }
- wr++;
- ch += 3;
- continue;
- }
- while (*ch != '\0' && *ch != '/') {
- *wr = *ch;
- wr++;
- ch++;
- }
- if (ch[1] != '\0') {
- *wr = *ch;
- wr++;
- ch++;
- } else {
- ch++;
- }
- }
- *wr = *ch;
- printf("%s\n", buf);
- }
- int main(void) {
- char *buf = "/a/b/c/";
- normalize_path(buf);
- buf[6] = 0;
- printf("%s\n", buf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement