Compare commits
4 Commits
6beea7f817
...
d9b7e8edc0
Author | SHA1 | Date | |
---|---|---|---|
d9b7e8edc0 | |||
3628464284 | |||
d1801d484c | |||
dcd93cce3a |
@ -1,6 +1,7 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <luna/PathParser.h>
|
||||
#include <luna/Sort.h>
|
||||
#include <luna/String.h>
|
||||
#include <luna/Vector.h>
|
||||
#include <os/Directory.h>
|
||||
@ -239,6 +240,7 @@ static Result<void> load_services()
|
||||
auto dir = TRY(os::Directory::open("/etc/init"));
|
||||
|
||||
auto services = TRY(dir->list(os::Directory::Filter::ParentAndBase));
|
||||
sort(services.begin(), services.end(), String::compare);
|
||||
|
||||
for (const auto& entry : services)
|
||||
{
|
||||
|
3
initrd/etc/init/00-motd
Normal file
3
initrd/etc/init/00-motd
Normal file
@ -0,0 +1,3 @@
|
||||
Name=motd
|
||||
Command=/bin/cat /etc/motd
|
||||
Wait=true
|
@ -1 +1,2 @@
|
||||
Hello, world!
|
||||
Welcome to the Luna system!
|
||||
Have a look around, you can run 'ls /bin' to list available commands.
|
||||
|
@ -35,16 +35,16 @@ extern "C"
|
||||
#endif
|
||||
|
||||
/* Return the absolute value of an integer. */
|
||||
int abs(int v);
|
||||
int abs(int);
|
||||
|
||||
/* Return the absolute value of a long integer. */
|
||||
long labs(long v);
|
||||
long labs(long);
|
||||
|
||||
/* 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. */
|
||||
div_t div(int num, int den);
|
||||
div_t div(int, int);
|
||||
|
||||
/* Return the result of dividing two long integers, including the remainder. */
|
||||
ldiv_t ldiv(long, long);
|
||||
@ -62,7 +62,7 @@ extern "C"
|
||||
void* realloc(void* ptr, size_t size);
|
||||
|
||||
/* Free heap memory. */
|
||||
void free(void*);
|
||||
void free(void* ptr);
|
||||
|
||||
/* Abort the program without performing any normal cleanup. */
|
||||
__noreturn void abort();
|
||||
@ -115,7 +115,9 @@ extern "C"
|
||||
/* Clear all environment variables. */
|
||||
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*));
|
||||
|
||||
/* Convert a multibyte character string to a wide character string. */
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <limits.h>
|
||||
#include <luna/Heap.h>
|
||||
#include <luna/NumberParsing.h>
|
||||
#include <luna/Sort.h>
|
||||
#include <luna/Utf8.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/syscall.h>
|
||||
@ -199,4 +200,9 @@ extern "C"
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
|
||||
{
|
||||
c_quicksort(base, nmemb, size, compar);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ file(GLOB HEADERS include/luna/*.h)
|
||||
set(FREESTANDING_SOURCES
|
||||
${HEADERS}
|
||||
src/Format.cpp
|
||||
src/Sort.cpp
|
||||
src/NumberParsing.cpp
|
||||
src/CString.cpp
|
||||
src/CPath.cpp
|
||||
|
26
libluna/include/luna/Sort.h
Normal file
26
libluna/include/luna/Sort.h
Normal 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);
|
||||
}
|
@ -65,6 +65,8 @@ class String
|
||||
static Result<String> from_cstring(const char* str);
|
||||
static Result<String> from_string_view(StringView str);
|
||||
|
||||
static int compare(const String* a, const String* b);
|
||||
|
||||
const char* chars() const
|
||||
{
|
||||
return m_inline ? m_inline_storage : m_string;
|
||||
|
53
libluna/src/Sort.cpp
Normal file
53
libluna/src/Sort.cpp
Normal 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);
|
||||
}
|
@ -180,3 +180,8 @@ Result<String> String::join(const Vector<String>& vec, StringView delim)
|
||||
|
||||
return sb.string();
|
||||
}
|
||||
|
||||
int String::compare(const String* a, const String* b)
|
||||
{
|
||||
return strcmp(a->chars(), b->chars());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user