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