paul@65 | 1 | /* |
paul@65 | 2 | * Test pipe operations. |
paul@65 | 3 | * |
paul@65 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@65 | 5 | * |
paul@65 | 6 | * This program is free software; you can redistribute it and/or |
paul@65 | 7 | * modify it under the terms of the GNU General Public License as |
paul@65 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@65 | 9 | * the License, or (at your option) any later version. |
paul@65 | 10 | * |
paul@65 | 11 | * This program is distributed in the hope that it will be useful, |
paul@65 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@65 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@65 | 14 | * GNU General Public License for more details. |
paul@65 | 15 | * |
paul@65 | 16 | * You should have received a copy of the GNU General Public License |
paul@65 | 17 | * along with this program; if not, write to the Free Software |
paul@65 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@65 | 19 | * Boston, MA 02110-1301, USA |
paul@65 | 20 | */ |
paul@65 | 21 | |
paul@65 | 22 | #include <l4/re/env.h> |
paul@65 | 23 | #include <l4/sys/err.h> |
paul@65 | 24 | |
paul@65 | 25 | #include <stdio.h> |
paul@65 | 26 | #include <string.h> |
paul@65 | 27 | #include <stdlib.h> |
paul@65 | 28 | |
paul@108 | 29 | #include <fsclient/client.h> |
paul@94 | 30 | #include <fsclient/file.h> |
paul@94 | 31 | #include <mem/memory_utils.h> |
paul@65 | 32 | |
paul@65 | 33 | |
paul@65 | 34 | |
paul@66 | 35 | const unsigned int PIPE_PAGES = 2; |
paul@66 | 36 | |
paul@65 | 37 | int main(void) |
paul@65 | 38 | { |
paul@65 | 39 | /* Obtain access to the filesystem. */ |
paul@65 | 40 | |
paul@65 | 41 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@65 | 42 | |
paul@65 | 43 | /* Invoke the open method to receive the file reference. */ |
paul@65 | 44 | |
paul@65 | 45 | file_t reader, writer; |
paul@66 | 46 | long err = pipe_open(page(PIPE_PAGES), &reader, &writer, server); |
paul@65 | 47 | |
paul@65 | 48 | if (err) |
paul@65 | 49 | { |
paul@65 | 50 | printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); |
paul@65 | 51 | return 1; |
paul@65 | 52 | } |
paul@65 | 53 | |
paul@65 | 54 | /* Use the writer to fill the pipe with data. */ |
paul@65 | 55 | |
paul@108 | 56 | offset_t size = 600; |
paul@108 | 57 | char buffer[size]; |
paul@108 | 58 | int region = 0; |
paul@108 | 59 | |
paul@108 | 60 | for (int loop = 0; loop < 3; loop++) |
paul@65 | 61 | { |
paul@108 | 62 | while (1) |
paul@108 | 63 | { |
paul@108 | 64 | printf("Writing %ld to pipe...\n", size); |
paul@65 | 65 | |
paul@108 | 66 | memset(buffer, (int) 'a' + region, size); |
paul@108 | 67 | |
paul@108 | 68 | offset_t nwritten = client_write(&writer, buffer, size); |
paul@65 | 69 | |
paul@108 | 70 | printf("Written %ld to pipe...\n", nwritten); |
paul@108 | 71 | |
paul@108 | 72 | for (offset_t i = 0; i < nwritten; i += 60) |
paul@108 | 73 | { |
paul@108 | 74 | fwrite(buffer + i, sizeof(char), nwritten - i > 60 ? 60 : nwritten - i, stdout); |
paul@108 | 75 | fputs("\n", stdout); |
paul@108 | 76 | } |
paul@65 | 77 | |
paul@108 | 78 | if (!nwritten) |
paul@108 | 79 | break; |
paul@108 | 80 | |
paul@108 | 81 | region++; |
paul@108 | 82 | |
paul@108 | 83 | if (region == 26) |
paul@108 | 84 | region = 0; |
paul@65 | 85 | } |
paul@65 | 86 | |
paul@108 | 87 | /* Use the reader to obtain data from the pipe. */ |
paul@65 | 88 | |
paul@108 | 89 | offset_t nread; |
paul@65 | 90 | |
paul@108 | 91 | do |
paul@65 | 92 | { |
paul@108 | 93 | printf("Reading %ld from pipe...\n", size); |
paul@108 | 94 | |
paul@108 | 95 | nread = client_read(&reader, buffer, size); |
paul@65 | 96 | |
paul@108 | 97 | printf("Read %ld from pipe...\n", nread); |
paul@65 | 98 | |
paul@108 | 99 | for (offset_t i = 0; i < nread; i += 60) |
paul@108 | 100 | { |
paul@108 | 101 | fwrite(buffer + i, sizeof(char), nread - i > 60 ? 60 : nread - i, stdout); |
paul@108 | 102 | fputs("\n", stdout); |
paul@108 | 103 | } |
paul@108 | 104 | } |
paul@108 | 105 | while (nread); |
paul@65 | 106 | } |
paul@65 | 107 | |
paul@65 | 108 | printf("Data shown.\n"); |
paul@65 | 109 | |
paul@65 | 110 | return 0; |
paul@65 | 111 | } |
paul@65 | 112 | |
paul@65 | 113 | // vim: tabstop=2 expandtab shiftwidth=2 |