1bb2e14f3Smrg/*
2bb2e14f3Smrg * Copyright 1999 by The XFree86 Project, Inc.
3bb2e14f3Smrg */
4bb2e14f3Smrg
5bb2e14f3Smrg/*
6bb2e14f3Smrg * Reverse a pathname.  It returns a relative path that can be used to undo
7bb2e14f3Smrg * 'cd argv[1]'.
8bb2e14f3Smrg *
9bb2e14f3Smrg * It is impossible to do this in general, but this handles the cases that
10bb2e14f3Smrg * come up in imake.  Maybe imake should use an absolute path for $(TOP)
11bb2e14f3Smrg * instead of a relative path so that this problem can be avoided?
12bb2e14f3Smrg */
13bb2e14f3Smrg
14bb2e14f3Smrg#include <stdio.h>
15bb2e14f3Smrg#include <string.h>
16bb2e14f3Smrg#include <stdlib.h>
17bb2e14f3Smrg
18bb2e14f3Smrgint
19bb2e14f3Smrgmain(int argc, char *argv[])
20bb2e14f3Smrg{
21bb2e14f3Smrg    int levels = 0;
22bb2e14f3Smrg    char *p;
23bb2e14f3Smrg
24bb2e14f3Smrg    /* Silently ignore invalid usage */
25bb2e14f3Smrg    if (argc != 2)
26bb2e14f3Smrg	exit(0);
27bb2e14f3Smrg
28bb2e14f3Smrg    /* Split the path and count the levels */
29bb2e14f3Smrg    p = strtok(argv[1], "/");
30bb2e14f3Smrg    while (p) {
31bb2e14f3Smrg	if (strcmp(p, ".") == 0)
32bb2e14f3Smrg	    ;
33bb2e14f3Smrg	else if (strcmp(p, "..") == 0)
34bb2e14f3Smrg	    levels--;
35bb2e14f3Smrg	else
36bb2e14f3Smrg	    levels++;
37bb2e14f3Smrg	p = strtok(NULL, "/");
38bb2e14f3Smrg    }
39bb2e14f3Smrg
40bb2e14f3Smrg    while (levels-- > 0)
41bb2e14f3Smrg	printf("../");
42bb2e14f3Smrg
43bb2e14f3Smrg    printf("\n");
44bb2e14f3Smrg
45bb2e14f3Smrg    exit(0);
46bb2e14f3Smrg}
47