#include <string.h>

#ifdef BUGGY_STATIC_CTORS
//#include <powerup/gcclib/powerup_protos.h>

typedef void (*fp)();

extern fp __CTOR_LIST__;
extern fp __DTOR_LIST__;
extern fp __END_CTOR_LIST__;
extern fp __END_DTOR_LIST__;


class stub_t {
  public:
    stub_t() {
	//PPCprintf("ctors: %lx-%lx\n",&__CTOR_LIST__,&__END_CTOR_LIST__);
	//PPCprintf("dtors: %lx-%lx\n",&__DTOR_LIST__,&__END_DTOR_LIST__);
	fp* p=&__CTOR_LIST__;
	while(*p++);
	while(p<&__END_CTOR_LIST__) {
	    if(*p)
		(*p)();
	    ++p;
	}
    }
    ~stub_t() {
	fp* p=&__DTOR_LIST__;
	while(*p++);
	while(p<&__END_DTOR_LIST__) {
	    if(*p)
		(*p)();
	    ++p;
	}
    }
};

static stub_t stub;
#endif

#ifdef BUGGY_DIVDI3

extern "C" long long PPCDivu64(long long x,long long y);

extern "C" long long __divdi3(long long x,long long y) {
    /* PPCDivs64 seems buggy too... */
    bool neg=false;
    if(x<0) {
	x=-x;
	neg=true;
    }
    if(y<0) {
	y=-y;
	neg=!neg;
    }
    x=PPCDivu64(x,y);
    return neg?-x:x;
}

#endif

#ifdef BUGGY_ATOI

extern "C" int atoi(const char *s) {
    return (int)strtol(s,NULL,10);
}

#endif

#ifdef BUGGY_STRNCPY

extern "C" char *strncpy(char *p0,const char *q,size_t len) {
    char *p=p0;
    if(len) {
	do {
	    if(!(*p++=*q++)) {
		if(--len)
		    memset(p,0,len);
		break;
	    }
	} while(--len);
    }
    return p0;
}

#endif

