]> err.no Git - sope/blob - sope-ldap/samples/ldapls.m
added key/value archiving to NSExpression
[sope] / sope-ldap / samples / ldapls.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 #include <EOControl/EOControl.h>
23 #include <NGLdap/NGLdapFileManager.h>
24 #include "common.h"
25
26 int main(int argc, char **argv, char **env) {
27   NSAutoreleasePool *pool;
28   NSUserDefaults *ud;
29   NSArray        *args;
30   NSFileManager  *fm;
31   unsigned i;
32   BOOL     doDeep = NO;
33
34   pool = [[NSAutoreleasePool alloc] init];
35 #if LIB_FOUNDATION_LIBRARY
36   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
37 #endif
38
39   args = [[NSProcessInfo processInfo] arguments];
40   if ([args count] < 1) {
41     NSLog(@"usage: %@ <files>", [args objectAtIndex:0]);
42     exit(1);
43   }
44   else if ([args count] == 1)
45     args = [args arrayByAddingObject:@"."];
46   
47   ud = [NSUserDefaults standardUserDefaults];
48
49   fm = [[NGLdapFileManager alloc]
50                            initWithHostName:[ud stringForKey:@"LDAPHost"]
51                            port:0
52                            bindDN:[ud stringForKey:@"LDAPBindDN"]
53                            credentials:[ud stringForKey:@"LDAPPassword"]
54                            rootDN:[ud stringForKey:@"LDAPRootDN"]];
55   fm = [fm autorelease];
56   
57   if (fm == nil) {
58     NSLog(@"could not open LDAP connection (got no filemanager).");
59     exit(2);
60   }
61   
62   // NSLog(@"LDAP: %@", fm);
63   
64   for (i = 1; i < [args count]; i++) {
65     NSString *path;
66     BOOL     isDir;
67     
68     path = [args objectAtIndex:i];
69
70     if ([path hasPrefix:@"-r"]) {
71       doDeep = YES;
72       continue;
73     }
74     
75     if ([path hasPrefix:@"-"]) {
76       i++;
77       continue;
78     }
79     
80     if (![fm fileExistsAtPath:path isDirectory:&isDir]) {
81       NSLog(@"file/directory does not exist: %@", path);
82       continue;
83     }
84     
85     if (isDir) {
86       NSArray  *dirContents;
87       unsigned i, count;
88       NSString *mid;
89       
90       dirContents = doDeep
91         ? [fm subpathsAtPath:path]
92         : [fm directoryContentsAtPath:path];
93       
94       for (i = 0, count = [dirContents count]; i < count; i++) {
95         NSString     *cpath, *apath;
96         NSDictionary *info;
97         NSString     *owner;
98         NSString     *date;
99         
100         cpath = [dirContents objectAtIndex:i];
101         apath = [path stringByAppendingPathComponent:cpath];
102         
103         info = [fm fileAttributesAtPath:apath
104                    traverseLink:NO];
105         
106         mid = [[info objectForKey:@"NSFileIdentifier"] description];
107         if ([mid length] > 39) {
108           mid = [mid substringToIndex:37];
109           mid = [mid stringByAppendingString:@"..."];
110         }
111
112         owner = [info objectForKey:NSFileOwnerAccountName];
113         date  = [[info objectForKey:NSFileModificationDate] description];
114
115         if (owner == nil)
116           owner = @"-";
117         if (date == nil)
118           date = @"-";
119         
120         /* id uid date name */
121         printf("%-34s  %20s  %-32s %s",
122                [mid   cString],
123                [owner cString],
124                [date  cString],
125                [apath cString]);
126
127         if ([[info objectForKey:NSFileType]
128                    isEqualToString:NSFileTypeDirectory])
129           printf("/\n");
130         else
131           printf("\n");
132       }
133     }
134     else {
135       /* a file */
136       NSData   *contents;
137       NSString *s;
138       
139       if ((contents = [fm contentsAtPath:path]) == nil) {
140         NSLog(@"could not get content of record: '%@'", path);
141       }
142       else {
143         s = [[NSString alloc] initWithData:contents
144                               encoding:[NSString defaultCStringEncoding]];
145         printf("%s\n", [s cString]);
146         [s release];
147       }
148     }
149   }
150
151   [pool release];
152   
153   exit(0);
154   return 0;
155 }