20 int main(int argc, char *argv[]);
21 void ShowUsage(char *ProgName);
22 void ParseArguments(int argc, char *argv[]);
24 void DisplayFile(char *Filename);
29 int gbShowImplicit = 0;
30 int gbViewExtended = 0;
31 int gbViewHumanReadable = 0;
33 char *gsDirectory = NULL;
34 // --- Working Set ---
39 * \fn int main(int argc, char *argv[])
42 int main(int argc, char *argv[])
51 ParseArguments(argc, argv);
54 fd = open(gsDirectory, OPENFLAG_READ|OPENFLAG_EXEC);
56 printf("Unable to open '%s' for reading\n", gsDirectory);
60 // Check that it is a directory
62 if( !(info.flags & FILEFLAG_DIRECTORY) ) {
63 fprintf(stderr, "'%s' is a directory\n", gsDirectory);
68 // Check if we should include the implicit . and ..
72 gFileList = malloc(space*sizeof(char*));
73 gFileList[giNumFiles++] = ".";
74 gFileList[giNumFiles++] = "..";
78 while( (tmp = readdir(fd, buf)) )
87 if(space == giNumFiles)
90 gFileList = realloc(gFileList, space*sizeof(char*));
91 if(gFileList == NULL) {
96 gFileList[giNumFiles++] = strdup(buf);
99 // Sort File List according to rules passed
102 // Show the file list
103 for( i = 0; i < giNumFiles; i ++ )
105 DisplayFile( gFileList[i] );
115 * \fn void ShowUsage(char *ProgName)
117 void ShowUsage(char *ProgName)
119 fprintf(stderr, "Usage: %s [options] [<directory>]\n", ProgName);
120 fprintf(stderr, "\n");
124 * \fn void ParseArguments(int argc, char *argv[])
125 * \brief Parse the command line arguments
127 void ParseArguments(int argc, char *argv[])
131 for( i = 1; i < argc; i ++ )
147 case 'a': gbShowAll = 1; gbShowImplicit = 1; continue;
149 case 'A': gbShowAll = 1; gbShowImplicit = 0; continue;
151 case 'l': gbViewExtended = 1; continue;
152 // Human readable sizes
153 case 'h': gbViewHumanReadable = 1; continue;
155 fprintf(stderr, "%s: Unknown option '%c'\n", *str);
163 if(gsDirectory == NULL) {
164 gsDirectory = argv[i];
169 if(!gsDirectory) gsDirectory = ".";
171 printf("gsDirectory = '%s'\n", gsDirectory);
175 * \fn int strcmpp(void *p1, void *p2)
176 * \brief Compares two strings given pointers to their pointers
178 int strcmpp(const void *p1, const void *p2)
180 return strcmp( *(char **)p1, *(char **)p2 );
184 * \fn void SortFileList()
185 * \brief Sorts the filled file list
189 qsort(gFileList, giNumFiles, sizeof(char*), strcmpp);
193 * \fn void DisplayFile(char *Filename)
194 * \brief Prints out the information for a file
196 void DisplayFile(char *Filename)
202 int dirLen = strlen(gsDirectory);
203 int type = FTYPE_NORMAL, perms = 0;
205 int owner = 0, group = 0;
207 // Check if we should skip this file
208 if(!gbShowAll && Filename[0] == '.') return;
211 path = malloc(dirLen+1+strlen(Filename)+1);
213 fprintf(stderr, "heap exhausted\n");
218 strcpy(path, gsDirectory);
220 strcpy(&path[dirLen+1], Filename);
225 fprintf(stderr, "Unable to open '%s'\n", path);
231 if(info.flags & FILEFLAG_DIRECTORY) {
234 else if(info.flags & FILEFLAG_SYMLINK) {
235 type = FTYPE_SYMLINK;
242 // Get Owner and Group
248 acl.group = 0; acl.id = info.uid;
249 _SysGetACL(fd, &acl);
250 if(acl.perms & 1) perms |= 0400; // R
251 if(acl.perms & 2) perms |= 0200; // W
252 if(acl.perms & 8) perms |= 0100; // X
254 acl.group = 1; acl.id = info.gid;
255 _SysGetACL(fd, &acl);
256 if(acl.perms & 1) perms |= 0040; // R
257 if(acl.perms & 2) perms |= 0020; // W
258 if(acl.perms & 8) perms |= 0010; // X
260 acl.group = 1; acl.id = -1;
261 _SysGetACL(fd, &acl);
262 if(acl.perms & 1) perms |= 0004; // R
263 if(acl.perms & 2) perms |= 0002; // W
264 if(acl.perms & 8) perms |= 0001; // X
266 free(path); // We're finished with it
271 char permStr[11] = " ---------";
272 if(type == FTYPE_DIR) permStr[0] = 'd';
273 if(perms & 0400) permStr[1] = 'r';
274 if(perms & 0200) permStr[2] = 'w';
275 if(perms & 0100) permStr[3] = 'x';
276 if(perms & 0040) permStr[4] = 'r';
277 if(perms & 0020) permStr[5] = 'w';
278 if(perms & 0010) permStr[6] = 'x';
279 if(perms & 0004) permStr[7] = 'r';
280 if(perms & 0002) permStr[8] = 'w';
281 if(perms & 0001) permStr[9] = 'x';
282 printf("%s %4i %4i ", permStr, owner, group);
283 if(gbViewHumanReadable) {
284 if(size < 2048) { // < 2 KiB
285 printf("%8lli B ", size);
287 else if(size < 2048*1024) { // < 2 MiB
288 printf("%8lli KiB ", size>>10);
290 else if(size < (uint64_t)2048*1024*1024) { // < 2 GiB
291 printf("%8lli MiB ", size>>20);
293 else if(size < (uint64_t)2048*1024*1024*1024) { // < 2 TiB
294 printf("%8lli GiB ", size>>30);
296 else { // Greater than 2 TiB
297 printf("%8i TiB ", size>>40);
300 printf("%8i ", size);
304 printf("%s\n", Filename);