1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-12-19 10:01:25 -05:00

Use size_t instead of unsigned ints

This commit is contained in:
Alex Kotov 2021-12-16 20:26:16 +05:00
parent b28c4327e7
commit 9636303f86
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
11 changed files with 38 additions and 37 deletions

View file

@ -3,15 +3,15 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
static const unsigned int ARGV_COUNT_MAX = 100; static const size_t ARGV_COUNT_MAX = 100;
static const unsigned int ARG_SIZE_MAX = 4096; static const size_t ARG_SIZE_MAX = 4096;
static const char *const cmdline = "foo bar\\ baz \"car cdr\""; static const char *const cmdline = "foo bar\\ baz \"car cdr\"";
int main() int main()
{ {
char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX]; char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
unsigned int argc; size_t argc;
char *argv[ARGV_COUNT_MAX]; char *argv[ARGV_COUNT_MAX];
char buffer[ARGV_COUNT_MAX * ARG_SIZE_MAX]; char buffer[ARGV_COUNT_MAX * ARG_SIZE_MAX];

View file

@ -7,7 +7,7 @@
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
static char buffer[BUFFER_SIZE]; static char buffer[BUFFER_SIZE];
static unsigned int buffer_index = 0; static size_t buffer_index = 0;
static void my_putchar(const char chr) static void my_putchar(const char chr)
{ {

View file

@ -7,7 +7,7 @@
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
static char buffer[BUFFER_SIZE]; static char buffer[BUFFER_SIZE];
static unsigned int buffer_index = 0; static size_t buffer_index = 0;
static void my_putchar(const char chr) static void my_putchar(const char chr)
{ {

View file

@ -6,6 +6,7 @@ extern "C" {
#endif #endif
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#define KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX 256 #define KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX 256
#define KERNAUX_CMDLINE_ERROR_MSG_SLEN_MAX \ #define KERNAUX_CMDLINE_ERROR_MSG_SLEN_MAX \
@ -14,11 +15,11 @@ extern "C" {
bool kernaux_cmdline_parse( bool kernaux_cmdline_parse(
const char *cmdline, const char *cmdline,
char *error_msg, char *error_msg,
unsigned int *argc, size_t *argc,
char **argv, char **argv,
char *buffer, char *buffer,
unsigned int argv_count_max, size_t argv_count_max,
unsigned int arg_size_max size_t arg_size_max
); );
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -5,12 +5,14 @@
extern "C" { extern "C" {
#endif #endif
#include <stddef.h>
void kernaux_console_print(const char *s); void kernaux_console_print(const char *s);
void kernaux_console_printf(const char *format, ...) void kernaux_console_printf(const char *format, ...)
__attribute__((format(printf, 1, 2))); __attribute__((format(printf, 1, 2)));
void kernaux_console_putc(char c); void kernaux_console_putc(char c);
void kernaux_console_puts(const char *s); void kernaux_console_puts(const char *s);
void kernaux_console_write(const char *data, unsigned int size); void kernaux_console_write(const char *data, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -5,8 +5,6 @@
#include <kernaux/cmdline.h> #include <kernaux/cmdline.h>
#include <kernaux/libc.h> #include <kernaux/libc.h>
#include <stddef.h>
enum State { enum State {
INITIAL, INITIAL,
FINAL, FINAL,
@ -20,11 +18,11 @@ enum State {
bool kernaux_cmdline_parse( bool kernaux_cmdline_parse(
const char *const cmdline, const char *const cmdline,
char *error_msg, char *error_msg,
unsigned int *const argc, size_t *const argc,
char **argv, char **argv,
char *buffer, char *buffer,
const unsigned int argv_count_max, const size_t argv_count_max,
const unsigned int arg_size_max const size_t arg_size_max
) { ) {
if ( if (
cmdline == NULL || cmdline == NULL ||
@ -40,7 +38,7 @@ bool kernaux_cmdline_parse(
memset(error_msg, '\0', KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX); memset(error_msg, '\0', KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX);
*argc = 0; *argc = 0;
for (unsigned int index = 0; index < argv_count_max; ++index) { for (size_t index = 0; index < argv_count_max; ++index) {
argv[index] = NULL; argv[index] = NULL;
} }
@ -52,9 +50,9 @@ bool kernaux_cmdline_parse(
enum State state = INITIAL; enum State state = INITIAL;
unsigned int buffer_size = 0; size_t buffer_size = 0;
for (unsigned int index = 0; ; ++index) { for (size_t index = 0; ; ++index) {
const char cur = cmdline[index]; const char cur = cmdline[index];
switch (state) { switch (state) {
@ -259,7 +257,7 @@ bool kernaux_cmdline_parse(
fail: fail:
*argc = 0; *argc = 0;
for (unsigned int index = 0; index < argv_count_max; ++index) { for (size_t index = 0; index < argv_count_max; ++index) {
argv[index] = NULL; argv[index] = NULL;
} }

View file

@ -28,9 +28,9 @@ void kernaux_console_puts(const char *const s)
kernaux_console_putc('\n'); kernaux_console_putc('\n');
} }
void kernaux_console_write(const char *const data, const unsigned int size) void kernaux_console_write(const char *const data, const size_t size)
{ {
for (unsigned int i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
kernaux_console_putc(data[i]); kernaux_console_putc(data[i]);
} }
} }

View file

@ -159,7 +159,7 @@ bool KernAux_Multiboot2_Tag_None_is_valid(
bool KernAux_Multiboot2_Tag_BootCmdLine_is_valid( bool KernAux_Multiboot2_Tag_BootCmdLine_is_valid(
const struct KernAux_Multiboot2_Tag_BootCmdLine *const tag const struct KernAux_Multiboot2_Tag_BootCmdLine *const tag
) { ) {
unsigned int index = 1; size_t index = 1;
for ( for (
const char *ptr = tag->cmdline; const char *ptr = tag->cmdline;
@ -178,7 +178,7 @@ bool KernAux_Multiboot2_Tag_BootCmdLine_is_valid(
bool KernAux_Multiboot2_Tag_BootLoaderName_is_valid( bool KernAux_Multiboot2_Tag_BootLoaderName_is_valid(
const struct KernAux_Multiboot2_Tag_BootLoaderName *const tag const struct KernAux_Multiboot2_Tag_BootLoaderName *const tag
) { ) {
unsigned int index = 1; size_t index = 1;
for ( for (
const char *ptr = tag->name; const char *ptr = tag->name;
@ -197,7 +197,7 @@ bool KernAux_Multiboot2_Tag_BootLoaderName_is_valid(
bool KernAux_Multiboot2_Tag_Module_is_valid( bool KernAux_Multiboot2_Tag_Module_is_valid(
const struct KernAux_Multiboot2_Tag_Module *const tag const struct KernAux_Multiboot2_Tag_Module *const tag
) { ) {
unsigned int index = 1; size_t index = 1;
for ( for (
const char *ptr = tag->cmdline; const char *ptr = tag->cmdline;

View file

@ -300,11 +300,11 @@ void KernAux_Multiboot2_Tag_MemoryMap_print(
(struct KernAux_Multiboot2_Tag_MemoryMap_EntryBase*)tag->data; (struct KernAux_Multiboot2_Tag_MemoryMap_EntryBase*)tag->data;
for ( for (
unsigned int index = 0; size_t index = 0;
index < (tag->base.size - sizeof(*tag)) / tag->entry_size; index < (tag->base.size - sizeof(*tag)) / tag->entry_size;
++index ++index
) { ) {
printf(" entry %u\n", index); printf(" entry %lu\n", index);
printf(" base addr: %llu\n", entries[index].base_addr); printf(" base addr: %llu\n", entries[index].base_addr);
printf(" length: %llu\n", entries[index].length); printf(" length: %llu\n", entries[index].length);
printf(" type: %u\n", entries[index].type); printf(" type: %u\n", entries[index].type);

View file

@ -9,17 +9,17 @@
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
static const unsigned int ARGV_COUNT_MAX = 100; static const size_t ARGV_COUNT_MAX = 100;
static const unsigned int ARG_SIZE_MAX = 4096; static const size_t ARG_SIZE_MAX = 4096;
static void test( static void test(
const char *cmdline, const char *cmdline,
unsigned int argv_count_max, size_t argv_count_max,
unsigned int arg_size_max, size_t arg_size_max,
bool expected_result, bool expected_result,
const char *expected_error_msg, const char *expected_error_msg,
unsigned int expected_argc, size_t expected_argc,
const char *const *const expected_argv const char *const *const expected_argv
); );
@ -236,12 +236,12 @@ int main()
void test( void test(
const char *const cmdline, const char *const cmdline,
unsigned int argv_count_max, size_t argv_count_max,
unsigned int arg_size_max, size_t arg_size_max,
const bool expected_result, const bool expected_result,
const char *const expected_error_msg, const char *const expected_error_msg,
unsigned int expected_argc, size_t expected_argc,
const char *const *const expected_argv const char *const *const expected_argv
) { ) {
if (argv_count_max == 0) { if (argv_count_max == 0) {
@ -253,7 +253,7 @@ void test(
} }
char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX]; char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
unsigned int argc = 1234; size_t argc = 1234;
char *argv[argv_count_max]; char *argv[argv_count_max];
char buffer[argv_count_max * arg_size_max]; char buffer[argv_count_max * arg_size_max];
@ -272,11 +272,11 @@ void test(
assert(strcmp(error_msg, expected_error_msg) == 0); assert(strcmp(error_msg, expected_error_msg) == 0);
assert(argc == expected_argc); assert(argc == expected_argc);
for (unsigned int index = 0; index < argc; ++index) { for (size_t index = 0; index < argc; ++index) {
assert(strcmp(argv[index], expected_argv[index]) == 0); assert(strcmp(argv[index], expected_argv[index]) == 0);
} }
for (unsigned int index = argc; index < argv_count_max; ++index) { for (size_t index = argc; index < argv_count_max; ++index) {
assert(argv[index] == NULL); assert(argv[index] == NULL);
} }
} }

View file

@ -12,7 +12,7 @@
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
static char buffer[BUFFER_SIZE]; static char buffer[BUFFER_SIZE];
static unsigned int buffer_index; static size_t buffer_index;
static void test_putchar(const char chr) static void test_putchar(const char chr)
{ {