Skip to content

Commit 57f6068

Browse files
committed
0.20191010: stdio.h: add error messages
1 parent f7c203f commit 57f6068

File tree

3 files changed

+176
-145
lines changed

3 files changed

+176
-145
lines changed

README

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ VL-LUG
77

88
BUILD
99

10-
$ bash build.sh
10+
$ make
1111

1212
INSTALL
1313

14-
$ sudo cp torread /usr/bin
15-
$ sudo cp torwrite /usr/bin
14+
$ sudo cp torread torwrite /usr/bin
1615
$ sudo cp torreadwrite.1.gz /usr/share/man/man1
1716
$ sudo ln -s /usr/share/man/man1/torreadwrite.1.gz /usr/share/man/man1/torread.1.gz
1817
$ sudo ln -s /usr/share/man/man1/torreadwrite.1.gz /usr/share/man/man1/torwrite.1.gz

src/torread.c

Lines changed: 82 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sys/stat.h>
55
#include <sys/mman.h>
66
#include <stdint.h>
7+
#include <stdio.h>
78
#include <stdlib.h>
89

910
const uint8_t sp=' ';
@@ -16,7 +17,6 @@ const uint8_t elst=']';
1617
const uint8_t bdct='(';
1718
const uint8_t edct=')';
1819
const uint8_t eqv='=';
19-
const int stdout=1;
2020

2121
enum obj_type {OBJ_NUL, OBJ_INT, OBJ_STR, OBJ_LST, OBJ_DCT};
2222

@@ -33,151 +33,167 @@ size_t PrintObj(const uint8_t* d, size_t shift, size_t len, int skip);
3333
void Byte2Hex(uint8_t ch)
3434
{
3535
static const uint8_t cnv[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
36-
write(stdout,cnv+((ch&(15<<4))>>4),1);
37-
write(stdout,cnv+(ch&15),1);
36+
fwrite(cnv + (( ch & (15 << 4)) >> 4), 1, 1, stdout);
37+
fwrite(cnv + (ch & 15), 1, 1, stdout);
3838
}
3939

4040
void PrintSpc(int num)
4141
{
4242
int i;
43-
for(i=0; i<num; i++) write(stdout,&sp,1);
43+
for(i = 0; i < num; i++) fwrite(&sp, 1, 1, stdout);
4444
}
4545

4646
enum obj_type DetType(const uint8_t* d, size_t shift)
4747
{
4848
// write(2,d+shift,1);
49-
if(d[shift]=='i') return OBJ_INT;
50-
if(d[shift]=='l') return OBJ_LST;
51-
if(d[shift]=='d') return OBJ_DCT;
52-
if(d[shift]>=49 && d[shift]<=57) return OBJ_STR;
49+
if(d[shift] == 'i') return OBJ_INT;
50+
if(d[shift] == 'l') return OBJ_LST;
51+
if(d[shift] == 'd') return OBJ_DCT;
52+
if(d[shift] >= 49 && d[shift] <= 57) return OBJ_STR;
5353
return OBJ_NUL;
5454
}
5555

5656
size_t FindSym(const uint8_t* d, uint8_t s, size_t shift, size_t len)
5757
{
5858
size_t i;
5959

60-
for(i=shift; i<len; i++) if(d[i]==s) return i;
60+
for(i = shift; i < len; i++) if(d[i] == s) return i;
6161
exit(2);
6262
}
6363

6464
size_t PrintObj(const uint8_t* d, size_t shift, size_t len, int skip)
6565
{
66-
switch(DetType(d,shift))
66+
switch(DetType(d, shift))
6767
{
6868
case(OBJ_NUL):
6969
exit(2);
7070
case(OBJ_INT):
71-
return PrintInt(d,shift,len);
71+
return PrintInt(d, shift, len);
7272
case(OBJ_STR):
73-
return PrintStr(d,shift,len);
73+
return PrintStr(d, shift, len);
7474
case(OBJ_LST):
75-
return PrintLst(d,shift,len,skip);
75+
return PrintLst(d, shift, len, skip);
7676
case(OBJ_DCT):
77-
return PrintDct(d,shift,len,skip);
77+
return PrintDct(d, shift, len, skip);
7878
}
7979
exit(2);
8080
}
8181

8282
size_t PrintInt(const uint8_t* d, size_t shift, size_t len)
8383
{
84-
size_t e=FindSym(d,'e',shift,len);
85-
write(stdout,d+shift+1,e-shift-1);
84+
size_t e = FindSym(d, 'e', shift, len);
85+
fwrite(d + shift + 1, e - shift - 1, 1, stdout);
8686
return e+1;
8787
}
8888

8989
size_t PrintStr(const uint8_t* d, size_t shift, size_t len)
9090
{
91-
size_t e=FindSym(d,':',shift,len);
92-
int l=atoi((const char*)(d+shift));
93-
if(e+l>=len) exit(2);
91+
size_t e = FindSym(d, ':', shift, len);
92+
int l = atoi((const char*)(d + shift));
93+
if(e + l >= len) exit(2);
9494
size_t p;
9595

96-
write(stdout,&quo,1);
97-
for(p=e+1; p<=e+l; p++)
96+
fwrite(&quo, 1, 1, stdout);
97+
for(p = e + 1; p <= e + l; p++)
9898
{
99-
if(d[p]==127 || d[p]<32)
99+
if(d[p] == 127 || d[p] < 32)
100100
{
101-
write(stdout,&scr,1);
102-
write(stdout,&hxp,1);
101+
fwrite(&scr, 1, 1, stdout);
102+
fwrite(&hxp, 1, 1, stdout);
103103
Byte2Hex(d[p]);
104104
}
105-
else if(d[p]=='\\')
105+
else if(d[p] == '\\')
106106
{
107-
write(stdout,&scr,1);
108-
write(stdout,&scr,1);
107+
fwrite(&scr, 1, 1, stdout);
108+
fwrite(&scr, 1, 1, stdout);
109109
}
110-
else if(d[p]=='\"')
110+
else if(d[p] == '\"')
111111
{
112-
write(stdout,&scr,1);
113-
write(stdout,&quo,1);
112+
fwrite(&scr, 1, 1, stdout);
113+
fwrite(&quo, 1, 1, stdout);
114114
}
115-
else write(stdout,d+p,1);
115+
else fwrite(d + p, 1, 1, stdout);
116116
}
117117

118-
write(stdout,&quo,1);
119-
return e+l+1;
118+
fwrite(&quo, 1, 1, stdout);
119+
return e + l + 1;
120120
}
121121

122122
size_t PrintLst(const uint8_t* d, size_t shift, size_t len, int skip)
123123
{
124124
size_t ishift=shift+1;
125125

126-
write(stdout,&blst,1);
127-
write(stdout,&ret,1);
126+
fwrite(&blst, 1, 1, stdout);
127+
fwrite(&ret, 1, 1, stdout);
128128

129-
while(d[ishift]!='e')
129+
while(d[ishift] != 'e')
130130
{
131-
PrintSpc(skip+1);
132-
ishift=PrintObj(d,ishift,len,skip+1);
133-
write(stdout,&ret,1);
134-
if(ishift>=len) exit(2);
131+
PrintSpc(skip + 1);
132+
ishift = PrintObj(d, ishift, len, skip + 1);
133+
fwrite(&ret, 1, 1, stdout);
134+
if(ishift >= len) exit(2);
135135
}
136136
PrintSpc(skip);
137-
write(stdout,&elst,1);
138-
return ishift+1;
137+
fwrite(&elst, 1, 1, stdout);
138+
return ishift + 1;
139139
}
140140

141141
size_t PrintDct(const uint8_t* d, size_t shift, size_t len, int skip)
142142
{
143-
size_t ishift=shift+1;
143+
size_t ishift = shift + 1;
144144

145-
write(stdout,&bdct,1);
146-
write(stdout,&ret,1);
145+
fwrite(&bdct, 1, 1, stdout);
146+
fwrite(&ret, 1, 1, stdout);
147147

148-
while(d[ishift]!='e')
148+
while(d[ishift] != 'e')
149149
{
150-
PrintSpc(skip+1);
151-
if(DetType(d,ishift)!=OBJ_STR) exit(2);
152-
ishift=PrintStr(d,ishift,len);
153-
write(stdout,&sp,1);
154-
write(stdout,&eqv,1);
155-
write(stdout,&sp,1);
156-
ishift=PrintObj(d,ishift,len,skip+1);
157-
write(stdout,&ret,1);
158-
if(ishift>=len) exit(2);
150+
PrintSpc(skip + 1);
151+
if(DetType(d, ishift) != OBJ_STR) exit(2);
152+
ishift = PrintStr(d, ishift, len);
153+
fwrite(&sp, 1, 1, stdout);
154+
fwrite(&eqv, 1, 1, stdout);
155+
fwrite(&sp, 1, 1, stdout);
156+
ishift = PrintObj(d, ishift, len, skip + 1);
157+
fwrite(&ret, 1, 1, stdout);
158+
if(ishift >= len) exit(2);
159159
}
160160
PrintSpc(skip);
161-
write(stdout,&edct,1);
162-
return ishift+1;
161+
fwrite(&edct, 1, 1, stdout);
162+
return ishift + 1;
163163
}
164164

165165
int main(int argc, char** argv)
166166
{
167-
if(argc!=2) return 1;
167+
if(argc != 2)
168+
{
169+
fprintf(stderr, "Usage: %s file.torrent > file.torrent.txt", argv[0]);
170+
return 1;
171+
}
168172

169173
int fd;
170174
struct stat st;
171175
uint8_t *pdata;
172176

173-
fd=open(argv[1],O_RDONLY);
174-
if(fd==-1) return 1;
175-
if(fstat(fd,&st)!=0) return 1;
176-
if(st.st_size==0) return 1;
177-
pdata=(uint8_t*) mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
178-
PrintDct(pdata,0,st.st_size,0);
179-
write(stdout,&ret,1);
180-
munmap(pdata,st.st_size);
177+
fd = open(argv[1],O_RDONLY);
178+
if(fd == -1)
179+
{
180+
fprintf(stderr, "ERROR: Can't open %s", argv[1]);
181+
return 1;
182+
}
183+
if(fstat(fd, &st) != 0)
184+
{
185+
fprintf(stderr, "ERROR: Can't read %s", argv[1]);
186+
return 1;
187+
}
188+
if(st.st_size == 0)
189+
{
190+
fprintf(stderr, "ERROR: File %s empty", argv[1]);
191+
return 1;
192+
}
193+
pdata = (uint8_t*) mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
194+
PrintDct(pdata, 0, st.st_size, 0);
195+
fwrite(&ret, 1, 1, stdout);
196+
munmap(pdata, st.st_size);
181197
close(fd);
182198

183199
return 0;

0 commit comments

Comments
 (0)