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

Re: [Lost] [Patch] Ein paar neue Libc-Funktionen



Am Sonntag, 13. Januar 2008 01.01:00 schrieb Kevin Wolf:
> Antoine Kaufmann schrieb:
> > +double atof(const char* str)
> > +{
> > +    // TODO
> > +    return 0;
> > +}
>
> Das macht mir wie gesagt noch Bauchschmerzen, weil das schwer zu
> findende Bugs verursachen könnte.
>
> => #define LIBC_STUBS in config.h und standardmäßig deaktiviert.
>
> > Index: src/include/ctype.h
> > ===================================================================
> > --- src/include/ctype.h	(Revision 688)
> > +++ src/include/ctype.h	(Arbeitskopie)
> > @@ -43,6 +43,7 @@
> >  #define isxdigit(c) (isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >=
> > 'A' && (c) <= 'F')) #define isupper(c) ((c) >= 'A' && (c) <= 'Z')
> >  #define islower(c) ((c) >= 'a' && (c) <= 'z')
> > +#define ispunct(c) ((!isspace(c)) && (!isalpha(c)))
>
> #define ispunct(c) (isprint(c) && (!isspace(c)) && (!isalpha(c)))
>
> > +double fabs(double x)
> > +{
> > +    return ((x < 0) ? -x : x);
> > +}
>
> Auch wieder in #ifdef
>
> > +// Kuenstliche Begrenzung fuer Programme die sie benutzen wollen
> > +#define MAXPATHLEN 4096
> > +
> > +#endif //ifndef _SYS_PARA;_H_
>
> Tippfehler im Kommentar. ;-)
>
> > +/**
> > + * Prueft ob die Annahme stimmt. Falls sie das nicht tut, wird mit einer
> > + * Fehlermeldung abgebrochen.
> > + */
> > +void _assert(const char* assertation, int expression)
> > +{
> > +    if (!expression) {
> > +        fprintf(stderr, "Annahme fehlgeschlagen: %s\n", assertation);
> > +    }
> > +}
> > +
>
> Das Wort heißt assertion und auf Deutsch Zusicherung. Außerdem muß das
> Programm nach Ausgabe der Fehlermeldung durch den Aufruf von abort()
> abgebrochen werden.


Hier eine überarbeitete Version des Patchs.

Index: src/include/string.h
===================================================================
--- src/include/string.h	(Revision 688)
+++ src/include/string.h	(Arbeitskopie)
@@ -3,7 +3,6 @@
 
 #include <types.h>
 #include "bsdtypes.h"
-#include <ctype.h>
 
 void* memcpy(void* dest, const void* src, size_t num);
 void* memmove(void* dest, const void* src, size_t num);
@@ -42,5 +41,7 @@
 
 char *strpbrk(const char *s1, const char *s2);
 
+char* strerror(int error_code);
+
 #endif /* ndef _STRING_H */
 
Index: src/lib/string/strerror.c
===================================================================
--- src/lib/string/strerror.c	(Revision 0)
+++ src/lib/string/strerror.c	(Revision 0)
@@ -0,0 +1,52 @@
+/*  
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR 
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <types.h>
+#include <string.h>
+
+/**
+ * Netter Fehlertext zu der angegebenen Fehlernummer zurueckgeben
+ * TODO: ATM nur stub
+ *
+ * @param error_code Fehlernummer
+ * 
+ * @return Pointer auf internen Buffer mit Fehlermeldung
+ */
+char* strerror(int error_code)
+{
+    static char* error_message = "Unbekannter Fehler";
+    return error_message;
+}
+
Index: src/include/stdlib.h
===================================================================
--- src/include/stdlib.h	(Revision 688)
+++ src/include/stdlib.h	(Arbeitskopie)
@@ -38,6 +38,7 @@
 
 #include "types.h"
 #include "string.h"
+#include <config.h>
 
 #define EXIT_SUCCESS 0
 #define EXIT_FAILURE 1
@@ -62,4 +63,9 @@
 
 char* mktemp(char* template);
 
+#ifndef CONFIG_LIBC_NO_STUBS
+double atof(const char* str);
 #endif
+int abs(int x);
+
+#endif
Index: src/lib/string.c
===================================================================
--- src/lib/string.c	(Revision 688)
+++ src/lib/string.c	(Arbeitskopie)
@@ -32,9 +32,10 @@
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+#include <config.h>
+#include <string.h>
+#include <stdlib.h>
 
-#include "string.h"
-
 size_t strnlen(const char *s, size_t maxlen)
 {
     size_t len = 0;
@@ -128,3 +129,12 @@
     }
     return result;
 }
+
+#ifndef CONFIG_LIBC_NO_STUBS
+double atof(const char* str)
+{
+    // TODO
+    return 0;
+}
+#endif
+
Index: src/lib/misc.c
===================================================================
--- src/lib/misc.c	(Revision 0)
+++ src/lib/misc.c	(Revision 0)
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <stdlib.h>
+
+/**
+ * Absolutbetrag einer Zahl errechnen
+ */
+int abs(int x)
+{
+    return (x < 0 ? -x : x);
+}
Index: src/include/ctype.h
===================================================================
--- src/include/ctype.h	(Revision 688)
+++ src/include/ctype.h	(Arbeitskopie)
@@ -36,6 +36,9 @@
 #ifndef _CTYPE_H_
 #define _CTYPE_H_
 
+#define isprint(c) ((c) >= (char) 0x20)
+#define iscntrl(c) ((c) < (char) 0x20)
+
 #define isspace(c) ((c) == ' ' || (c) == '\n' || (c) == '\t' || (c) == '\r')
 #define isdigit(c) ((c) >= '0' && (c) <= '9')
 #define isalpha(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
@@ -43,10 +46,8 @@
 #define isxdigit(c) (isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
 #define isupper(c) ((c) >= 'A' && (c) <= 'Z')
 #define islower(c) ((c) >= 'a' && (c) <= 'z')
+#define ispunct(c) (isprint(c) && (!isspace(c)) && (!isalpha(c)))
 
-#define isprint(c) ((c) >= (char) 0x20)
-#define iscntrl(c) ((c) < (char) 0x20)
-
 #define tolower(c) (isupper(c) ? (c) - 'A' + 'a' : (c))
 #define toupper(c) (islower(c) ? (c) - 'a' + 'A' : (c))
 
Index: src/modules/include/math.h
===================================================================
--- src/modules/include/math.h	(Revision 688)
+++ src/modules/include/math.h	(Arbeitskopie)
@@ -35,5 +35,11 @@
 
 #ifndef _MATH_H_
 #define _MATH_H_
+#include <config.h>
 
+// Betrag einer reelen Zahl errechen
+#ifndef CONFIG_LIBC_NO_STUBS
+double fabs(double x);
 #endif
+
+#endif
Index: src/modules/lib/stdlibc/math.c
===================================================================
--- src/modules/lib/stdlibc/math.c	(Revision 0)
+++ src/modules/lib/stdlibc/math.c	(Revision 0)
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <math.h>
+#include <config.h>
+
+// FIXME: Nur bis der Float-Kram vom Kernel unterstuetzt wird
+#ifndef CONFIG_LIBC_NO_STUBS
+
+/**
+ * Betrag einer reellen Zahl errechnen
+ */
+double fabs(double x)
+{
+    return ((x < 0) ? -x : x);
+}
+
+#endif
+
Index: src/modules/include/assert.h
===================================================================
--- src/modules/include/assert.h	(Revision 688)
+++ src/modules/include/assert.h	(Arbeitskopie)
@@ -36,7 +36,15 @@
 #ifndef _ASSERT_H_
 #define _ASSERT_H_
 
-//FIXME
+#ifdef NDEBUG
 #define assert(x)
+#else
+void _assert(const char* assertation, const char* file, int line,
+    int expression);
 
+// Das #x verwandelt x in einen String
+#define assert(x) _assert(#x, __FILE__, __LINE__, x)
 #endif
+
+#endif
+
Index: src/modules/lib/stdlibc/assert.c
===================================================================
--- src/modules/lib/stdlibc/assert.c	(Revision 0)
+++ src/modules/lib/stdlibc/assert.c	(Revision 0)
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/**
+ * Wenn die uebergebene Bedingung nicht erfuellt wird, wird das Programm
+ * mit einer Fehlermeldung abgebrochen.
+ */
+void _assert(const char* assertation, const char* file, int line,
+    int expression)
+{
+    if (!expression) {
+        fprintf(stderr, "Annahme fehlgeschlagen: '%s' in '%d' auf Zeile %d\n",
+            assertation, file, line);
+        abort();
+    }
+}
+