Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define CHUNK 16
- #define NULL_CHECK(p) if (p == NULL) return NULL
- char* stdinGetLine() {
- char *line = NULL, ch;
- int i = 0, currAlloc = 0;
- while ((ch = getchar()) != EOF) {
- if (i == currAlloc) {
- currAlloc += CHUNK;
- line = realloc(line, sizeof(char) * currAlloc);
- NULL_CHECK(line);
- }
- if (ch != '\n') {
- *(line + i) = ch;
- } else {
- break;
- }
- i++;
- }
- if (i == currAlloc) {
- currAlloc++;
- line = realloc(line, sizeof(char) * currAlloc);
- }
- NULL_CHECK(line);
- *(line + i) = 0;
- return line;
- }
- int main(void) {
- char *s = stdinGetLine();
- if (s == NULL) {
- perror("Error!");
- return 0;
- }
- puts(s);
- free(s);
- s = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement