]> err.no Git - sope/blob - sope-core/samples/fmdls.m
fixed NGObjWeb for SOPE 3.3
[sope] / sope-core / samples / fmdls.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #import <Foundation/Foundation.h>
24 #import <EOControl/EOControl.h>
25 #include <NGExtensions/NSFileManager+Extensions.h>
26 #include <NGExtensions/NGFileFolderInfoDataSource.h>
27 #include <NGExtensions/NGExtensions.h>
28
29 int main(int argc, char **argv, char **env) {
30   NSAutoreleasePool *pool;
31   NSArray       *args;
32   NSFileManager *fm;
33   int           i;
34   
35 #if LIB_FOUNDATION_LIBRARY
36   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
37 #endif
38   pool = [[NSAutoreleasePool alloc] init];
39   
40   args = [[NSProcessInfo processInfo] arguments];
41   if ([args count] < 1) {
42     NSLog(@"usage: %@ <files>", [args objectAtIndex:0]);
43     exit(1);
44   }
45   else if ([args count] == 1)
46     args = [args arrayByAddingObject:@"."];
47
48   fm = [NSFileManager defaultManager];
49
50   for (i = 1; i < [args count]; i++) {
51     NSString *path;
52     BOOL     isDir;
53     
54     path = [args objectAtIndex:i];
55
56     if ([path hasPrefix:@"-"]) {
57       i++;
58       continue;
59     }
60     
61     if (![fm fileExistsAtPath:path isDirectory:&isDir]) {
62       NSLog(@"file/directory does not exist: %@", path);
63       continue;
64     }
65     
66     if (isDir) {
67       EODataSource *ds;
68       NSEnumerator *records;
69       NSDictionary *record;
70       NSArray      *sortOrderings;
71       EOQualifier  *qualifier;
72       id tmp;
73       
74       if ((ds = [fm dataSourceAtPath:path]) == nil) {
75         NSLog(@"could not get datasource for path: '%@'", path);
76         continue;
77       }
78
79       /* build fetch specification */
80       
81       tmp = [[NSUserDefaults standardUserDefaults] stringForKey:@"qualifier"];
82       if ([tmp length] > 0) {
83         qualifier = [EOQualifier qualifierWithQualifierFormat:tmp];
84         if (qualifier == nil)
85           NSLog(@"could not parse qualifier: %@", tmp);
86       }
87       else
88         qualifier = nil;
89
90       tmp = [EOSortOrdering sortOrderingWithKey:@"NSFileName"
91                             selector:EOCompareAscending];
92       sortOrderings = [NSArray arrayWithObject:tmp];
93       
94       if ((qualifier != nil) || (sortOrderings != nil)) {
95         EOFetchSpecification *fs;
96         
97         fs = [[EOFetchSpecification alloc] init];
98         [fs setQualifier:qualifier];
99         [fs setSortOrderings:sortOrderings];
100
101         [(id)ds setFetchSpecification:fs];
102         [fs release]; fs = nil;
103       }
104       
105       /* perform fetch */
106       
107       records = [[ds fetchObjects] objectEnumerator];
108       
109       /* print out */
110
111       while ((record = [records nextObject])) {
112         /* id uid gid date name */
113         NSString *fileId;
114         NSString *owner;
115         int      gid;
116         unsigned size;
117         NSString *modDate;
118         NSString *fname;
119         NSString *ftype;
120         
121         fileId  = [[record objectForKey:@"NSFileIdentifier"] description];
122         owner   = [record  objectForKey:NSFileOwnerAccountName];
123         gid     = [[record objectForKey:@"NSFileGroupOwnerAccountNumber"] intValue];
124         size    = [[record  objectForKey:NSFileSize] intValue];
125         modDate = [[record objectForKey:NSFileModificationDate] description];
126         fname   = [record  objectForKey:@"NSFileName"];
127         ftype   = [record  objectForKey:NSFileType];
128
129         if ([ftype isEqualToString:NSFileTypeDirectory])
130           fname = [fname stringByAppendingString:@"/"];
131         else if ([ftype isEqualToString:NSFileTypeSocket])
132           fname = [fname stringByAppendingString:@"="];
133         else if ([ftype isEqualToString:NSFileTypeSymbolicLink])
134           fname = [fname stringByAppendingString:@"@"];
135         
136         //NSLog(@"record: %@", record);
137         
138         printf("%8s  %8s  %8i  %8i  %8s  %s\n",
139                [fileId cString],
140                [owner  cString],
141                gid,
142                size,
143                [modDate cString],
144                [fname cString]);
145       }
146     }
147     else {
148       /* a file */
149     }
150   }
151   [pool release];
152   
153   exit(0);
154   return 0;
155 }