-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from fischerling/implement_dup
Implement dup syscall
- Loading branch information
Showing
11 changed files
with
161 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/// @file dup.c | ||
/// @brief | ||
/// @copyright (c) 2024 This file is distributed under the MIT License. | ||
/// See LICENSE.md for details. | ||
|
||
#include "sys/unistd.h" | ||
#include "system/syscall_types.h" | ||
#include "sys/errno.h" | ||
|
||
_syscall1(int, dup, int, fd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# List of programs. | ||
set(TEST_LIST | ||
t_dup.c | ||
t_creat.c | ||
t_write_read.c | ||
t_gid.c | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/// @file t_dup.c | ||
/// @brief Test the dup syscall | ||
/// @copyright (c) 2024 This file is distributed under the MIT License. | ||
/// See LICENSE.md for details. | ||
|
||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/unistd.h> | ||
#include <strerror.h> | ||
#include <string.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
char *file = "t_dup_file"; | ||
int fd1, fd2; | ||
int flags = O_WRONLY | O_CREAT | O_TRUNC; | ||
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | ||
|
||
fd1 = open(file, flags, mode); | ||
if (fd1 < 0) { | ||
printf("Failed to open file %s: %s\n", file, strerror(errno)); | ||
exit(1); | ||
} | ||
|
||
fd2 = dup(fd1); | ||
if (fd2 < 0) { | ||
printf("Failed to dup fd %d: %s\n", fd1, strerror(errno)); | ||
exit(1); | ||
} | ||
|
||
if (write(fd1, "foo", 3) != 3) { | ||
printf("Writing to fd %d failed: %s\n", fd1, strerror(errno)); | ||
exit(1); | ||
} | ||
close(fd1); | ||
|
||
if (write(fd2, "bar", 3) != 3) { | ||
printf("Writing to fd %d failed: %s\n", fd2, strerror(errno)); | ||
exit(1); | ||
} | ||
close(fd2); | ||
|
||
fd1 = open(file, O_RDONLY, 0); | ||
if (fd1 < 0) { | ||
printf("Failed to open file %s: %s\n", file, strerror(errno)); | ||
exit(1); | ||
} | ||
|
||
char buf[7]; | ||
buf[6] = 0; | ||
if (read(fd1, &buf, 6) < 0) { | ||
printf("Reading from fd %d failed: %s\n", fd1, strerror(errno)); | ||
exit(1); | ||
} | ||
|
||
if (strcmp(buf, "foobar") != 0) { | ||
printf("Unexpected file content: %s\n", buf); | ||
exit(1); | ||
} | ||
|
||
unlink(file); | ||
return 0; | ||
} |