cline.c

Mon, 23 May 2011 16:42:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 23 May 2011 16:42:44 +0200
changeset 0
518bfd1cc1e8
child 1
34a5e235d16e
permissions
-rw-r--r--

cline project init

0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 #include <stdio.h>
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 #include <string.h>
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 #include <stdbool.h>
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 #include <stdlib.h>
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 #include <dirent.h>
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 static int suffixc;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 static char** suffixv;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 static bool recursive;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 static bool includeSuffixes;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 bool testSuffix(char* filename)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 bool ret = false;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 int tokenlen, fnamelen = strlen(filename);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 for (int t = 0 ; t < suffixc ; t++)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 tokenlen = strlen(suffixv[t]);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 if (fnamelen >= tokenlen)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 ret = true;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 break;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 return ret ^ !includeSuffixes;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 int scanDirectory(DIR *dir, const int spaces, char* currdir)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 DIR *subdir;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 char* subdirname;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 struct dirent *entry;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 int lines, digits, a;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 int lineSum = 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 while ((entry = readdir(dir)) != NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 // Print occurence
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 char entryname[strlen(entry->d_name)+spaces];
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 for (int t = 0 ; t < spaces ; t++)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 entryname[t]=' ';
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 entryname[spaces] = 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 strcat(entryname, entry->d_name);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 printf("%-60s", entryname);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 // Check for subdirectory
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 strcpy(subdirname, currdir);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 strcat(subdirname, "/");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 strcat(subdirname, entry->d_name);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 if ((subdir = opendir(subdirname)) != NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 printf("\n");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 if (recursive)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 lineSum += scanDirectory(subdir, spaces+1, subdirname);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 closedir(subdir);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 continue;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 // Count lines
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 lines = 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 char filename[(1+strlen(currdir)+strlen(entry->d_name))];
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 strcpy(filename, currdir);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 strcat(filename, "/");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 strcat(filename, entry->d_name);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 if (testSuffix(filename))
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 FILE *file = fopen(filename, "r");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 if (file == NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 perror(" File acces failed");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 continue;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 do
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 a = fgetc(file);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 if (a == 10)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 lines++;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 } while (a != EOF);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 fclose(file);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 // Print line count
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 printf("%14d lines\n", lines);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 lineSum += lines;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 else
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 // Print hint
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 printf("%20s\n", "no match");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 return lineSum;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 int main(int argc, char** argv)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 // Help text
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 const char* helpText =
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 "\nUsage:\n%s [-h|--help|(-s=<suffix>|-S=<suffix>)|(-r|-R)|<directory>]"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 "\n\nCounts the line terminator characters (\\n) within all"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 " files in the specified\ndirectory."
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 "\n\nOptions:"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 "\n -h, --help - this help text"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 "\n -s=<suffixes> - only count files with these suffixes (separated"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 "\n by commas)"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 "\n -S=<suffixes> - count any file except those with these suffixes"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 "\n (separated by commas)"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 "\n -R - excludes subdirectories"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 "\n -r - includes subdirecotires"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 "\n\n"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 "The default call without any options is:"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 "\n %s -r ./ -S=\n"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 "That means each file in each subdirectory is counted. If you want to count"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 "\nC source code in your working directory and its subdirectories, type:"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 "\n %s -s=.c\n";
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 // Program name
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 char* prgName = strrchr(argv[0], '/');
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 if (prgName == NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 prgName = argv[0];
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 else
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 prgName++;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 // Defaults
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 char* _suffix = "";
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 char* _directory = "./";
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 // Get arguments
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 char* directory;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 char* suffix;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 bool showHelp = false;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 recursive = true;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 includeSuffixes = false;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 char checked = 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 for (int t = 1 ; t < argc ; t++)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 if (strncmp(argv[t], "-s=", 3) == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 if ((checked & 1) > 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 printf(helpText, prgName, prgName, prgName);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 return -1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 includeSuffixes = true;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 suffix = argv[t]+3;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 checked |= 1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 else if (strncmp(argv[t], "-S=", 3) == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 if ((checked & 1) > 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 printf(helpText, prgName, prgName, prgName);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 return -1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 includeSuffixes = false;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 suffix = argv[t]+3;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 checked |= 1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 else if (strcmp(argv[t], "-h") == 0 || strcmp(argv[t], "--help") == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 if ((checked & 2) > 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 printf(helpText, prgName, prgName, prgName);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 return -1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 checked |= 2;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 showHelp = true;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 else if (strcmp(argv[t], "-r") == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 if ((checked & 4) > 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 printf(helpText, prgName, prgName, prgName);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 return -1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 checked |= 4;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 recursive = true;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 else if (strcmp(argv[t], "-R") == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 if ((checked & 4) > 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 printf(helpText, prgName, prgName, prgName);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 return -1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 checked |= 4;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 recursive = false;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 else
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 if ((checked & 8) > 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 printf(helpText, prgName, prgName, prgName);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 return -1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 checked |= 8;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 directory = argv[t];
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 // Show help and quit
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 if (showHelp)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 printf(helpText, prgName, prgName, prgName);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 return 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 // Default values
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 if ((checked & 1) == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 suffix = _suffix;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 if ((checked & 8) == 0)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 directory = _directory;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 // Find tokens
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 char* finder;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 suffixc = 1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 finder = strchr(suffix, ',');
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 while (finder != NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 suffixc++;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 finder = strchr(finder+1, ',');
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 suffixv = (char**) malloc(sizeof(suffixv)*suffixc);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 if (suffixv == NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 fprintf(stderr, "Memory allocation failed.\n");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 return 1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 finder = strtok(suffix, ",");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 int c = 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 while (finder != NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 suffixv[c] = finder;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 c++;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 finder = strtok(NULL, ",");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 // Open directory
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 DIR *dir = opendir(directory);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 if (dir == NULL)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 perror("Operation failed");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 free(suffixv);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 return 1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 // Scan directory
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273 int lines = scanDirectory(dir, 0, directory);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 // Print double line and line count
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 for (int t = 0 ; t < 80 ; t++)
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 {
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 printf("=");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 printf("\n%74d lines\n", lines);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 closedir(dir);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 free(suffixv);
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 return 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 }

mercurial