]> err.no Git - sope/blob - libFoundation/examples/fmls.m
tweaked layout
[sope] / libFoundation / examples / fmls.m
1 /*
2    fmls.m
3
4    Copyright (C) 2000 Helge Hess.
5    All rights reserved.
6
7    Author: Helge Hess <helge.hess@mdlink.de>
8    Date:   April 2000
9
10    This file is part of libFoundation.
11
12    Permission to use, copy, modify, and distribute this software and its
13    documentation for any purpose and without fee is hereby granted, provided
14    that the above copyright notice appear in all copies and that both that
15    copyright notice and this permission notice appear in supporting
16    documentation.
17
18    We disclaim all warranties with regard to this software, including all
19    implied warranties of merchantability and fitness, in no event shall
20    we be liable for any special, indirect or consequential damages or any
21    damages whatsoever resulting from loss of use, data or profits, whether in
22    an action of contract, negligence or other tortious action, arising out of
23    or in connection with the use or performance of this software.
24 */
25
26 /*
27   This is an example for using the NSFileManager class to remove
28   files in the filesystem.
29 */
30
31 #include <Foundation/Foundation.h>
32
33 int main(int argc, char **argv, char **env) {
34   NSArray       *args;
35   NSFileManager *fm;
36   BOOL          ok;
37   int           i;
38   
39 #if LIB_FOUNDATION_LIBRARY
40   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
41 #endif
42   
43   args = [[NSProcessInfo processInfo] arguments];
44   if ([args count] < 1) {
45     NSLog(@"usage: %@ <files>", [args objectAtIndex:0]);
46     exit(1);
47   }
48   else if ([args count] == 1)
49     args = [args arrayByAddingObject:@"."];
50
51   fm = [NSFileManager defaultManager];
52   
53   for (i = 1; i < [args count]; i++) {
54     NSString *path = [args objectAtIndex:i];
55     BOOL     isDir;
56
57     path = [args objectAtIndex:i];
58     
59     if (![fm fileExistsAtPath:path isDirectory:&isDir]) {
60       NSLog(@"file/directory does not exist: %@", path);
61       continue;
62     }
63     
64     if (isDir) {
65       NSEnumerator *contents;
66       NSString *p;
67       
68       contents = [[[fm directoryContentsAtPath:path]
69                        sortedArrayUsingSelector:@selector(compare:)]
70                        objectEnumerator];
71       
72       while ((p = [contents nextObject])) {
73         NSDictionary *info;
74         NSString *fp;
75
76         fp = [path stringByAppendingPathComponent:p];
77         info = [fm fileAttributesAtPath:fp traverseLink:YES];
78
79         if (info == nil) {
80           NSLog(@"couldn't get info of entry: %@", fp);
81           continue;
82         }
83
84         /* id uid gid date name */
85         printf("%8s  %8s  %8s  %8i  %8s  %s\n",
86                [[[info objectForKey:NSFileIdentifier] description] cString],
87                [[info objectForKey:NSFileOwnerAccountName]      cString],
88                [[info objectForKey:NSFileGroupOwnerAccountName] cString],
89                [[info objectForKey:NSFileSize] intValue],
90                [[[info objectForKey:NSFileModificationDate] description] cString],
91                [p cString]);
92       }
93     }
94     else {
95     }
96   }
97   
98   exit(0);
99   return 0;
100 }