home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / installinit / minilibc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-10  |  3.0 KB  |  192 lines

  1. #define MINILIBC_INTERNAL
  2.  
  3. #include "minilibc.h"
  4.  
  5. int atexit (void (*__func) (void)) {
  6.     return 0;
  7. }
  8.  
  9. void exit() {
  10. }
  11.  
  12. char ** _environ = NULL;
  13. int errno = 0;
  14.  
  15. void _init (int __status) {
  16. }
  17.  
  18. void __libc_init_first (int __status) {
  19. }
  20.  
  21. void _fini (int __status) {
  22. }
  23.  
  24. inline int socket(int a, int b, int c) {
  25.     unsigned long args[] = { a, b, c };
  26.  
  27.     return socketcall(SYS_SOCKET, args);
  28. }
  29.  
  30. inline int bind(int a, void * b, int c) {
  31.     unsigned long args[] = { a, (long) b, c };
  32.  
  33.     return socketcall(SYS_BIND, args);
  34. }
  35.  
  36. inline int listen(int a, int b) {
  37.     unsigned long args[] = { a, b, 0 };
  38.  
  39.     return socketcall(SYS_LISTEN, args);
  40. }
  41.  
  42. inline int accept(int a, void * addr, void * addr2) {
  43.     unsigned long args[] = { a, (long) addr, (long) addr2 };
  44.  
  45.     return socketcall(SYS_ACCEPT, args);
  46. }
  47.  
  48. int strlen(const char * string) {
  49.     int i = 0;
  50.  
  51.     while (*string++) i++;
  52.  
  53.     return i;
  54. }
  55.  
  56. char * strncpy(char * dst, const char * src, int len) {
  57.     char * chptr = dst;
  58.     int i = 0;
  59.  
  60.     while (*src && i < len) *dst++ = *src++, i++;
  61.     if (i < len) *dst = '\0';
  62.  
  63.     return chptr;
  64. }
  65.  
  66. char * strcpy(char * dst, const char * src) {
  67.     char * chptr = dst;
  68.  
  69.     while (*src) *dst++ = *src++;
  70.     *dst = '\0';
  71.  
  72.     return chptr;
  73. }
  74.  
  75. void * memcpy(void * dst, const void * src, int count) {
  76.     char * a = dst;
  77.     const char * b = src;
  78.  
  79.     while (count--)
  80.     *a++ = *b++;
  81.  
  82.     return dst;
  83. }
  84.  
  85. void sleep(int secs) {
  86.     struct timeval tv;
  87.  
  88.     tv.tv_sec = secs;
  89.     tv.tv_usec = 0;
  90.  
  91.     select(0, NULL, NULL, NULL, &tv);
  92. }
  93.  
  94. int strcmp(const char * a, const char * b) {
  95.     int i, j;  
  96.  
  97.     i = strlen(a); j = strlen(b);
  98.     if (i < j)
  99.     return -1;
  100.     else if (j < i)
  101.     return 1;
  102.  
  103.     while (*a && (*a == *b)) a++, b++;
  104.  
  105.     if (!*a) return 0;
  106.  
  107.     if (*a < *b)
  108.     return -1;
  109.     else
  110.     return 1;
  111. }
  112.  
  113. int strncmp(const char * a, const char * b, int len) {
  114.     char buf1[1000], buf2[1000];
  115.  
  116.     strncpy(buf1, a, len);
  117.     strncpy(buf2, b, len);
  118.     buf1[len] = '\0';
  119.     buf2[len] = '\0';
  120.  
  121.     return strcmp(buf1, buf2);
  122. }
  123.  
  124. void printint(int i) {
  125.     char buf[10];
  126.     char * chptr = buf + 9;
  127.     int j = 0;
  128.  
  129.     if (i < 0) {
  130.     write(1, "-", 1);
  131.     i = -1 * i;
  132.     }
  133.  
  134.     while (i) {
  135.     *chptr-- = '0' + (i % 10);
  136.     j++;
  137.     i = i / 10;
  138.     }
  139.  
  140.     write(1, chptr + 1, j);
  141. }
  142.  
  143. char * strchr(char * str, int ch) {
  144.     char * chptr;
  145.  
  146.     chptr = str;
  147.     while (*chptr) {
  148.     if (*chptr == ch) return chptr;
  149.     chptr++;
  150.     }
  151.  
  152.     return NULL;
  153. }
  154.  
  155. void printf(char * fmt, ...) {
  156.     char buf[2048];
  157.     char * start = buf;
  158.     char * chptr = buf;
  159.     va_list args;
  160.     char * strarg;
  161.     int numarg;
  162.  
  163.     strcpy(buf, fmt);
  164.     va_start(args, fmt);
  165.  
  166.     while (start) {
  167.     while (*chptr != '%' && *chptr) chptr++;
  168.  
  169.     if (*chptr == '%') {
  170.         *chptr++ = '\0';
  171.         printstr(start);
  172.  
  173.         switch (*chptr++) {
  174.           case 's': 
  175.         strarg = va_arg(args, char *);
  176.         printstr(strarg);
  177.         break;
  178.  
  179.           case 'd':
  180.         numarg = va_arg(args, int);
  181.         printint(numarg);
  182.         break;
  183.         }
  184.  
  185.         start = chptr;
  186.     } else {
  187.         printstr(start);
  188.         start = NULL;
  189.     }
  190.     }
  191. }
  192.