Big update: 5/N fix

This commit is contained in:
Your Name 2023-11-07 17:06:27 +03:00
parent 935b750eb5
commit 5582a0cc18

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
unsigned int s_flag; unsigned int s_flag;
@ -53,45 +54,48 @@ FILE *file_open(const char *path) {
return fp; return fp;
} }
int main(const int argc, const char **argv) { int main(int argc, char **argv) {
int i; int opt;
for (i = 1; i < argc; i++) { while ((opt = getopt(argc, argv, "f")) != -1) {
if (argv[i][0] != '-') switch (opt) {
break; case 'f':
s_flag = 1;
break;
else if (!strcmp(argv[i], "-n")) default:
s_flag = 1; printf("cmp file1 file2 [skip1] [skip2]\n\t[-f Silent]\n");
return 0;
else if (!strcmp(argv[i], "--help")) {
printf("cmp FILE1 FILE2 [SKIP1] [SKIP2]\n\t[-f Silent]\n");
return 0;
} }
} }
argv += optind;
argc -= optind;
long skip1 = 0; long skip1 = 0;
long skip2 = 0; long skip2 = 0;
FILE *fp1 = NULL; FILE *fp1 = NULL;
FILE *fp2 = stdin; FILE *fp2 = stdin;
switch (argc - i) { switch (argc) {
case 4: case 4:
skip2 = parse_int(argv[i + 3]); skip2 = parse_int(argv[3]);
/* fallthrough */ /* fallthrough */
case 3: case 3:
skip1 = parse_int(argv[i + 2]); skip1 = parse_int(argv[2]);
/* fallthrough */ /* fallthrough */
case 2: case 2:
fp2 = file_open(argv[i + 1]); fp2 = file_open(argv[1]);
/* fallthrough */ /* fallthrough */
case 1: case 1:
fp1 = file_open(argv[i]); fp1 = file_open(argv[0]);
break; break;
default: default:
fprintf(stderr, "cmp: missing operand\n");
return 1; return 1;
} }