Subversion Repositories DevTools

Rev

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

Rev 2085 Rev 2310
Line 8... Line 8...
8
**
8
**
9
**  Description     :
9
**  Description     :
10
**                      The program mimics - but a lot faster a Jats Makefile fragment
10
**                      The program mimics - but a lot faster a Jats Makefile fragment
11
**                      The program will:
11
**                      The program will:
12
**
12
**
13
**                      Operate in two modes
13
**                      Operate in 5 modes
14
**                          CopyFile
14
**                          CopyFile
15
**                          DeleteFile
15
**                          DeleteFile
16
**                          Remove Files
16
**                          Remove Files (wildcard)
17
**                          DeleteDir after deleting specified files
17
**                          DeleteDir after deleting specified files (wildcard)
-
 
18
**                          Delete Directory Tree
-
 
19
**
-
 
20
**                          JatsFileUtil.exe c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
-
 
21
**                          JatsFileUtil.exe d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
-
 
22
**                          JatsFileUtil.exe r9 'deleteFile'  a1 b2 c3
-
 
23
**                          JatsFileUtil.exe D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
-
 
24
**                          JatsFileUtil.exe T9 'DeleteTree'  interface
-
 
25
**
-
 
26
**                      First two arguments are common to all
-
 
27
**                          argv[1]     - Mode specifier, Debug specifier
-
 
28
**                          argv[2]     - Display Text
18
**
29
**
19
**                      CopyFile (5 args)
30
**                      CopyFile (5 args)
20
**                          1) Display the ----- Text dest
31
**                          1) Display the ----- Text dest
21
**                          2) Error if the source is not found
32
**                          2) Error if the source is not found
22
**                          3) Create target path if not alraedy existing
33
**                          3) Create target path if not alraedy existing
Line 37... Line 48...
37
**                       DeleteDir (> 2 args)
48
**                       DeleteDir (> 2 args)
38
**                          1) Silenty delete a list of files. The files may
49
**                          1) Silenty delete a list of files. The files may
39
**                             be readonly so the attributes will need to be fixed.
50
**                             be readonly so the attributes will need to be fixed.
40
**                          2) Delete the directory if its empty
51
**                          2) Delete the directory if its empty
41
**
52
**
-
 
53
**                        Delete directory tree
-
 
54
**                          1) Silently delete an entire directory tree
-
 
55
**                             including files and subdiretories
-
 
56
**
42
**                      Make paths use '/'
57
**                      Make paths use '/'
43
**
58
**
44
**
59
**
45
**  Information     :
60
**  Information     :
46
**   Compiler       : ANSI C++
61
**   Compiler       : ANSI C++
Line 55... Line 70...
55
#include <unistd.h>
70
#include <unistd.h>
56
#include <stdlib.h>
71
#include <stdlib.h>
57
#include <dirent.h>
72
#include <dirent.h>
58
#include <string.h>
73
#include <string.h>
59
 
74
 
60
#define MAX_FILE 2000
-
 
61
 
-
 
62
void ErrorExit (char * lpszMessage, char * lpszMessage2);
75
void ErrorExit (char * lpszMessage, char * lpszMessage2);
63
void createPaths ( char *path );
76
void createPaths ( char *path );
64
int CopyFile ( char *src, char *dst, mode_t st_mode );
77
int CopyFile ( char *src, char *dst, mode_t st_mode );
65
void deleteFileList( int argc, char* argv[] );
-
 
66
void DeleteDir( int argc, char* argv[] );
78
void DeleteDir( int argc, char* argv[] );
-
 
79
void DeleteDirTree( int argc, char* argv[] );
67
int wildcmp(char *string, char *wild );
80
int wildcmp(char *string, char *wild );
-
 
81
char * makePath( char *base, char *path);
-
 
82
int DeleteOneFile (char * dst );
-
 
83
void DeleteOneDirectoryTree( char* baseDir );
-
 
84
void copyOneFile( int argc, char* argv[] );
68
 
85
 
69
/*
86
/*
70
**  Global
87
**  Global
71
*/
88
*/
72
char  verbose = 1;                          /* Debugging aid */
89
char  verbose = 1;                          /* Debugging aid */
73
char  copyMode = 0;                         /* Mode */
-
 
74
char  dst [MAX_FILE + 1];                   /* Scratch string workspace */
-
 
75
 
90
 
76
/*----------------------------------------------------------------------------
91
/*----------------------------------------------------------------------------
77
** FUNCTION           : main
92
** FUNCTION           : main
78
**
93
**
79
** DESCRIPTION        : Main entry points
94
** DESCRIPTION        : Main entry points
Line 90... Line 105...
90
**
105
**
91
----------------------------------------------------------------------------*/
106
----------------------------------------------------------------------------*/
92
 
107
 
93
int main(int argc, char* argv[])
108
int main(int argc, char* argv[])
94
{
109
{
95
 
-
 
96
	int rv;
-
 
97
    struct stat fstat;
-
 
98
    char *dst;
-
 
99
    char *src;
-
 
100
    mode_t src_mode;
-
 
101
 
-
 
102
    /*
110
    /*
103
    **  Examine the first argument
111
    **  Examine the first argument
104
    **  Must be a character string of
112
    **  Must be a character string of
105
    **      [0] - Mode : c or d or r or l
113
    **      [0] - Mode : c or d or r or l
106
    **      [1] - Verbose : 0 .. 9
114
    **      [1] - Verbose : 0 .. 9
107
    */
115
    */
108
    if ( argc > 1 )
116
    if ( argc > 1 && ( argv[1][1] >= '0' && argv[1][1] <= '9' ) )
109
    {
117
    {
110
        if ( argv[1][0] == 'c' ) {
-
 
111
            copyMode = 1;
-
 
112
        } else if ( argv[1][0] == 'd' ) {
-
 
113
            copyMode = 2;
-
 
114
        } else if ( argv[1][0] == 'r' ) {
-
 
115
            copyMode = 3;
-
 
116
        } else if ( argv[1][0] == 'D' ) {
-
 
117
            copyMode = 4;
-
 
118
        } else {
-
 
119
            ErrorExit("Unknown mode: ",argv[1]);
-
 
120
        }
-
 
121
 
-
 
122
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
-
 
123
            verbose = argv[1][1] - '0';
118
            verbose = argv[1][1] - '0';
124
        }
-
 
125
    }
119
    }
126
 
120
 
127
    /*
121
    /*
128
    **  If Verbose, then display arguments
122
    **  If Verbose, then display arguments
129
    */
123
    */
130
    if ( verbose > 1 )
124
    if ( verbose > 2 )
131
    {
125
    {
132
        int ii;
126
        int ii;
133
        for ( ii = 0; ii < argc ; ii++ )
127
        for ( ii = 0; ii < argc ; ii++ )
134
        {
128
        {
135
            fprintf(stderr, "Arg%d: %s:\n", ii, argv[ii] );
129
            fprintf(stderr, "Arg%d: %s:\n", ii, argv[ii] );
136
        }
130
        }
137
        fprintf(stderr, "Mode   : %d:\n", copyMode );
-
 
138
        fprintf(stderr, "Verbose: %d:\n", verbose );
-
 
139
        fflush(stderr) ;
131
        fflush(stderr) ;
140
    }
132
    }
141
 
133
 
142
    /*
134
    /*
143
    **  Determine required operation
135
    **  Switch to required operation
144
    */
136
    */
145
    if ( copyMode == 3 )
137
    switch ( argv[1][0]  )
146
    {
138
    {
147
        deleteFileList(argc - 2, argv + 2 );
-
 
148
        return 0;
139
        case 'c':
149
    }
-
 
150
 
-
 
151
    if ( copyMode == 4 )
-
 
152
    {
-
 
153
        DeleteDir(argc - 2, argv + 2 );
140
            copyOneFile(argc, argv);
154
        return 0;
141
            break;
155
    }
-
 
156
 
-
 
157
    /*
-
 
158
    **  Determine mode of operation
-
 
159
    **      Copy or Delete
-
 
160
    */
-
 
161
    if ( copyMode == 1 && argc == 6 ) {
-
 
162
        src = argv[4];
-
 
163
        dst = argv[3];
-
 
164
 
-
 
165
    } else if ( copyMode == 2 && argc == 4) {
-
 
166
        dst = argv[3];
-
 
167
 
-
 
168
    } else {
-
 
169
        fprintf(stderr, "Mode: %d, Args: %d\n", copyMode, argc);
-
 
170
        ErrorExit("Incorrect argument count for mode","");
-
 
171
    }
-
 
172
 
-
 
173
    /*
-
 
174
    **  Display user text
-
 
175
    */
-
 
176
    fprintf(stderr, "---- %s %s\n", argv[2], argv[3]);
-
 
177
    fflush(stderr) ;
-
 
178
 
-
 
179
    /*
-
 
180
    **   Check that the source is a file
-
 
181
    */
-
 
182
    if ( copyMode == 1 )
-
 
183
    {
-
 
184
        if ( verbose )
-
 
185
            fprintf(stderr, "Validate Source File: %s\n", src);
-
 
186
        
-
 
187
        rv = stat( src, &fstat );
-
 
188
        if ( rv != 0 )
-
 
189
        {
-
 
190
    /* Need to be a better message */
-
 
191
            fprintf(stderr, "Source: %s\n", src);
-
 
192
            ErrorExit("Source File not found: ", argv[4]);
-
 
193
        }
-
 
194
        src_mode = fstat.st_mode;
-
 
195
    }
-
 
196
 
142
 
197
    /*
-
 
198
    **  Remove the ReadOnly attribute on the dest file
-
 
199
    */
-
 
200
    if ( verbose )
-
 
201
        fprintf(stderr, "Remove target file: %s\n", dst);
-
 
202
    rv = stat( dst, &fstat );
-
 
203
    if ( rv == 0 )
-
 
204
    {
-
 
205
        if ( verbose )
-
 
206
            fprintf(stderr, "FileExists with attr: 0%o\n", fstat.st_mode);
-
 
207
        if ( !(fstat.st_mode & S_IWRITE) )
-
 
208
        {
143
        case 'd':
209
            fstat.st_mode |= S_IWRITE;
-
 
210
            rv = chmod( dst, fstat.st_mode );
-
 
211
            if ( rv != 0 )
-
 
212
            {
144
            {
-
 
145
                if ( argc != 4 )
213
                ErrorExit("Attempt to allow write access: ", argv[3]);
146
                    ErrorExit("Incorrect argument count for mode","");
214
            }
-
 
215
        }
-
 
216
 
147
 
217
        if ( unlink( dst ) )
148
                /*
218
        {
-
 
219
                ErrorExit("Deleting file: ", argv[3]);
149
                **  Display user text
220
        }
-
 
221
    }
-
 
222
    
-
 
223
    if ( copyMode == 1 )
-
 
224
    {
-
 
225
        /*
150
                */
226
        **  Create directories
-
 
227
        **  Use the path to the target - not the provided directory
151
                fprintf(stderr, "---- %s %s\n", argv[2], argv[3]);
228
        **  as the createPaths function will not create the last element
-
 
229
        */
-
 
230
        createPaths( dst );
152
                fflush(stderr) ;
231
 
-
 
232
        /*
-
 
233
        **   Copy the file
153
                DeleteOneFile(argv[3]);
234
        */
154
            }
235
        if ( ! CopyFile( src, dst, src_mode ) )
-
 
236
        {
-
 
237
            ErrorExit("Copy Error: ", argv[4]);
-
 
238
        }
155
            break;
239
 
156
 
240
        /*
-
 
241
        **  Test for files existence
-
 
242
        */
157
        case 'r':
243
        if ( verbose )
158
                {
244
            fprintf(stderr, "Test target was copied: %s\n", dst);
-
 
245
        rv = stat( dst, &fstat );
-
 
246
        if ( rv != 0 )
159
                    int ii;
247
        {
-
 
248
    /* Need to be a better message */
-
 
249
            ErrorExit("File not found after copy: ", argv[3]);
-
 
250
        }
-
 
251
 
160
 
252
        /*
-
 
253
        **  Set the files attributes
-
 
254
        **      Assume read-only
-
 
255
        */
-
 
256
        if ( strstr( argv[5], "-w" ) )
-
 
257
        {
-
 
258
            if ( verbose > 1 )
161
                    if ( argc < 4 )
259
                fprintf(stderr, "Set target read-only: %s\n", dst);
162
                        ErrorExit("Incorrect argument count for mode","");
260
            fstat.st_mode &= ~(S_IWRITE | S_IWOTH | S_IWGRP );
-
 
261
        }
-
 
262
 
163
 
-
 
164
                    /*
263
        if ( strstr( argv[5], "+w" ) )
165
                    **  Display user text
264
        {
166
                    */
-
 
167
                    if ( argv[2][0] )
265
            if ( verbose > 1 )
168
                    {
266
                fprintf(stderr, "Set target writable: %s\n", dst);
169
                        fprintf(stderr, "%s\n", argv[2]);
267
            fstat.st_mode |= (S_IWRITE | S_IWOTH | S_IWGRP );
170
                        fflush(stderr) ;
268
        }
171
                    }
269
 
172
 
270
        if ( strstr( argv[5], "+x" ) )
173
                    for ( ii = 3; ii < argc ; ii++ )
271
        {
174
                    {
272
            if ( verbose > 1 )
175
                        DeleteOneFile(argv[ii]);
273
                fprintf(stderr, "Set target executable: %s\n", dst);
176
                    }
274
            fstat.st_mode |= ( S_IXUSR | S_IXOTH | S_IXGRP );
177
                }
275
        }
178
            break;
276
 
179
 
277
        if ( strstr( argv[5], "-x" ) )
-
 
278
        {
-
 
279
            if ( verbose > 1)
-
 
280
                fprintf(stderr, "Set target executable: %s\n", dst);
-
 
281
            fstat.st_mode &= ~( S_IXUSR | S_IXOTH | S_IXGRP );
-
 
282
        }
180
        case 'D':
283
        
-
 
284
        if ( verbose )
-
 
285
            fprintf(stderr, "Set target perms: %s, 0%o\n", dst, fstat.st_mode);
-
 
286
        rv = chmod( dst, fstat.st_mode );
-
 
287
        if ( rv != 0 )
-
 
288
        {
-
 
289
            ErrorExit("Setting ReadOnly: ", argv[3]);
181
            DeleteDir(argc - 2, argv + 2 );
290
            }
182
            break;
291
        }
-
 
292
 
183
 
-
 
184
        case 'T':
-
 
185
            DeleteDirTree(argc - 2, argv + 2 );
-
 
186
            break;
-
 
187
            
-
 
188
        default :
-
 
189
            ErrorExit("Unknown mode: ",argv[1]);
-
 
190
            break;
-
 
191
    }
293
    return 0;
192
    return 0;
294
}
193
}
295
 
194
 
296
/*----------------------------------------------------------------------------
195
/*----------------------------------------------------------------------------
297
** FUNCTION           : createPaths
196
** FUNCTION           : createPaths
Line 314... Line 213...
314
    while ( *ptr )
213
    while ( *ptr )
315
    {
214
    {
316
        if ( *ptr == '/' )
215
        if ( *ptr == '/' )
317
        {
216
        {
318
            *ptr = 0;
217
            *ptr = 0;
319
 
-
 
-
 
218
/* fprintf(stderr, "createPaths: %s\n", path); */
320
            rv = stat( path, &fstat );
219
            rv = stat( path, &fstat );
321
            if ( rv )
220
            if ( rv )
322
            {
221
            {
323
                if ( verbose > 1 )
222
                if ( verbose > 1 )
324
                {
223
                {
Line 336... Line 235...
336
        ptr++;
235
        ptr++;
337
    }
236
    }
338
}
237
}
339
 
238
 
340
/*----------------------------------------------------------------------------
239
/*----------------------------------------------------------------------------
-
 
240
** FUNCTION           : copyOneFile
-
 
241
**
-
 
242
** DESCRIPTION        : Copy one file to a target
-
 
243
**                      Used to package and install files
-
 
244
**
-
 
245
**
-
 
246
** INPUTS             : argc            - Argc count
-
 
247
**                      argv            - Argument list
-
 
248
**                          argv[2]     - Display text Prefix
-
 
249
**                          argv[3]     - Target path
-
 
250
**                          argv[4]     - Source Path
-
 
251
**                          argv[5]     - File attributes
-
 
252
**
-
 
253
** RETURNS            :
-
 
254
**
-
 
255
----------------------------------------------------------------------------*/
-
 
256
 
-
 
257
void copyOneFile( int argc, char* argv[] )
-
 
258
{
-
 
259
	int    rv;
-
 
260
    char * src;
-
 
261
    char * dst;
-
 
262
    struct stat fstat;
-
 
263
    
-
 
264
    if ( argc != 6 )
-
 
265
        ErrorExit("Incorrect argument count for file copy","");
-
 
266
    
-
 
267
    /*
-
 
268
    **  Display user text
-
 
269
    */
-
 
270
    fprintf(stderr, "---- %s %s\n", argv[2], argv[3]);
-
 
271
    fflush(stderr) ;
-
 
272
 
-
 
273
    dst = argv[3];
-
 
274
    src = argv[4];
-
 
275
 
-
 
276
    /*
-
 
277
    **   Check that the source is a file
-
 
278
    */
-
 
279
    if ( verbose )
-
 
280
        fprintf(stderr, "Validate Source File: %s\n", src);
-
 
281
        
-
 
282
    rv = stat( src, &fstat );
-
 
283
    if ( rv != 0 )
-
 
284
    {
-
 
285
/* Need to be a better message */
-
 
286
        fprintf(stderr, "Source: %s\n", src);
-
 
287
        ErrorExit("Source File not found: ", argv[4]);
-
 
288
    }
-
 
289
 
-
 
290
    /*
-
 
291
    **  Remove the ReadOnly attribute on the dest file
-
 
292
    */
-
 
293
    DeleteOneFile(dst);
-
 
294
 
-
 
295
    /*
-
 
296
    **  Create directories
-
 
297
    **  Use the path to the target - not the provided directory
-
 
298
    **  as the createPaths function will not create the last element
-
 
299
    */
-
 
300
    createPaths( dst );
-
 
301
 
-
 
302
    /*
-
 
303
    **   Copy the file
-
 
304
    */
-
 
305
    if ( ! CopyFile( src, dst, fstat.st_mode ) )
-
 
306
    {
-
 
307
        ErrorExit("Copy Error: ", argv[4]);
-
 
308
    }
-
 
309
 
-
 
310
    /*
-
 
311
    **  Test for files existence
-
 
312
    */
-
 
313
    if ( verbose )
-
 
314
        fprintf(stderr, "Test target was copied: %s\n", dst);
-
 
315
 
-
 
316
    rv = stat( dst, &fstat );
-
 
317
    if ( rv != 0 )
-
 
318
    {
-
 
319
/* Need to be a better message */
-
 
320
        ErrorExit("File not found after copy: ", argv[3]);
-
 
321
    }
-
 
322
 
-
 
323
    /*
-
 
324
    **  Set the files attributes
-
 
325
    **      Assume read-only
-
 
326
    */
-
 
327
    if ( strstr( argv[5], "-w" ) )
-
 
328
    {
-
 
329
        if ( verbose > 1 )
-
 
330
            fprintf(stderr, "Set target read-only: %s\n", dst);
-
 
331
        fstat.st_mode &= ~(S_IWRITE | S_IWOTH | S_IWGRP );
-
 
332
    }
-
 
333
 
-
 
334
    if ( strstr( argv[5], "+w" ) )
-
 
335
    {
-
 
336
        if ( verbose > 1 )
-
 
337
            fprintf(stderr, "Set target writable: %s\n", dst);
-
 
338
        fstat.st_mode |= (S_IWRITE | S_IWOTH | S_IWGRP );
-
 
339
    }
-
 
340
 
-
 
341
    if ( strstr( argv[5], "+x" ) )
-
 
342
    {
-
 
343
        if ( verbose > 1 )
-
 
344
            fprintf(stderr, "Set target executable: %s\n", dst);
-
 
345
        fstat.st_mode |= ( S_IXUSR | S_IXOTH | S_IXGRP );
-
 
346
    }
-
 
347
 
-
 
348
    if ( strstr( argv[5], "-x" ) )
-
 
349
    {
-
 
350
        if ( verbose > 1)
-
 
351
            fprintf(stderr, "Set target executable: %s\n", dst);
-
 
352
        fstat.st_mode &= ~( S_IXUSR | S_IXOTH | S_IXGRP );
-
 
353
    }
-
 
354
        
-
 
355
    if ( verbose )
-
 
356
        fprintf(stderr, "Set target perms: %s, 0%o\n", dst, fstat.st_mode);
-
 
357
    rv = chmod( dst, fstat.st_mode );
-
 
358
    if ( rv != 0 )
-
 
359
    {
-
 
360
        ErrorExit("Setting ReadOnly: ", argv[3]);
-
 
361
    }
-
 
362
}
-
 
363
 
-
 
364
/*----------------------------------------------------------------------------
341
** FUNCTION           : CopyFile
365
** FUNCTION           : CopyFile
342
**
366
**
343
** DESCRIPTION        : Just copy a file
367
** DESCRIPTION        : Just copy a file
344
**
368
**
345
**
369
**
Line 400... Line 424...
400
    }
424
    }
401
    return 1;
425
    return 1;
402
}
426
}
403
 
427
 
404
/*----------------------------------------------------------------------------
428
/*----------------------------------------------------------------------------
405
** FUNCTION           : deleteFileList
-
 
406
**
-
 
407
** DESCRIPTION        : Delete a list of files
-
 
408
**                      Ensure files are writable
-
 
409
**
-
 
410
**
-
 
411
** INPUTS             : argc    - count of args
-
 
412
**                      argv    - list of files to delete
-
 
413
**
-
 
414
** RETURNS            : Wil not return on error
-
 
415
**
-
 
416
----------------------------------------------------------------------------*/
-
 
417
 
-
 
418
void deleteFileList( int argc, char* argv[] )
-
 
419
{
-
 
420
	int rv;
-
 
421
    struct stat fstat;
-
 
422
 
-
 
423
    for ( ; argc ; argc--, argv++ )
-
 
424
    {
-
 
425
        /*
-
 
426
        **  Remove the ReadOnly attribute on the dest file
-
 
427
        */
-
 
428
        if ( verbose > 1 )
-
 
429
            fprintf(stderr, "Remove target file: %s\n", *argv);
-
 
430
 
-
 
431
 
-
 
432
        rv = stat( *argv, &fstat );
-
 
433
        if ( rv == 0 )
-
 
434
        {
-
 
435
            if ( verbose )
-
 
436
                fprintf(stderr, "Delete file: %s : Attr: 0%o\n", *argv, fstat.st_mode);
-
 
437
            if ( !(fstat.st_mode & S_IWRITE) )
-
 
438
            {
-
 
439
                fstat.st_mode |= S_IWRITE;
-
 
440
                rv = chmod( *argv, fstat.st_mode );
-
 
441
                if ( rv != 0 )
-
 
442
                {
-
 
443
                    fprintf(stderr, "Warning: Attempt to allow write access: %s\n", *argv);
-
 
444
                }
-
 
445
            }
-
 
446
 
-
 
447
            if ( unlink( *argv ) )
-
 
448
            {
-
 
449
                fprintf(stderr, "Warning: Did not remove file: %s\n", *argv);
-
 
450
            }
-
 
451
        }
-
 
452
    }
-
 
453
}
-
 
454
 
-
 
455
/*----------------------------------------------------------------------------
-
 
456
** FUNCTION           : DeleteDir
429
** FUNCTION           : DeleteDir
457
**
430
**
458
** DESCRIPTION        : Delete a list of files in a specified directory
431
** DESCRIPTION        : Delete a list of files in a specified directory
459
**                      Ensure files are writable
432
**                      Ensure files are writable
460
**                      Wilcarding is supported
433
**                      Wilcarding is supported
Line 464... Line 437...
464
**
437
**
465
** INPUTS             : argc    - count of args
438
** INPUTS             : argc    - count of args
466
**                      argv    - list of files to delete
439
**                      argv    - list of files to delete
467
**                                [0]:  Text to display
440
**                                [0]:  Text to display
468
**                                [1]:  Base directory
441
**                                [1]:  Base directory
469
**                                [2]+  File in dir to delete
442
**                                [2]+  Files in dir to delete
470
**
443
**
471
** RETURNS            : Wil not return on error
444
** RETURNS            : Will not return on error
472
**
445
**
473
----------------------------------------------------------------------------*/
446
----------------------------------------------------------------------------*/
474
 
447
 
475
void DeleteDir( int argc, char* argv[] )
448
void DeleteDir( int argc, char* argv[] )
476
{
449
{
477
	int rv;
450
	int rv;
478
    struct stat fstat;
451
    struct stat fstat;
479
    char* baseDir;
452
    char* baseDir;
480
    DIR *dir;
453
    DIR *dir;
481
    struct dirent *dirent;
454
    struct dirent *dirent;
-
 
455
    char *target;
482
 
456
 
483
    if ( argc < 3 )
457
    if ( argc < 3 )
484
        ErrorExit("Insuffiecient arguments for DeleteDir","");
458
        ErrorExit("Insuffiecient arguments for DeleteDir","");
485
        
459
 
486
    /*
460
    /*
487
    **  Display the user message
461
    **  Display the user message
488
    **      Supress display if the message is empty
462
    **      Supress display if the message is empty
489
    */
463
    */
490
    if ( argv[0][0] )
464
    if ( argv[0][0] )
Line 566... Line 540...
566
 
540
 
567
            if ( wildcmp(dirent->d_name, argv[ii] )  )
541
            if ( wildcmp(dirent->d_name, argv[ii] )  )
568
            {
542
            {
569
                if ( verbose > 1 )
543
                if ( verbose > 1 )
570
                    fprintf(stderr, "Matching: %s, %s --- Found\n", dirent->d_name, argv[ii] );
544
                    fprintf(stderr, "Matching: %s, %s --- Found\n", dirent->d_name, argv[ii] );
-
 
545
                
571
                /*
546
                /*
572
                **  Matching file found
547
                **  Matching file found
573
                */
548
                */
574
                strcpy( dst, baseDir);
-
 
575
                strcat( dst, "/");
-
 
576
                strcat( dst, dirent->d_name);
549
                target = makePath( baseDir, dirent->d_name);
577
 
-
 
578
                rv = stat( dst, &fstat );
550
                DeleteOneFile(target);
579
                if ( rv == 0 )
551
                free(target);
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
            }
552
            }
599
        }
553
        }
600
    }
554
    }
601
    closedir(dir);
555
    closedir(dir);
602
 
556
 
Line 606... Line 560...
606
    */
560
    */
607
    if ( strcmp( ".", baseDir) != 0 )
561
    if ( strcmp( ".", baseDir) != 0 )
608
    {
562
    {
609
        if ( verbose > 1 )
563
        if ( verbose > 1 )
610
            fprintf(stderr, "Delete Directory: %s\n", baseDir);
564
            fprintf(stderr, "Delete Directory: %s\n", baseDir);
-
 
565
        
611
        if ( rmdir (baseDir ) )
566
        if ( rmdir (baseDir ) )
612
        {
567
        {
613
            if ( verbose )
568
            if ( verbose )
614
                fprintf(stderr, "Directory not deleted: %s\n", baseDir);
569
                fprintf(stderr, "Directory not deleted: %s\n", baseDir);
615
        }
570
        }
616
    }
571
    }
617
}
572
}
618
 
573
 
619
/*----------------------------------------------------------------------------
574
/*----------------------------------------------------------------------------
-
 
575
** FUNCTION           : DeleteDirTree
-
 
576
**
-
 
577
** DESCRIPTION        : Delete an entire directory tree(s)
-
 
578
**
-
 
579
** INPUTS             : argc    - count of args
-
 
580
**                      argv    - list of files to delete
-
 
581
**                                [0]:  Text to display
-
 
582
**                                [1]+  Base directory
-
 
583
**
-
 
584
** RETURNS            : Will not return on error
-
 
585
**
-
 
586
----------------------------------------------------------------------------*/
-
 
587
 
-
 
588
void DeleteDirTree( int argc, char* argv[] )
-
 
589
{
-
 
590
    int ii;
-
 
591
 
-
 
592
    if ( argc < 2 )
-
 
593
        ErrorExit("Insuffiecient arguments for DeleteDir","");
-
 
594
        
-
 
595
    /*
-
 
596
    **  Display the user message
-
 
597
    **      Supress display if the message is empty
-
 
598
    */
-
 
599
    if ( argv[0][0] )
-
 
600
    {
-
 
601
        fprintf(stderr, "%s\n", argv[0]);
-
 
602
        fflush(stderr) ;
-
 
603
    }
-
 
604
 
-
 
605
    for ( ii = 1; ii < argc ; ii++)
-
 
606
    {
-
 
607
        DeleteOneDirectoryTree( argv[ii] );
-
 
608
    }
-
 
609
}
-
 
610
 
-
 
611
/*----------------------------------------------------------------------------
-
 
612
** FUNCTION           : DeleteOneDirectoryTree
-
 
613
**
-
 
614
** DESCRIPTION        : Delete an entire directory tree(s)
-
 
615
**
-
 
616
** INPUTS             : path    - Dir to delete
-
 
617
**
-
 
618
** RETURNS            : Will not return on error
-
 
619
**
-
 
620
----------------------------------------------------------------------------*/
-
 
621
 
-
 
622
void DeleteOneDirectoryTree( char* baseDir )
-
 
623
{
-
 
624
    struct stat fstat;
-
 
625
    DIR *dir;
-
 
626
    struct dirent *dirent;
-
 
627
    char *target;
-
 
628
    int rv;
-
 
629
 
-
 
630
    /*
-
 
631
    **  A bit of a sanity test
-
 
632
    */
-
 
633
    if ( strcmp( ".", baseDir) == 0 || strcmp( "..", baseDir) == 0 )
-
 
634
    {
-
 
635
        fprintf(stderr, "DeleteOneDirectoryTree will not delete '.' or '..'\n");
-
 
636
        return;
-
 
637
    }
-
 
638
 
-
 
639
    /*
-
 
640
    **  Process all entries
-
 
641
    */
-
 
642
    dir = opendir( baseDir );
-
 
643
    if ( dir == NULL )
-
 
644
    {
-
 
645
        /*
-
 
646
        **  Target is not a directory
-
 
647
        **  Don't do anything
-
 
648
        */
-
 
649
        if ( verbose > 1 )
-
 
650
            fprintf(stderr, "Base dir is not a directory: %s\n", baseDir);
-
 
651
        return;
-
 
652
    }
-
 
653
 
-
 
654
    /*
-
 
655
    **  Read next directory entry
-
 
656
    */
-
 
657
    while ( (dirent = readdir(dir)) != NULL )
-
 
658
    {
-
 
659
        if ( verbose > 2 )
-
 
660
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
-
 
661
        
-
 
662
        if ( strcmp( ".", dirent->d_name ) == 0 )
-
 
663
            continue;
-
 
664
 
-
 
665
        if ( strcmp( "..", dirent->d_name ) == 0 )
-
 
666
            continue;
-
 
667
 
-
 
668
        target = makePath( baseDir, dirent->d_name);
-
 
669
        rv = stat( target, &fstat );
-
 
670
        if ( rv == 0 )
-
 
671
        {
-
 
672
            if ( fstat.st_mode & S_IFDIR )
-
 
673
            {
-
 
674
                DeleteOneDirectoryTree( target );
-
 
675
            }
-
 
676
            else
-
 
677
            {
-
 
678
                DeleteOneFile (target);
-
 
679
            }
-
 
680
        }
-
 
681
        free(target);
-
 
682
    }
-
 
683
    closedir(dir);
-
 
684
 
-
 
685
    /*
-
 
686
    **  Finally delete the directory
-
 
687
    */
-
 
688
    if ( verbose )
-
 
689
        fprintf(stderr, "Delete Directory: %s\n", baseDir);
-
 
690
 
-
 
691
    if ( rmdir (baseDir ) )
-
 
692
    {
-
 
693
        if ( verbose )
-
 
694
            fprintf(stderr, "Directory not deleted: %s\n", baseDir);
-
 
695
    }
-
 
696
}
-
 
697
 
-
 
698
/*----------------------------------------------------------------------------
-
 
699
** FUNCTION           : DeleteOneFile
-
 
700
**
-
 
701
** DESCRIPTION        : Delete a file
-
 
702
**                      Force it writable before deletion
-
 
703
**
-
 
704
** INPUTS             : path        - path to the file
-
 
705
**
-
 
706
** RETURNS            : 0           - OK (file deleted or not present)
-
 
707
**
-
 
708
----------------------------------------------------------------------------*/
-
 
709
 
-
 
710
int DeleteOneFile (char * dst )
-
 
711
{
-
 
712
	int rv;
-
 
713
    struct stat fstat;
-
 
714
    int result = 0;
-
 
715
 
-
 
716
    if ( verbose > 1)
-
 
717
        fprintf(stderr, "Delete File: %s\n", dst);
-
 
718
 
-
 
719
    rv = stat( dst, &fstat );
-
 
720
    if ( rv == 0 )
-
 
721
    {
-
 
722
        if ( verbose )
-
 
723
            fprintf(stderr, "Delete file: %s : Attr: 0%o\n", dst, fstat.st_mode);
-
 
724
        if ( !(fstat.st_mode & S_IWRITE) )
-
 
725
        {
-
 
726
            fstat.st_mode |= S_IWRITE;
-
 
727
            rv = chmod( dst, fstat.st_mode );
-
 
728
            if ( rv != 0 )
-
 
729
            {
-
 
730
                fprintf(stderr, "Warning: Attempt to allow write access: %s\n", dst);
-
 
731
            }
-
 
732
        }
-
 
733
 
-
 
734
        if ( unlink(dst) )
-
 
735
        {
-
 
736
            fprintf(stderr, "Warning: Did not remove file: %s\n", dst);
-
 
737
            result = 1;
-
 
738
        }
-
 
739
    }
-
 
740
    return result;
-
 
741
}
-
 
742
 
-
 
743
/*----------------------------------------------------------------------------
-
 
744
** FUNCTION           : makePath
-
 
745
**
-
 
746
** DESCRIPTION        : Create a path from two elements
-
 
747
**                      Allocate memory
-
 
748
**
-
 
749
**
-
 
750
** INPUTS             : base        - Part 1
-
 
751
**                      file        - Part 2
-
 
752
**
-
 
753
** RETURNS            :
-
 
754
**
-
 
755
----------------------------------------------------------------------------*/
-
 
756
 
-
 
757
char * makePath( char *base, char *path)
-
 
758
{
-
 
759
    int len1 = strlen(base);
-
 
760
    int len2 = strlen(path);
-
 
761
    char *data;
-
 
762
 
-
 
763
    data = (char *)malloc(len1 + len2 + 10);
-
 
764
    if ( data == NULL )
-
 
765
    {
-
 
766
        ErrorExit ("Malloc error:makePath","");
-
 
767
    }
-
 
768
 
-
 
769
    strcpy( data,base);
-
 
770
    strcpy( data + len1, "/");
-
 
771
    strcpy( data + len1 + 1, path);
-
 
772
 
-
 
773
    return data;
-
 
774
}
-
 
775
 
-
 
776
/*----------------------------------------------------------------------------
620
** FUNCTION           : wildcmp
777
** FUNCTION           : wildcmp
621
**
778
**
622
** DESCRIPTION        : Wildcard comparision
779
** DESCRIPTION        : Wildcard comparision
623
**
780
**
624
**
781
**