Compare commits

...

4 Commits

Author SHA1 Message Date
d9b7e8edc0
init: Read and launch service files in order using sort()
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-02 20:56:28 +02:00
3628464284
libc: Fix some naming inconsistencies 2023-05-02 20:56:28 +02:00
d1801d484c
libc: Add qsort() 2023-05-02 20:56:27 +02:00
dcd93cce3a
libluna: Add a sort() function 2023-05-02 20:56:27 +02:00
10 changed files with 108 additions and 7 deletions

View File

@ -1,6 +1,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <luna/PathParser.h> #include <luna/PathParser.h>
#include <luna/Sort.h>
#include <luna/String.h> #include <luna/String.h>
#include <luna/Vector.h> #include <luna/Vector.h>
#include <os/Directory.h> #include <os/Directory.h>
@ -239,6 +240,7 @@ static Result<void> load_services()
auto dir = TRY(os::Directory::open("/etc/init")); auto dir = TRY(os::Directory::open("/etc/init"));
auto services = TRY(dir->list(os::Directory::Filter::ParentAndBase)); auto services = TRY(dir->list(os::Directory::Filter::ParentAndBase));
sort(services.begin(), services.end(), String::compare);
for (const auto& entry : services) for (const auto& entry : services)
{ {

3
initrd/etc/init/00-motd Normal file
View File

@ -0,0 +1,3 @@
Name=motd
Command=/bin/cat /etc/motd
Wait=true

View File

@ -1 +1,2 @@
Hello, world! Welcome to the Luna system!
Have a look around, you can run 'ls /bin' to list available commands.

View File

@ -35,16 +35,16 @@ extern "C"
#endif #endif
/* Return the absolute value of an integer. */ /* Return the absolute value of an integer. */
int abs(int v); int abs(int);
/* Return the absolute value of a long integer. */ /* Return the absolute value of a long integer. */
long labs(long v); long labs(long);
/* Return the absolute value of a long long integer. */ /* Return the absolute value of a long long integer. */
long long llabs(long long v); long long llabs(long long);
/* Return the result of dividing two integers, including the remainder. */ /* Return the result of dividing two integers, including the remainder. */
div_t div(int num, int den); div_t div(int, int);
/* Return the result of dividing two long integers, including the remainder. */ /* Return the result of dividing two long integers, including the remainder. */
ldiv_t ldiv(long, long); ldiv_t ldiv(long, long);
@ -62,7 +62,7 @@ extern "C"
void* realloc(void* ptr, size_t size); void* realloc(void* ptr, size_t size);
/* Free heap memory. */ /* Free heap memory. */
void free(void*); void free(void* ptr);
/* Abort the program without performing any normal cleanup. */ /* Abort the program without performing any normal cleanup. */
__noreturn void abort(); __noreturn void abort();
@ -115,7 +115,9 @@ extern "C"
/* Clear all environment variables. */ /* Clear all environment variables. */
int clearenv(void); int clearenv(void);
void qsort(void*, size_t, size_t, int (*)(const void*, const void*)); /* Sort an array of arbitrary elements using a comparison function. */
void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
void* bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*)); void* bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*));
/* Convert a multibyte character string to a wide character string. */ /* Convert a multibyte character string to a wide character string. */

View File

@ -2,6 +2,7 @@
#include <limits.h> #include <limits.h>
#include <luna/Heap.h> #include <luna/Heap.h>
#include <luna/NumberParsing.h> #include <luna/NumberParsing.h>
#include <luna/Sort.h>
#include <luna/Utf8.h> #include <luna/Utf8.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/syscall.h> #include <sys/syscall.h>
@ -199,4 +200,9 @@ extern "C"
return status; return status;
} }
void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
{
c_quicksort(base, nmemb, size, compar);
}
} }

View File

@ -5,6 +5,7 @@ file(GLOB HEADERS include/luna/*.h)
set(FREESTANDING_SOURCES set(FREESTANDING_SOURCES
${HEADERS} ${HEADERS}
src/Format.cpp src/Format.cpp
src/Sort.cpp
src/NumberParsing.cpp src/NumberParsing.cpp
src/CString.cpp src/CString.cpp
src/CPath.cpp src/CPath.cpp

View File

@ -0,0 +1,26 @@
#pragma once
#include <luna/Types.h>
typedef int (*compar_t)(const void*, const void*);
void c_quicksort(void* base, usize nmemb, usize size, compar_t compar);
template <typename T> void sort(T* base, usize nmemb, int (*compar)(const T*, const T*))
{
return c_quicksort(base, nmemb, sizeof(T), (compar_t)compar);
}
template <typename T> void sort(T* base, usize nmemb, compar_t compar)
{
return c_quicksort(base, nmemb, sizeof(T), compar);
}
template <typename T> void sort(T* begin, T* end, int (*compar)(const T*, const T*))
{
return c_quicksort(begin, end - begin, sizeof(T), (compar_t)compar);
}
template <typename T> void sort(T* begin, T* end, compar_t compar)
{
return c_quicksort(begin, end - begin, sizeof(T), compar);
}

View File

@ -65,6 +65,8 @@ class String
static Result<String> from_cstring(const char* str); static Result<String> from_cstring(const char* str);
static Result<String> from_string_view(StringView str); static Result<String> from_string_view(StringView str);
static int compare(const String* a, const String* b);
const char* chars() const const char* chars() const
{ {
return m_inline ? m_inline_storage : m_string; return m_inline ? m_inline_storage : m_string;

53
libluna/src/Sort.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <luna/Alignment.h>
#include <luna/Sort.h>
static void swap_sized(void* ptr1, void* ptr2, usize size)
{
char* x = (char*)ptr1;
char* y = (char*)ptr2;
while (size--)
{
char t = *x;
*x = *y;
*y = t;
x += 1;
y += 1;
}
}
static usize partition(void* base, usize start, usize end, usize size, compar_t compar)
{
auto atindex = [&base, &size](usize index) { return offset_ptr(base, index * size); };
void* pivot = atindex(end);
usize i = (start - 1);
for (usize j = start; j <= end - 1; j++)
{
if (compar(atindex(j), pivot) < 0)
{
i++;
swap_sized(atindex(i), atindex(j), size);
}
}
swap_sized(atindex(i + 1), pivot, size);
return i + 1;
}
static void quicksort_impl(void* base, usize start, usize end, usize size, compar_t compar)
{
if (start < end)
{
usize pivot = partition(base, start, end, size, compar);
if ((end - start) < 2) return;
quicksort_impl(base, start, pivot - 1, size, compar);
quicksort_impl(base, pivot + 1, end, size, compar);
}
}
void c_quicksort(void* base, usize nmemb, usize size, compar_t compar)
{
quicksort_impl(base, 0, nmemb - 1, size, compar);
}

View File

@ -180,3 +180,8 @@ Result<String> String::join(const Vector<String>& vec, StringView delim)
return sb.string(); return sb.string();
} }
int String::compare(const String* a, const String* b)
{
return strcmp(a->chars(), b->chars());
}