limit.c (1049B)
1 /* This program is licensed under the terms of GNU GPL v3 or (at your option) 2 * any later version. Copyright (C) 2022-2026 Страхиња Радић. 3 * See the file LICENSE for exact copyright and license details. */ 4 5 #include <errno.h> 6 #include <fcntl.h> 7 #include <limits.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 11 #include "limit.h" 12 13 long limits[L_LIMIT_COUNT]; 14 15 #ifndef LINE_MAX 16 #define LINE_MAX _POSIX2_LINE_MAX 17 #endif 18 19 #ifndef PATH_MAX 20 #define PATH_MAX _POSIX_PATH_MAX 21 #endif 22 23 int 24 init_limits(const char* dir) 25 { 26 int cwdfd; 27 int result = 0; 28 29 errno = 0; 30 31 if ((cwdfd = open(dir, O_RDONLY)) < 0) 32 return -1; 33 34 limits[L_PATH_MAX] = fpathconf(cwdfd, _PC_PATH_MAX); 35 if (limits[L_PATH_MAX] == -1) 36 { 37 if (errno) 38 { 39 result = -1; 40 goto init_limits_cleanup; 41 } 42 limits[L_PATH_MAX] = PATH_MAX; 43 } 44 45 limits[L_LINE_MAX] = sysconf(_SC_LINE_MAX); 46 if (limits[L_LINE_MAX] == -1) 47 { 48 if (errno) 49 { 50 result = -1; 51 goto init_limits_cleanup; 52 } 53 limits[L_LINE_MAX] = LINE_MAX; 54 } 55 56 init_limits_cleanup: 57 close(cwdfd); 58 59 return result; 60 }