Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding Makfile/README and copy utility #1

Open
wants to merge 1 commit into
base: testbranch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mjb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TOPDIR=$(shell pwd)
include $(TOPDIR)/include.mk

all: copy

copy: copy.o
$(CC) -o $@ $<

7 changes: 7 additions & 0 deletions mjb/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This directory contains my own implementation of examples/problems from Maurice J Bach's
"The Design of Unix Operating System".

Index (Chronological order)
=====
1. copy.c: Figure 1.3 Copy a file

84 changes: 84 additions & 0 deletions mjb/copy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* copy.c: My own implementation of Maurice J. Bach Figure 1.3 from
* The design of Unix Operating System.
* Usage: copy srcfile dstfile
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define DEF_IO_BLOCK_SIZE 2048
char buffer[DEF_IO_BLOCK_SIZE];
int version = 1;

// copy srcfd to dstfd: 0 on success, -1 on failure
int copyfd(int srcfd, int dstfd)
{
int count = 0;
char buffer[DEF_IO_BLOCK_SIZE];

while ((count = read(srcfd, buffer, sizeof buffer)) > 0) {
if (write(dstfd, buffer, count) == -1) {
fprintf(stderr, "write error on fd: %d\n", dstfd);
return -1;
}
}

// check of read error
if (count == -1) {
fprintf(stderr, "read error on fd: %d\n", srcfd);
return -1;
} else {
return 0;
}
}

int main(int argc, char *argv[])
{
int ret, srcfd, dstfd;
char *srcfile, *dstfile;

if (argc < 3) {
fprintf(stderr, "%s requires 2 arguments\n", argv[0]);
fprintf(stdout, "Usage: %s srcfile dstfile\n", argv[0]);
exit(EXIT_FAILURE);
} else {
srcfile = argv[1];
dstfile = argv[2];
}

// open src file in read-only mode
srcfd = open(argv[1], O_RDONLY);
if (srcfd == -1) {
fprintf(stderr, "cannot open file: %s due to: %s\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}

// create the dst. file
dstfd = creat(argv[2], 0666);
if (dstfd == -1) {
fprintf(stderr, "cannot create file: %s due to %s\n", argv[2],
strerror(errno));
close(srcfd);
exit(EXIT_FAILURE);
}

// copy the src file to dst. file
if (copyfd(srcfd, dstfd) == -1) {
fprintf(stderr, "failed to copy file: %s to file: %s\n", srcfile,
dstfile);
ret = EXIT_FAILURE;
} else {
ret = EXIT_SUCCESS;
}

// cleanup and exit
close(srcfd);
close(dstfd);
exit(ret);
}
5 changes: 5 additions & 0 deletions mjb/include.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CFLAGS=-g -Wall
INCLUDES=-I $(TOPDIR)/include

%.o: %.c
$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@