Mercurial > repo
comparison interps/cfunge/cfunge-src/src/support.c @ 996:859f9b4339e6
<Gregor> tar xf egobot.tar.xz
author | HackBot |
---|---|
date | Sun, 09 Dec 2012 19:30:08 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
995:6883f5911eb7 | 996:859f9b4339e6 |
---|---|
1 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- | |
2 * | |
3 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C. | |
4 * Copyright (C) 2008-2009 Arvid Norlander <anmaster AT tele2 DOT se> | |
5 * | |
6 * This program is free software: you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation, either version 3 of the License, or | |
9 * (at the proxy's option) any later version. Arvid Norlander is a | |
10 * proxy who can decide which future versions of the GNU General Public | |
11 * License can be used. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 */ | |
21 | |
22 #include "global.h" | |
23 #include "support.h" | |
24 | |
25 #include <stdio.h> | |
26 #include <limits.h> | |
27 #include <stdlib.h> | |
28 #include <errno.h> | |
29 | |
30 #ifndef SIZE_MAX | |
31 # define SIZE_MAX ((size_t) -1) | |
32 #endif | |
33 #ifndef SSIZE_MAX | |
34 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2)) | |
35 #endif | |
36 | |
37 // This function is from Gnulib | |
38 FUNGE_ATTR_FAST FUNGE_ATTR_WARN_UNUSED | |
39 static inline ssize_t | |
40 cf_getdelim(char **lineptr, size_t *n, int delimiter, FILE *fp) | |
41 { | |
42 ssize_t result; | |
43 size_t cur_len = 0; | |
44 | |
45 if (lineptr == NULL || n == NULL || fp == NULL) { | |
46 errno = EINVAL; | |
47 return -1; | |
48 } | |
49 | |
50 cf_flockfile(fp); | |
51 | |
52 if (*lineptr == NULL || *n == 0) { | |
53 *n = 120; | |
54 *lineptr = (char *) cf_realloc(*lineptr, *n); | |
55 if (*lineptr == NULL) { | |
56 result = -1; | |
57 goto unlock_return; | |
58 } | |
59 } | |
60 | |
61 for (;;) { | |
62 int i; | |
63 | |
64 i = cf_getc_unlocked(fp); | |
65 if (i == EOF) { | |
66 result = -1; | |
67 break; | |
68 } | |
69 | |
70 /* Make enough space for len+1 (for final NUL) bytes. */ | |
71 if (cur_len + 1 >= *n) { | |
72 size_t needed_max = | |
73 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX; | |
74 size_t needed = 2 * *n + 1; /* Be generous. */ | |
75 char *new_lineptr; | |
76 | |
77 if (needed_max < needed) | |
78 needed = needed_max; | |
79 if (cur_len + 1 >= needed) { | |
80 result = -1; | |
81 // This ifdef is for Windows only really. Most OS defines EOVERFLOW. | |
82 #ifdef EOVERFLOW | |
83 errno = EOVERFLOW; | |
84 #endif | |
85 goto unlock_return; | |
86 } | |
87 | |
88 new_lineptr = (char *) cf_realloc(*lineptr, needed); | |
89 if (new_lineptr == NULL) { | |
90 result = -1; | |
91 goto unlock_return; | |
92 } | |
93 | |
94 *lineptr = new_lineptr; | |
95 *n = needed; | |
96 } | |
97 | |
98 (*lineptr)[cur_len] = (char)i; | |
99 cur_len++; | |
100 | |
101 if (i == delimiter) | |
102 break; | |
103 } | |
104 (*lineptr)[cur_len] = '\0'; | |
105 result = cur_len ? (ssize_t)cur_len : result; | |
106 | |
107 unlock_return: | |
108 cf_funlockfile(fp); // doesn't set errno | |
109 | |
110 return result; | |
111 } | |
112 | |
113 // This function is from Gnulib | |
114 FUNGE_ATTR_FAST ssize_t | |
115 cf_getline(char **lineptr, size_t *n, FILE *stream) | |
116 { | |
117 return cf_getdelim(lineptr, n, '\n', stream); | |
118 } |