forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
145 lines (130 loc) · 3.4 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* 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 examples
* @{
*
* @file
* @brief File system usage example application
*
* @author Vincent Dupont <vincent@otakeys.com>
*
* @}
*/
#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;
}
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(cat, "print the content of a file", _cat);
SHELL_COMMAND(tee, "write a string in a file", _tee);
/* constfs example */
#include "fs/constfs.h"
#define HELLO_WORLD_CONTENT "Hello World!\n"
#define HELLO_RIOT_CONTENT "Hello RIOT!\n"
/* this defines two const files in the constfs */
static constfs_file_t constfs_files[] = {
{
.path = "/hello-world",
.size = sizeof(HELLO_WORLD_CONTENT),
.data = (const uint8_t *)HELLO_WORLD_CONTENT,
},
{
.path = "/hello-riot",
.size = sizeof(HELLO_RIOT_CONTENT),
.data = (const uint8_t *)HELLO_RIOT_CONTENT,
}
};
/* this is the constfs specific descriptor */
static constfs_t constfs_desc = {
.nfiles = ARRAY_SIZE(constfs_files),
.files = constfs_files,
};
/* constfs mount point, as for previous example, it needs a file system driver,
* a mount point and private_data as a pointer to the constfs descriptor */
static vfs_mount_t const_mount = {
.fs = &constfs_file_system,
.mount_point = "/const",
.private_data = &constfs_desc,
};
int main(void)
{
int res = vfs_mount(&const_mount);
if (res < 0) {
puts("Error while mounting constfs");
}
else {
puts("constfs mounted successfully");
}
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}