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
|
/*
!!DESCRIPTION!! varargs test
!!ORIGIN!!
!!LICENCE!! public domain
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
void chk0(char *format,...);
void chk1(int fd,char *format,...);
#if 0
// old workaround for broken varargs
void chk0(char *format,...){
__asm__ ("pha"); // save argument size
{
//va_list ap;
char *ap;
char *_format;
static char string[0x100];
// va_start(ap,format);
__asm__ ("pla"); // restore argument size
__asm__ ("ldx #$00"); // clear hibyte of AX
ap=__AX__;
ap+=(char*)&format;
// get value of format
ap-=2;
_format=*((char**)ap);
// vsprintf(string,format,ap);
vsprintf(&string[0],_format,ap);
printf("format:%s,string:%s\n",_format,string);
// va_end(ap);
}
}
void chk1(int fd,char *format,...){
__asm__ ("pha"); // save argument size
{
//va_list ap;
char *ap;
char *_format;
int _fd;
static char string[0x100];
// va_start(ap,format);
__asm__ ("pla"); // restore argument size
__asm__ ("ldx #$00"); // clear hibyte of AX
ap=__AX__;
ap+=(char*)&format;
// get value of fd
ap-=2;
_fd=*((int*)ap);
// get value of format
ap-=2;
_format=*((char**)ap);
// vsprintf(string,format,ap);
vsprintf(&string[0],_format,ap);
printf("fd:%d,format:%s,string:%s\n",_fd,_format,string);
// va_end(ap);
}
}
#endif
void chk0(char *format,...){
va_list ap;
static char string[0x100];
va_start(ap,format);
vsprintf(string,format,ap);
printf("format:%s,string:%s\n",format,string);
va_end(ap);
}
void chk1(int fd,char *format,...){
va_list ap;
static char string[0x100];
va_start(ap,format);
vsprintf(string,format,ap);
printf("fd:%d,format:%s,string:%s\n",fd,format,string);
va_end(ap);
}
int main(int argc,char **argv) {
printf("varargs test\n");
printf("\nchk0/0:\n");chk0("chk0 %s","arg0");
printf("\nchk0/1:\n");chk0("chk0 %s %s","arg0","arg1");
printf("\nchk0/2:\n");chk0("chk0 %s %s %s","arg0","arg1","arg2");
printf("\nchk1/0:\n");chk1(0xfd,"chk1 %s","arg0");
printf("\nchk1/1:\n");chk1(0xfd,"chk1 %s %s","arg0","arg1");
printf("\nchk1/2:\n");chk1(0xfd,"chk1 %s %s %s","arg0","arg1","arg2");
return 0;
}
|