Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2073 dpurdie 1
/*============================================================================
2
** Copyright (C) 1998-2012 Vix Technology, All rights reserved
3
**============================================================================
4
**
5
**  Project/Product : 
2075 dpurdie 6
**  Filename        : JatsFileUtil.c
2073 dpurdie 7
**  Author(s)       : DDP
8
**
2311 dpurdie 9
**  Description     :   Jats Build System File utility
10
**                      Used by the generated makefiles to perform specific operations
2073 dpurdie 11
**
2311 dpurdie 12
**                      The program exists to solve problems:
13
**                          Windows: Shell is very very slow to start up
14
**                          Windows: Some commands have ~260 character path length issue
15
**                          Windows/Solaris/Linux: Compatibility issues with the 'rm' command
16
**                          All: Consistent use of '/' as a path separator
2073 dpurdie 17
**
2311 dpurdie 18
**                      Note: There are two flavors of this program that MUST be
19
**                            kept in sync.
2310 dpurdie 20
**
2311 dpurdie 21
**                      The program will perform the following operations:
22
**                          (c) CopyFile
23
**                          (d) DeleteFile
24
**                          (r) Remove Files (wildcard)
25
**                          (D) DeleteDir after deleting specified files (wildcard)
2313 dpurdie 26
**                          (T) Delete Directory Trees
27
**                          (R) Delete Empty Directories
2311 dpurdie 28
**
29
**                      Example Usage
30
**
31
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
32
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
33
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
34
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
35
**                          JatsFileUtil T9 'DeleteTree'  interface
2313 dpurdie 36
**                          JatsFileUtil R9 'RmDir'       build
2311 dpurdie 37
**
2310 dpurdie 38
**                      First two arguments are common to all
39
**                          argv[1]     - Mode specifier, Debug specifier
40
**                          argv[2]     - Display Text
41
**
2073 dpurdie 42
**  Information     :
2311 dpurdie 43
**   Compiler       : ANSI C
44
**   Target         : Windows 2000+, Linux, Solaris8+
2073 dpurdie 45
**
46
***==========================================================================*/
47
 
48
#include <stdio.h>
49
#include <sys/types.h>
50
#include <sys/stat.h>
51
#include <fcntl.h>
52
#include <unistd.h>
53
#include <stdlib.h>
2085 dpurdie 54
#include <dirent.h>
2073 dpurdie 55
#include <string.h>
56
 
57
void ErrorExit (char * lpszMessage, char * lpszMessage2);
58
void createPaths ( char *path );
2085 dpurdie 59
void DeleteDir( int argc, char* argv[] );
2310 dpurdie 60
void DeleteDirTree( int argc, char* argv[] );
61
char * makePath( char *base, char *path);
62
int DeleteOneFile (char * dst );
63
void DeleteOneDirectoryTree( char* baseDir );
64
void copyOneFile( int argc, char* argv[] );
2311 dpurdie 65
int CopyFile ( char *src, char *dst, mode_t st_mode );
2313 dpurdie 66
void RmDir( int argc, char* argv[] );
2311 dpurdie 67
int wildcmp(char *string, char *wild );
2073 dpurdie 68
 
69
/*
70
**  Global
71
*/
72
char  verbose = 1;                          /* Debugging aid */
73
 
74
/*----------------------------------------------------------------------------
75
** FUNCTION           : main
76
**
77
** DESCRIPTION        : Main entry points
78
**
79
**
2311 dpurdie 80
** INPUTS             : argc            - Argument Count
81
**                      argv            - Argument Vector
2073 dpurdie 82
**
83
** RETURNS            : 0 - All is good
84
**
85
----------------------------------------------------------------------------*/
86
 
87
int main(int argc, char* argv[])
88
{
89
    /*
2313 dpurdie 90
    **  User must provide some thing
91
    */
92
    if ( argc < 2 )
93
    {
94
        ErrorExit("Insufficient arguments","");
95
    }
96
 
97
    /*
2073 dpurdie 98
    **  Examine the first argument
99
    **  Must be a character string of
2311 dpurdie 100
    **      [0] - Mode : One character
2073 dpurdie 101
    **      [1] - Verbose : 0 .. 9
102
    */
2310 dpurdie 103
    if ( argc > 1 && ( argv[1][1] >= '0' && argv[1][1] <= '9' ) )
2073 dpurdie 104
    {
2311 dpurdie 105
        verbose = argv[1][1] - '0';
2073 dpurdie 106
 
2311 dpurdie 107
        /*
108
        **  If Verbose, then display arguments
109
        */
110
        if ( verbose > 2 )
2073 dpurdie 111
        {
2311 dpurdie 112
            int ii;
113
            for ( ii = 0; ii < argc ; ii++ )
114
            {
115
                fprintf(stderr, "Arg%d: %s:\n", ii, argv[ii] );
116
            }
117
            fflush(stderr) ;
2073 dpurdie 118
        }
119
    }
2311 dpurdie 120
 
2073 dpurdie 121
    /*
2310 dpurdie 122
    **  Switch to required operation
2075 dpurdie 123
    */
2311 dpurdie 124
    switch( argv[1][0] )
2075 dpurdie 125
    {
2311 dpurdie 126
        /*
127
        **  CopyFile
128
        **      argv[2] - Text
129
        **      argv[3] - target path
130
        **      argv[4] - Source path
131
        **      argv[5] - File attributes
132
        */
2310 dpurdie 133
        case 'c':
134
            copyOneFile(argc, argv);
135
            break;
2075 dpurdie 136
 
2311 dpurdie 137
        /*
138
        **  UnCopy a file
139
        **      argv[2] - Text
140
        **      argv[3] - target path
141
        */
2310 dpurdie 142
        case 'd':
143
            {
144
                if ( argc != 4 )
145
                    ErrorExit("Incorrect argument count for mode","");
2085 dpurdie 146
 
2310 dpurdie 147
                /*
148
                **  Display user text
149
                */
150
                fprintf(stderr, "---- %s %s\n", argv[2], argv[3]);
151
                fflush(stderr) ;
152
                DeleteOneFile(argv[3]);
153
            }
154
            break;
2073 dpurdie 155
 
2311 dpurdie 156
        /*
157
        **  Remove named files
158
        **      argv[2]     - Text
159
        **      argv[3]+    - target path
160
        */
2310 dpurdie 161
        case 'r':
162
                {
163
                    int ii;
2073 dpurdie 164
 
2310 dpurdie 165
                    if ( argc < 4 )
166
                        ErrorExit("Incorrect argument count for mode","");
2073 dpurdie 167
 
2310 dpurdie 168
                    /*
169
                    **  Display user text
170
                    */
171
                    if ( argv[2][0] )
172
                    {
173
                        fprintf(stderr, "%s\n", argv[2]);
174
                        fflush(stderr) ;
175
                    }
2073 dpurdie 176
 
2310 dpurdie 177
                    for ( ii = 3; ii < argc ; ii++ )
178
                    {
179
                        DeleteOneFile(argv[ii]);
180
                    }
181
                }
182
            break;
2073 dpurdie 183
 
2311 dpurdie 184
        /*
185
        **  Delete files in directory - with wildcards
186
        **      argv[2]     - Text
187
        **      argv[3]     - Base directory
188
        **      argv[4]+    - Files in dir to delete.
189
        */
2310 dpurdie 190
        case 'D':
191
            DeleteDir(argc - 2, argv + 2 );
192
            break;
2073 dpurdie 193
 
2311 dpurdie 194
        /*
195
        **  Delete files recursively
196
        **      argv[2] - Text
197
        **      argv[3]+  Base directory
198
        */
2310 dpurdie 199
        case 'T':
200
            DeleteDirTree(argc - 2, argv + 2 );
201
            break;
2313 dpurdie 202
 
203
        /*
204
        **  Delete Empty Directories
205
        **      argv[2] - Text
206
        **      argv[3]+  Base directory
207
        */
208
        case 'R':
209
            RmDir(argc - 2, argv + 2 );
210
            break;
2310 dpurdie 211
 
2313 dpurdie 212
 
2310 dpurdie 213
        default :
214
            ErrorExit("Unknown mode: ",argv[1]);
215
            break;
2073 dpurdie 216
    }
217
    return 0;
218
}
219
 
220
/*----------------------------------------------------------------------------
221
** FUNCTION           : createPaths
222
**
223
** DESCRIPTION        : Create the path to the target
224
**
225
**
226
** INPUTS             : path
227
**
228
** RETURNS            : Will not return in error
229
**
230
----------------------------------------------------------------------------*/
231
 
232
void createPaths ( char *path )
233
{
234
    struct stat fstat;
235
    int  rv;
236
    char *ptr = path;
237
 
238
    while ( *ptr )
239
    {
240
        if ( *ptr == '/' )
241
        {
242
            *ptr = 0;
2310 dpurdie 243
/* fprintf(stderr, "createPaths: %s\n", path); */
2073 dpurdie 244
            rv = stat( path, &fstat );
245
            if ( rv )
246
            {
247
                if ( verbose > 1 )
248
                {
249
                    fprintf(stderr, "createPaths: %s\n", path);
250
                    fflush(stderr) ;
251
                }
252
                rv = mkdir( path, 0777 );
253
                if ( rv )
254
                {
2075 dpurdie 255
                    ErrorExit("Could not create directories:", path);
2073 dpurdie 256
                }
257
            }
258
            *ptr = '/';
259
        }
260
        ptr++;
261
    }
262
}
263
 
264
/*----------------------------------------------------------------------------
2310 dpurdie 265
** FUNCTION           : copyOneFile
266
**
267
** DESCRIPTION        : Copy one file to a target
268
**                      Used to package and install files
269
**
270
**
271
** INPUTS             : argc            - Argc count
272
**                      argv            - Argument list
273
**                          argv[2]     - Display text Prefix
274
**                          argv[3]     - Target path
275
**                          argv[4]     - Source Path
276
**                          argv[5]     - File attributes
277
**
278
** RETURNS            :
279
**
280
----------------------------------------------------------------------------*/
281
 
282
void copyOneFile( int argc, char* argv[] )
283
{
284
	int    rv;
285
    char * src;
286
    char * dst;
287
    struct stat fstat;
288
 
289
    if ( argc != 6 )
290
        ErrorExit("Incorrect argument count for file copy","");
291
 
292
    /*
293
    **  Display user text
294
    */
295
    fprintf(stderr, "---- %s %s\n", argv[2], argv[3]);
296
    fflush(stderr) ;
297
 
298
    dst = argv[3];
299
    src = argv[4];
300
 
301
    /*
302
    **   Check that the source is a file
303
    */
304
    if ( verbose )
305
        fprintf(stderr, "Validate Source File: %s\n", src);
306
 
307
    rv = stat( src, &fstat );
308
    if ( rv != 0 )
309
    {
310
/* Need to be a better message */
311
        fprintf(stderr, "Source: %s\n", src);
312
        ErrorExit("Source File not found: ", argv[4]);
313
    }
314
 
315
    /*
316
    **  Remove the ReadOnly attribute on the dest file
317
    */
318
    DeleteOneFile(dst);
319
 
320
    /*
321
    **  Create directories
322
    **  Use the path to the target - not the provided directory
323
    **  as the createPaths function will not create the last element
324
    */
325
    createPaths( dst );
326
 
327
    /*
328
    **   Copy the file
329
    */
330
    if ( ! CopyFile( src, dst, fstat.st_mode ) )
331
    {
332
        ErrorExit("Copy Error: ", argv[4]);
333
    }
334
 
335
    /*
336
    **  Test for files existence
337
    */
338
    if ( verbose )
339
        fprintf(stderr, "Test target was copied: %s\n", dst);
340
 
341
    rv = stat( dst, &fstat );
342
    if ( rv != 0 )
343
    {
344
/* Need to be a better message */
345
        ErrorExit("File not found after copy: ", argv[3]);
346
    }
347
 
348
    /*
349
    **  Set the files attributes
350
    **      Assume read-only
351
    */
352
    if ( strstr( argv[5], "-w" ) )
353
    {
354
        if ( verbose > 1 )
355
            fprintf(stderr, "Set target read-only: %s\n", dst);
356
        fstat.st_mode &= ~(S_IWRITE | S_IWOTH | S_IWGRP );
357
    }
358
 
359
    if ( strstr( argv[5], "+w" ) )
360
    {
361
        if ( verbose > 1 )
362
            fprintf(stderr, "Set target writable: %s\n", dst);
363
        fstat.st_mode |= (S_IWRITE | S_IWOTH | S_IWGRP );
364
    }
365
 
366
    if ( strstr( argv[5], "+x" ) )
367
    {
368
        if ( verbose > 1 )
369
            fprintf(stderr, "Set target executable: %s\n", dst);
370
        fstat.st_mode |= ( S_IXUSR | S_IXOTH | S_IXGRP );
371
    }
372
 
373
    if ( strstr( argv[5], "-x" ) )
374
    {
375
        if ( verbose > 1)
376
            fprintf(stderr, "Set target executable: %s\n", dst);
377
        fstat.st_mode &= ~( S_IXUSR | S_IXOTH | S_IXGRP );
378
    }
379
 
380
    if ( verbose )
381
        fprintf(stderr, "Set target perms: %s, 0%o\n", dst, fstat.st_mode);
382
    rv = chmod( dst, fstat.st_mode );
383
    if ( rv != 0 )
384
    {
385
        ErrorExit("Setting ReadOnly: ", argv[3]);
386
    }
387
}
388
 
389
/*----------------------------------------------------------------------------
2073 dpurdie 390
** FUNCTION           : CopyFile
391
**
392
** DESCRIPTION        : Just copy a file
393
**
394
**
395
** INPUTS             : src - source path - already exists
396
**                      dst - path. Dirs already exist
397
**                      st_mode - Creation  mode. Copied from source file
398
**
399
** RETURNS            : false - Error
400
**
401
----------------------------------------------------------------------------*/
402
 
403
#define COPYSIZE  1024
404
int CopyFile ( char *src, char *dst, mode_t st_mode )
405
{
406
 
407
     ssize_t rlen = 0 ;
408
     ssize_t wlen = 0 ;
409
     int in;
410
     int out;
411
     int ferror = 0;
412
    char buffer[COPYSIZE] = { '\0' } ;
413
 
414
    if ( verbose )
415
    {
416
        fprintf(stderr, "CopyFile: Output Mode: 0%o\n", st_mode);
417
        fflush(stderr) ;
418
    }
419
 
420
    in = open( src, O_RDONLY ) ;
421
    if ( in < 0 )
2075 dpurdie 422
        ErrorExit("Could not open source:", src);
2073 dpurdie 423
 
424
    out = open( dst, O_WRONLY | O_CREAT, st_mode | S_IWRITE );
425
    if ( out < 0 )
2075 dpurdie 426
        ErrorExit("Could not open dst:", dst);
2073 dpurdie 427
 
428
    while( (rlen = read( in, buffer, COPYSIZE )) > 0 )
429
    {
430
        wlen = write( out, buffer, rlen ) ;
431
        if ( wlen != rlen )
432
        {
433
            ferror = 1;
434
            break;
435
        }
436
    }
437
 
438
    close(in) ;
439
    close(out) ;
440
 
441
    /*
442
    **  File error
443
    **  Delete target
444
    */
445
    if ( ferror || rlen < 0 )
446
    {
447
        unlink(dst) ;
448
        return 0;
449
    }
450
    return 1;
451
}
452
 
2075 dpurdie 453
/*----------------------------------------------------------------------------
2085 dpurdie 454
** FUNCTION           : DeleteDir
455
**
456
** DESCRIPTION        : Delete a list of files in a specified directory
457
**                      Ensure files are writable
458
**                      Wilcarding is supported
459
**
460
**                      Then delete the directory - if its empty
461
**
462
**
463
** INPUTS             : argc    - count of args
464
**                      argv    - list of files to delete
465
**                                [0]:  Text to display
466
**                                [1]:  Base directory
2310 dpurdie 467
**                                [2]+  Files in dir to delete
2085 dpurdie 468
**
2310 dpurdie 469
** RETURNS            : Will not return on error
2085 dpurdie 470
**
471
----------------------------------------------------------------------------*/
2075 dpurdie 472
 
2085 dpurdie 473
void DeleteDir( int argc, char* argv[] )
474
{
475
	int rv;
476
    struct stat fstat;
477
    char* baseDir;
478
    DIR *dir;
479
    struct dirent *dirent;
2310 dpurdie 480
    char *target;
2085 dpurdie 481
 
482
    if ( argc < 3 )
2311 dpurdie 483
        ErrorExit("Insufficient arguments for DeleteDir","");
2310 dpurdie 484
 
2085 dpurdie 485
    /*
486
    **  Display the user message
487
    **      Supress display if the message is empty
488
    */
489
    if ( argv[0][0] )
490
    {
491
        fprintf(stderr, "%s\n", argv[0]);
492
        fflush(stderr) ;
493
    }
494
 
495
    /*
496
    **  Extract the base directory from the argument list
497
    **  This must be a directory
498
    */
499
    baseDir = argv[1];
500
    argc -= 2;
501
    argv+=2;
502
 
503
    /*
504
    **  Ensure that the base directory exists
505
    */
506
    rv = stat( baseDir, &fstat );
507
    if ( rv != 0 )
508
    {
509
        /*
510
        **  Directory does not exists
511
        **  Assume its aleady deleted
512
        */
513
        if ( verbose > 1 )
514
            fprintf(stderr, "Base dir does not exist: %s\n", baseDir);
515
        return;
516
    }
517
 
518
    if ( !(fstat.st_mode & S_IFDIR))
519
    {
520
        /*
521
        **  Target is not a directory
522
        **  Don't do anything
523
        */
524
        if ( verbose > 1 )
525
            fprintf(stderr, "Base dir is not a directory: %s\n", baseDir);
526
        return;
527
    }
528
 
529
    /*
530
    **  Process all the suffixes
531
    **  They may contain a wild card
532
    */
533
    dir = opendir( baseDir );
534
    if ( dir == NULL )
535
    {
536
        /*
537
        **  Target is not a directory
538
        **  Don't do anything
539
        */
540
        if ( verbose > 1 )
541
            fprintf(stderr, "Base dir is not a directory: %s\n", baseDir);
542
        return;
543
    }
544
 
545
    /*
546
    **  Read next directory entry
547
    */
548
    while ( (dirent = readdir(dir)) != NULL )
549
    {
550
        int ii;
551
        if ( strcmp( ".", dirent->d_name ) == 0 )
552
            continue;
553
 
554
        if ( strcmp( "..", dirent->d_name ) == 0 )
555
            continue;
556
 
2313 dpurdie 557
        if ( verbose > 2 )
558
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
559
 
2085 dpurdie 560
        /*
561
        **  Compare against each item in the user list
562
        */
563
        for ( ii = 0; ii < argc ; ii++)
564
        {
565
 
566
            if ( wildcmp(dirent->d_name, argv[ii] )  )
567
            {
568
                if ( verbose > 1 )
569
                    fprintf(stderr, "Matching: %s, %s --- Found\n", dirent->d_name, argv[ii] );
2310 dpurdie 570
 
2085 dpurdie 571
                /*
572
                **  Matching file found
573
                */
2310 dpurdie 574
                target = makePath( baseDir, dirent->d_name);
575
                DeleteOneFile(target);
576
                free(target);
2085 dpurdie 577
            }
578
        }
579
    }
580
    closedir(dir);
581
 
582
    /*
583
    **  Finally delete the diretory
584
    **      Unless its '.'
585
    */
586
    if ( strcmp( ".", baseDir) != 0 )
587
    {
588
        if ( verbose > 1 )
589
            fprintf(stderr, "Delete Directory: %s\n", baseDir);
2310 dpurdie 590
 
2085 dpurdie 591
        if ( rmdir (baseDir ) )
592
        {
593
            if ( verbose )
594
                fprintf(stderr, "Directory not deleted: %s\n", baseDir);
595
        }
596
    }
597
}
598
 
2073 dpurdie 599
/*----------------------------------------------------------------------------
2310 dpurdie 600
** FUNCTION           : DeleteDirTree
601
**
602
** DESCRIPTION        : Delete an entire directory tree(s)
603
**
604
** INPUTS             : argc    - count of args
605
**                      argv    - list of files to delete
606
**                                [0]:  Text to display
607
**                                [1]+  Base directory
608
**
609
** RETURNS            : Will not return on error
610
**
611
----------------------------------------------------------------------------*/
612
 
613
void DeleteDirTree( int argc, char* argv[] )
614
{
615
    int ii;
616
 
617
    if ( argc < 2 )
2311 dpurdie 618
        ErrorExit("Insufficient arguments for DeleteDirTree","");
2310 dpurdie 619
 
620
    /*
621
    **  Display the user message
622
    **      Supress display if the message is empty
623
    */
624
    if ( argv[0][0] )
625
    {
626
        fprintf(stderr, "%s\n", argv[0]);
627
        fflush(stderr) ;
628
    }
629
 
630
    for ( ii = 1; ii < argc ; ii++)
631
    {
632
        DeleteOneDirectoryTree( argv[ii] );
633
    }
634
}
635
 
636
/*----------------------------------------------------------------------------
637
** FUNCTION           : DeleteOneDirectoryTree
638
**
639
** DESCRIPTION        : Delete an entire directory tree(s)
2313 dpurdie 640
**                      Force it writable and searchable before deletion
641
**                      Don't follow symbolic links - just delete them
2310 dpurdie 642
**
643
** INPUTS             : path    - Dir to delete
644
**
645
** RETURNS            : Will not return on error
646
**
647
----------------------------------------------------------------------------*/
648
 
649
void DeleteOneDirectoryTree( char* baseDir )
650
{
651
    struct stat fstat;
652
    DIR *dir;
653
    struct dirent *dirent;
654
    char *target;
655
    int rv;
656
 
657
    /*
658
    **  A bit of a sanity test
659
    */
660
    if ( strcmp( ".", baseDir) == 0 || strcmp( "..", baseDir) == 0 )
661
    {
662
        fprintf(stderr, "DeleteOneDirectoryTree will not delete '.' or '..'\n");
663
        return;
664
    }
665
 
666
    /*
2313 dpurdie 667
    **  Ensure that the directory exists
668
    **  Force writable and searchable
669
    */
670
    rv = stat( baseDir, &fstat );
671
    if ( rv == 0 )
672
    {
673
        if ( fstat.st_mode & S_IFDIR )
674
        {
675
            mode_t newMode =  fstat.st_mode | S_IRUSR | S_IWUSR  | S_IXUSR;
676
            if ( fstat.st_mode != newMode )
677
            {
678
                if ( verbose > 1 )
679
                    fprintf(stderr, "Force u+rwx: %s\n", baseDir);
680
 
681
                rv = chmod( baseDir, newMode );
682
                if ( rv != 0 )
683
                {
684
                    fprintf(stderr, "Warning: Attempt to allow u+rwx access: %s\n", baseDir);
685
                }
686
            }
687
        }
688
    }
689
 
690
 
691
    /*
2310 dpurdie 692
    **  Process all entries
693
    */
694
    dir = opendir( baseDir );
695
    if ( dir == NULL )
696
    {
697
        /*
698
        **  Target is not a directory
699
        **  Don't do anything
700
        */
701
        if ( verbose > 1 )
702
            fprintf(stderr, "Base dir is not a directory: %s\n", baseDir);
703
        return;
704
    }
705
 
706
    /*
707
    **  Read next directory entry
708
    */
709
    while ( (dirent = readdir(dir)) != NULL )
710
    {
711
        if ( strcmp( ".", dirent->d_name ) == 0 )
712
            continue;
713
 
714
        if ( strcmp( "..", dirent->d_name ) == 0 )
715
            continue;
716
 
2313 dpurdie 717
        if ( verbose > 2 )
718
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
719
 
2310 dpurdie 720
        target = makePath( baseDir, dirent->d_name);
2313 dpurdie 721
        rv = lstat( target, &fstat );
2310 dpurdie 722
        if ( rv == 0 )
723
        {
724
            if ( fstat.st_mode & S_IFDIR )
725
            {
726
                DeleteOneDirectoryTree( target );
727
            }
728
            else
729
            {
730
                DeleteOneFile (target);
731
            }
732
        }
733
        free(target);
734
    }
735
    closedir(dir);
736
 
737
    /*
738
    **  Finally delete the directory
739
    */
740
    if ( verbose )
741
        fprintf(stderr, "Delete Directory: %s\n", baseDir);
742
 
743
    if ( rmdir (baseDir ) )
744
    {
745
        if ( verbose )
746
            fprintf(stderr, "Directory not deleted: %s\n", baseDir);
747
    }
748
}
749
 
750
/*----------------------------------------------------------------------------
751
** FUNCTION           : DeleteOneFile
752
**
753
** DESCRIPTION        : Delete a file
754
**                      Force it writable before deletion
2313 dpurdie 755
**                      Don't follow symbolic links - just delete them
2310 dpurdie 756
**
757
** INPUTS             : path        - path to the file
758
**
759
** RETURNS            : 0           - OK (file deleted or not present)
760
**
761
----------------------------------------------------------------------------*/
762
 
763
int DeleteOneFile (char * dst )
764
{
765
	int rv;
766
    struct stat fstat;
767
    int result = 0;
768
 
769
    if ( verbose > 1)
770
        fprintf(stderr, "Delete File: %s\n", dst);
771
 
2313 dpurdie 772
    rv = lstat( dst, &fstat );
2310 dpurdie 773
    if ( rv == 0 )
774
    {
775
        if ( verbose )
776
            fprintf(stderr, "Delete file: %s : Attr: 0%o\n", dst, fstat.st_mode);
777
        if ( !(fstat.st_mode & S_IWRITE) )
778
        {
779
            fstat.st_mode |= S_IWRITE;
780
            rv = chmod( dst, fstat.st_mode );
781
            if ( rv != 0 )
782
            {
783
                fprintf(stderr, "Warning: Attempt to allow write access: %s\n", dst);
784
            }
785
        }
786
 
787
        if ( unlink(dst) )
788
        {
789
            fprintf(stderr, "Warning: Did not remove file: %s\n", dst);
790
            result = 1;
791
        }
792
    }
793
    return result;
794
}
795
 
796
/*----------------------------------------------------------------------------
2313 dpurdie 797
** FUNCTION           : RmDir
798
**
799
** DESCRIPTION        : Remove Empty directories
800
**
801
** INPUTS             : argc    - count of args
802
**                      argv    - list of files to delete
803
**                                [0]:  Text to display
804
**                                [1]+  Base directory
805
**
806
** RETURNS            : Will not return on error
807
**
808
----------------------------------------------------------------------------*/
809
 
810
void RmDir( int argc, char* argv[] )
811
{
812
    int ii;
813
 
814
    if ( argc < 2 )
815
        ErrorExit("Insufficient arguments for RmDir","");
816
 
817
    /*
818
    **  Display the user message
819
    **      Suppress display if the message is empty
820
    */
821
    if ( argv[0][0] )
822
    {
823
        fprintf(stderr, "%s\n", argv[0]);
824
        fflush(stderr) ;
825
    }
826
 
827
    for ( ii = 1; ii < argc ; ii++)
828
    {
829
        if ( verbose > 2)
830
            fprintf(stderr, "RmDir: %s\n", argv[ii]);
831
 
832
        if ( rmdir (argv[ii] ) )
833
        {
834
            if ( verbose )
835
                fprintf(stderr, "Directory not deleted: %s\n", argv[ii]);
836
        }
837
    }
838
}
839
 
840
/*----------------------------------------------------------------------------
2310 dpurdie 841
** FUNCTION           : makePath
842
**
843
** DESCRIPTION        : Create a path from two elements
844
**                      Allocate memory
845
**
846
**
847
** INPUTS             : base        - Part 1
848
**                      file        - Part 2
849
**
850
** RETURNS            :
851
**
852
----------------------------------------------------------------------------*/
853
 
854
char * makePath( char *base, char *path)
855
{
856
    int len1 = strlen(base);
857
    int len2 = strlen(path);
858
    char *data;
859
 
860
    data = (char *)malloc(len1 + len2 + 10);
861
    if ( data == NULL )
862
    {
863
        ErrorExit ("Malloc error:makePath","");
864
    }
865
 
866
    strcpy( data,base);
867
    strcpy( data + len1, "/");
868
    strcpy( data + len1 + 1, path);
869
 
870
    return data;
871
}
872
 
873
/*----------------------------------------------------------------------------
2085 dpurdie 874
** FUNCTION           : wildcmp
875
**
876
** DESCRIPTION        : Wildcard comparision
877
**
878
**
879
** INPUTS             : string          - String
880
**                      wild            - Wildcard template
881
**
882
** RETURNS            : TRUE - Match
883
**
884
----------------------------------------------------------------------------*/
885
 
886
int wildcmp(char *string, char *wild )
887
{
888
    char *cp, *mp;
889
    while ((*string) && (*wild != '*'))
890
    {
891
        if ((*wild != *string) && (*wild != '?'))
892
        {
893
            return 0;
894
        }
895
         wild++;
896
         string++;
897
    }
898
 
899
    while (*string)
900
    {
901
        if (*wild == '*')
902
        {
903
            if (!*++wild)
904
            {
905
                return 1;
906
            }
907
            mp = wild;
908
            cp = string+1;
909
        }
910
        else if ((*wild == *string) || (*wild == '?'))
911
        {
912
            wild++;
913
            string++;
914
        }
915
        else
916
        {
917
            wild = mp;
918
            string = cp++;
919
        }
920
    }
921
 
922
    while (*wild == '*')
923
    {
924
        wild++;
925
    }
926
 
927
    return !*wild;
928
}
929
 
930
 
931
 
932
 
933
/*----------------------------------------------------------------------------
2073 dpurdie 934
** FUNCTION           : ErrorExit
935
**
936
** DESCRIPTION        : Error processing
937
**                      Report an error and terminate process
938
**
939
**
940
** INPUTS             : lpszMessage     - Message to display
941
**
942
** RETURNS            : Does't return
943
**                      Will exit with bad code
944
**
945
----------------------------------------------------------------------------*/
946
 
947
void ErrorExit (char * lpszMessage, char * lpszMessage2)
948
{ 
2075 dpurdie 949
   fprintf(stderr, "JatsFileUtil:Error: %s%s\n", lpszMessage,lpszMessage2);
2073 dpurdie 950
   fflush(stderr) ;
951
   exit(-1);
952
} 
953