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 <string.h>
static const unsigned int ARGV_COUNT_MAX = 100;
static const unsigned int ARG_SIZE_MAX = 4096;
static const size_t ARGV_COUNT_MAX = 100;
static const size_t ARG_SIZE_MAX = 4096;
static const char *const cmdline = "foo bar\\ baz \"car cdr\"";
int main()
{
char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
unsigned int argc;
size_t argc;
char *argv[ARGV_COUNT_MAX];
char buffer[ARGV_COUNT_MAX * ARG_SIZE_MAX];

View File

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

View File

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

View File

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

View File

@ -5,12 +5,14 @@
extern "C" {
#endif
#include <stddef.h>
void kernaux_console_print(const char *s);
void kernaux_console_printf(const char *format, ...)
__attribute__((format(printf, 1, 2)));
void kernaux_console_putc(char c);
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
}

View File

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

View File

@ -28,9 +28,9 @@ void kernaux_console_puts(const char *const s)
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]);
}
}

View File

@ -159,7 +159,7 @@ bool KernAux_Multiboot2_Tag_None_is_valid(
bool KernAux_Multiboot2_Tag_BootCmdLine_is_valid(
const struct KernAux_Multiboot2_Tag_BootCmdLine *const tag
) {
unsigned int index = 1;
size_t index = 1;
for (
const char *ptr = tag->cmdline;
@ -178,7 +178,7 @@ bool KernAux_Multiboot2_Tag_BootCmdLine_is_valid(
bool KernAux_Multiboot2_Tag_BootLoaderName_is_valid(
const struct KernAux_Multiboot2_Tag_BootLoaderName *const tag
) {
unsigned int index = 1;
size_t index = 1;
for (
const char *ptr = tag->name;
@ -197,7 +197,7 @@ bool KernAux_Multiboot2_Tag_BootLoaderName_is_valid(
bool KernAux_Multiboot2_Tag_Module_is_valid(
const struct KernAux_Multiboot2_Tag_Module *const tag
) {
unsigned int index = 1;
size_t index = 1;
for (
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;
for (
unsigned int index = 0;
size_t index = 0;
index < (tag->base.size - sizeof(*tag)) / tag->entry_size;
++index
) {
printf(" entry %u\n", index);
printf(" entry %lu\n", index);
printf(" base addr: %llu\n", entries[index].base_addr);
printf(" length: %llu\n", entries[index].length);
printf(" type: %u\n", entries[index].type);

View File

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

View File

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