Subversion Repositories DevTools

Rev

Rev 2075 | Rev 2310 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2075 Rev 2085
Line 12... Line 12...
12
**
12
**
13
**                      Operate in two modes
13
**                      Operate in two modes
14
**                          CopyFile
14
**                          CopyFile
15
**                          DeleteFile
15
**                          DeleteFile
16
**                          Remove Files
16
**                          Remove Files
-
 
17
**                          DeleteDir after deleting specified files
17
**
18
**
18
**                      CopyFile (5 args)
19
**                      CopyFile (5 args)
19
**                          1) Display the ----- Text dest
20
**                          1) Display the ----- Text dest
20
**                          2) Error if the source is not found
21
**                          2) Error if the source is not found
21
**                          3) Create target path if not alraedy existing
22
**                          3) Create target path if not alraedy existing
Line 29... Line 30...
29
**
30
**
30
**                      RemoveFiles ( >1 arg)
31
**                      RemoveFiles ( >1 arg)
31
**                          1) Silenty delete a list of files. The files may
32
**                          1) Silenty delete a list of files. The files may
32
**                             be readonly so the attributes will need to be fixed.
33
**                             be readonly so the attributes will need to be fixed.
33
**
34
**
-
 
35
**                             Assumes the current directory
-
 
36
**
-
 
37
**                       DeleteDir (> 2 args)
-
 
38
**                          1) Silenty delete a list of files. The files may
-
 
39
**                             be readonly so the attributes will need to be fixed.
-
 
40
**                          2) Delete the directory if its empty
-
 
41
**
34
**                      Make paths use '/'
42
**                      Make paths use '/'
35
**
43
**
36
**
44
**
37
**  Information     :
45
**  Information     :
38
**   Compiler       : ANSI C++
46
**   Compiler       : ANSI C++
Line 44... Line 52...
44
#include <sys/types.h>
52
#include <sys/types.h>
45
#include <sys/stat.h>
53
#include <sys/stat.h>
46
#include <fcntl.h>
54
#include <fcntl.h>
47
#include <unistd.h>
55
#include <unistd.h>
48
#include <stdlib.h>
56
#include <stdlib.h>
-
 
57
#include <dirent.h>
49
#include <string.h>
58
#include <string.h>
50
 
59
 
-
 
60
#define MAX_FILE 2000
-
 
61
 
51
void ErrorExit (char * lpszMessage, char * lpszMessage2);
62
void ErrorExit (char * lpszMessage, char * lpszMessage2);
52
void createPaths ( char *path );
63
void createPaths ( char *path );
53
int CopyFile ( char *src, char *dst, mode_t st_mode );
64
int CopyFile ( char *src, char *dst, mode_t st_mode );
54
void deleteFileList( int argc, char* argv[] );
65
void deleteFileList( int argc, char* argv[] );
-
 
66
void DeleteDir( int argc, char* argv[] );
-
 
67
int wildcmp(char *string, char *wild );
55
 
68
 
56
/*
69
/*
57
**  Global
70
**  Global
58
*/
71
*/
59
char  verbose = 1;                          /* Debugging aid */
72
char  verbose = 1;                          /* Debugging aid */
60
char  copyMode = 0;                         /* Mode */
73
char  copyMode = 0;                         /* Mode */
-
 
74
char  dst [MAX_FILE + 1];                   /* Scratch string workspace */
61
 
75
 
62
/*----------------------------------------------------------------------------
76
/*----------------------------------------------------------------------------
63
** FUNCTION           : main
77
** FUNCTION           : main
64
**
78
**
65
** DESCRIPTION        : Main entry points
79
** DESCRIPTION        : Main entry points
Line 86... Line 100...
86
    mode_t src_mode;
100
    mode_t src_mode;
87
 
101
 
88
    /*
102
    /*
89
    **  Examine the first argument
103
    **  Examine the first argument
90
    **  Must be a character string of
104
    **  Must be a character string of
91
    **      [0] - Mode : c or d or r
105
    **      [0] - Mode : c or d or r or l
92
    **      [1] - Verbose : 0 .. 9
106
    **      [1] - Verbose : 0 .. 9
93
    */
107
    */
94
    if ( argc > 1 )
108
    if ( argc > 1 )
95
    {
109
    {
96
        if ( argv[1][0] == 'c' ) {
110
        if ( argv[1][0] == 'c' ) {
97
            copyMode = 1;
111
            copyMode = 1;
98
        } else if ( argv[1][0] == 'd' ) {
112
        } else if ( argv[1][0] == 'd' ) {
99
            copyMode = 2;
113
            copyMode = 2;
100
        } else if ( argv[1][0] == 'r' ) {
114
        } else if ( argv[1][0] == 'r' ) {
101
            copyMode = 3;
115
            copyMode = 3;
-
 
116
        } else if ( argv[1][0] == 'D' ) {
-
 
117
            copyMode = 4;
102
        } else {
118
        } else {
103
            ErrorExit("Unknown mode: ",argv[1]);
119
            ErrorExit("Unknown mode: ",argv[1]);
104
        }
120
        }
105
 
121
 
106
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
122
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
Line 130... Line 146...
130
    {
146
    {
131
        deleteFileList(argc - 2, argv + 2 );
147
        deleteFileList(argc - 2, argv + 2 );
132
        return 0;
148
        return 0;
133
    }
149
    }
134
 
150
 
-
 
151
    if ( copyMode == 4 )
-
 
152
    {
-
 
153
        DeleteDir(argc - 2, argv + 2 );
-
 
154
        return 0;
-
 
155
    }
-
 
156
 
135
    /*
157
    /*
136
    **  Determine mode of operation
158
    **  Determine mode of operation
137
    **      Copy or Delete
159
    **      Copy or Delete
138
    */
160
    */
139
    if ( copyMode == 1 && argc == 6 ) {
161
    if ( copyMode == 1 && argc == 6 ) {
Line 428... Line 450...
428
            }
450
            }
429
        }
451
        }
430
    }
452
    }
431
}
453
}
432
 
454
 
-
 
455
/*----------------------------------------------------------------------------
-
 
456
** FUNCTION           : DeleteDir
-
 
457
**
-
 
458
** DESCRIPTION        : Delete a list of files in a specified directory
-
 
459
**                      Ensure files are writable
-
 
460
**                      Wilcarding is supported
-
 
461
**
-
 
462
**                      Then delete the directory - if its empty
-
 
463
**
-
 
464
**
-
 
465
** INPUTS             : argc    - count of args
-
 
466
**                      argv    - list of files to delete
-
 
467
**                                [0]:  Text to display
-
 
468
**                                [1]:  Base directory
-
 
469
**                                [2]+  File in dir to delete
-
 
470
**
-
 
471
** RETURNS            : Wil not return on error
-
 
472
**
-
 
473
----------------------------------------------------------------------------*/
-
 
474
 
-
 
475
void DeleteDir( int argc, char* argv[] )
-
 
476
{
-
 
477
	int rv;
-
 
478
    struct stat fstat;
-
 
479
    char* baseDir;
-
 
480
    DIR *dir;
-
 
481
    struct dirent *dirent;
-
 
482
 
-
 
483
    if ( argc < 3 )
-
 
484
        ErrorExit("Insuffiecient arguments for DeleteDir","");
-
 
485
        
-
 
486
    /*
-
 
487
    **  Display the user message
-
 
488
    **      Supress display if the message is empty
-
 
489
    */
-
 
490
    if ( argv[0][0] )
-
 
491
    {
-
 
492
        fprintf(stderr, "%s\n", argv[0]);
-
 
493
        fflush(stderr) ;
-
 
494
    }
-
 
495
 
-
 
496
    /*
-
 
497
    **  Extract the base directory from the argument list
-
 
498
    **  This must be a directory
-
 
499
    */
-
 
500
    baseDir = argv[1];
-
 
501
    argc -= 2;
-
 
502
    argv+=2;
-
 
503
 
-
 
504
    /*
-
 
505
    **  Ensure that the base directory exists
-
 
506
    */
-
 
507
    rv = stat( baseDir, &fstat );
-
 
508
    if ( rv != 0 )
-
 
509
    {
-
 
510
        /*
-
 
511
        **  Directory does not exists
-
 
512
        **  Assume its aleady deleted
-
 
513
        */
-
 
514
        if ( verbose > 1 )
-
 
515
            fprintf(stderr, "Base dir does not exist: %s\n", baseDir);
-
 
516
        return;
-
 
517
    }
-
 
518
 
-
 
519
    if ( !(fstat.st_mode & S_IFDIR))
-
 
520
    {
-
 
521
        /*
-
 
522
        **  Target is not a directory
-
 
523
        **  Don't do anything
-
 
524
        */
-
 
525
        if ( verbose > 1 )
-
 
526
            fprintf(stderr, "Base dir is not a directory: %s\n", baseDir);
-
 
527
        return;
-
 
528
    }
-
 
529
 
-
 
530
    /*
-
 
531
    **  Process all the suffixes
-
 
532
    **  They may contain a wild card
-
 
533
    */
-
 
534
    dir = opendir( baseDir );
-
 
535
    if ( dir == NULL )
-
 
536
    {
-
 
537
        /*
-
 
538
        **  Target is not a directory
-
 
539
        **  Don't do anything
-
 
540
        */
-
 
541
        if ( verbose > 1 )
-
 
542
            fprintf(stderr, "Base dir is not a directory: %s\n", baseDir);
-
 
543
        return;
-
 
544
    }
-
 
545
 
-
 
546
    /*
-
 
547
    **  Read next directory entry
-
 
548
    */
-
 
549
    while ( (dirent = readdir(dir)) != NULL )
-
 
550
    {
-
 
551
        int ii;
-
 
552
        if ( verbose > 2 )
-
 
553
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
-
 
554
        
-
 
555
        if ( strcmp( ".", dirent->d_name ) == 0 )
-
 
556
            continue;
-
 
557
 
-
 
558
        if ( strcmp( "..", dirent->d_name ) == 0 )
-
 
559
            continue;
-
 
560
 
-
 
561
        /*
-
 
562
        **  Compare against each item in the user list
-
 
563
        */
-
 
564
        for ( ii = 0; ii < argc ; ii++)
-
 
565
        {
-
 
566
 
-
 
567
            if ( wildcmp(dirent->d_name, argv[ii] )  )
-
 
568
            {
-
 
569
                if ( verbose > 1 )
-
 
570
                    fprintf(stderr, "Matching: %s, %s --- Found\n", dirent->d_name, argv[ii] );
-
 
571
                /*
-
 
572
                **  Matching file found
-
 
573
                */
-
 
574
                strcpy( dst, baseDir);
-
 
575
                strcat( dst, "/");
-
 
576
                strcat( dst, dirent->d_name);
-
 
577
 
-
 
578
                rv = stat( dst, &fstat );
-
 
579
                if ( rv == 0 )
-
 
580
                {
-
 
581
                    if ( verbose )
-
 
582
                        fprintf(stderr, "Delete file: %s : Attr: 0%o\n", dst, fstat.st_mode);
-
 
583
                    if ( !(fstat.st_mode & S_IWRITE) )
-
 
584
                    {
-
 
585
                        fstat.st_mode |= S_IWRITE;
-
 
586
                        rv = chmod( dst, fstat.st_mode );
-
 
587
                        if ( rv != 0 )
-
 
588
                        {
-
 
589
                            fprintf(stderr, "Warning: Attempt to allow write access: %s\n", dst);
-
 
590
                        }
-
 
591
                    }
-
 
592
 
-
 
593
                    if ( unlink(dst) )
-
 
594
                    {
-
 
595
                        fprintf(stderr, "Warning: Did not remove file: %s\n", dst);
-
 
596
                    }
-
 
597
                }
-
 
598
            }
-
 
599
        }
-
 
600
    }
-
 
601
    closedir(dir);
-
 
602
 
-
 
603
    /*
-
 
604
    **  Finally delete the diretory
-
 
605
    **      Unless its '.'
-
 
606
    */
-
 
607
    if ( strcmp( ".", baseDir) != 0 )
-
 
608
    {
-
 
609
        if ( verbose > 1 )
-
 
610
            fprintf(stderr, "Delete Directory: %s\n", baseDir);
-
 
611
        if ( rmdir (baseDir ) )
-
 
612
        {
-
 
613
            if ( verbose )
-
 
614
                fprintf(stderr, "Directory not deleted: %s\n", baseDir);
-
 
615
        }
-
 
616
    }
-
 
617
}
-
 
618
 
-
 
619
/*----------------------------------------------------------------------------
-
 
620
** FUNCTION           : wildcmp
-
 
621
**
-
 
622
** DESCRIPTION        : Wildcard comparision
-
 
623
**
-
 
624
**
-
 
625
** INPUTS             : string          - String
-
 
626
**                      wild            - Wildcard template
-
 
627
**
-
 
628
** RETURNS            : TRUE - Match
-
 
629
**
-
 
630
----------------------------------------------------------------------------*/
-
 
631
 
-
 
632
int wildcmp(char *string, char *wild )
-
 
633
{
-
 
634
    char *cp, *mp;
-
 
635
    while ((*string) && (*wild != '*'))
-
 
636
    {
-
 
637
        if ((*wild != *string) && (*wild != '?'))
-
 
638
        {
-
 
639
            return 0;
-
 
640
        }
-
 
641
         wild++;
-
 
642
         string++;
-
 
643
    }
-
 
644
 
-
 
645
    while (*string)
-
 
646
    {
-
 
647
        if (*wild == '*')
-
 
648
        {
-
 
649
            if (!*++wild)
-
 
650
            {
-
 
651
                return 1;
-
 
652
            }
-
 
653
            mp = wild;
-
 
654
            cp = string+1;
-
 
655
        }
-
 
656
        else if ((*wild == *string) || (*wild == '?'))
-
 
657
        {
-
 
658
            wild++;
-
 
659
            string++;
-
 
660
        }
-
 
661
        else
-
 
662
        {
-
 
663
            wild = mp;
-
 
664
            string = cp++;
-
 
665
        }
-
 
666
    }
-
 
667
 
-
 
668
    while (*wild == '*')
-
 
669
    {
-
 
670
        wild++;
-
 
671
    }
-
 
672
 
-
 
673
    return !*wild;
-
 
674
}
-
 
675
 
-
 
676
 
-
 
677
 
433
 
678
 
434
/*----------------------------------------------------------------------------
679
/*----------------------------------------------------------------------------
435
** FUNCTION           : ErrorExit
680
** FUNCTION           : ErrorExit
436
**
681
**
437
** DESCRIPTION        : Error processing
682
** DESCRIPTION        : Error processing