-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
99 lines (91 loc) · 1.62 KB
/
main.c
File metadata and controls
99 lines (91 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
#include "main.h"
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char **q, **r;
int l1, l2, i;
struct lcslines x;
char *file1, *file2;
file1 = (char*)malloc(sizeof(char) * 100);
if(file1 == NULL)
{
perror("malloc failed\n");
exit(1);
}
file2 = (char*)malloc(sizeof(char) * 100);
if(file2 == NULL)
{
perror("malloc failed\n");
exit(1);
}
if(argc < 3)
{
if(strcmp(argv[1], "--help") == 0)
{
help();
return 0;
}
else
{
perror("invalid input\n");
return errno;
}
}
switch(argc)
{
case 3:
strcpy(file1, argv[1]);
strcpy(file2, argv[2]);
fp1 = fopen(argv[1], "r+");
if(!fp1)
{
perror("fopen fails\n");
return errno;
}
fp2 = fopen(argv[2], "r+");
if(!fp2)
{
perror("fopen fails\n");
return errno;
}
break;
case 4:
strcpy(file1, argv[2]);
strcpy(file2, argv[3]);
fp1 = fopen(argv[2], "r+");
if(!fp1)
{
perror("fopen fails\n");
return errno;
}
fp2 = fopen(argv[3], "r+");
if(!fp2)
{
perror("fopen fails\n");
return errno;
}
break;
default:
break;
}
// returns if two input files are identical
if(!strcmp(file1, file2))
return 0;
l1 = length(fp1); //give no of lines in file1 and file2
l2 = length(fp2);
fseek(fp1, 0, SEEK_SET); // sets the file pointer to start of file
fseek(fp2, 0, SEEK_SET);
q = readline(fp1); //read lines from files
r = readline(fp2);
output(q, r, l1, l2, argv, file1, file2);
free(file1);
free(file2);
fclose(fp1);
fclose(fp2);
return 0;
}