Thu, 18 Jan 2024 21:58:58 +0100
allow NULL argument für asc_text()
universe@15 | 1 | /* |
universe@15 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@15 | 3 | * Copyright 2023 Mike Becker. All rights reserved. |
universe@15 | 4 | * |
universe@15 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@15 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@15 | 7 | * |
universe@15 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@15 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@15 | 10 | * |
universe@15 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@15 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@15 | 13 | * documentation and/or other materials provided with the distribution. |
universe@15 | 14 | * |
universe@15 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@15 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@15 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@15 | 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@15 | 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@15 | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@15 | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@15 | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@15 | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@15 | 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@15 | 25 | * POSSIBILITY OF SUCH DAMAGE. |
universe@15 | 26 | */ |
universe@15 | 27 | |
universe@15 | 28 | #include "ascension/files.h" |
universe@15 | 29 | #include "ascension/error.h" |
universe@15 | 30 | |
universe@15 | 31 | #include <sys/mman.h> |
universe@15 | 32 | #include <sys/stat.h> |
universe@15 | 33 | #include <fcntl.h> |
universe@15 | 34 | #include <unistd.h> |
universe@15 | 35 | |
universe@15 | 36 | asc_file asc_file_mmap_rdonly(char const *filename) { |
universe@15 | 37 | int fd = open(filename, O_RDONLY); |
universe@15 | 38 | if (fd < 0) { |
universe@15 | 39 | asc_dprintf("Failed to open file %s", filename); |
universe@15 | 40 | return (cxstring) {NULL, 0}; |
universe@15 | 41 | } |
universe@15 | 42 | |
universe@15 | 43 | struct stat statbuf; |
universe@15 | 44 | int err = fstat(fd, &statbuf); |
universe@15 | 45 | if (err < 0) { |
universe@15 | 46 | close(fd); |
universe@15 | 47 | asc_dprintf("Failed to stat file %s", filename); |
universe@15 | 48 | return (cxstring) {NULL, 0}; |
universe@15 | 49 | } |
universe@15 | 50 | |
universe@15 | 51 | char const *data = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); |
universe@15 | 52 | if (data == MAP_FAILED) { |
universe@15 | 53 | close(fd); |
universe@15 | 54 | asc_dprintf("Failed to map file %s into memory", filename); |
universe@15 | 55 | return (cxstring) {NULL, 0}; |
universe@15 | 56 | } |
universe@15 | 57 | close(fd); |
universe@15 | 58 | |
universe@15 | 59 | return cx_strn(data, statbuf.st_size); |
universe@15 | 60 | } |
universe@15 | 61 | |
universe@15 | 62 | void asc_file_unmap(asc_file file) { |
universe@15 | 63 | munmap((void *) file.ptr, file.length); |
universe@15 | 64 | } |