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