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

Done lab #3

Open
wants to merge 2 commits into
base: master
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ UPROGS=\
_usertests\
_wc\
_zombie\
_myprogram\
_cp\
_mv\

fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
Expand Down Expand Up @@ -253,6 +256,9 @@ qemu-nox-gdb: fs.img xv6.img .gdbinit
EXTRA=\
mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\
ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
myprogram.c\
cp.c\
mv.c\
printf.c umalloc.c\
README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
.gdbinit.tmpl gdbutil\
Expand Down
52 changes: 52 additions & 0 deletions cp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "types.h"
#include "user.h"
#include "fcntl.h"
#include "stat.h"

int is_directory(struct stat *st) {
return st->type == T_DIR;
}

void cp(char *from, char *to) {
int fd1, fd2, n;
char buf[512];

struct stat st_1;

if (stat(from, &st_1) < 0){
printf(1, "Error in source file\n");
}

if (is_directory(&st_1)) {
printf(1, "Can not copy from directory\n");
}

fd1 = open(from, O_RDONLY);
fd2 = open(to, O_CREATE | O_WRONLY);

if(fd1 < 0) {
printf(1, "Cannot open source file\n");
exit();
}

if(fd2 < 0) {
printf(1, "Cannot open destination file\n");
exit();
}

while((n = read(fd1, buf, sizeof(buf))) > 0) {
write(fd2, buf, n);
}

close(fd1);
close(fd2);
}

int main(int argc, char *argv[]) {
if(argc != 3) {
printf(1, "Wrong number of arguments\n");
exit();
}
cp(argv[1], argv[2]);
exit();
}
21 changes: 21 additions & 0 deletions mv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "types.h"
#include "user.h"

int main(int argc, char *argv[]) {
if(argc != 3) {
printf(1, "Usage: mv <from_file> <to_file>\n");
exit();
}

if(link(argv[1], argv[2]) < 0) {
printf(1, "Cannot link first path to second\n");
exit();
}

if(unlink(argv[1]) < 0) {
printf(1, "Cannot unlink file from source path\n");
exit();
}

exit();
}
9 changes: 9 additions & 0 deletions myprogram.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "types.h"
#include "stat.h"
#include "user.h"

int main(void)
{
printf(1, "Hello world!\n");
exit();
}
Empty file added test_file1.txt
Empty file.