-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test_dup3 |
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,20 @@ | ||
ifeq ($(ARCH), x86_64) | ||
CROSS_COMPILE=x86_64-linux-musl- | ||
else ifeq ($(ARCH), riscv64) | ||
CROSS_COMPILE=riscv64-linux-musl- | ||
endif | ||
|
||
CC=$(CROSS_COMPILE)gcc | ||
|
||
.PHONY: all | ||
all: main.c | ||
$(CC) -static -o test_dup3 main.c | ||
|
||
.PHONY: install clean | ||
install: all | ||
mv test_dup3 $(DADK_CURRENT_BUILD_DIR)/test_dup3 | ||
|
||
clean: | ||
rm test_dup3 *.o | ||
|
||
fmt: |
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,30 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
|
||
int main() { | ||
int fd = open("/history_commands.txt", O_RDONLY); | ||
if (fd < 0) { | ||
perror("Failed to open file"); | ||
return 1; | ||
} | ||
|
||
int new_fd = 777; | ||
int rt = dup3(fd, new_fd, O_CLOEXEC); | ||
if (rt < 0) { | ||
perror("Failed to duplicate file descriptor with flags"); | ||
} | ||
|
||
char buffer[100]; | ||
int bytes_read = read(new_fd, buffer, sizeof(buffer)); | ||
if (bytes_read < 0) { | ||
perror("Failed to read data"); | ||
return 1; | ||
} | ||
|
||
printf("Data:\n %.*s\n", bytes_read, buffer); | ||
|
||
close(fd); | ||
close(new_fd); | ||
return 0; | ||
} |
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,22 @@ | ||
{ | ||
"name": "test_dup3", | ||
"version": "0.1.0", | ||
"description": "测试dup3", | ||
"task_type": { | ||
"BuildFromSource": { | ||
"Local": { | ||
"path": "apps/test_dup3" | ||
} | ||
} | ||
}, | ||
"depends": [], | ||
"build": { | ||
"build_command": "make install" | ||
}, | ||
"install": { | ||
"in_dragonos_path": "/bin" | ||
}, | ||
"clean": { | ||
"clean_command": "make clean" | ||
} | ||
} |