lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Can you offer more detail of this minimal layer?

Here are the functions from the standard C library that are needed by
the Lua 5.1 core.

/*
* libc.c
* functions from the standard C library that are needed by the Lua 5.1 core
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
* 08 Nov 2007 09:24:22
* This code is hereby placed in the public domain.
*/

#include <string.h>

size_t strlen(const char *s)
{
 size_t n=0;
 while (*s++) n++;
 return n;
}

char *strchr(const char *s, int c)
{
 while (*s!=c && *s!=0) s++;
 return (*s==c) ? (char*)s : NULL;
}

char *strcpy(char *d, const char *s)
{
 char *t=d;
 while ((*t++=*s++)) ;
 return d;
}

char *strncpy(char *d, const char *s, size_t n)
{
 char *t=d;
 while (n-- && (*t++=*s++)) ;
#if 0
 if (n+1) while (n--) *t++='-';
#endif
 return d;
}

char *strcat(char *d, const char *s)
{
 return strcpy(d+strlen(d),s);
}

char *strncat(char *d, const char *s, size_t n)
{
 return strncpy(d+strlen(d),s,n);
}

int strcmp(const char *s1, const char *s2)
{
 const unsigned char *a=s1;
 const unsigned char *b=s2;
 while (*a==*b && *a!=0 && *b!=0) a++,b++;
 return *a-*b;
}

int strcoll(const char *s1, const char *s2)
{
 return strcmp(s1,s2);
}

size_t strcspn(const char *s, const char *reject)
{
 size_t n=0;
 for (n=0; *s; n++,s++)
 {
  const char *r;
  for (r=reject; *r; r++) if (*r==*s) return n;
 }
 return n;
}

void *memcpy(void *d, const void *s, size_t n)
{
 char *a=d;
 const char *b=s;
 while (n--) *a++=*b++;
 return d;
}

int memcmp(const void *s1, const void *s2, size_t n)
{
 const unsigned char *a=s1;
 const unsigned char *b=s2;
 if (n==0) return 0;
 while (--n && *a==*b) a++,b++;
 return *a-*b;
}

double floor(double x)
{
 return (double)(int)x;
}

int abs(int x)
{
 return (x>=0) ? x : -x;
}

#include <ctype.h>

int (isalpha)(int c)
{
 return (c>='A' && c<='Z') || (c>='a' && c<='z');
}

int (isalnum)(int c)
{
 return (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9');
}

int (isdigit)(int c)
{
 return (c>='0' && c<='9');
}

int (isspace)(int c)
{
 return (c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\f' || c=='\v');

}

int (iscntrl)(int c)
{
 return (c>=0 && c<' ') || c==127;
}


#ifdef TEST
#include <stdio.h>

int main(void)
{
 char a[100];
 int i;
 for (i=0; i<10; i++)
 {
  char* b;
  strcpy(a,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  b=strncpy(a,"lhf",i);
  printf("%d [%s]\n",i,b);
  b[strlen(b)]='!';
  printf("%d [%s]\n",i,b);
 }
 return 0;
}
#endif

/*

pow
localeconv

sprintf
strtod

setjmp
longjmp

exit

http://doxygen.postgresql.org/strtoul_8c-source.html
http://www.italios.it/os2/stdlib_8c-source.html

http://www.cab.u-szeged.hu/linux/kernel/linux/lib/string.c.html
http://www.italios.it/os2/string_8c-source.html
http://www.fefe.de/dietlibc/

OpenBSD has vsprintf under a 3-clause BSD license:
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/vsprintf.c

fmtfp in http://www.ClearSilver.net/

strtod
needed for the lexer
comp.sources.misc v04i013: Replacement for strtod()
Lua 3.2 has a reasonable one

sprintf
needed only for conversion in lvm.c and so can do a sloppy job.
need a good one for the string library though.

math functions for freestanding C
http://www.mindspring.com/~pfilandr/C/fs_math/

*/