-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathname.pl
executable file
·28 lines (22 loc) · 912 Bytes
/
pathname.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/perl
# Output the canonical path of a file given as argument.
# The argument can be any file (regular file, directory, symlink, fifo, etc.)
# The file given as argument may not exist; unlike other tools such as readlink
# or realpath, pathname.{pl,sh} never tries to resolve the path name given as
# argument, which can be absolute or relative.
# Usage:
# $ pathname.pl ../../..//foobar/.././etc///blah/../fstab//./blah/..
# /etc/fstab
use strict;
use warnings;
use 5.010;
use Cwd;
my $name = $0;
$name =~ s,.*/,,;
defined $ARGV[0] or die "$name: missing argument";
my $pathname = $ARGV[0];
unless ( $pathname =~ m:^/: ) { $pathname = getcwd."/$pathname"; }
while ( $pathname =~ m:/\.{0,2}(/|$): ) { s:(/+\.?)+/+:/:g, s:^(/+\.\.)+(/+|$):/:, s:[^/]+/+\.\.(/+|$):/:, s:/+:/:g, s:(/+\.?)+$:: for $pathname; }
if ( $pathname =~ /^$/ ) { $pathname = "/"; }
say "$pathname";
# vim: et sts=4 sw=4 ts=4