<commit_before>#include "unistd.h"
#include "stdio.h"
#include "string.h"
#include "errno.h"
#include "stdlib.h"
#include "sys/fcntl.h"
#include "assert.h"
#include "ctype.h"

#define PRINTF_OPTIMISE

static int _stdin  = 0;
static int _stdout = 1;
static int _stderr = 2;

FILE *stdin  = &_stdin;
FILE *stdout = &_stdout;
FILE *stderr = &_stderr;

static const char *nums = "0123456789abcdef";

/* Private */
static void printd_rec(int fd, int n, int base)
{
	int d;
	d = n / base;
	if(d)
		printd_rec(fd, d, base);
	write(fd, nums + n % base, 1);
}

static void printn(int fd, int n, int base, int is_signed)
{
	if(is_signed && n < 0){
		write(fd, "-", 1);
		n = -n;
	}

	printd_rec(fd, n, base);
}

static void printd(int fd, int n, int is_signed)
{
	printn(fd, n, 10, is_signed);
}

static void printx(int fd, int n, int is_signed)
{
	printn(fd, n, 16, is_signed);
}


/* Public */
FILE *fopen(const char *path, char *smode)
{
	FILE *f;
	int fd, mode;
	int got_primary;

#define PRIMARY_CHECK() \
	if(got_primary) \
		goto inval; \
	got_primary = 1

	got_primary = mode = 0;
	while(*smode)
		switch(*smode++){
			case 'b':
				break;
			case 'r':
				PRIMARY_CHECK();
				if(mode & O_CREAT)
					goto inval;
				mode |= O_RDONLY;
				break;
			case 'w':
				PRIMARY_CHECK();
				mode |= O_WRONLY | O_CREAT | O_TRUNC;
				break;
			case 'a':
				PRIMARY_CHECK();
				mode |= O_WRONLY | O_CREAT; /* ? */
				break;
			case '+':
				mode &= ~(O_WRONLY | O_RDONLY);
				mode |= O_RDWR;
				break;
			default:
inval:
				errno = EINVAL;
				return NULL;
		}
	if(!mode)
		goto inval;

#undef PRIMARY_CHECK

	f = malloc(sizeof *f);
	if(!f)
		return NULL;

	fd = open(path, mode, 0644);
	if(fd == -1)
		return NULL;

	*f = fd;

	return f;
}

int fclose(FILE *f)
{
	int r = close(*f) == 0 ? 0 : EOF;
	free(f);
	return r;
}

int fputc(int c, FILE *f)
{
	return write(*f, &c, 1) == 1 ? c : EOF;
}

int putchar(int c)
{
	return fputc(c, stdout);
}

int vfprintf(FILE *file, char *fmt, va_list ap)
{
	int fd = *file;
#ifdef PRINTF_OPTIMISE
	char *buf  = fmt;
	int buflen = 0;
#else
# warning printf unoptimised
#endif

	while(*fmt){
		if(*fmt == '%'){
			int pad = 0;

#ifdef PRINTF_OPTIMISE
			if(buflen)
				write(fd, buf, buflen);
#endif

			fmt++;

			if(*fmt == '0'){
				// %0([0-9]+)d
				fmt++;
				pad = 0;
				while(isdigit(*fmt)){
					pad += *fmt - '0';
					fmt++;
				}
			}

			switch(*fmt){
				case 's':
				{
					char *s = va_arg(ap, char *);
					if(!s)
						s = "(null)";
					write(fd, s, strlen(s));
					break;
				}
				case 'c':
					fputc(va_arg(ap, char), file);
					break;
				case 'u':
				case 'd':
number:
				{
					const int n = va_arg(ap, int);

					if(pad){
						if(n){
							int len = 0, copy = n;

							while(copy){
								copy /= 10;
								len++;
							}

							while(pad-- > len)
								putchar('0');
						}else{
							while(--pad)
								putchar('0');
						}
					}

					printd(fd, n, *fmt == 'd');
					break;
				}
				case 'p':
					write(fd, "0x", 2);
				case 'x':
					printx(fd, va_arg(ap, int), 1);
					break;

				default:
					write(fd, fmt, 1); /* default to just printing the char */
			}

#ifdef PRINTF_OPTIMISE
			buf = fmt + 1;
			buflen = 0;
#endif
		}else{
#ifdef PRINTF_OPTIMISE
			buflen++;
#else
			write(fd, fmt, 1);
#endif
		}<commit_msg>Added "(nil)" for null pointers<commit_after>#include "unistd.h"
#include "stdio.h"
#include "string.h"
#include "errno.h"
#include "stdlib.h"
#include "sys/fcntl.h"
#include "assert.h"
#include "ctype.h"

#define PRINTF_OPTIMISE

static int _stdin  = 0;
static int _stdout = 1;
static int _stderr = 2;

FILE *stdin  = &_stdin;
FILE *stdout = &_stdout;
FILE *stderr = &_stderr;

static const char *nums = "0123456789abcdef";

/* Private */
static void printd_rec(int fd, int n, int base)
{
	int d;
	d = n / base;
	if(d)
		printd_rec(fd, d, base);
	write(fd, nums + n % base, 1);
}

static void printn(int fd, int n, int base, int is_signed)
{
	if(is_signed && n < 0){
		write(fd, "-", 1);
		n = -n;
	}

	printd_rec(fd, n, base);
}

static void printd(int fd, int n, int is_signed)
{
	printn(fd, n, 10, is_signed);
}

static void printx(int fd, int n, int is_signed)
{
	printn(fd, n, 16, is_signed);
}


/* Public */
FILE *fopen(const char *path, char *smode)
{
	FILE *f;
	int fd, mode;
	int got_primary;

#define PRIMARY_CHECK() \
	if(got_primary) \
		goto inval; \
	got_primary = 1

	got_primary = mode = 0;
	while(*smode)
		switch(*smode++){
			case 'b':
				break;
			case 'r':
				PRIMARY_CHECK();
				if(mode & O_CREAT)
					goto inval;
				mode |= O_RDONLY;
				break;
			case 'w':
				PRIMARY_CHECK();
				mode |= O_WRONLY | O_CREAT | O_TRUNC;
				break;
			case 'a':
				PRIMARY_CHECK();
				mode |= O_WRONLY | O_CREAT; /* ? */
				break;
			case '+':
				mode &= ~(O_WRONLY | O_RDONLY);
				mode |= O_RDWR;
				break;
			default:
inval:
				errno = EINVAL;
				return NULL;
		}
	if(!mode)
		goto inval;

#undef PRIMARY_CHECK

	f = malloc(sizeof *f);
	if(!f)
		return NULL;

	fd = open(path, mode, 0644);
	if(fd == -1)
		return NULL;

	*f = fd;

	return f;
}

int fclose(FILE *f)
{
	int r = close(*f) == 0 ? 0 : EOF;
	free(f);
	return r;
}

int fputc(int c, FILE *f)
{
	return write(*f, &c, 1) == 1 ? c : EOF;
}

int putchar(int c)
{
	return fputc(c, stdout);
}

int vfprintf(FILE *file, char *fmt, va_list ap)
{
	int fd = *file;
#ifdef PRINTF_OPTIMISE
	char *buf  = fmt;
	int buflen = 0;
#else
# warning printf unoptimised
#endif

	while(*fmt){
		if(*fmt == '%'){
			int pad = 0;

#ifdef PRINTF_OPTIMISE
			if(buflen)
				write(fd, buf, buflen);
#endif

			fmt++;

			if(*fmt == '0'){
				// %0([0-9]+)d
				fmt++;
				pad = 0;
				while(isdigit(*fmt)){
					pad += *fmt - '0';
					fmt++;
				}
			}

			switch(*fmt){
				case 's':
				{
					char *s = va_arg(ap, char *);
					if(!s)
						s = "(null)";
					write(fd, s, strlen(s));
					break;
				}
				case 'c':
					fputc(va_arg(ap, char), file);
					break;
				case 'u':
				case 'd':
number:
				{
					const int n = va_arg(ap, int);

					if(pad){
						if(n){
							int len = 0, copy = n;

							while(copy){
								copy /= 10;
								len++;
							}

							while(pad-- > len)
								putchar('0');
						}else{
							while(--pad)
								putchar('0');
						}
					}

					printd(fd, n, *fmt == 'd');
					break;
				}
				case 'p':
				{
					void *p = va_arg(ap, void *);
					if(p){
						fputs(file, "0x");
						printx(fd, p, 0);
					}else{
						fputs(file, "(nil)");
					}
					break;
				}
				case 'x':
					printx(fd, va_arg(ap, int), 1);
					break;

				default:
					write(fd, fmt, 1); /* default to just printing the char */
			}

#ifdef PRINTF_OPTIMISE
			buf = fmt + 1;
			buflen = 0;
#endif
		}else{
#ifdef PRINTF_OPTIMISE
			buflen++;
#else
			write(fd, fmt, 1);
#endif
		}
{'fraction_non_alphanumeric': 0.17369507519905633, 'fraction_numerical': 0.015039811265113535, 'mean_word_length': 2.4, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '558695', 'n_tokens_mistral': 3646, 'n_tokens_neox': 2874, 'n_words': 753}