Compare commits

...

3 Commits

Author SHA1 Message Date
qorg11 382582dd0e
syserror() improvement and some usage in the coreutils 2020-06-04 01:46:22 +02:00
qorg11 0ff31634d9
Created error.h and error.c for syserror() 2020-06-04 01:22:06 +02:00
qorg11 aff3c77b37
Readme in org 2020-06-04 00:28:53 +02:00
6 changed files with 33 additions and 12 deletions

View File

@ -1,5 +1,6 @@
# k9core
* k9core
Extremely minimalist coreutils.
** License
Licensed under the k9pl. See LICENSE for more information.

View File

@ -2,6 +2,8 @@
#include <unistd.h>
#include <fcntl.h>
#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]);

View File

@ -2,6 +2,8 @@
#include <stdlib.h>
#include <signal.h>
#include "lib/error.h"
int
main(int argc, char *argv[])
{
@ -21,6 +23,10 @@ main(int argc, char *argv[])
fprintf(stderr, "Specify who to kill\n");
return 1;
}
kill(pid,sig);
int fd = kill(pid,sig);
if(fd == -1)
syserror("Could not kill process\n");
return 0;
}

12
src/lib/error.c Normal file
View File

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

3
src/lib/error.h Normal file
View File

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

View File

@ -1,5 +1,7 @@
#include <stdio.h>
#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]);
}
}