-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaths.pl
executable file
·411 lines (351 loc) · 11 KB
/
paths.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#######################################################################
# paths.pl, ABr, 22MAR00
#
# Path manipulation functions.
#
# This code is copyrighted by Andy Bruce ("the author"). It is freely
# available for use in any application, but the source code must be
# released as-is. No warranty, either express or implied, is provided
# with this code. No implied warranty, nor implication of fitness for
# use or purpose, is provided with this code. This code is provided to
# the computing community in the hope that it will be useful. While the
# author is interested in hearing of defects or suggested improvements,
# the author makes no provision or promise to implement any suggestions
# or corrections.
package ABR ;
use strict ;
require "os.pl" ;
require "debug.pl" ;
# defines the current TEMP file name
$ABR::paths_CurTempFile = 0 ;
#######################################################################
# this routine makes the slashes correct for the OS and ensures
# that the passed path does *not* end with a slash!
# @param $path - path to check
# @return Fixed result
sub ABR::path_fixPath {
my( $path ) = @_ ;
debugprint( "Fixing '$path'..." ) ;
# fix slashes for OS
if( $ABR::gOsIsWindows ) {
# first, check for the CYGWIN pwd stuff
if( $path=~ /^\/(cygdrive)?\/([A-Za-z])\/(.*)/ ) {
# cygwin PWD stuff ("/cygdrive/<drive>/<path>")
$path = $2 . ":/" . $3 ;
} elsif( $path =~ /^\/\/([A-Za-z])\/(.*)/ ) {
# bogus "//<drive>/" output by some pwds
$path = $1 . ":/" . $2 ;
} #if
$path =~ s/\//\\/g ;
} else {
$path =~ s/\\/\//g ;
} #if
# clean out any doubled slashes
if( $ABR::gOsIsWindows ) {
$path =~ s/\\\\/\\/g ;
} else {
$path =~ s/\/\//\//g ;
} #if
# ensure we don't end with a slash
my $token ;
if( $ABR::gOsIsWindows ) {
$token = "\\\\" ;
} else {
$token = "\/" ;
} #if
my @paths = split( /$token/, $path ) ;
my $result = "" ;
my $first = 1 ;
foreach my $i (@paths) {
if( $first ) {
#if( !$ABR::gOsIsWindows ) { $result .= $ABR::gOsSlash ; }
$first = 0 ;
} else {
$result .= $ABR::gOsSlash ;
} #if
$result .= $i ;
} #foreach
debugprint( "'$result'\n" ) ;
return $result ;
} #path_fixPath
#######################################################################
# join a series of path elements to each other properly
# @param \@args - variable length list of args that get joined together,
# separated by the OS-specific path separator
# @return Fixed result
sub ABR::path_makePath {
my $first = 1 ;
my $result = "" ;
foreach my $i (@_) {
# ignore empty args
if( length( $i ) ) {
# don't prepend slash for first non-empty arg
if( $first ) {
$first = 0 ;
} else {
$result .= $ABR::gOsSlash ;
} #if
$result .= $i ;
} #if
} #foreach
debugprint( "Made path '$result'\n" ) ;
return $result ;
} #path_makePath
#######################################################################
# Extract the filename portion of a pathspec
# @param $path - path to analyze
# @return Filename of the pathspec
sub ABR::path_extractFname {
my( $path ) = @_ ;
# find last occurrence of path sep
my $idx = rindex( $path, "/") ;
$idx = rindex( $path, "\\") if( $idx < 0 );
return $path if( $idx < 0 ) ;
# return it
return substr( $path, $idx + 1 ) ;
} #path_extractFname
#######################################################################
# Extract the path portion of a pathspec
# @param $path - path to analyze
# @return Path of the pathspec
sub ABR::path_extractPath {
my( $path ) = @_ ;
# find last occurrence of path sep
my $idx = rindex( $path, "/") ;
$idx = rindex( $path, "\\") if( $idx < 0 );
return $path if( $idx < 0 ) ;
# return it
return substr( $path, 0, $idx ) ;
} #path_extractFname
#######################################################################
# create a subdirectory, including all parents
# @param $path - path to create
# @return Zero upon success
sub ABR::path_mkdirs {
my $path = $_[0] ;
# ensure we don't end with a slash
my $token ;
if( $ABR::gOsIsWindows ) {
$token = "\\\\" ;
} else {
$token = "\/" ;
} #if
my @paths = split( /$token/, $path ) ;
my $dir = "" ;
my $first = 1 ;
foreach my $i (@paths) {
if( $first ) {
if( !$ABR::gOsIsWindows ) { $dir .= $ABR::gOsSlash ; }
$first = 0 ;
} else {
$dir .= $ABR::gOsSlash ;
} #if
$dir .= $i ;
if( length( $dir ) ) {
if( !( -e $dir ) ) {
if( !mkdir( $dir, 0777 ) ) {
print( "Cannot create directory '$dir': $!\n" ) ;
return 1 ;
} #if
} #if
} #if
} #foreach
return 0 ;
} #path_mkdirs
#######################################################################
# Extract the DIRECTORY portion of a path
# @param $path - path to analyze
# @return Scalar containing the directory name *with* the trailing
# slash ("./" if not specified). This directory name has already
# been "cooked" to be correct for the operating system.
sub ABR::path_getPath {
my( $path ) = @_ ;
# first, normalize the path
my $fixedPath = ABR::path_fixPath( $path ) ;
# second, look for the last path separator
my $pos = rindex( $fixedPath, "/") ;
$pos = rindex( $fixedPath, "\\") if( $pos < 0);
# if not found, return "."
if( $pos < 0 ) { return "." . $ABR::gOsSlash ; }
# return the string
return substr( $fixedPath, 0, $pos + 1 ) ;
} #path_getPath
#######################################################################
# Extract the FILE portion of a path
# @param $path - path to analyze
# @return Scalar containing the file name.
sub ABR::path_getFile {
my( $path ) = @_ ;
# first, normalize the path
my $fixedPath = ABR::path_fixPath( $path ) ;
# second, look for the last path separator
my $pos = rindex( $fixedPath, "/") ;
$pos = rindex( $fixedPath, "\\") if( $pos < 0);
# if not found, return empty string
if( $pos < 0 ) { return "" ; }
# return the string
return substr( $fixedPath, $pos + 1 ) ;
} #path_getFile
#######################################################################
# Copy a file to another file, without using any OS commands
# @param $src - source file
# @param $dest - destination file
# @return Zero upon success
sub ABR::path_cpFile {
my( $src, $dest ) = @_ ;
# access files
if( !sysopen( SRC, $src, 0 ) ) {
print "ABR::path_cpFile: Error opening SRC '$src': $!\n" ;
return 1 ;
} #if
if( !open( DEST, ">$dest" ) ) {
print "ABR::path_cpFile: Error opening DEST '$dest': $!\n" ;
close SRC ;
return 1 ;
} #if
close DEST ;
if( !sysopen( DEST, "$dest", 1 ) ) {
print "ABR::path_cpFile: Error opening DEST '$dest': $!\n" ;
close SRC ;
return 1 ;
} #if
binmode SRC ;
binmode DEST ;
# copy file
my $buff, my $len ;
while( $len = sysread( SRC, $buff, 4096 ) ) {
my $offset = 0 ;
while( $len ) {
my $written = syswrite( DEST, $buff, $len, $offset ) ;
die( "ABR::path_cpFile: Error writing to '$dest': $!\n" )
unless defined $written ;
$len -= $written ;
$offset += $written ;
} #while
} #while
# done with files
close SRC ;
close DEST ;
# it worked
return 0 ;
} #path_cpFile
#######################################################################
# Load a text file to a string variable.
# @param $src - file to read
# @return Scalar variable containing file data.
sub ABR::path_readFile {
my( $file ) = @_ ;
# access
if( !open( FILE, $file ) ) {
print( "ABR::path_readFile: Error opening file '$file': $!\n" ) ;
return ;
} #if
# read the file
my $lines ;
while( my $line = <FILE> ) {
$lines .= $line ;
} #while
# done
close FILE ;
return $lines ;
} #ABR::gPATHS_IPL_DEFINED
#######################################################################
# Store a string variable to a text file
# @param $src - file to write
# @param $lines - Scalar variable containing file data.
# @return Non-zero on error
sub ABR::path_writeFile {
my( $file, $lines ) = @_ ;
# access
if( !open( FILE, ">$file" ) ) {
print( "ABR::path_writeFile: Error opening file '$file': $!\n" ) ;
return ;
} #if
# write the file
print FILE $lines ;
# done
close FILE ;
return 0 ;
} #path_writeFile
#######################################################################
# Load a complete directory structure without using os commands.
# @param $dir - starting directory
# @param $spec - file spec to find
# @param $recurse - TRUE if we should recurse directories
# @return List of file names (full file path!)
sub ABR::path_readDirSpec {
my( $dir, $spec, $recurse ) = @_ ;
# load up for the big load!
$dir = path_fixPath( $dir ) ;
chomp( my $pwd = os_execBacktickCmd( "pwd" ) ) ;
chdir( $dir ) ;
# read the entries
my @result ;
path_readDirSpecInternal( $dir, $spec, $recurse, \@result ) ;
# change back to original directory
chdir( $pwd ) ;
return @result ;
} #path_readDirSpec
sub ABR::path_readDirSpecInternal {
my( $dir, $spec, $recurse, $refResult ) = @_ ;
chdir( $dir ) ;
# open the directory and read all files matching filespec
my @files = glob( $spec ) ;
my $file ;
foreach $file (@files) {
last if( $file =~ /^(\.|\.\.)$/ ) ;
my $fullPath = path_makePath( $dir, $file ) ;
push( @$refResult, $fullPath ) ;
} #foreach
# now we glob *all* files, and get the directories in a list
my @dirs ;
if( utils_isTrue( $recurse ) ) {
@files = glob( "*" ) ;
foreach $file (@files) {
last if( $file =~ /^(\.|\.\.)$/ ) ;
my $fullPath = path_makePath( $dir, $file ) ;
if( -d $fullPath ) {
push( @dirs, $fullPath ) ;
} #if
} #foreach
} #if
# recurse if necessary
my $dir ;
foreach $dir (@dirs) {
path_readDirSpecInternal( $dir, $spec, $recurse, $refResult ) ;
} #foreach
} #path_readDirSpecInternal
#######################################################################
# Create a temporary file in the named directory, with the named
# prefix.
# @param strPrefix - if undefined, defaults to "TMP"
# @param strExt - if undefined, defaults to ".TMP"
# @param strDir - if undefined, defaults to "."
sub path_makeTempFile {
my( $strPrefix, $strExt, $strDir ) = @_ ;
$strPrefix = "TMP" if( !defined( $strPrefix ) ) ;
$strExt = ".TMP" if( !defined( $strExt ) ) ;
$strDir = "." if( !defined( $strDir ) ) ;
# create the temp dir
my $fullPrefix = path_makePath( $strDir, "$strPrefix" ) ;
debugprint ("makeTempFile: fullPrefix='$fullPrefix'\n") ;
# attempt to locate a file
my( $continue, $result ) ;
$continue = 1, $result = "" ;
while( $continue ) {
my $fileName = sprintf( "%s%05d%s",
$fullPrefix, $ABR::paths_CurTempFile, $strExt ) ;
debugprint ("makeTempFile: fileName='$fileName'\n") ;
++$ABR::paths_CurTempFile ;
$continue = ( -f $fileName ) ;
if( !$continue ) {
$continue = !open( PATHS_TEMP_FILE, ">$fileName" ) ;
if( !$continue ) {
$result = $fileName ;
close PATHS_TEMP_FILE ;
} #if
} #if
} #while
return $result ;
} #path_makeTempFile
1 ;