Serenity Operating System
at portability 82 lines 2.7 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 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 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Vector.h> 28#include <assert.h> 29#include <errno.h> 30#include <fcntl.h> 31#include <stdio.h> 32#include <stdlib.h> 33#include <string.h> 34#include <unistd.h> 35 36int main(int argc, char** argv) 37{ 38 if (pledge("stdio rpath", nullptr) < 0) { 39 perror("pledge"); 40 return 1; 41 } 42 43 Vector<int> fds; 44 if (argc > 1) { 45 for (int i = 1; i < argc; i++) { 46 int fd; 47 if ((fd = open(argv[i], O_RDONLY)) == -1) { 48 fprintf(stderr, "Failed to open %s: %s\n", argv[i], strerror(errno)); 49 continue; 50 } 51 fds.append(fd); 52 } 53 } else { 54 fds.append(0); 55 } 56 57 if (pledge("stdio", nullptr) < 0) { 58 perror("pledge"); 59 return 1; 60 } 61 62 for (auto& fd : fds) { 63 for (;;) { 64 char buf[32768]; 65 ssize_t nread = read(fd, buf, sizeof(buf)); 66 if (nread == 0) 67 break; 68 if (nread < 0) { 69 perror("read"); 70 return 2; 71 } 72 ssize_t nwritten = write(1, buf, nread); 73 if (nwritten < 0) { 74 perror("write"); 75 return 3; 76 } 77 ASSERT(nwritten == nread); 78 } 79 close(fd); 80 } 81 return 0; 82}