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