Prusa MINI Firmware overview
getline.h File Reference
#include <stdio.h>

Go to the source code of this file.

Macros

#define getline(lineptr, n, stream)   getdelim (lineptr, n, '\n', stream)
 

Functions

ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
 

Macro Definition Documentation

◆ getline

#define getline (   lineptr,
  n,
  stream 
)    getdelim (lineptr, n, '\n', stream)

Function Documentation

◆ getdelim()

ssize_t getdelim ( char **  lineptr,
size_t *  n,
int  delimiter,
FILE *  fp 
)
60  {
61  ssize_t result;
62  size_t cur_len = 0;
63 
64  if (lineptr == NULL || n == NULL || fp == NULL) {
65  errno = EINVAL;
66  return -1;
67  }
68 
69  flockfile (fp);
70 
71  if (*lineptr == NULL || *n == 0) {
72  *n = 120;
73  *lineptr = (char *) malloc(*n);
74  if (*lineptr == NULL) {
75  result = -1;
76  goto unlock_return;
77  }
78  }
79 
80  for (;;) {
81  int i;
82 
83  i = getc(fp);
84  if (i == EOF) {
85  result = -1;
86  break;
87  }
88 
89  /* Make enough space for len+1 (for final NUL) bytes. */
90  if (cur_len + 1 >= *n) {
91  size_t needed_max =
92  SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
93  size_t needed = 2 * *n + 1; /* Be generous. */
94  char *new_lineptr;
95 
96  if (needed_max < needed)
97  needed = needed_max;
98  if (cur_len + 1 >= needed) {
99  result = -1;
100  goto unlock_return;
101  }
102 
103  new_lineptr = (char *) realloc (*lineptr, needed);
104  if (new_lineptr == NULL) {
105  result = -1;
106  goto unlock_return;
107  }
108 
109  *lineptr = new_lineptr;
110  *n = needed;
111  }
112 
113  (*lineptr)[cur_len] = i;
114  cur_len++;
115 
116  if (i == delimiter) break;
117  }
118  (*lineptr)[cur_len] = '\0';
119  result = cur_len ? (int) cur_len : (int) result;
120 
121  unlock_return:
122  funlockfile(fp);
123  return result;
124 }
SSIZE_MAX
#define SSIZE_MAX
Definition: getline.c:42
funlockfile
#define funlockfile(x)
Definition: getline.c:50
i
uint8_t i
Definition: screen_test_graph.c:72
NULL
#define NULL
Definition: usbd_def.h:53
errno
int errno
flockfile
#define flockfile(x)
Definition: getline.c:46
createSpeedLookupTable.int
int
Definition: createSpeedLookupTable.py:15
SIZE_MAX
#define SIZE_MAX
Definition: getline.c:39
EOF
#define EOF
Definition: ff.h:286