ctpathadjust 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/local/bin/perl
  2. # find the longest common directory prefix of the 2 input paths
  3. # input is:
  4. # $_[0] = path 1
  5. # $_[1] = path 2
  6. #
  7. # output is:
  8. # relative path
  9. sub CTCommonPrefix {
  10. local( @path1 ) ;
  11. local( @path2 ) ;
  12. local( $count ) = 0 ;
  13. local( $thresh ) ;
  14. local( $tmp ) = $_[0] ;
  15. $tmp =~ s/^\/// ;
  16. @path1 = split ( /\//, $tmp ) ;
  17. $tmp = $_[1] ;
  18. $tmp =~ s/^\/// ;
  19. @path2 = split ( /\//, $tmp ) ;
  20. if ( $path1[0] eq "view" ) {
  21. $thresh = 1 ;
  22. } else {
  23. $thresh = 0 ;
  24. }
  25. while (( $path1[0] eq $path2[0] ) &&
  26. ( $#path1 >= 0 ) &&
  27. ( $#path2 > 0 )) {
  28. shift @path1;
  29. shift @path2;
  30. $count++ ;
  31. }
  32. local( $ret ) ;
  33. if ( $count > $thresh ) {
  34. while ( $#path1 >= 0 ) {
  35. $ret = $ret . "../" ;
  36. shift @path1 ;
  37. }
  38. while ( $#path2 >= 0 ) {
  39. $ret = $ret . $path2[0] . "/" ;
  40. shift @path2 ;
  41. }
  42. $ret =~ s/\/$// ;
  43. } else {
  44. $ret = $_[1] ;
  45. }
  46. $ret ;
  47. }
  48. # $pwd = $ENV{"PWD"} ;
  49. # sometimes the PWD environment variable lies, and getcwd by itself doesn't
  50. # report the view extention.
  51. use Cwd ;
  52. $pwd = getcwd() ;
  53. if ( $pwd =~ /^\/vobs/ ) {
  54. open( VFILE, "/usr/atria/bin/cleartool pwv -short |" ) ;
  55. $view = <VFILE> ;
  56. close( VFILE ) ;
  57. $view =~ s/\n$// ;
  58. $pwd = "/view/" . $view . $pwd ;
  59. }
  60. $output = "" ;
  61. foreach $item ( @ARGV ) {
  62. $common = &CTCommonPrefix( $pwd, $item ) ;
  63. if ( length( $common ) > 0 ) {
  64. if ( $output ne "" ) {
  65. $output = $output . " " ;
  66. }
  67. $output = $output . $common ;
  68. }
  69. }
  70. print $output . "\n" ;