Commit 666a74df 666a74df56d2ded3f8d7077d3b3b438a952041b5 by Sergey Poznyakoff

New function: mu_normalize_path() moved from imap4d/util.c

1 parent 71aa9d86
...@@ -442,3 +442,69 @@ mu_get_user_email (char *name) ...@@ -442,3 +442,69 @@ mu_get_user_email (char *name)
442 sprintf (email, "%s@%s", name, domainpart); 442 sprintf (email, "%s@%s", name, domainpart);
443 return email; 443 return email;
444 } 444 }
445
446 /* mu_normalize_path: convert pathname containig relative paths specs (../)
447 into an equivalent absolute path. Strip trailing delimiter if present,
448 unless it is the only character left. E.g.:
449
450 /home/user/../smith --> /home/smith
451 /home/user/../.. --> /
452
453 FIXME: delim is superfluous. The function deals with unix filesystem
454 paths, so delim should be always "/" */
455 char *
456 mu_normalize_path (char *path, const char *delim)
457 {
458 int len;
459 char *p;
460
461 if (!path)
462 return path;
463
464 len = strlen (path);
465
466 /* Empty string is returned as is */
467 if (len == 0)
468 return path;
469
470 /* delete trailing delimiter if any */
471 if (len && path[len-1] == delim[0])
472 path[len-1] = 0;
473
474 /* Eliminate any /../ */
475 for (p = strchr (path, '.'); p; p = strchr (p, '.'))
476 {
477 if (p > path && p[-1] == delim[0])
478 {
479 if (p[1] == '.' && (p[2] == 0 || p[2] == delim[0]))
480 /* found */
481 {
482 char *q, *s;
483
484 /* Find previous delimiter */
485 for (q = p-2; *q != delim[0] && q >= path; q--)
486 ;
487
488 if (q < path)
489 break;
490 /* Copy stuff */
491 s = p + 2;
492 p = q;
493 while ((*q++ = *s++))
494 ;
495 continue;
496 }
497 }
498
499 p++;
500 }
501
502 if (path[0] == 0)
503 {
504 path[0] = delim[0];
505 path[1] = 0;
506 }
507
508 return path;
509 }
510
......