52 return code; |
52 return code; |
53 } |
53 } |
54 |
54 |
55 int main(int argc, char** argv) { |
55 int main(int argc, char** argv) { |
56 |
56 |
57 // Settings |
57 /* Settings */ |
58 settings_t *settings = new_settings_t(); |
58 settings_t *settings = new_settings_t(); |
59 if (settings == NULL) { |
59 if (settings == NULL) { |
60 fprintf(stderr, "Memory allocation failed.\n"); |
60 fprintf(stderr, "Memory allocation failed.\n"); |
61 return 1; |
61 return 1; |
62 } |
62 } |
63 |
63 |
64 // Get arguments |
64 /* Get arguments */ |
65 char* directory = "./"; |
65 char* directory = "./"; |
66 char* suffix = " "; |
66 char* suffix = " "; |
67 int checked = 0; |
67 int checked = 0; |
68 |
68 |
69 for (int t = 1 ; t < argc ; t++) { |
69 for (int t = 1 ; t < argc ; t++) { |
70 |
70 |
71 int argflags = checkArgument(argv[t], "hsSrRmvVb"); |
71 int argflags = checkArgument(argv[t], "hsSrRmvVb"); |
72 |
72 |
73 // s, S |
73 /* s, S */ |
74 if ((argflags & 6) > 0) { |
74 if ((argflags & 6) > 0) { |
75 if (registerArgument(&checked, 6)) { |
75 if (registerArgument(&checked, 6)) { |
76 return exit_with_help(settings, 1); |
76 return exit_with_help(settings, 1); |
77 } |
77 } |
78 settings->includeSuffixes = (argflags & 2) > 0; |
78 settings->includeSuffixes = (argflags & 2) > 0; |
80 if (t >= argc) { |
80 if (t >= argc) { |
81 return exit_with_help(settings, 1); |
81 return exit_with_help(settings, 1); |
82 } |
82 } |
83 suffix = argv[t]; |
83 suffix = argv[t]; |
84 } |
84 } |
85 // h |
85 /* h */ |
86 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { |
86 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { |
87 return exit_with_help(settings, 0); |
87 return exit_with_help(settings, 0); |
88 } |
88 } |
89 // r, R |
89 /* r, R */ |
90 if ((argflags & 24) > 0) { |
90 if ((argflags & 24) > 0) { |
91 if (registerArgument(&checked, 24)) { |
91 if (registerArgument(&checked, 24)) { |
92 return exit_with_help(settings, 1); |
92 return exit_with_help(settings, 1); |
93 } |
93 } |
94 settings->recursive = true; |
94 settings->recursive = true; |
95 } |
95 } |
96 // m |
96 /* m */ |
97 if ((argflags & 32) > 0) { |
97 if ((argflags & 32) > 0) { |
98 if (registerArgument(&checked, 32)) { |
98 if (registerArgument(&checked, 32)) { |
99 return exit_with_help(settings, 1); |
99 return exit_with_help(settings, 1); |
100 } |
100 } |
101 settings->matchesOnly = true; |
101 settings->matchesOnly = true; |
102 } |
102 } |
103 // v |
103 /* v */ |
104 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { |
104 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { |
105 return exit_with_version(settings); |
105 return exit_with_version(settings); |
106 } |
106 } |
107 // V |
107 /* V */ |
108 if ((argflags & 128) > 0) { |
108 if ((argflags & 128) > 0) { |
109 if (registerArgument(&checked, 128)) { |
109 if (registerArgument(&checked, 128)) { |
110 return exit_with_help(settings, 1); |
110 return exit_with_help(settings, 1); |
111 } |
111 } |
112 settings->verbose = false; |
112 settings->verbose = false; |
113 } |
113 } |
114 // b |
114 /* b */ |
115 if ((argflags & 256) > 0) { |
115 if ((argflags & 256) > 0) { |
116 if (registerArgument(&checked, 256)) { |
116 if (registerArgument(&checked, 256)) { |
117 return exit_with_help(settings, 1); |
117 return exit_with_help(settings, 1); |
118 } |
118 } |
119 t++; |
119 t++; |
130 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; |
130 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; |
131 } else { |
131 } else { |
132 return exit_with_help(settings, 1); |
132 return exit_with_help(settings, 1); |
133 } |
133 } |
134 } |
134 } |
135 // Path |
135 /* Path */ |
136 if (argflags == 0) { |
136 if (argflags == 0) { |
137 if (registerArgument(&checked, 1024)) { |
137 if (registerArgument(&checked, 1024)) { |
138 return exit_with_help(settings, 1); |
138 return exit_with_help(settings, 1); |
139 } |
139 } |
140 directory = argv[t]; |
140 directory = argv[t]; |
141 } |
141 } |
142 } |
142 } |
143 |
143 |
144 // Configure output |
144 /* Configure output */ |
145 if (!settings->verbose) { |
145 if (!settings->verbose) { |
146 close_stdout(); |
146 close_stdout(); |
147 } |
147 } |
148 |
148 |
149 // Find tokens |
149 /* Find tokens */ |
150 char* finder = strtok(suffix, ","); |
150 char* finder = strtok(suffix, ","); |
151 while (finder != NULL) { |
151 while (finder != NULL) { |
152 add_string(settings->suffixList, finder); |
152 add_string(settings->suffixList, finder); |
153 finder = strtok(NULL, ","); |
153 finder = strtok(NULL, ","); |
154 } |
154 } |
155 |
155 |
156 // Open directory |
156 /* Open directory */ |
157 DIR *dir = opendir(directory); |
157 DIR *dir = opendir(directory); |
158 if (dir == NULL) { |
158 if (dir == NULL) { |
159 perror("Operation failed"); |
159 perror("Operation failed"); |
160 destroy_settings_t(settings); |
160 destroy_settings_t(settings); |
161 return 1; |
161 return 1; |
162 } |
162 } |
163 |
163 |
164 // Scan directory |
164 /* Scan directory */ |
165 int lines = scanDirectory(dir, 0, directory, settings); |
165 int lines = scanDirectory(dir, 0, directory, settings); |
166 closedir(dir); |
166 closedir(dir); |
167 destroy_settings_t(settings); |
167 destroy_settings_t(settings); |
168 |
168 |
169 // Print double line and line count |
169 /* Print double line and line count */ |
170 for (int t = 0 ; t < 79 ; t++) { |
170 for (int t = 0 ; t < 79 ; t++) { |
171 printf("="); |
171 printf("="); |
172 } |
172 } |
173 printf("\n%73d lines\n", lines); |
173 printf("\n%73d lines\n", lines); |
174 |
174 |