<commit_before>/*
 * Copyright (c) 2015-2016 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#if defined HAVE_FTRUNCATE && defined HAVE_FUTIMENS

# ifndef TEST_SYSCALL_STR
#  error TEST_SYSCALL_STR must be defined
# endif
# ifndef TEST_SYSCALL_INVOKE
#  error TEST_SYSCALL_INVOKE must be defined
# endif
# ifndef PRINT_SYSCALL_HEADER
#  error PRINT_SYSCALL_HEADER must be defined
# endif
# ifndef PRINT_SYSCALL_FOOTER
#  error PRINT_SYSCALL_FOOTER must be defined
# endif

# include <stdio.h>
# include <stddef.h>
# include <time.h>
# include <unistd.h>

# if defined MAJOR_IN_SYSMACROS
#  include <sys/sysmacros.h>
# elif defined MAJOR_IN_MKDEV
#  include <sys/mkdev.h>
# else
#  include <sys/types.h>
# endif

static void
print_time(const time_t t)
{
	if (!t) {
		printf("0");
		return;
	}

	struct tm *p = localtime(&t);

	if (p)
		printf("%02d/%02d/%02d-%02d:%02d:%02d",
		       p->tm_year + 1900, p->tm_mon + 1, p->tm_mday,
		       p->tm_hour, p->tm_min, p->tm_sec);
	else
		printf("%llu", (unsigned long long) t);
}

# ifndef STRUCT_STAT
#  define STRUCT_STAT struct stat
#  define STRUCT_STAT_STR "struct stat"
#  define STRUCT_STAT_IS_STAT64 0
# endif
# ifndef SAMPLE_SIZE
#  define SAMPLE_SIZE 43147718418
# endif

typedef off_t libc_off_t;

# ifdef USE_ASM_STAT
#  define stat libc_stat
#  define stat64 libc_stat64
# endif
# include <fcntl.h>
# include <sys/stat.h>
# ifdef USE_ASM_STAT
#  undef stat
#  undef stat64
# endif

# ifdef USE_ASM_STAT
#  undef st_atime
#  undef st_mtime
#  undef st_ctime
#  include "asm_stat.h"
#  if STRUCT_STAT_IS_STAT64
#   undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
#   define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
#  endif /* STRUCT_STAT_IS_STAT64 */
# else /* !USE_ASM_STAT */
#  undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
#  ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
#   define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
#   undef st_atime_nsec
#   define st_atime_nsec st_atim.tv_nsec
#   undef st_ctime_nsec
#   define st_ctime_nsec st_ctim.tv_nsec
#   undef st_mtime_nsec
#   define st_mtime_nsec st_mtim.tv_nsec
#  endif /* HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC */
# endif

static void
print_ftype(const unsigned int mode)
{
	if (S_ISREG(mode))
		printf("S_IFREG");
	else if (S_ISDIR(mode))
		printf("S_IFDIR");
	else if (S_ISCHR(mode))
		printf("S_IFCHR");
	else if (S_ISBLK(mode))
		printf("S_IFBLK");
	else
		printf("%#o", mode & S_IFMT);
}

static void
print_perms(const unsigned int mode)
{
	printf("%#o", mode & ~S_IFMT);
}

static void
print_stat(const STRUCT_STAT *st)
{
	printf("{st_dev=makedev(%u, %u)",
	       (unsigned int) major(st->st_dev),
	       (unsigned int) minor(st->st_dev));
	printf(", st_ino=%llu", (unsigned long long) st->st_ino);
	printf(", st_mode=");
	print_ftype(st->st_mode);
	printf("|");
	print_perms(st->st_mode);
	printf(", st_nlink=%u", (unsigned int) st->st_nlink);
	printf(", st_uid=%u", (unsigned int) st->st_uid);
	printf(", st_gid=%u", (unsigned int) st->st_gid);
	printf(", st_blksize=%u", (unsigned int) st->st_blksize);
	printf(", st_blocks=%u", (unsigned int) st->st_blocks);

	switch (st->st_mode & S_IFMT) {
	case S_IFCHR: case S_IFBLK:
		printf(", st_rdev=makedev(%u, %u)",
		       (unsigned int) major(st->st_rdev),
		       (unsigned int) minor(st->st_rdev));
		break;
	default:
		printf(", st_size=%llu", (unsigned long long) st->st_size);
	}

	printf(", st_atime=");
	print_time(st->st_atime);
# ifdef HAVE_STRUCT_STAT_ST_MTIME_NSEC
	if (st->st_atime_nsec)
		printf(".%09lu", (unsigned long) st->st_atime_nsec);
# endif
	printf(", st_mtime=");
	print_time(st->st_mtime);
# ifdef HAVE_STRUCT_STAT_ST_MTIME_NSEC
	if (st->st_mtime_nsec)
		printf(".%09lu", (unsigned long) st->st_mtime_nsec);
# endif
	printf(", st_ctime=");
	print_time(st->st_ctime);
# ifdef HAVE_STRUCT_STAT_ST_MTIME_NSEC
	if (st->st_ctime_nsec)
		printf(".%09lu", (unsigned long) st->st_ctime_nsec);
# endif
	printf("}");
}

static int
create_sample(const char *fname, const libc_off_t size)
{
	static const struct timespec ts[] = {
#ifdef __mips64
		// On mips64, struct stat cannot handle negative timestamps
		{10841, 246}, {10843, 135}
#else
		{-10843, 135}, {-10841, 246}
#endif
	};
<commit_msg>Revert "tests/xstatx.c: fix stat syscall tests on mips64"<commit_after>/*
 * Copyright (c) 2015-2016 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#if defined HAVE_FTRUNCATE && defined HAVE_FUTIMENS

# ifndef TEST_SYSCALL_STR
#  error TEST_SYSCALL_STR must be defined
# endif
# ifndef TEST_SYSCALL_INVOKE
#  error TEST_SYSCALL_INVOKE must be defined
# endif
# ifndef PRINT_SYSCALL_HEADER
#  error PRINT_SYSCALL_HEADER must be defined
# endif
# ifndef PRINT_SYSCALL_FOOTER
#  error PRINT_SYSCALL_FOOTER must be defined
# endif

# include <stdio.h>
# include <stddef.h>
# include <time.h>
# include <unistd.h>

# if defined MAJOR_IN_SYSMACROS
#  include <sys/sysmacros.h>
# elif defined MAJOR_IN_MKDEV
#  include <sys/mkdev.h>
# else
#  include <sys/types.h>
# endif

static void
print_time(const time_t t)
{
	if (!t) {
		printf("0");
		return;
	}

	struct tm *p = localtime(&t);

	if (p)
		printf("%02d/%02d/%02d-%02d:%02d:%02d",
		       p->tm_year + 1900, p->tm_mon + 1, p->tm_mday,
		       p->tm_hour, p->tm_min, p->tm_sec);
	else
		printf("%llu", (unsigned long long) t);
}

# ifndef STRUCT_STAT
#  define STRUCT_STAT struct stat
#  define STRUCT_STAT_STR "struct stat"
#  define STRUCT_STAT_IS_STAT64 0
# endif
# ifndef SAMPLE_SIZE
#  define SAMPLE_SIZE 43147718418
# endif

typedef off_t libc_off_t;

# ifdef USE_ASM_STAT
#  define stat libc_stat
#  define stat64 libc_stat64
# endif
# include <fcntl.h>
# include <sys/stat.h>
# ifdef USE_ASM_STAT
#  undef stat
#  undef stat64
# endif

# ifdef USE_ASM_STAT
#  undef st_atime
#  undef st_mtime
#  undef st_ctime
#  include "asm_stat.h"
#  if STRUCT_STAT_IS_STAT64
#   undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
#   define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
#  endif /* STRUCT_STAT_IS_STAT64 */
# else /* !USE_ASM_STAT */
#  undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
#  ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
#   define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
#   undef st_atime_nsec
#   define st_atime_nsec st_atim.tv_nsec
#   undef st_ctime_nsec
#   define st_ctime_nsec st_ctim.tv_nsec
#   undef st_mtime_nsec
#   define st_mtime_nsec st_mtim.tv_nsec
#  endif /* HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC */
# endif

static void
print_ftype(const unsigned int mode)
{
	if (S_ISREG(mode))
		printf("S_IFREG");
	else if (S_ISDIR(mode))
		printf("S_IFDIR");
	else if (S_ISCHR(mode))
		printf("S_IFCHR");
	else if (S_ISBLK(mode))
		printf("S_IFBLK");
	else
		printf("%#o", mode & S_IFMT);
}

static void
print_perms(const unsigned int mode)
{
	printf("%#o", mode & ~S_IFMT);
}

static void
print_stat(const STRUCT_STAT *st)
{
	printf("{st_dev=makedev(%u, %u)",
	       (unsigned int) major(st->st_dev),
	       (unsigned int) minor(st->st_dev));
	printf(", st_ino=%llu", (unsigned long long) st->st_ino);
	printf(", st_mode=");
	print_ftype(st->st_mode);
	printf("|");
	print_perms(st->st_mode);
	printf(", st_nlink=%u", (unsigned int) st->st_nlink);
	printf(", st_uid=%u", (unsigned int) st->st_uid);
	printf(", st_gid=%u", (unsigned int) st->st_gid);
	printf(", st_blksize=%u", (unsigned int) st->st_blksize);
	printf(", st_blocks=%u", (unsigned int) st->st_blocks);

	switch (st->st_mode & S_IFMT) {
	case S_IFCHR: case S_IFBLK:
		printf(", st_rdev=makedev(%u, %u)",
		       (unsigned int) major(st->st_rdev),
		       (unsigned int) minor(st->st_rdev));
		break;
	default:
		printf(", st_size=%llu", (unsigned long long) st->st_size);
	}

	printf(", st_atime=");
	print_time(st->st_atime);
# ifdef HAVE_STRUCT_STAT_ST_MTIME_NSEC
	if (st->st_atime_nsec)
		printf(".%09lu", (unsigned long) st->st_atime_nsec);
# endif
	printf(", st_mtime=");
	print_time(st->st_mtime);
# ifdef HAVE_STRUCT_STAT_ST_MTIME_NSEC
	if (st->st_mtime_nsec)
		printf(".%09lu", (unsigned long) st->st_mtime_nsec);
# endif
	printf(", st_ctime=");
	print_time(st->st_ctime);
# ifdef HAVE_STRUCT_STAT_ST_MTIME_NSEC
	if (st->st_ctime_nsec)
		printf(".%09lu", (unsigned long) st->st_ctime_nsec);
# endif
	printf("}");
}

static int
create_sample(const char *fname, const libc_off_t size)
{
	static const struct timespec ts[] = {
		{-10843, 135}, {-10841, 246}
	};

{'fraction_non_alphanumeric': 0.11579712779494637, 'fraction_numerical': 0.01599709143792038, 'mean_word_length': 4.610912799592045, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2588411', 'n_tokens_mistral': 4387, 'n_tokens_neox': 4079, 'n_words': 1126}