mirror of
https://github.com/fumiama/base16384.git
synced 2026-06-07 19:40:23 +08:00
优化 perror 输出
This commit is contained in:
28
base16384.c
28
base16384.c
@@ -34,12 +34,12 @@ static int encode_file(const char* input, const char* output) {
|
||||
fp = stdin;
|
||||
} else inputsize = get_file_size(input);
|
||||
if(inputsize < 0) {
|
||||
perror("Get file size error: ");
|
||||
perror("Get file size error");
|
||||
return 1;
|
||||
}
|
||||
fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb");
|
||||
if(!fpo) {
|
||||
perror("Fopen output file error: ");
|
||||
perror("Fopen output file error");
|
||||
return 2;
|
||||
}
|
||||
if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use encbuf & fread
|
||||
@@ -49,7 +49,7 @@ static int encode_file(const char* input, const char* output) {
|
||||
#endif
|
||||
if(!fp) fp = fopen(input, "rb");
|
||||
if(!fp) {
|
||||
perror("Fopen input file error: ");
|
||||
perror("Fopen input file error");
|
||||
return 3;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ static int encode_file(const char* input, const char* output) {
|
||||
while((cnt = fread(encbuf, sizeof(char), inputsize, fp))) {
|
||||
int n = encode(encbuf, cnt, decbuf, outputsize);
|
||||
if(fwrite(decbuf, n, 1, fpo) <= 0) {
|
||||
perror("Write file error: ");
|
||||
perror("Write file error");
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
@@ -72,12 +72,12 @@ static int encode_file(const char* input, const char* output) {
|
||||
} else { // small file, use mmap & fwrite
|
||||
int fd = open(input, O_RDONLY);
|
||||
if(fd <= 0) {
|
||||
perror("Open input file error: ");
|
||||
perror("Open input file error");
|
||||
return 5;
|
||||
}
|
||||
char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if(input_file == MAP_FAILED) {
|
||||
perror("Map input file error: ");
|
||||
perror("Map input file error");
|
||||
return 6;
|
||||
}
|
||||
int outputsize = encode_len(inputsize)+16;
|
||||
@@ -85,7 +85,7 @@ static int encode_file(const char* input, const char* output) {
|
||||
fputc(0xFF, fpo);
|
||||
int n = encode(input_file, (int)inputsize, decbuf, outputsize);
|
||||
if(fwrite(decbuf, n, 1, fpo) <= 0) {
|
||||
perror("Write file error: ");
|
||||
perror("Write file error");
|
||||
return 7;
|
||||
}
|
||||
munmap(input_file, (size_t)inputsize);
|
||||
@@ -122,12 +122,12 @@ static int decode_file(const char* input, const char* output) {
|
||||
fp = stdin;
|
||||
} else inputsize = get_file_size(input);
|
||||
if(inputsize < 0) {
|
||||
perror("Get file size error: ");
|
||||
perror("Get file size error");
|
||||
return 1;
|
||||
}
|
||||
fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb");
|
||||
if(!fpo) {
|
||||
perror("Fopen output file error: ");
|
||||
perror("Fopen output file error");
|
||||
return 2;
|
||||
}
|
||||
if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use decbuf & fread
|
||||
@@ -137,7 +137,7 @@ static int decode_file(const char* input, const char* output) {
|
||||
#endif
|
||||
if(!fp) fp = fopen(input, "rb");
|
||||
if(!fp) {
|
||||
perror("Fopen input file error: ");
|
||||
perror("Fopen input file error");
|
||||
return 3;
|
||||
}
|
||||
int outputsize = decode_len(inputsize, 0)+16;
|
||||
@@ -150,7 +150,7 @@ static int decode_file(const char* input, const char* output) {
|
||||
decbuf[cnt++] = end;
|
||||
}
|
||||
if(fwrite(encbuf, decode(decbuf, cnt, encbuf, outputsize), 1, fpo) <= 0) {
|
||||
perror("Write file error: ");
|
||||
perror("Write file error");
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
@@ -162,18 +162,18 @@ static int decode_file(const char* input, const char* output) {
|
||||
} else { // small file, use mmap & fwrite
|
||||
int fd = open(input, O_RDONLY);
|
||||
if(fd <= 0) {
|
||||
perror("Open input file error: ");
|
||||
perror("Open input file error");
|
||||
return 5;
|
||||
}
|
||||
char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if(input_file == MAP_FAILED) {
|
||||
perror("Map input file error: ");
|
||||
perror("Map input file error");
|
||||
return 6;
|
||||
}
|
||||
int outputsize = decode_len(inputsize, 0)+16;
|
||||
int off = skip_offset(input_file);
|
||||
if(fwrite(encbuf, decode(input_file+off, inputsize-off, encbuf, outputsize), 1, fpo) <= 0) {
|
||||
perror("Write file error: ");
|
||||
perror("Write file error");
|
||||
return 7;
|
||||
}
|
||||
munmap(input_file, (size_t)inputsize);
|
||||
|
||||
Reference in New Issue
Block a user