]> err.no Git - sope/blob - sope-core/samples/fmdls.m
added OSX framework support
[sope] / sope-core / samples / fmdls.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import <Foundation/Foundation.h>
23 #import <EOControl/EOControl.h>
24 #include <NGExtensions/NSFileManager+Extensions.h>
25 #include <NGExtensions/NGFileFolderInfoDataSource.h>
26 #include <NGExtensions/NGExtensions.h>
27
28 static void printDirRecord(NSString *path, NSDictionary *record) {
29   /* id uid gid date name */
30   NSString *fileId;
31   NSString *owner;
32   int      gid;
33   unsigned size;
34   NSString *modDate;
35   NSString *fname;
36   NSString *ftype;
37         
38   fileId  = [[record objectForKey:@"NSFileIdentifier"] description];
39   owner   = [record  objectForKey:NSFileOwnerAccountName];
40   gid     = [[record objectForKey:@"NSFileGroupOwnerAccountNumber"] intValue];
41   size    = [[record  objectForKey:NSFileSize] intValue];
42   modDate = [[record objectForKey:NSFileModificationDate] description];
43   fname   = [record  objectForKey:@"NSFileName"];
44   ftype   = [record  objectForKey:NSFileType];
45
46   if ([ftype isEqualToString:NSFileTypeDirectory])
47     fname = [fname stringByAppendingString:@"/"];
48   else if ([ftype isEqualToString:NSFileTypeSocket])
49     fname = [fname stringByAppendingString:@"="];
50   else if ([ftype isEqualToString:NSFileTypeSymbolicLink])
51     fname = [fname stringByAppendingString:@"@"];
52         
53   //NSLog(@"record: %@", record);
54         
55   printf("%8s  %8s  %8i  %8i  %8s  %s\n",
56          [fileId cString],
57          [owner  cString],
58          gid,
59          size,
60          [modDate cString],
61          [fname cString]);
62 }
63
64 static void runOnDirPath(NSString *path) {
65   NSFileManager *fm;
66   EODataSource *ds;
67   NSEnumerator *records;
68   NSDictionary *record;
69   NSArray      *sortOrderings;
70   EOQualifier  *qualifier;
71   id tmp;
72   
73   fm = [NSFileManager defaultManager];
74
75   if ((ds = [fm dataSourceAtPath:path]) == nil) {
76     NSLog(@"could not get datasource for path: '%@'", path);
77     return;
78   }
79
80   /* build fetch specification */
81       
82   tmp = [[NSUserDefaults standardUserDefaults] stringForKey:@"qualifier"];
83   if ([tmp length] > 0) {
84     qualifier = [EOQualifier qualifierWithQualifierFormat:tmp];
85     if (qualifier == nil)
86       NSLog(@"could not parse qualifier: %@", tmp);
87   }
88   else
89     qualifier = nil;
90
91   tmp = [EOSortOrdering sortOrderingWithKey:@"NSFileName"
92                         selector:EOCompareAscending];
93   sortOrderings = [NSArray arrayWithObject:tmp];
94       
95   if ((qualifier != nil) || (sortOrderings != nil)) {
96     EOFetchSpecification *fs;
97         
98     fs = [[EOFetchSpecification alloc] init];
99     [fs setQualifier:qualifier];
100     [fs setSortOrderings:sortOrderings];
101
102     [(id)ds setFetchSpecification:fs];
103     [fs release]; fs = nil;
104   }
105       
106   /* perform fetch */
107       
108   records = [[ds fetchObjects] objectEnumerator];
109       
110   /* print out */
111
112   while ((record = [records nextObject]))
113     printDirRecord(path, record);
114 }
115
116 static void runOnPath(NSString *path) {
117   NSFileManager *fm;
118   BOOL     isDir;
119
120   fm = [NSFileManager defaultManager];
121   
122   if (![fm fileExistsAtPath:path isDirectory:&isDir]) {
123     NSLog(@"file/directory does not exist: %@", path);
124     return;
125   }
126   
127   if (isDir)
128     runOnDirPath(path);
129   else
130     /* a file */;
131 }
132
133 static void runit(NSArray *args) {
134   int i;
135   
136   for (i = 1; i < [args count]; i++) {
137     NSString *path;
138     
139     path = [args objectAtIndex:i];
140     
141     if ([path hasPrefix:@"-"]) { // TODO: there is a NSProcessInfo ext for that
142       i++;
143       continue;
144     }
145     
146     runOnPath(path);
147   }
148 }
149
150 int main(int argc, char **argv, char **env) {
151   NSAutoreleasePool *pool;
152   NSArray       *args;
153   
154 #if LIB_FOUNDATION_LIBRARY
155   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
156 #endif
157   pool = [[NSAutoreleasePool alloc] init];
158   
159   args = [[NSProcessInfo processInfo] arguments];
160   if ([args count] < 1) {
161     NSLog(@"usage: %@ <files>", [args objectAtIndex:0]);
162     exit(1);
163   }
164   else if ([args count] == 1)
165     args = [args arrayByAddingObject:@"."];
166
167   runit(args);
168   [pool release];
169   
170   exit(0);
171   return 0;
172 }