Subversion Repositories DevTools

Rev

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