mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-21 02:16:49 +08:00
file_ops begun
This commit is contained in:
parent
cc00e6fa56
commit
5e63912d39
3 changed files with 37 additions and 13 deletions
|
|
@ -19,16 +19,26 @@ fd .req w28
|
|||
main: stp x29, x30, [sp, -16]!
|
||||
stp x27, x28, [sp, -16]!
|
||||
bl open_file
|
||||
cbz x0, 1f
|
||||
// w0 will contain either the file descriptor of the new
|
||||
// file or -1 for a failure. Note that the value in w0
|
||||
// has also been copied to "fd" - a register alias.
|
||||
cmp w0, wzr
|
||||
bge 1f
|
||||
// If we get here, the open has failed. Use perror() to
|
||||
// print a meaningful error and exit.
|
||||
// print a meaningful error and branch to exit. The return
|
||||
// code of the program will be set to non-zero inside open_fail.
|
||||
bl open_fail
|
||||
b 99f
|
||||
|
||||
1: // When we get here, the file is open.
|
||||
1: // When we get here, the file is open. Write some data to it.
|
||||
// If write_file returns non-zero, it signifies an error. If
|
||||
// so, branch to the file closing code since the file is open.
|
||||
// The error message will be printed in write_data.
|
||||
bl write_data
|
||||
cbnz w0, 10f
|
||||
|
||||
// When we get here, we are done. Close the file.
|
||||
mov w0, fd
|
||||
10: mov w0, fd
|
||||
bl close
|
||||
mov retval, wzr
|
||||
|
||||
|
|
@ -45,8 +55,12 @@ main: stp x29, x30, [sp, -16]!
|
|||
|
||||
open_file:
|
||||
stp x29, x30, [sp, -16]!
|
||||
ldp x29, x30, [sp], 16
|
||||
mov x0, 1
|
||||
ldr x0, =fname
|
||||
mov w1, 0102
|
||||
mov w2, 0600
|
||||
bl open
|
||||
mov fd, w0
|
||||
ldp x29, x30, [sp], 16
|
||||
ret
|
||||
|
||||
|
||||
|
|
@ -62,9 +76,14 @@ open_fail:
|
|||
ldp x29, x30, [sp], 16
|
||||
ret
|
||||
|
||||
write_data:
|
||||
stp x29, x30, [sp, -16]!
|
||||
ldp x29, x30, [sp], 16
|
||||
ret
|
||||
|
||||
.data
|
||||
prog: .asciz "file_ops"
|
||||
fname: .asciz "test.txt"
|
||||
|
||||
txt: .asciz "some data\n"
|
||||
txt_s: .word txt_s - txt - 1 // strlen(txt)
|
||||
.end
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@
|
|||
int main(int argc, char **argv) {
|
||||
char * fname = "test.txt";
|
||||
char * p_legend = NULL;
|
||||
int fd = open(fname, O_RDWR | O_CREAT, 00600);
|
||||
int retval = 0;
|
||||
int fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
||||
int retval = 0;
|
||||
off_t seek_return;
|
||||
char buffer[80];
|
||||
|
||||
|
|
@ -15,7 +15,10 @@ int main(int argc, char **argv) {
|
|||
p_legend = fname;
|
||||
goto out;
|
||||
} else {
|
||||
printf("Flags used to open file: %o\n", O_RDWR | O_CREAT);
|
||||
printf("Flags used to open file: 0%o 0%o\n",
|
||||
O_RDWR | O_CREAT,
|
||||
S_IRUSR | S_IWUSR
|
||||
);
|
||||
for (int counter = 0; counter < 10; counter++) {
|
||||
if (write(fd, "data\n", 5) != 5) {
|
||||
p_legend = "On writing";
|
||||
|
|
@ -39,10 +42,12 @@ int main(int argc, char **argv) {
|
|||
|
||||
out: if (fd >= 0) {
|
||||
close(fd);
|
||||
if (unlink(fname)) {
|
||||
/* if (unlink(fname)) {
|
||||
p_legend = "On unlinking";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
printf("All went well. File is deleted.\n");
|
||||
}
|
||||
*/ }
|
||||
|
||||
if (p_legend != NULL) {
|
||||
perror(p_legend);
|
||||
|
|
|
|||
Loading…
Reference in a new issue