48 lines
1016 B
C
48 lines
1016 B
C
/*
|
|
* Filename: dyninput.c
|
|
* Authors(s): Roland (Roland@example.com)
|
|
* Description: Short description here
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
char* dyninput_str(int_least64_t maxsize)
|
|
{
|
|
char* str = malloc(3 * sizeof(char));
|
|
/*if (!str)
|
|
puts("Error allocating memory!");
|
|
return NULL;*/
|
|
|
|
int_least64_t size = 3, i = 0;
|
|
char c = 0;
|
|
|
|
do
|
|
{
|
|
c = getchar();
|
|
if (i + 2 == size)
|
|
{
|
|
char* tmp = realloc(str, size + 1);
|
|
if (!tmp)
|
|
{
|
|
puts("Error (re)allocating memory!");
|
|
return NULL;
|
|
}
|
|
str = tmp;
|
|
size++;
|
|
}
|
|
|
|
str[i] = c;
|
|
i++;
|
|
} while (c && c != '\n');
|
|
str[i] = '\0';
|
|
|
|
return str;
|
|
}
|