stdio   [plain text]


################################################################
## Need to hide certain names such as _doprnt, _doscan
################################################################

#pragma weak note{ compile system supports symbol transform }end link{
#	main() { return _xyz(); }
#	#pragma weak   _xyz =  __xyz
#	#define	        _xyz    __xyz
#	_xyz() { return 0; }
#}end

################################################################
## To emulate old-style stdio at binary level
################################################################

tst - output{
	#include	<stdio.h>
	main() { printf("#define _siz_fpos_t	%d\n", sizeof(fpos_t)); exit(0); }
}end

FILE _cnt note{ field FILE.__cnt exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->__cnt == 0) ? 0 : -1; }
}end
FILE _ptr note{ field FILE.__ptr exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->__ptr == 0) ? 0 : -1; }
}end
FILE _file note{ field FILE.__file exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->__file == 0) ? 0 : -1; }
}end
FILE _flag note{ field FILE.__flag exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->__flag == 0) ? 0 : -1; }
}end

FILE cnt note{ field FILE._cnt exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->_cnt == 0) ? 0 : -1; }
}end
FILE ptr note{ field FILE._ptr exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->_ptr == 0) ? 0 : -1; }
}end
FILE file note{ field FILE._file exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->_file == 0) ? 0 : -1; }
}end
FILE flag note{ field FILE._flag exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->_flag == 0) ? 0 : -1; }
}end
FILE flags note{ field FILE._flags exists }end compile{
	#include	<stdio.h>
	main() { return (stdin->_flags == 0) ? 0 : -1; }
}end

cat{
	#if _FILE__cnt && ! _FILE_cnt
	#define _FILE_cnt	1
	#define _cnt		__cnt
	#endif
	#if _FILE__ptr && ! _FILE_ptr
	#define _FILE_ptr	1
	#define _ptr		__ptr
	#endif
	#if _FILE__flag && ! _FILE_flag
	#define _FILE_flag	1
	#define _flag		__flag
	#endif
	#if _FILE__file && ! _FILE_file
	#define _FILE_file	1
	#define _file		__file
	#endif
}end

############################################################################
# To emulate Linux-stdio at binary level
############################################################################
FILE readptr note{ fields FILE._IO_read_ptr/end exist }end execute{
	#include	<stdio.h>
	main()
	{ if(sizeof(stdin->_IO_read_ptr) != sizeof(char*) ) return 1;
	  if(sizeof(stdin->_IO_read_end) != sizeof(char*) ) return 1;
	  return 0;
	}
}end
FILE writeptr note{ fields FILE._IO_write_ptr/end exist }end execute{
	#include	<stdio.h>
	main()
	{ if(sizeof(stdin->_IO_write_ptr) != sizeof(char*) ) return 1;
	  if(sizeof(stdin->_IO_write_end) != sizeof(char*) ) return 1;
	  return 0;
	}
}end
FILE flags note{ field FILE._flags exists }end compile{
	#include	<stdio.h>
	main()
	{ return stdin->_flags == 0 ? 1 : 0;
	}
}end
FILE fileno note{ field FILE._fileno exists }end compile{
	#include	<stdio.h>
	main()
	{ return stdin->_fileno == 0 ? 1 : 0;
	}
}end
u flow note{ uflow() does bump buffer }end execute{
	#include	<stdio.h>
	#if _STD_
	main(int argc, char** argv)
	#else
	main(argc,argv)
	int     argc;
	char**  argv;
	#endif
	{ FILE*	f;
	  char	file[BUFSIZ];
	  sprintf(file,"%s.D",argv[0]);
	  if(!(f = fopen(file,"w+")) )
		exit(1);
	  unlink(file);
	  setbuf(f,file);
	  fputs("123456789",f);
	  fseek(f,0L,0);
	  if(__uflow(f) == EOF)
		exit(1);
	  if(*f->_IO_read_ptr == '1')
		exit(1);
	  else	exit(0);
	}
}end
under flow note{ underflow() does not bump buffer }end execute{
	#include	<stdio.h>
	#if _STD_
	main(int argc, char** argv)
	#else
	main(argc,argv)
	int     argc;
	char**  argv;
	#endif
	{ FILE*	f;
	  char	file[BUFSIZ];
	  sprintf(file,"%s.D",argv[0]);
	  if(!(f = fopen(file,"w+")) )
		exit(1);
	  unlink(file);
	  setbuf(f,file);
	  fputs("123456789",f);
	  fseek(f,0L,0);
	  if(__underflow(f) == EOF)
		exit(1);
	  if(*f->_IO_read_ptr == '1')
		exit(0);
	  else	exit(1);
	}
}end

######################################################################
# To emulate BSD-style stdio
######################################################################
FILE p	note{ FILE._p field }end compile{
	#include <stdio.h>
	main() { return stdin->_p == 0 ? 0 : 1; }
}end
FILE r	note{ FILE._r field }end compile{
	#include <stdio.h>
	main() { return stdin->_r == 0 ? 0 : 1; }
}end
FILE w	note{ FILE._w field }end compile{
	#include <stdio.h>
	main() { return stdout->_w == 0 ? 0 : 1; }
}end

lib __swbuf
lib __srget

lib __uflow
lib __underflow
lib __overflow

lib _IO_getc
lib _IO_putc

lib clearerr_unlocked
lib feof_unlocked
lib ferror_unlocked
lib fflush_unlocked
lib fgetc_unlocked
lib fgets_unlocked
lib fileno_unlocked
lib fputc_unlocked
lib fputs_unlocked
lib fread_unlocked
lib fwrite_unlocked
lib getc_unlocked
lib getchar_unlocked
lib putc_unlocked
lib putchar_unlocked

lib __snprintf
lib __vsnprintf

# detect microsoft dll declaration of _iob
mac	_iob	stdio.h
msft	iob note{ microsoft's oddity }end compile{
	#include	<stdio.h>
	#if _mac__iob
	_CRTIMP extern FILE * __cdecl __p__iob(void);
	FILE* foo() { return &(_iob[0]); }
	#else
	this should fail;
	#endif
}end

dat	_iob,__iob	stdio.h
native	iob note{ ok to use stdio's _iob }end compile{
	#define	_doprnt		_____doprnt
	#define	_doscan		_____doscan
	#define	fclose		____fclose
	#define	fdopen		____fdopen
	#define	fflush		____fflush
	#define	fgetc		____fgetc
	#define	fgets		____fgets
	#define	_filbuf		_____filbuf
	#define	_flsbuf		_____flsbuf
	#define	_cleanup	_____cleanup
	#define	fopen		____fopen
	#define	fprintf		____fprintf
	#define	fputc		____fputc
	#define	fputs		____fputs
	#define	fread		____fread
	#define	freopen		____freopen
	#define	fscanf		____fscanf
	#define	fseek		____fseek
	#define	ftell		____ftell
	#define	fgetpos		____fgetpos
	#define	fsetpos		____fsetpos
	#define	fpurge		____fpurge
	#define	fwrite		____fwrite
	#define	gets		____gets
	#define	getw		____getw
	#define	pclose		____pclose
	#define	popen		____popen
	#define	printf		____printf
	#define	puts		____puts
	#define	putw		____putw
	#define	rewind		____rewind
	#define	scanf		____scanf
	#define	setbuf		____setbuf
	#define	setbuffer	____setbuffer
	#define	setlinebuf	____setlinebuf
	#define	setvbuf		____setvbuf
	#define	sprintf		____sprintf
	#define	sscanf		____sscanf
	#define	tmpfile		____tmpfile
	#define	ungetc		____ungetc
	#define	vfprintf	____vfprintf
	#define	vfscanf		____vfscanf
	#define	vprintf		____vprintf
	#define	vscanf		____vscanf
	#define	vsprintf	____vsprintf
	#define	vsscanf		____vsscanf
	#define	flockfile	____flockfile
	#define	funlockfile	____funlockfile
	#define	ftrylockfile	____ftrylockfile
	
	#include	<stdio.h>
	
	#undef IOB
	#if defined(_dat_iob) && !defined(IOB)
	#define IOB	iob
	#endif
	#if defined(_dat__iob) && !defined(IOB)
	#define IOB	_iob
	#endif
	#if defined(_dat___iob) && !defined(IOB)
	#define IOB	__iob
	#endif
	
	static void* addr() { return((void*)IOB); }
	int main() { addr(); exit(0); }
	
	#undef	_doprnt
	#undef	_doscan
	#undef	fclose
	#undef	fdopen
	#undef	fflush
	#undef	fgetc
	#undef	fgets
	#undef	_filbuf
	#undef	_flsbuf
	#undef	_cleanup
	#undef	fopen
	#undef	fprintf
	#undef	fputc
	#undef	fputs
	#undef	fread
	#undef	freopen
	#undef	fscanf
	#undef	fseek
	#undef	ftell
	#undef	fgetpos
	#undef	fsetpos
	#undef	fpurge
	#undef	fwrite
	#undef	gets
	#undef	getw
	#undef	pclose
	#undef	popen
	#undef	printf
	#undef	puts
	#undef	putw
	#undef	rewind
	#undef	scanf
	#undef	setbuf
	#undef	setbuffer
	#undef	setlinebuf
	#undef	setvbuf
	#undef	sprintf
	#undef	sscanf
	#undef	tmpfile
	#undef	ungetc
	#undef	vfprintf
	#undef	vfscanf
	#undef	vprintf
	#undef	vscanf
	#undef	vsprintf
	#undef	vsscanf
	#undef	flockfile
	#undef	funlockfile
	#undef	ftrylockfile
	
	void	_doprnt() {}
	void	_doscan() {}
	void	fclose() {}
	void	fdopen() {}
	void	fflush() {}
	void	fgetc() {}
	void	fgets() {}
	void	_filbuf() {}
	void	_flsbuf() {}
	void	_cleanup() {}
	void	fopen() {}
	void	fprintf() {}
	void	fputc() {}
	void	fputs() {}
	void	fread() {}
	void	freopen() {}
	void	fscanf() {}
	void	fseek() {}
	void	ftell() {}
	void	fgetpos() {}
	void	fsetpos() {}
	void	fpurge() {}
	void	fwrite() {}
	void	gets() {}
	void	getw() {}
	void	pclose() {}
	void	popen() {}
	void	printf() {}
	void	puts() {}
	void	putw() {}
	void	rewind() {}
	void	scanf() {}
	void	setbuf() {}
	void	setbuffer() {}
	void	setlinebuf() {}
	void	setvbuf() {}
	void	sprintf() {}
	void	sscanf() {}
	void	tmpfile() {}
	void	ungetc() {}
	void	vfprintf() {}
	void	vfscanf() {}
	void	vprintf() {}
	void	vscanf() {}
	void	vsprintf() {}
	void	vsscanf() {}
	void	flockfile() {}
	void	funlockfile() {}
	void	ftrylockfile() {}
}end