NeoMutt  2021-10-29-220-g2b1eec
Teaching an old dog new tricks
DOXYGEN
atoi.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 
50 static const char *str_atol_clamp(const char *str, long *dst, long lmin, long lmax)
51 {
52  if (dst)
53  {
54  *dst = 0;
55  }
56 
57  if (!str || (*str == '\0'))
58  {
59  return NULL;
60  }
61 
62  char *e = NULL;
63  errno = 0;
64  long res = strtol(str, &e, 10);
65  if ((e == str) || (((res == LONG_MIN) || (res == LONG_MAX)) && (errno == ERANGE)) ||
66  (res < lmin) || (res > lmax))
67  {
68  return NULL;
69  }
70 
71  if (dst)
72  {
73  *dst = res;
74  }
75 
76  return e;
77 }
78 
93 static const char *str_atoull_clamp(const char *str, unsigned long long *dst,
94  unsigned long long ullmax)
95 {
96  if (dst)
97  {
98  *dst = 0;
99  }
100 
101  if (!str || (*str == '\0'))
102  {
103  return str;
104  }
105 
106  char *e = NULL;
107  errno = 0;
108  unsigned long long res = strtoull(str, &e, 10);
109  if ((e == str) || ((res == ULLONG_MAX) && (errno == ERANGE)) || (res > ullmax))
110  {
111  return NULL;
112  }
113 
114  if (dst)
115  {
116  *dst = res;
117  }
118 
119  return e;
120 }
121 
135 const char *mutt_str_atol(const char *str, long *dst)
136 {
137  return str_atol_clamp(str, dst, LONG_MIN, LONG_MAX);
138 }
139 
153 const char *mutt_str_atos(const char *str, short *dst)
154 {
155  long l;
156  const char *res = str_atol_clamp(str, &l, SHRT_MIN, SHRT_MAX);
157  if (dst)
158  {
159  *dst = res ? l : 0;
160  }
161  return res;
162 }
163 
178 const char *mutt_str_atoi(const char *str, int *dst)
179 {
180  long l;
181  const char *res = str_atol_clamp(str, &l, INT_MIN, INT_MAX);
182  if (dst)
183  {
184  *dst = res ? l : 0;
185  }
186  return res;
187 }
188 
202 const char *mutt_str_atoui(const char *str, unsigned int *dst)
203 {
204  unsigned long long l;
205  const char *res = str_atoull_clamp(str, &l, UINT_MAX);
206  if (dst)
207  {
208  *dst = res ? l : 0;
209  }
210  return res;
211 }
212 
226 const char *mutt_str_atoul(const char *str, unsigned long *dst)
227 {
228  unsigned long long l;
229  const char *res = str_atoull_clamp(str, &l, ULONG_MAX);
230  if (dst)
231  {
232  *dst = res ? l : 0;
233  }
234  return res;
235 }
236 
250 const char *mutt_str_atous(const char *str, unsigned short *dst)
251 {
252  unsigned long long l;
253  const char *res = str_atoull_clamp(str, &l, USHRT_MAX);
254  if (dst)
255  {
256  *dst = res ? l : 0;
257  }
258  return res;
259 }
260 
274 const char *mutt_str_atoull(const char *str, unsigned long long *dst)
275 {
276  return str_atoull_clamp(str, dst, ULLONG_MAX);
277 }
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition: atoi.c:250
static const char * str_atol_clamp(const char *str, long *dst, long lmin, long lmax)
Convert ASCII string to a long and clamp.
Definition: atoi.c:50
const char * mutt_str_atoul(const char *str, unsigned long *dst)
Convert ASCII string to an unsigned long.
Definition: atoi.c:226
const char * mutt_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: atoi.c:135
const char * mutt_str_atoull(const char *str, unsigned long long *dst)
Convert ASCII string to an unsigned long long.
Definition: atoi.c:274
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition: atoi.c:202
static const char * str_atoull_clamp(const char *str, unsigned long long *dst, unsigned long long ullmax)
Convert ASCII string to an unsigned long long and clamp.
Definition: atoi.c:93
const char * mutt_str_atos(const char *str, short *dst)
Convert ASCII string to a short.
Definition: atoi.c:153
const char * mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: atoi.c:178