diff --git a/src/cat.c b/src/cat.c index d672a83..6a98d17 100644 --- a/src/cat.c +++ b/src/cat.c @@ -2,6 +2,8 @@ #include #include +#include "lib/error.h" + int cat(int fd, char *string) { @@ -11,15 +13,10 @@ cat(int fd, char *string) while((lines=read(fd, buf, (long)sizeof(buf)))>0) { if(write(1,buf,lines)!=lines) - { - fprintf(stderr,"Error copying. %s",string); - return 1; - } + syserror("Error copying string %s\n",string); + if (lines < 0) - { - fprintf(stderr,"Error reading %s",string); - return 1; - } + syserror("Error reading %s\n",string); } return 0; } @@ -37,7 +34,7 @@ main(int argc, char *argv[]) { fd = open(argv[i],O_RDONLY); if(fd<0) - fprintf(stderr,"Cannot open %s\n",argv[i]); + syserror("Cannot open %s\n",argv[i]); else { cat(fd,argv[i]); diff --git a/src/lib/error.c b/src/lib/error.c index 8822555..2566a76 100644 --- a/src/lib/error.c +++ b/src/lib/error.c @@ -1,8 +1,12 @@ #include +#include #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; } diff --git a/src/lib/error.h b/src/lib/error.h index 9cf87d7..20b04d4 100644 --- a/src/lib/error.h +++ b/src/lib/error.h @@ -1 +1,3 @@ -int syserror(char *msg); +#include + +int syserror(const char *msg, ...); diff --git a/src/rm.c b/src/rm.c index bd43721..20363d2 100644 --- a/src/rm.c +++ b/src/rm.c @@ -1,5 +1,7 @@ #include +#include "lib/error.h" + int main(int argc, char *argv[]) { @@ -14,7 +16,7 @@ main(int argc, char *argv[]) int fd = remove(argv[i]); if(fd == -1) { - fprintf(stderr,"Error removing file: %s\n",argv[i]); + syserror("Error removing file: %s\n",argv[i]); } }