1 /* 2 * Test dataspace operations. 3 * 4 * Copyright (C) 2020, 2021 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 <stdio.h> 26 #include <string.h> 27 #include <stdlib.h> 28 29 #include "file.h" 30 #include "memory_utils.h" 31 32 33 34 int main(int argc, char *argv[]) 35 { 36 if (argc < 4) 37 { 38 printf("Need filename, step and sample size.\n"); 39 return 1; 40 } 41 42 /* Obtain filename and access parameters. */ 43 44 char *filename = argv[1]; 45 unsigned long step = atoi(argv[2]); 46 unsigned long sample = atoi(argv[3]); 47 48 /* Allocate a buffer for sampling from the file. */ 49 50 char buf[sample + 1]; 51 52 /* Obtain access to the filesystem. */ 53 54 l4_cap_idx_t server = l4re_env_get_cap("server"); 55 56 /* Invoke the open method to receive the file reference. */ 57 58 file_t file; 59 long err = file_open(&file, filename, server); 60 61 if (err) 62 { 63 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 64 return 1; 65 } 66 67 /* A region of the file is mapped. */ 68 69 err = file_mmap(&file, 0, page(10)); 70 71 if (err) 72 { 73 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 74 return 1; 75 } 76 77 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) 78 { 79 unsigned long remaining = file_populated_span(&file) - offset; 80 unsigned long sample_remaining = remaining < sample ? remaining : sample; 81 82 printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); 83 strncpy(buf, (file.memory + offset), sample_remaining); 84 buf[sample_remaining] = '\0'; 85 printf("%s\n", buf); 86 } 87 88 printf("File shown.\n"); 89 90 return 0; 91 } 92 93 // vim: tabstop=2 expandtab shiftwidth=2