Use getopt, replace stoi_mini with atoi

This commit is contained in:
Gitea 2020-12-13 10:09:04 -06:00
parent 9c84e90039
commit fb6226dd6b

36
head.c
View file

@ -4,8 +4,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
int stoi_mini(char * str);
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
FILE * current_input_file; FILE * current_input_file;
int i = 1, j = 0; char c; int i = 1, j = 0; char c;
@ -15,15 +13,14 @@ int main(int argc, char *argv[]) {
int error_occurred; int error_occurred;
while((c = getopt(argc, argv, "n:")) != -1) {
if(argv[i][0] == '-') { if(c == 'n') {
if(argv[i][1] == 'n') { chars_copy = atoi(optarg);
i++;
chars_copy = stoi_mini(argv[i]);
i++;
} }
} }
i = optind;
if(argc - i > 1) { if(argc - i > 1) {
print_head = 1; print_head = 1;
} }
@ -66,26 +63,3 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} }
} }
int stoi_mini(char * str) {
int i, str_int = 0;
for(i = 0; str[i] != '\0' && str[i] != ','; i++) {
str_int *= 10;
switch(str[i]) {
case '0': str_int += 0; break;
case '1': str_int += 1; break;
case '2': str_int += 2; break;
case '3': str_int += 3; break;
case '4': str_int += 4; break;
case '5': str_int += 5; break;
case '6': str_int += 6; break;
case '7': str_int += 7; break;
case '8': str_int += 8; break;
case '9': str_int += 9; break;
default: return -1;
}
}
return str_int;
}