Fri, 27 May 2011 13:20:15 +0200
Fixed memory leak when exiting the programm ahead of time
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
1 | #include "cline.h" |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
2 | #include "functions.h" |
0 | 3 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
4 | settings_t* new_settings_t() { |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
5 | settings_t *settings = malloc(sizeof(settings_t*)); |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
6 | if (settings != NULL) { |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
7 | #ifdef _WIN32 |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
8 | settings->fileSeparator = '\\'; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
9 | #else |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
10 | settings->fileSeparator = '/'; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
11 | #endif /* _WIN32 */ |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
12 | settings->suffixc = 1; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
13 | settings->recursive = false; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
14 | settings->includeSuffixes = false; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
15 | settings->matchesOnly = false; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
16 | } |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
17 | |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
18 | return settings; |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
19 | } |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
20 | |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
21 | void destroy_settings_t(settings_t* settings) { |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
22 | if (settings->suffixv != NULL) { |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
23 | free(settings->suffixv); |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
24 | } |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
25 | free(settings); |
0 | 26 | } |
27 | ||
1 | 28 | void printHelpText(const char* prgName) { |
0 | 29 | // Help text |
30 | const char* helpText = | |
1 | 31 | "\nUsage:" |
32 | "\n %s [-hrm][-s suffix][<directory>]" | |
33 | "\n %s [-hrm][-S suffix][<directory>]" | |
0 | 34 | "\n\nCounts the line terminator characters (\\n) within all" |
35 | " files in the specified\ndirectory." | |
36 | "\n\nOptions:" | |
37 | "\n -h, --help - this help text" | |
1 | 38 | "\n -m - print information about matching files only" |
39 | "\n -s <suffixes> - only count files with these suffixes (separated" | |
0 | 40 | "\n by commas)" |
1 | 41 | "\n -S <suffixes> - count any file except those with these suffixes" |
0 | 42 | "\n (separated by commas)" |
1 | 43 | "\n -r, -R - includes subdirectories" |
0 | 44 | "\n\n" |
1 | 45 | "The default call without any options is:" |
46 | "\n %s ./\n" | |
0 | 47 | "That means each file in each subdirectory is counted. If you want to count" |
48 | "\nC source code in your working directory and its subdirectories, type:" | |
1 | 49 | "\n %s -rs .c\n"; |
50 | ||
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
51 | printf(helpText, prgName, prgName, prgName, prgName); |
1 | 52 | } |
53 | ||
54 | int main(int argc, char** argv) { | |
0 | 55 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
56 | // Settings |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
57 | settings_t *settings = new_settings_t(); |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
58 | if (settings == NULL) { |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
59 | fprintf(stderr, "Memory allocation failed.\n"); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
60 | return 1; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
61 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
62 | |
0 | 63 | // Program name |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
64 | char* prgName = strrchr(argv[0], settings->fileSeparator); |
1 | 65 | |
66 | if (prgName == NULL) { | |
0 | 67 | prgName = argv[0]; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
68 | } else { |
0 | 69 | prgName++; |
70 | } | |
71 | ||
72 | // Defaults | |
1 | 73 | char* _suffix = " "; |
74 | char _directory[3]; | |
75 | _directory[0] = '.'; | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
76 | _directory[1] = settings->fileSeparator; |
1 | 77 | _directory[2] = 0; |
0 | 78 | |
79 | // Get arguments | |
80 | char* directory; | |
81 | char* suffix; | |
82 | bool showHelp = false; | |
83 | char checked = 0; | |
84 | ||
1 | 85 | for (int t = 1 ; t < argc ; t++) { |
86 | ||
87 | int argflags = checkArgument(argv[t], "hsSrRm"); | |
88 | ||
89 | // s | |
90 | if ((argflags & 2) > 0) { | |
91 | if ((checked & 1) > 0) { | |
92 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
93 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
94 | return 1; |
0 | 95 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
96 | settings->includeSuffixes = true; |
1 | 97 | t++; |
98 | if (t >= argc) { | |
99 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
100 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
101 | return 1; |
1 | 102 | } |
103 | suffix = argv[t]; | |
0 | 104 | checked |= 1; |
105 | } | |
1 | 106 | // S |
107 | if ((argflags & 4) > 0) { | |
108 | if ((checked & 1) > 0) { | |
109 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
110 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
111 | return 1; |
0 | 112 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
113 | settings->includeSuffixes = false; |
1 | 114 | t++; |
115 | if (t >= argc) { | |
116 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
117 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
118 | return 1; |
1 | 119 | } |
120 | suffix = argv[t]; | |
0 | 121 | checked |= 1; |
122 | } | |
1 | 123 | // h |
124 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { | |
125 | if ((checked & 2) > 0) { | |
126 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
127 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
128 | return 1; |
0 | 129 | } |
130 | checked |= 2; | |
131 | showHelp = true; | |
132 | } | |
1 | 133 | // r, R |
134 | if ((argflags & 24) > 0) { | |
135 | if ((checked & 4) > 0) { | |
136 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
137 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
138 | return 1; |
0 | 139 | } |
140 | checked |= 4; | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
141 | settings->recursive = true; |
0 | 142 | } |
1 | 143 | if ((argflags & 32) > 0) { |
144 | if ((checked & 32) > 0) { | |
145 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
146 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
147 | return 1; |
0 | 148 | } |
1 | 149 | checked |= 32; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
150 | settings->matchesOnly = true; |
0 | 151 | } |
1 | 152 | // other |
153 | if (argflags == 0) { | |
154 | if ((checked & 8) > 0) { | |
155 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
156 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
157 | return 1; |
0 | 158 | } |
1 | 159 | checked |= 8; |
0 | 160 | directory = argv[t]; |
161 | } | |
162 | } | |
163 | ||
164 | // Show help and quit | |
1 | 165 | if (showHelp) { |
166 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
167 | destroy_settings_t(settings); |
0 | 168 | return 0; |
169 | } | |
170 | ||
171 | // Default values | |
1 | 172 | if ((checked & 1) == 0) { |
0 | 173 | suffix = _suffix; |
174 | } | |
175 | ||
1 | 176 | if ((checked & 8) == 0) { |
0 | 177 | directory = _directory; |
178 | } | |
179 | ||
180 | // Find tokens | |
181 | char* finder; | |
182 | finder = strchr(suffix, ','); | |
1 | 183 | while (finder != NULL) { |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
184 | settings->suffixc++; |
0 | 185 | finder = strchr(finder+1, ','); |
186 | } | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
187 | settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc); |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
188 | if (settings->suffixv == NULL) { |
0 | 189 | fprintf(stderr, "Memory allocation failed.\n"); |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
190 | destroy_settings_t(settings); |
0 | 191 | return 1; |
192 | } | |
193 | finder = strtok(suffix, ","); | |
194 | int c = 0; | |
1 | 195 | while (finder != NULL) { |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
196 | settings->suffixv[c] = finder; |
0 | 197 | c++; |
198 | finder = strtok(NULL, ","); | |
199 | } | |
200 | ||
201 | // Open directory | |
202 | DIR *dir = opendir(directory); | |
1 | 203 | if (dir == NULL) { |
0 | 204 | perror("Operation failed"); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
205 | destroy_settings_t(settings); |
0 | 206 | return 1; |
207 | } | |
208 | ||
209 | // Scan directory | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
210 | int lines = scanDirectory(dir, 0, directory, settings); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
211 | closedir(dir); |
0 | 212 | |
213 | // Print double line and line count | |
1 | 214 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
215 | const int columns = 79; |
1 | 216 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
217 | const int columns = 80; |
1 | 218 | #endif /* _WIN32 */ |
219 | ||
220 | for (int t = 0 ; t < columns ; t++) { | |
0 | 221 | printf("="); |
222 | } | |
1 | 223 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
224 | printf("\n%73d lines\n", lines); |
1 | 225 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
226 | printf("\n%74d lines\n", lines); |
1 | 227 | #endif /* _WIN32 */ |
0 | 228 | |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
229 | destroy_settings_t(settings); |
0 | 230 | return 0; |
231 | } |