-
Notifications
You must be signed in to change notification settings - Fork 2k
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
sys/shell/cmds: add cat and tee cmds #20568
Open
Enoch247
wants to merge
1
commit into
RIOT-OS:master
Choose a base branch
from
Enoch247:mv-shell-cmds
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,63 @@ | ||
/* | ||
* Copyright (C) 2018 OTA keys S.A. | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup sys_shell_commands | ||
* @{ | ||
* | ||
* @file | ||
* @brief Print the content of a file | ||
* | ||
* @author Vincent Dupont <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include <stdio.h> | ||
/*#include <string.h>*/ | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
|
||
#include "shell.h" | ||
|
||
static int _cat(int argc, char **argv) | ||
{ | ||
if (argc < 2) { | ||
printf("Usage: %s <file>\n", argv[0]); | ||
return 1; | ||
} | ||
/* With newlib or picolibc, low-level syscalls are plugged to RIOT vfs | ||
* on native, open/read/write/close/... are plugged to RIOT vfs */ | ||
#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) | ||
FILE *f = fopen(argv[1], "r"); | ||
if (f == NULL) { | ||
printf("file %s does not exist\n", argv[1]); | ||
return 1; | ||
} | ||
char c; | ||
while (fread(&c, 1, 1, f) != 0) { | ||
putchar(c); | ||
} | ||
fclose(f); | ||
#else | ||
int fd = open(argv[1], O_RDONLY); | ||
if (fd < 0) { | ||
printf("file %s does not exist\n", argv[1]); | ||
return 1; | ||
} | ||
char c; | ||
while (read(fd, &c, 1) != 0) { | ||
putchar(c); | ||
} | ||
close(fd); | ||
#endif | ||
fflush(stdout); | ||
return 0; | ||
} | ||
|
||
SHELL_COMMAND(cat, "print the content of a file", _cat); |
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,58 @@ | ||
/* | ||
* Copyright (C) 2018 OTA keys S.A. | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup sys_shell_commands | ||
* @{ | ||
* | ||
* @file | ||
* @brief Write a string in a file | ||
* | ||
* @author Vincent Dupont <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
|
||
#include "shell.h" | ||
|
||
static int _tee(int argc, char **argv) | ||
{ | ||
if (argc != 3) { | ||
printf("Usage: %s <file> <str>\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) | ||
FILE *f = fopen(argv[1], "w+"); | ||
if (f == NULL) { | ||
printf("error while trying to create %s\n", argv[1]); | ||
return 1; | ||
} | ||
if (fwrite(argv[2], 1, strlen(argv[2]), f) != strlen(argv[2])) { | ||
puts("Error while writing"); | ||
} | ||
fclose(f); | ||
#else | ||
int fd = open(argv[1], O_RDWR | O_CREAT, 00777); | ||
if (fd < 0) { | ||
printf("error while trying to create %s\n", argv[1]); | ||
return 1; | ||
} | ||
if (write(fd, argv[2], strlen(argv[2])) != (ssize_t)strlen(argv[2])) { | ||
puts("Error while writing"); | ||
} | ||
close(fd); | ||
#endif | ||
return 0; | ||
} | ||
|
||
SHELL_COMMAND(tee, "write a string in a file", _tee); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's going on here?