forked from garagakteja/Custom-Unix-Commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniq.c
129 lines (124 loc) · 2.44 KB
/
uniq.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "types.h"
#include "stat.h"
#include "user.h"
char buf[5120000];
char *args;
int uniq(int fd, char *name)
{
int final=0,k,p,q,index=0,m=0,l=0,b,n,i,count=0,count1 = 0; char pilot[10000];
char *output[10000];
int repeat[1000];
while((n = read(fd, buf, sizeof(buf))) > 0)
{ for(b=0; b<n; b++)
{
if(buf[b] == '\n')
l++;
}
for(i=0;buf[i]!='\n';i++)
{
count++;
pilot[i]=buf[i];
}
pilot[i]='\0';
repeat[0]=1;
output[0]=(char*)malloc((count+1)*sizeof(char*));
for(i=0;i<count+1;i++)
{
output[index][i]=pilot[i];
}
output[index][i]='\0';
k=i;
while(final<l-1)
{
final++;
count1=0;
for(i=k;buf[i]!='\n';i++)
{
count1++;
pilot[m++]=buf[i];
}
pilot[m]='\0';
k=k+count1+1;
m=0;
if(strcmp(output[index],pilot)!=0)
{
index = index + 1;
output[index]=(char*)malloc((count1+1)*sizeof(char*));
for(p=0;p<count1;p++)
{
output[index][p]=pilot[p];
}
output[index][p]='\0';
repeat[index]=1;
}
else
{
repeat[index]=repeat[index]+1;
}
}
}
if(strcmp(args,"-c")==0 || strcmp(args,"-C")==0 )
{
for(q=0;q<index+1;q++)
{
printf(1,"%d %s \n",repeat[q],output[q]);
}
}
else if(strcmp(args,"-d")==0 || strcmp(args,"-D")==0 )
{
for(q=0;q<index+1;q++)
{
if(repeat[q]>1)
{
printf(1,"%s \n",output[q]);
}
}
}
else
{
for(q=0;q<index+1;q++)
{
printf(1,"%s \n",output[q]);
}
}
free(output);
return 0;
}
int main(int argc, char **argv)
{
int fd, r;
if(argc <= 1)
{
uniq(0, "");
exit();
}
else if(argc == 2)
{
for(r = 1; r < argc; r++)
{
if((fd = open(argv[r], 0)) < 0)
{
printf(1,"uniq: cannot open %s\n", argv[r]);
exit();
}
uniq(fd, argv[r]);
close(fd);
}
exit();
}
else
{
for(r = 2; r < argc; r++)
{
if((fd = open(argv[r], 0)) < 0)
{
printf(1,"uniq: cannot open %s\n", argv[r]);
exit();
}
args=argv[1];
uniq(fd, argv[r]);
close(fd);
}
exit();
}
}