-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.c
More file actions
89 lines (87 loc) · 2.36 KB
/
patch.c
File metadata and controls
89 lines (87 loc) · 2.36 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
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[]){
char *x[100];
int m = 0, i, j;
char str1[200];
int size1 = sizeof(str1);
FILE *fp1, *fp2, *fp3;
if(argc == 1){
printf("patch: missing operand after '%s'\n", argv[0]);
printf("patch: Try 'diff --help' for more information.\n");
exit(0);
}
else if(argc == 2){
if((strcmp(argv[1],"--help")) == 0){
printf("******************************************************\n\n");
printf("Name: Shreeraj Vijay Bhamare\n");
printf("MIS: 111903098 ; Div: 02 ; Batch: S1\n");
printf("Project: Diff-Patch Implementation in C programming\n\n");
printf("******************************************************\n\n\n");
printf("\t\tUsage: patch [OPTION]... [ORIGFILE [PATCHFILE]]\n\n\n");
exit(0);
}
else{
printf("patch: missing operand after '%s'\n", argv[1]);
printf("patch: Try 'diff --help' for more information.\n");
exit(0);
}
}
else if(argc == 3){
fp1 = fopen("tempfile.txt", "w+");
fp2 = fopen(argv[2], "r+");
fp3 = fopen(argv[1], "r");
if(fp1 == NULL){
printf("patch: %s: No such file or directory\n", argv[1]);
exit(0);
}
if(fp2 == NULL){
printf("patch: %s: No such file or directory\n", argv[2]);
exit(0);
}
}
else if(argc > 3){
printf("patch: extra operand '%s'\n", argv[3]);
printf("patch: Try 'patch --help' for more information.\n");
exit(0);
}
while(fgets(str1, size1, fp2) != NULL){
x[m] = (char *)malloc(size1);
strcpy(x[m], str1);
m++;
}
for(i = 3; i < m; i++){
if(x[i][0] == '+'){
int len;
len = strlen(x[i]);
for(j = 1; j < len; j++)
fprintf(fp1, "%c", x[i][j]);
}
else if(x[i][0] == ' '){
int len;
len = strlen(x[i]);
for(j = 1; j < len; j++)
fprintf(fp1, "%c", x[i][j]);
}
else if(x[i][0] == '-'){
continue;
}
}
char arr1[20] = "tempfile.txt";
char arr2[20];
strcpy(arr2, argv[1]);
int val = rename(arr1, arr2);
/*if(val ==0){
printf("File renamed successfully");
}
else
printf("Rename failed");*/
printf("patching file %s\n", argv[1]);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}