From ac044784a6ce64ff15610d4b70750065a7f01b80 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 1 Sep 2015 21:07:54 +0200 Subject: start on makefile and fixing warnings and errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/errno.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/string/memccpy.c | 12 ++++++------ src/string/memcpy.c | 10 +++++++--- src/string/memfrob.c | 1 - src/string/strcat.c | 1 - src/string/strchr.c | 19 ++++++++++++------- src/string/strcmp.c | 1 - src/string/strcpy.c | 3 +-- src/string/strdup.c | 1 - src/string/strerror.c | 2 +- src/string/strfry.c | 5 ++++- src/string/strlen.c | 1 - src/string/strmove.c | 3 +-- src/string/strspn.c | 8 +++++--- src/string/strstr.c | 1 - src/string/strtok.c | 3 +-- src/strings/bzero.c | 2 +- src/wchar/wcscat.c | 3 +-- src/wchar/wcschr.c | 4 +++- src/wchar/wcscmp.c | 1 - src/wchar/wcscpy.c | 17 ++++++++--------- src/wchar/wcsdup.c | 1 - src/wchar/wcslen.c | 1 - src/wchar/wcsmove.c | 15 +++++++-------- src/wchar/wcsspn.c | 6 ++++-- src/wchar/wcsstr.c | 1 - src/wchar/wcstok.c | 3 +-- src/wchar/wmemccpy.c | 8 ++++---- src/wchar/wmemset.c | 2 +- 29 files changed, 119 insertions(+), 67 deletions(-) create mode 100644 src/errno.c (limited to 'src') diff --git a/src/errno.c b/src/errno.c new file mode 100644 index 0000000..80b46fe --- /dev/null +++ b/src/errno.c @@ -0,0 +1,51 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * This is the name that was used to invoke the program + * running in the current process. This is the value + * of `argv[0]` from the `main` function (where `argv` + * is the second parameter). If `argc` is zero, this + * variable will have the value `NULL`. This is not + * necessarily a proper comman name. For example, + * login shells are usually prefixes with a dash, + * for example "-bash", despite that there is no such + * command. Often, but not always, this will not contain + * directory. + * + * This string may be edited if `program_invocation_short_name` + * or `argv[0]` is edited. + * + * This is a GNU and slibc extension. + */ +char* program_invocation_name = NULL; + +/** + * Variant of `program_invocation_name` that is + * guaranteed to not include the directory. + * + * This string may be edited if `program_invocation_name` + * or `argv[0]` is edited. + * + * This is a GNU extension. + */ +char* program_invocation_short_name = NULL; + diff --git a/src/string/memccpy.c b/src/string/memccpy.c index 85378d9..4212419 100644 --- a/src/string/memccpy.c +++ b/src/string/memccpy.c @@ -35,10 +35,10 @@ */ void* memccpy(void* restrict whither, const void* restrict whence, int c, size_t size) { - char_t* stop = memchr(whence, c, size); - void* r = NULL + char* stop = memchr(whence, c, size); + void* r = NULL; if (stop != NULL) - size = (size_t)(stop - whence), r = whither + size; + size = (size_t)(stop - (const char*)whence), r = whither + size; memcpy(whither, whence, size); return r; } @@ -62,10 +62,10 @@ void* memccpy(void* restrict whither, const void* restrict whence, int c, size_t */ void* memcmove(void* whither, const void* whence, int c, size_t size) { - char_t* stop = memchr(whence, c, size); - void* r = NULL + char* stop = memchr(whence, c, size); + void* r = NULL; if (stop != NULL) - size = (size_t)(stop - whence), r = whither + size; + size = (size_t)(stop - (const char*)whence), r = whither + size; memmove(whither, whence, size); return r; } diff --git a/src/string/memcpy.c b/src/string/memcpy.c index 4582544..02479cd 100644 --- a/src/string/memcpy.c +++ b/src/string/memcpy.c @@ -18,6 +18,9 @@ #include +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + /** * Copy a memory segment to another, non-overlapping, segment. @@ -30,10 +33,11 @@ void* memcpy(void* restrict whither, const void* restrict whence, size_t size) { /* TODO improve implementation of memcpy */ - void* r = whither; + char* d = whither; + char* s = whence; while (size--) - *whither++ = *whence++; - return r; + *d++ = *s++; + return whither; } diff --git a/src/string/memfrob.c b/src/string/memfrob.c index e43496a..7ec9abd 100644 --- a/src/string/memfrob.c +++ b/src/string/memfrob.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include diff --git a/src/string/strcat.c b/src/string/strcat.c index ed10cc2..2205f39 100644 --- a/src/string/strcat.c +++ b/src/string/strcat.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include diff --git a/src/string/strchr.c b/src/string/strchr.c index 21345d4..2b49c2e 100644 --- a/src/string/strchr.c +++ b/src/string/strchr.c @@ -16,7 +16,9 @@ * along with this program. If not, see . */ #include -#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" @@ -31,9 +33,10 @@ */ void* memchr(const void* segment, int c, size_t size) { + char* s = segment; while (size--) - if (*segment++ == c) - return segment - 1; + if (*s++ == c) + return s - 1; return NULL; } @@ -50,9 +53,10 @@ void* memchr(const void* segment, int c, size_t size) */ void* rawmemchr(const void* segment, int c) { + char* s = segment; for (;;) - if (*segment++ == c) - return segment - 1; + if (*s++ == c) + return s - 1; } @@ -71,9 +75,10 @@ void* rawmemchr(const void* segment, int c) */ void* memrchr(const void* segment, int c, size_t size) { + char* s = segment; while (size--) - if (segment[size] == c) - return segment + size; + if (s[size] == c) + return s + size; return NULL; } diff --git a/src/string/strcmp.c b/src/string/strcmp.c index 966bb06..f626f91 100644 --- a/src/string/strcmp.c +++ b/src/string/strcmp.c @@ -17,7 +17,6 @@ */ #include #include -#include #include #include diff --git a/src/string/strcpy.c b/src/string/strcpy.c index dfbdc75..17dd339 100644 --- a/src/string/strcpy.c +++ b/src/string/strcpy.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include @@ -93,7 +92,7 @@ char* strstrcpy(char* restrict whither, const char* restrict whence, const char* { const char* stop = str == NULL ? NULL : strstr(whence, str); size_t n = stop == NULL ? strlen(whence) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL ? whither + n; + char* r = stop == NULL ? NULL : (whither + n); memcpy(whither, whence, n); whither[n] = 0; return r; diff --git a/src/string/strdup.c b/src/string/strdup.c index c65970e..c23548b 100644 --- a/src/string/strdup.c +++ b/src/string/strdup.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include diff --git a/src/string/strerror.c b/src/string/strerror.c index 7366f63..87f3261 100644 --- a/src/string/strerror.c +++ b/src/string/strerror.c @@ -16,7 +16,7 @@ * along with this program. If not, see . */ #include -#include +#include diff --git a/src/string/strfry.c b/src/string/strfry.c index f020202..cf8d643 100644 --- a/src/string/strfry.c +++ b/src/string/strfry.c @@ -34,13 +34,16 @@ char* strfry(char* anagram) { size_t i, j; + int r; char t; if (anagram == NULL) return NULL; for (i = strlen(anagram); --i;) { - j = (int)((double)rand() / (RAND_MAX + 1)); + r = rand(); + j = (int)((double)r / (RAND_MAX + 1)); t = anagram[i], anagram[i] = anagram[j], anagram[j] = t; } + return anagram; } diff --git a/src/string/strlen.c b/src/string/strlen.c index 7023cc0..7a08fa0 100644 --- a/src/string/strlen.c +++ b/src/string/strlen.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include diff --git a/src/string/strmove.c b/src/string/strmove.c index 86298cf..596822a 100644 --- a/src/string/strmove.c +++ b/src/string/strmove.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include @@ -97,7 +96,7 @@ char* strstrmove(char* whither, const char* whence, const char* restrict str) { const char* stop = str == NULL ? NULL : strstr(whence, str); size_t n = stop == NULL ? strlen(whence) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL ? whither + n; + char* r = stop == NULL ? NULL : (whither + n); memmove(whither, whence, n); whither[n] = 0; return r; diff --git a/src/string/strspn.c b/src/string/strspn.c index cf82529..3facfd8 100644 --- a/src/string/strspn.c +++ b/src/string/strspn.c @@ -16,7 +16,9 @@ * along with this program. If not, see . */ #include -#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" @@ -59,7 +61,7 @@ size_t strcspn(const char* string, const char* stopset) char c; const char* s = string; memset(set, 0, 256); - while ((c = *skipset++)) + while ((c = *stopset++)) set[(size_t)c] = 1; while ((c = *s++)) if (!set[(size_t)c]) @@ -86,7 +88,7 @@ char* stpbrk(const char* string, const char* stopset) char c; const char* s = string; memset(set, 0, 256); - while ((c = *skipset++)) + while ((c = *stopset++)) set[(size_t)c] = 1; while ((c = *s++)) if (!set[(size_t)c]) diff --git a/src/string/strstr.c b/src/string/strstr.c index a7e490a..68cc647 100644 --- a/src/string/strstr.c +++ b/src/string/strstr.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include #include diff --git a/src/string/strtok.c b/src/string/strtok.c index 04668fc..1520431 100644 --- a/src/string/strtok.c +++ b/src/string/strtok.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include @@ -102,7 +101,7 @@ char* strsep(char** restrict string, const char* restrict delimiters) if (r == NULL) return NULL; - next = stpbrk(string, delimiters); + next = stpbrk(r, delimiters); if (next != NULL) *next++ = 0; *string = next; diff --git a/src/strings/bzero.c b/src/strings/bzero.c index ab7a5f8..4be0948 100644 --- a/src/strings/bzero.c +++ b/src/strings/bzero.c @@ -23,7 +23,7 @@ /** * `memset`, except calls to it cannot be removed by the compiler. */ -void* (volatile *__slibc_explicit_memset)(void*, int, size_t) = memset; +void* (*volatile __slibc_explicit_memset)(void*, int, size_t) = memset; diff --git a/src/wchar/wcscat.c b/src/wchar/wcscat.c index c7b1c5e..491f9b7 100644 --- a/src/wchar/wcscat.c +++ b/src/wchar/wcscat.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include @@ -55,7 +54,7 @@ wchar_t* wcscat(wchar_t* restrict whither, const wchar_t* restrict whence) */ wchar_t* wcsncat(wchar_t* restrict whither, const wchar_t* restrict whence, size_t maxlen) { - wcsncpy(whither + wcslen(whither), whence, laxmen); + wcsncpy(whither + wcslen(whither), whence, maxlen); return whither; } diff --git a/src/wchar/wcschr.c b/src/wchar/wcschr.c index 493f0a7..16ecac2 100644 --- a/src/wchar/wcschr.c +++ b/src/wchar/wcschr.c @@ -16,7 +16,9 @@ * along with this program. If not, see . */ #include -#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" diff --git a/src/wchar/wcscmp.c b/src/wchar/wcscmp.c index ebac2df..c9d1180 100644 --- a/src/wchar/wcscmp.c +++ b/src/wchar/wcscmp.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include #include #include diff --git a/src/wchar/wcscpy.c b/src/wchar/wcscpy.c index 7d75a98..37b8ed4 100644 --- a/src/wchar/wcscpy.c +++ b/src/wchar/wcscpy.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include @@ -67,9 +66,9 @@ wchar_t* wcpcpy(wchar_t* restrict whither, const wchar_t* restrict whence) * one character passed the last written non-NUL * character. */ -wchar_t* wcsccpy(wchar_t* restrict whither, const wchar_t* restrict whence, wchat_t c) +wchar_t* wcsccpy(wchar_t* restrict whither, const wchar_t* restrict whence, wchar_t c) { - wchar_t* r = wmemccopy(whither, whence, c, wcslen(whence) + 1); + wchar_t* r = wmemccpy(whither, whence, c, wcslen(whence) + 1); if (r) *r = 0; return r; @@ -96,7 +95,7 @@ wchar_t* wcswcscpy(wchar_t* restrict whither, const wchar_t* restrict whence, co { const wchar_t* stop = str == NULL ? NULL : wcsstr(whence, str); size_t n = stop == NULL ? wcslen(whence) : (size_t)(stop - whence); - wchar_t* r = stop == NULL ? NULL ? whither + n; + wchar_t* r = stop == NULL ? NULL : (whither + n); wmemcpy(whither, whence, n); whither[n] = 0; return r; @@ -176,11 +175,11 @@ wchar_t* wcpncpy(wchar_t* restrict whither, const wchar_t* restrict whence, size * one character passed the last written non-NUL * character. */ -wchar_t* wcscncpy(wchar_t* restrict whither, const wchar_t* restrict whence, wchat_t c, size_t maxlen) +wchar_t* wcscncpy(wchar_t* restrict whither, const wchar_t* restrict whence, wchar_t c, size_t maxlen) { - const char* stop = wmemchr(whence, c, maxlen); + const wchar_t* stop = wmemchr(whence, c, maxlen); size_t n = stop == NULL ? wcsnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); + wchar_t* r = stop == NULL ? NULL : (whither + n); wmemcpy(whither, whence, n); wmemset(whither, 0, maxlen - n); return r; @@ -213,9 +212,9 @@ wchar_t* wcscncpy(wchar_t* restrict whither, const wchar_t* restrict whence, wch wchar_t* wcswcsncpy(wchar_t* restrict whither, const wchar_t* restrict whence, const wchar_t* restrict str, size_t maxlen) { - const char* stop = wcsnstr(whence, str, maxlen); + const wchar_t* stop = wcsnstr(whence, str, maxlen); size_t n = stop == NULL ? wcsnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); + wchar_t* r = stop == NULL ? NULL : (whither + n); wmemcpy(whither, whence, n); wmemset(whither, 0, maxlen - n); return r; diff --git a/src/wchar/wcsdup.c b/src/wchar/wcsdup.c index dbbbb4c..37b57f9 100644 --- a/src/wchar/wcsdup.c +++ b/src/wchar/wcsdup.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include diff --git a/src/wchar/wcslen.c b/src/wchar/wcslen.c index ad81227..b4a07c6 100644 --- a/src/wchar/wcslen.c +++ b/src/wchar/wcslen.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include diff --git a/src/wchar/wcsmove.c b/src/wchar/wcsmove.c index c84e4ee..bf86928 100644 --- a/src/wchar/wcsmove.c +++ b/src/wchar/wcsmove.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include @@ -69,7 +68,7 @@ wchar_t* wcpmove(wchar_t* whither, const wchar_t* whence) * one character passed the last written non-NUL * character. */ -wchar_t* wcscmove(wchar_t* whither, const wchar_t* whence, wchat_t c) +wchar_t* wcscmove(wchar_t* whither, const wchar_t* whence, wchar_t c) { wchar_t* r = wmemcmove(whither, whence, c, wcslen(whence) + 1); if (r) @@ -98,7 +97,7 @@ wchar_t* wcswcsmove(wchar_t* whither, const wchar_t* whence, const wchar_t* rest { const wchar_t* stop = str == NULL ? NULL : wcsstr(whence, str); size_t n = stop == NULL ? wcslen(whence) : (size_t)(stop - whence); - wchar_t* r = stop == NULL ? NULL ? whither + n; + wchar_t* r = stop == NULL ? NULL : (whither + n); wmemmove(whither, whence, n); whither[n] = 0; return r; @@ -179,11 +178,11 @@ wchar_t* wcpnmove(wchar_t* whither, const wchar_t* whence, size_t maxlen) * one character passed the last written non-NUL * character. */ -wchar_t* wcscnmove(wchar_t* whither, const wchar_t* whence, wchat_t c, size_t maxlen) +wchar_t* wcscnmove(wchar_t* whither, const wchar_t* whence, wchar_t c, size_t maxlen) { - const char* stop = wmemchr(whence, c, maxlen); + const wchar_t* stop = wmemchr(whence, c, maxlen); size_t n = stop == NULL ? wcsnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); + wchar_t* r = stop == NULL ? NULL : (whither + n); wmemmove(whither, whence, n); wmemset(whither, 0, maxlen - n); return r; @@ -215,9 +214,9 @@ wchar_t* wcscnmove(wchar_t* whither, const wchar_t* whence, wchat_t c, size_t ma */ wchar_t* wcswcsnmove(wchar_t* whither, const wchar_t* whence, const wchar_t* restrict str, size_t maxlen) { - const char* stop = wcsnstr(whence, str, maxlen); + const wchar_t* stop = wcsnstr(whence, str, maxlen); size_t n = stop == NULL ? wcsnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); + wchar_t* r = stop == NULL ? NULL : (whither + n); wmemmove(whither, whence, n); wmemset(whither, 0, maxlen - n); return r; diff --git a/src/wchar/wcsspn.c b/src/wchar/wcsspn.c index 360ab5f..d0ce908 100644 --- a/src/wchar/wcsspn.c +++ b/src/wchar/wcsspn.c @@ -16,7 +16,9 @@ * along with this program. If not, see . */ #include -#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" @@ -54,7 +56,7 @@ size_t wcscspn(const wchar_t* string, const wchar_t* stopset) size_t end = wcslen(string); wchar_t* p; wchar_t c; - while ((c = *skipset++)) + while ((c = *stopset++)) if (p = wcsnchr(string, c, end), p != NULL) end = (size_t)(p - string); return end; diff --git a/src/wchar/wcsstr.c b/src/wchar/wcsstr.c index bba7d96..0ae7b71 100644 --- a/src/wchar/wcsstr.c +++ b/src/wchar/wcsstr.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include #include #define WIDE diff --git a/src/wchar/wcstok.c b/src/wchar/wcstok.c index 1598d07..2694dc0 100644 --- a/src/wchar/wcstok.c +++ b/src/wchar/wcstok.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include -#include @@ -80,7 +79,7 @@ wchar_t* wcssep(wchar_t** restrict string, const wchar_t* restrict delimiters) if (r == NULL) return NULL; - next = wcpbrk(string, delimiters); + next = wcpbrk(r, delimiters); if (next != NULL) *next++ = 0; *string = next; diff --git a/src/wchar/wmemccpy.c b/src/wchar/wmemccpy.c index fbabe0b..cde1a6a 100644 --- a/src/wchar/wmemccpy.c +++ b/src/wchar/wmemccpy.c @@ -38,10 +38,10 @@ wchar_t* wmemccpy(wchar_t* restrict whither, const wchar_t* restrict whence, wchar_t c, size_t size) { wchar_t* stop = wmemchr(whence, c, size); - wchar_t* r = NULL + wchar_t* r = NULL; if (stop != NULL) size = (size_t)(stop - whence), r = whither + size; - memcpy(whither, whence, size); + wmemcpy(whither, whence, size); return r; } @@ -65,10 +65,10 @@ wchar_t* wmemccpy(wchar_t* restrict whither, const wchar_t* restrict whence, wch wchar_t* wmemcmove(wchar_t* whither, const wchar_t* whence, wchar_t c, size_t size) { wchar_t* stop = wmemchr(whence, c, size); - wchar_t* r = NULL + wchar_t* r = NULL; if (stop != NULL) size = (size_t)(stop - whence), r = whither + size; - memmove(whither, whence, size); + wmemmove(whither, whence, size); return r; } diff --git a/src/wchar/wmemset.c b/src/wchar/wmemset.c index 2a60b45..95ec70f 100644 --- a/src/wchar/wmemset.c +++ b/src/wchar/wmemset.c @@ -27,7 +27,7 @@ * @param size The number of wide characters in the memory segment. * @return `segment` is returned. */ -wchar_t* wmemset(wchar_t* segment, wchar_t c, size_t size); +wchar_t* wmemset(wchar_t* segment, wchar_t c, size_t size) { wchar_t* r = segment; while (size--) -- cgit v1.2.3-70-g09d2