1 /* 2 * Test dataspace operations. 3 * 4 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <systypes/fcntl.h> 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <stdlib.h> 30 31 #include <fsclient/file.h> 32 #include <mem/memory_utils.h> 33 34 35 36 int main(int argc, char *argv[]) 37 { 38 if (argc < 4) 39 { 40 printf("Need filename, step and sample size.\n"); 41 return 1; 42 } 43 44 /* Obtain filename and access parameters. */ 45 46 char *filename = argv[1]; 47 unsigned long step = atoi(argv[2]); 48 unsigned long sample = atoi(argv[3]); 49 50 /* Allocate a buffer for sampling from the file. */ 51 52 char buf[sample + 1]; 53 54 /* Obtain access to the filesystem. */ 55 56 l4_cap_idx_t server = l4re_env_get_cap("server"); 57 58 /* Invoke the open method to receive the file reference. */ 59 60 file_t file; 61 long err = file_open(&file, filename, O_RDWR, server); 62 63 if (err) 64 { 65 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 66 return 1; 67 } 68 69 /* A region of the file is mapped. */ 70 71 err = file_mmap(&file, 0, page(10), 0, 0, file_region_flags(file.flags)); 72 73 if (err) 74 { 75 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 76 return 1; 77 } 78 79 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) 80 { 81 unsigned long remaining = file_populated_span(&file) - offset; 82 unsigned long sample_remaining = remaining < sample ? remaining : sample; 83 84 printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); 85 strncpy(buf, (file.memory + offset), sample_remaining); 86 buf[sample_remaining] = '\0'; 87 printf("%s\n", buf); 88 } 89 90 printf("File shown.\n"); 91 92 return 0; 93 } 94 95 // vim: tabstop=2 expandtab shiftwidth=2