UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2024 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef DAV_SYSTEM_H 30 #define DAV_SYSTEM_H 31 32 #include <stdio.h> 33 #include <time.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 37 38 #ifdef _WIN32 39 #include <Windows.h> 40 #define mode_t unsigned int 41 #else 42 #include <dirent.h> 43 #endif 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 typedef struct SysDirEnt { 50 char *name; 51 } SysDirEnt; 52 53 #ifdef _WIN32 54 struct WinDir { 55 int first; 56 HANDLE handle; 57 WIN32_FIND_DATAW finddata; 58 SysDirEnt *ent; 59 }; 60 #define SYS_DIR struct WinDir* 61 #define SYS_STAT struct __stat64 62 63 typedef int uid_t; 64 typedef int gid_t; 65 66 #define SYS_ISLINK(path, mode) sys_islink(path) 67 #define SYS_LINK_EXT ".lnk" 68 69 #else 70 71 typedef struct SysDir { 72 DIR *dir; 73 SysDirEnt *ent; 74 } SysDir; 75 76 #define SYS_DIR SysDir* 77 #define SYS_STAT struct stat 78 79 #define SYS_ISLINK(p, s) S_ISLNK(s.st_mode) 80 81 #endif 82 83 84 #if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG) 85 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 86 #endif 87 88 #ifdef _WIN32 89 #ifndef S_ISDIR 90 #define S_ISDIR(mode) ((mode) & _S_IFMT) == _S_IFDIR 91 #define S_ISREG(mode) ((mode) & _S_IFMT) == _S_IFREG 92 #endif 93 #endif 94 95 96 typedef int(*stat_func)(const char*, SYS_STAT *); 97 98 void sys_init(void); 99 void sys_uninit(void); 100 101 void sys_freedirent(SysDirEnt *ent); 102 SYS_DIR sys_opendir(const char *path); 103 SysDirEnt* sys_readdir(SYS_DIR dir); 104 void sys_closedir(SYS_DIR dir); 105 106 FILE* sys_fopen(const char *path, const char *mode); 107 108 int sys_stat(const char *path, SYS_STAT *s); 109 int sys_lstat(const char *path, SYS_STAT *s); 110 111 int sys_islink(const char *path); 112 113 int sys_rename(const char *oldpath, const char *newpath); 114 int sys_unlink(const char *path); 115 int sys_mkdir(const char *path); 116 117 char* sys_readlink(const char *path, SYS_STAT *s); 118 119 int sys_symlink(const char *target, const char *linkpath); 120 121 int sys_truncate(const char* path, off_t length); 122 123 #ifdef __cplusplus 124 } 125 #endif 126 127 #endif /* DAV_SYSTEM_H */ 128 129