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 <ipc/thread.h> 30 31 #include "file.h" 32 #include "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 file_t context; 58 59 long err = file_context(&context, server); 60 61 if (err) 62 { 63 printf("Could not obtain context: %s\n", l4sys_errtostr(err)); 64 return 1; 65 } 66 67 /* Write the filename. */ 68 69 strcpy(context.memory, filename); 70 71 /* Invoke the open method to receive the file reference. */ 72 73 file_t file; 74 75 err = file_open(&file, &context); 76 77 if (err) 78 { 79 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 80 return 1; 81 } 82 83 /* A region of the file is mapped. */ 84 85 err = file_mmap(&file, 0, page(10)); 86 87 if (err) 88 { 89 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 90 return 1; 91 } 92 93 for (unsigned long offset = 0; offset < file_span(&file); offset += step) 94 { 95 unsigned long remaining = file_span(&file) - offset; 96 unsigned long sample_remaining = remaining < sample ? remaining : sample; 97 98 printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); 99 strncpy(buf, (file.memory + offset), sample_remaining); 100 buf[sample_remaining] = '\0'; 101 printf("%s\n", buf); 102 } 103 104 return 0; 105 } 106 107 // vim: tabstop=2 expandtab shiftwidth=2