syserror() improvement and some usage in the coreutils

This commit is contained in:
qorg11 2020-06-04 01:46:22 +02:00
parent 0ff31634d9
commit 382582dd0e
Signed by: qorg11
GPG Key ID: 343FC20A4ACA62B9
4 changed files with 18 additions and 13 deletions

View File

@ -2,6 +2,8 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include "lib/error.h"
int int
cat(int fd, char *string) cat(int fd, char *string)
{ {
@ -11,15 +13,10 @@ cat(int fd, char *string)
while((lines=read(fd, buf, (long)sizeof(buf)))>0) while((lines=read(fd, buf, (long)sizeof(buf)))>0)
{ {
if(write(1,buf,lines)!=lines) if(write(1,buf,lines)!=lines)
{ syserror("Error copying string %s\n",string);
fprintf(stderr,"Error copying. %s",string);
return 1;
}
if (lines < 0) if (lines < 0)
{ syserror("Error reading %s\n",string);
fprintf(stderr,"Error reading %s",string);
return 1;
}
} }
return 0; return 0;
} }
@ -37,7 +34,7 @@ main(int argc, char *argv[])
{ {
fd = open(argv[i],O_RDONLY); fd = open(argv[i],O_RDONLY);
if(fd<0) if(fd<0)
fprintf(stderr,"Cannot open %s\n",argv[i]); syserror("Cannot open %s\n",argv[i]);
else else
{ {
cat(fd,argv[i]); cat(fd,argv[i]);

View File

@ -1,8 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include "error.h" #include "error.h"
int syserror(char *msg) int syserror(const char *msg, ...)
{ {
fprintf(stderr,"%s",msg); va_list args;
va_start(args,msg);
vfprintf(stderr,msg,args);
va_end(args);
return 1; return 1;
} }

View File

@ -1 +1,3 @@
int syserror(char *msg); #include <stdio.h>
int syserror(const char *msg, ...);

View File

@ -1,5 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include "lib/error.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -14,7 +16,7 @@ main(int argc, char *argv[])
int fd = remove(argv[i]); int fd = remove(argv[i]);
if(fd == -1) if(fd == -1)
{ {
fprintf(stderr,"Error removing file: %s\n",argv[i]); syserror("Error removing file: %s\n",argv[i]);
} }
} }