|
CS471/571 - Operating Systems
|
Displaying ./code/System-calls/cp.c
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define K 1024
extern int errno;
void putstr(char *mesg)
{
write(STDERR_FILENO, mesg, strlen(mesg));
}
void printerror(char *mesg)
{
putstr(mesg);
switch(errno) {
case EACCES: putstr(": Access denied\n"); break;
case ENOENT: putstr(": File not found\n"); break;
case EPERM: putstr(": Permission denied\n"); break;
default: putstr(": Some other error\n");
}
}
int main(int argc, char *argv[])
{
if (argc < 3) {
putstr("Usage: cp <input> <output>\n");
return 1;
}
int input = open(argv[1], O_RDONLY);
if (input < 0) {
printerror("input");
return 1;
}
int output = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (output < 0) {
printerror("output");
return 1;
}
char block[K];
int r; // How much was read by read()
while ( (r = read(input, block, K)) > 0 ) {
write(output, block, r);
}
close(input);
close(output);
return 0;
}
|