| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | #include "containers/containers.h" |
| 31 | #include "containers/core/containers_logging.h" |
| 32 | #include "containers/core/containers_io.h" |
| 33 | |
| 34 | #include "nb_io.h" |
| 35 | |
| 36 | #define MAX_BUFFER_SIZE 2048 |
| 37 | |
| 38 | int main(int argc, char **argv) |
| 39 | { |
| 40 | char buffer[MAX_BUFFER_SIZE]; |
| 41 | VC_CONTAINER_IO_T *read_io, *write_io; |
| 42 | VC_CONTAINER_STATUS_T status; |
| 43 | size_t received; |
| 44 | bool ready = true; |
| 45 | |
| 46 | if (argc < 3) |
| 47 | { |
| 48 | LOG_INFO(NULL, "Usage:\n%s <read URI> <write URI>\n" , argv[0]); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | read_io = vc_container_io_open(argv[1], VC_CONTAINER_IO_MODE_READ, &status); |
| 53 | if (!read_io) |
| 54 | { |
| 55 | LOG_INFO(NULL, "Opening <%s> for read failed: %d\n" , argv[1], status); |
| 56 | return 2; |
| 57 | } |
| 58 | |
| 59 | write_io = vc_container_io_open(argv[2], VC_CONTAINER_IO_MODE_WRITE, &status); |
| 60 | if (!write_io) |
| 61 | { |
| 62 | vc_container_io_close(read_io); |
| 63 | LOG_INFO(NULL, "Opening <%s> for write failed: %d\n" , argv[2], status); |
| 64 | return 3; |
| 65 | } |
| 66 | |
| 67 | nb_set_nonblocking_input(1); |
| 68 | |
| 69 | while (ready) |
| 70 | { |
| 71 | size_t total_written = 0; |
| 72 | |
| 73 | received = vc_container_io_read(read_io, buffer, sizeof(buffer)); |
| 74 | while (ready && total_written < received) |
| 75 | { |
| 76 | total_written += vc_container_io_write(write_io, buffer + total_written, received - total_written); |
| 77 | ready &= (write_io->status == VC_CONTAINER_SUCCESS); |
| 78 | } |
| 79 | ready &= (read_io->status == VC_CONTAINER_SUCCESS); |
| 80 | |
| 81 | if (nb_char_available()) |
| 82 | { |
| 83 | char c = nb_get_char(); |
| 84 | |
| 85 | switch (c) |
| 86 | { |
| 87 | case 'q': |
| 88 | case 'Q': |
| 89 | case 0x04: /* CTRL+D */ |
| 90 | case 0x1A: /* CTRL+Z */ |
| 91 | case 0x1B: /* Escape */ |
| 92 | ready = false; |
| 93 | break; |
| 94 | default: |
| 95 | ;/* Do nothing */ |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (read_io->status != VC_CONTAINER_SUCCESS && read_io->status != VC_CONTAINER_ERROR_EOS) |
| 101 | { |
| 102 | LOG_INFO(NULL, "Read failed: %d\n" , read_io->status); |
| 103 | } |
| 104 | |
| 105 | if (write_io->status != VC_CONTAINER_SUCCESS) |
| 106 | { |
| 107 | LOG_INFO(NULL, "Write failed: %d\n" , write_io->status); |
| 108 | } |
| 109 | |
| 110 | vc_container_io_close(read_io); |
| 111 | vc_container_io_close(write_io); |
| 112 | |
| 113 | nb_set_nonblocking_input(0); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |