Fix incorrect path traversal
[mfat.git] / examples / fatcat.c
blob700acca0b40e4550b17b328952ba63517870c78a
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (C) 2022 Marcus Geelnard
3 //
4 // Redistribution and use in source and binary forms, with or without modification, are permitted
5 // provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this list of
8 // conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11 // conditions and the following disclaimer in the documentation and/or other materials provided
12 // with the distribution.
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
17 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
21 // WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 //--------------------------------------------------------------------------------------------------
24 #include <mfat.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <unistd.h>
31 static int blkread(char* ptr, unsigned block_no, void* custom) {
32 int fd = *(int*)custom;
33 if (lseek(fd, MFAT_BLOCK_SIZE * (size_t)block_no, SEEK_SET) == -1) {
34 return -1;
36 size_t num_bytes = read(fd, ptr, MFAT_BLOCK_SIZE);
37 return (num_bytes != MFAT_BLOCK_SIZE) && (num_bytes != 0) ? -1 : 0;
40 static int blkwrite(const char* ptr, unsigned block_no, void* custom) {
41 int fd = *(int*)custom;
42 if (lseek(fd, MFAT_BLOCK_SIZE * (size_t)block_no, SEEK_SET) == -1) {
43 return -1;
45 size_t num_bytes = write(fd, ptr, MFAT_BLOCK_SIZE);
46 return (num_bytes != MFAT_BLOCK_SIZE) && (num_bytes != 0) ? -1 : 0;
49 int main(int argc, char** argv) {
50 // Get arguments.
51 if (argc != 3) {
52 printf("Usage: %s FATIMAGE FILE\n", argv[0]);
53 return 1;
55 const char* img_path = argv[1];
56 const char* file_name = argv[2];
58 // Open the FAT image file or device.
59 int img_fd = open(img_path, O_RDONLY);
60 if (img_fd == -1) {
61 fprintf(stderr, "*** Failed to open the FAT image\n");
62 return 1;
65 // Mount the image in MFAT.
66 if (mfat_mount(blkread, blkwrite, &img_fd) == -1) {
67 close(img_fd);
68 fprintf(stderr, "*** Failed to init MFAT\n");
69 return 1;
72 // Print the contents of the file to stdout.
73 int fd = mfat_open(file_name, MFAT_O_RDONLY);
74 if (fd != -1) {
75 uint8_t buf[100];
76 while (1) {
77 ssize_t bytes_read = mfat_read(fd, &buf[0], sizeof(buf));
78 if (bytes_read > 0) {
79 ssize_t bytes_written = write(STDOUT_FILENO, &buf[0], bytes_read);
80 (void)bytes_written; // Silence compiler warnings.
81 } else {
82 // 0 = EOF, -1 = Error
83 if (bytes_read == -1) {
84 fprintf(stderr, "*** Failed to read %s\n", file_name);
86 break;
90 // Close the file.
91 mfat_close(fd);
92 } else {
93 fprintf(stderr, "*** Failed to open %s\n", file_name);
96 // Unmount and close down.
97 mfat_unmount();
98 close(img_fd);
100 return 0;