Cleanup
[tpg/acess2.git] / Usermode / Applications / ls_src / main.c
1 /*
2  * Acess2 LS command
3  */
4 #include <acess/sys.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #define BUF_SIZE        1024
10
11 // === PROTOTYPES ===
12  int    main(int argc, char *argv[]);
13 void    ShowUsage(char *ProgName);
14 void    ParseArguments(int argc, char *argv[]);
15 void    SortFileList();
16
17 // === GLOBALS ===
18 // --- Flags ---
19  int    gbShowAll = 0;
20  int    gbShowImplicit = 0;
21  int    gbViewExtended = 0;
22  int    gbViewHumanReadable = 0;
23 // --- Parameters ---
24 char    *gsDirectory = NULL;
25 // --- Working Set ---
26 char    **gFileList;
27  int    giNumFiles = 0;
28
29 /**
30  * \fn int main(int argc, char *argv[])
31  * \brief Entrypoint
32  */
33 int main(int argc, char *argv[])
34 {
35          int    fd, tmp;
36         char    buf[BUF_SIZE+1];
37         t_sysFInfo      info;
38          int    space = 0;
39
40         // Arguments Check
41         ParseArguments(argc, argv);
42
43         // Open Directory
44         fd = open(gsDirectory, OPENFLAG_READ|OPENFLAG_EXEC);
45         if(fd == -1) {
46                 printf("Unable to open '%s' for reading\n", gsDirectory);
47                 return EXIT_FAILURE;
48         }
49
50         // Check that it is a directory
51         finfo(fd, &info, 0);
52         if( !(info.flags & FILEFLAG_DIRECTORY) ) {
53                 fprintf(stderr, "'%s' is a directory\n", gsDirectory);
54                 close(fd);
55                 return EXIT_FAILURE;
56         }
57
58         // Traverse Directory
59         while( (tmp = readdir(fd, buf)) )
60         {
61                 // Error check
62                 if(tmp < 0) {
63                         close(fd);
64                         return EXIT_FAILURE;
65                 }
66                 
67                 // Allocate Space
68                 if(space == giNumFiles)
69                 {
70                         space += 16;
71                         gFileList = realloc(gFileList, space*sizeof(char*));
72                         if(gFileList == NULL) {
73                                 close(fd);
74                                 return EXIT_FAILURE;
75                         }
76                 }
77                 gFileList[giNumFiles++] = strdup(buf);
78         }
79
80         // Sort File List according to rules passed
81         SortFileList();
82
83         close(fd);
84         printf("\n");
85
86         return EXIT_SUCCESS;
87 }
88
89 /**
90  * \fn void ShowUsage(char *ProgName)
91  */
92 void ShowUsage(char *ProgName)
93 {
94         fprintf(stderr, "Usage: %s [options] [<directory>]\n", ProgName);
95         fprintf(stderr, "\n");
96 }
97
98 /**
99  * \fn void ParseArguments(int argc, char *argv[])
100  * \brief Parse the command line arguments
101  */
102 void ParseArguments(int argc, char *argv[])
103 {
104          int    i;
105         char    *str;
106         for( i = 1; i < argc; i ++ )
107         {
108                 str = argv[i];
109                 // Flags
110                 if(str[0] == '-')
111                 {
112                         if(str[1] == '-')
113                         {
114                                 continue;
115                         }
116                         str = &str[1];
117                         for( ; *str; str++ )
118                         {
119                                 switch(*str)
120                                 {
121                                 // Show All
122                                 case 'a':       gbShowAll = 1;  gbShowImplicit = 1;     continue;
123                                 // Almost All
124                                 case 'A':       gbShowAll = 1;  gbShowImplicit = 0;     continue;
125                                 // Extended List
126                                 case 'l':       gbViewExtended = 1;     continue;
127                                 // Human readable sizes
128                                 case 'h':       gbViewHumanReadable = 1;        continue;
129                                 default:
130                                         fprintf(stderr, "%s: Unknown option '%c'\n", *str);
131                                         ShowUsage(argv[0]);
132                                         exit(EXIT_FAILURE);
133                                 }
134                         }
135                         continue;
136                 }
137                 
138                 if(gsDirectory == NULL) {
139                         gsDirectory = argv[i];
140                 }
141         }
142         
143         // Apply Defaults
144         if(!gsDirectory)        gsDirectory = ".";
145         
146         printf("gsDirectory = '%s'\n", gsDirectory);
147 }
148
149 /**
150  * \fn int strcmpp(void *p1, void *p2)
151  * \brief Compares two strings given pointers to their pointers
152  */
153 int strcmpp(const void *p1, const void *p2)
154 {
155         return strcmp( *(char **)p1, *(char **)p2 );
156 }
157
158 /**
159  * \fn void SortFileList()
160  * \brief Sorts the filled file list
161  */
162 void SortFileList()
163 {
164         qsort(gFileList, giNumFiles, sizeof(char*), strcmpp);
165 }

UCC git Repository :: git.ucc.asn.au