Dmitry V. Levin | b0e6115 | 2015-12-02 01:02:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.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 |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #if defined FSTATAT_NAME && defined HAVE_FSTATAT \ |
| 29 | && defined HAVE_FTRUNCATE && defined HAVE_FUTIMENS |
| 30 | |
| 31 | # include <errno.h> |
| 32 | # include <fcntl.h> |
| 33 | # include <stdio.h> |
| 34 | # include <time.h> |
| 35 | # include <unistd.h> |
| 36 | # include <sys/stat.h> |
| 37 | |
| 38 | #if defined MAJOR_IN_SYSMACROS |
| 39 | # include <sys/sysmacros.h> |
| 40 | #elif defined MAJOR_IN_MKDEV |
| 41 | # include <sys/mkdev.h> |
| 42 | #else |
| 43 | # include <sys/types.h> |
| 44 | #endif |
| 45 | |
| 46 | static void |
| 47 | print_ftype(const unsigned int mode) |
| 48 | { |
| 49 | if (S_ISREG(mode)) |
| 50 | printf("S_IFREG"); |
| 51 | else if (S_ISDIR(mode)) |
| 52 | printf("S_IFDIR"); |
| 53 | else if (S_ISCHR(mode)) |
| 54 | printf("S_IFCHR"); |
| 55 | else if (S_ISBLK(mode)) |
| 56 | printf("S_IFBLK"); |
| 57 | else |
| 58 | printf("%#o", mode & S_IFMT); |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | print_perms(const unsigned int mode) |
| 63 | { |
| 64 | printf("%#o", mode & ~S_IFMT); |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | print_time(const time_t t) |
| 69 | { |
| 70 | if (!t) { |
| 71 | printf("0"); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | struct tm *p = localtime(&t); |
| 76 | |
| 77 | if (p) |
| 78 | printf("%02d/%02d/%02d-%02d:%02d:%02d", |
| 79 | p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, |
| 80 | p->tm_hour, p->tm_min, p->tm_sec); |
| 81 | else |
| 82 | printf("%llu", (unsigned long long) t); |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | print_stat(const struct stat *st) |
| 87 | { |
| 88 | printf("{st_dev=makedev(%u, %u)", |
| 89 | (unsigned int) major(st->st_dev), |
| 90 | (unsigned int) minor(st->st_dev)); |
| 91 | printf(", st_ino=%Lu", (unsigned long long) st->st_ino); |
| 92 | printf(", st_mode="); |
| 93 | print_ftype(st->st_mode); |
| 94 | printf("|"); |
| 95 | print_perms(st->st_mode); |
| 96 | printf(", st_nlink=%u", (unsigned int) st->st_nlink); |
| 97 | printf(", st_uid=%u", (unsigned int) st->st_uid); |
| 98 | printf(", st_gid=%u", (unsigned int) st->st_gid); |
| 99 | printf(", st_blksize=%u", (unsigned int) st->st_blksize); |
| 100 | printf(", st_blocks=%u", (unsigned int) st->st_blocks); |
| 101 | |
| 102 | switch (st->st_mode & S_IFMT) { |
| 103 | case S_IFCHR: case S_IFBLK: |
| 104 | printf(", st_rdev=makedev(%u, %u)", |
| 105 | (unsigned int) major(st->st_rdev), |
| 106 | (unsigned int) minor(st->st_rdev)); |
| 107 | break; |
| 108 | default: |
| 109 | printf(", st_size=%Lu", (unsigned long long) st->st_size); |
| 110 | } |
| 111 | |
| 112 | printf(", st_atime="); |
| 113 | print_time(st->st_atime); |
| 114 | if (st->st_atim.tv_nsec) |
| 115 | printf(".%09lu", (unsigned long) st->st_atim.tv_nsec); |
| 116 | printf(", st_mtime="); |
| 117 | print_time(st->st_mtime); |
| 118 | if (st->st_mtim.tv_nsec) |
| 119 | printf(".%09lu", (unsigned long) st->st_mtim.tv_nsec); |
| 120 | printf(", st_ctime="); |
| 121 | print_time(st->st_ctime); |
| 122 | if (st->st_ctim.tv_nsec) |
| 123 | printf(".%09lu", (unsigned long) st->st_ctim.tv_nsec); |
| 124 | printf("}"); |
| 125 | } |
| 126 | |
| 127 | int |
| 128 | main(void) |
| 129 | { |
| 130 | static const char sample[] = FSTATAT_NAME ".sample"; |
| 131 | static const struct timespec ts[] = { |
| 132 | {-10843, 135}, {-10841, 246} |
| 133 | }; |
| 134 | const off_t size = 46118400291; |
| 135 | struct stat st; |
| 136 | |
| 137 | (void) close(0); |
| 138 | if (open(sample, O_RDWR | O_CREAT | O_TRUNC, 0640)) { |
| 139 | perror(sample); |
| 140 | return 77; |
| 141 | } |
| 142 | if (ftruncate(0, size)) { |
| 143 | perror("ftruncate"); |
| 144 | return 77; |
| 145 | } |
| 146 | if (futimens(0, ts)) { |
| 147 | perror("futimens"); |
| 148 | return 77; |
| 149 | } |
| 150 | if (fstatat(AT_FDCWD, sample, &st, AT_SYMLINK_NOFOLLOW)) { |
| 151 | perror("fstatat"); |
| 152 | return 77; |
| 153 | } |
| 154 | (void) unlink(sample); |
| 155 | |
| 156 | printf("%s(AT_FDCWD, \"%s\", ", FSTATAT_NAME, sample); |
| 157 | print_stat(&st); |
| 158 | puts(", AT_SYMLINK_NOFOLLOW) = 0"); |
| 159 | |
| 160 | puts("+++ exited with 0 +++"); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | #else |
| 165 | |
| 166 | int |
| 167 | main(void) |
| 168 | { |
| 169 | return 77; |
| 170 | } |
| 171 | |
| 172 | #endif |