1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/*
* Copyright (c) 2025 Interri Kft.
* All rights reserved.
*
* Free for personal use and commercial trial.
* Non-trial commercial use requires licenses available from https://firebuild.com.
* Modification and redistribution are permitted, but commercial use of derivative
* works is subject to the same requirements of this license
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Test stat family syscalls: stat, lstat, fstat, fstatat, statx,
* and glibc-internal __xstat variants.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef __linux__
/* For statx if available */
#ifndef STATX_TYPE
#include <linux/stat.h>
#endif
#endif
#define _STAT_VER 1
static void fail(const char *msg) {
perror(msg);
exit(1);
}
int main(void) {
struct stat st;
/* Create test files */
const char *regular = "test_stat_regular";
int fd = creat(regular, 0644);
if (fd == -1) fail("creat regular");
if (write(fd, "test", 4) != 4) fail("write");
close(fd);
const char *symlink_name = "test_stat_symlink";
if (symlink(regular, symlink_name) != 0) fail("symlink");
/* Test standard POSIX stat family */
if (stat(regular, &st) != 0) {
fail("stat");
}
if (!S_ISREG(st.st_mode)) {
fprintf(stderr, "stat: not regular\n");
exit(1);
}
if (st.st_size != 4) {
fprintf(stderr, "stat: size mismatch\n");
exit(1);
}
if (lstat(symlink_name, &st) != 0) {
fail("lstat");
}
if (!S_ISLNK(st.st_mode)) {
fprintf(stderr, "lstat: should return symlink\n");
exit(1);
}
fd = open(regular, O_RDONLY);
if (fd == -1) {
fail("open for fstat");
}
if (fstat(fd, &st) != 0) {
fail("fstat");
}
if (!S_ISREG(st.st_mode)) {
fprintf(stderr, "fstat: not regular\n");
exit(1);
}
close(fd);
if (fstatat(AT_FDCWD, regular, &st, 0) != 0) {
fail("fstatat");
}
if (!S_ISREG(st.st_mode)) {
fprintf(stderr, "fstatat: not regular\n");
exit(1);
}
if (fstatat(AT_FDCWD, symlink_name, &st, AT_SYMLINK_NOFOLLOW) != 0) {
fail("fstatat AT_SYMLINK_NOFOLLOW");
}
if (!S_ISLNK(st.st_mode)) {
fprintf(stderr, "fstatat AT_SYMLINK_NOFOLLOW: should be symlink\n");
exit(1);
}
#ifdef __x86_64__
/* Test 64-bit standard variants */
struct stat64 st64;
if (stat64(regular, &st64) != 0) {
fail("stat64");
}
if (!S_ISREG(st64.st_mode)) {
fprintf(stderr, "stat64: not regular\n");
exit(1);
}
if (lstat64(symlink_name, &st64) != 0) {
fail("lstat64");
}
if (!S_ISLNK(st64.st_mode)) {
fprintf(stderr, "lstat64: should be symlink\n");
exit(1);
}
fd = open(regular, O_RDONLY);
if (fd == -1) {
fail("open for fstat64");
}
if (fstat64(fd, &st64) != 0) {
fail("fstat64");
}
if (!S_ISREG(st64.st_mode)) {
fprintf(stderr, "fstat64: not regular\n");
exit(1);
}
close(fd);
if (fstatat64(AT_FDCWD, regular, &st64, 0) != 0) {
fail("fstatat64");
}
if (!S_ISREG(st64.st_mode)) {
fprintf(stderr, "fstatat64: not regular\n");
exit(1);
}
#endif
#ifdef STATX_TYPE
/* Test statx (Linux-specific, kernel 4.11+) */
struct statx stx;
if (statx(AT_FDCWD, regular, 0, STATX_BASIC_STATS, &stx) == 0) {
if (!S_ISREG(stx.stx_mode)) {
fprintf(stderr, "statx: not regular\n");
exit(1);
}
if (stx.stx_size != 4) {
fprintf(stderr, "statx: size mismatch\n");
exit(1);
}
} else if (errno != ENOSYS) {
fail("statx");
}
if (statx(AT_FDCWD, symlink_name, AT_SYMLINK_NOFOLLOW,
STATX_BASIC_STATS, &stx) == 0) {
if (!S_ISLNK(stx.stx_mode)) {
fprintf(stderr, "statx AT_SYMLINK_NOFOLLOW: should be symlink\n");
exit(1);
}
} else if (errno != ENOSYS) {
fail("statx AT_SYMLINK_NOFOLLOW");
}
#endif
/* Cleanup */
unlink(symlink_name);
unlink(regular);
printf("ok\n");
return 0;
}
|