Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <4E9F570F.5A16@mindspring.com> (permalink) |
|---|---|
| Date | 2011-10-19 19:02 -0400 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| Newsgroups | comp.lang.c |
| Subject | Re: Analogue of ReadLine() in C |
| References | <91b648bd-bf1e-4093-97a4-77b7300a94c5@c1g2000vbw.googlegroups.com> |
=?UTF-8?B?0JzQsNC60YHQuNC8INCk0L7QvNC40L0=?= wrote:
>
> I need function similar to ReadLine() in any other language.
> The program below fails[1] when data more then LAG_SIZE is passed
>
> #include <stdio.h>
> #include <stdbool.h>
> #include <string.h>
> #include <stdlib.h>
>
> char * read_line(void)
> {
> bool read_more = true;
> enum { LAG_SIZE = 32 };
> char *return_buf = NULL;
> char *new_line_pos = NULL;
> char tmp_buf[LAG_SIZE] = {'\0'};
> size_t size = 0;
> while (read_more && fgets(tmp_buf, LAG_SIZE, stdin)){
> char *tmp_realloc;
> size += LAG_SIZE;
> tmp_realloc = realloc(return_buf, size);
> if (!tmp_realloc){
> free(return_buf);
> return_buf = NULL;
> break;
> }
> return_buf = tmp_realloc;
> memset(return_buf + size - LAG_SIZE, '\0', LAG_SIZE);
> new_line_pos = strchr(tmp_buf, '\n');
> if (new_line_pos){
> *new_line_pos = '\0';
> read_more = false;
> }
> strcat(return_buf, tmp_buf);
> memset(tmp_buf, '\0', LAG_SIZE);
> }
> return return_buf;
> }
>
> void loop(void)
> {
> bool repeat = true;
> while (repeat){
> char *command;
> printf("Enter> ");
> command = read_line();
> if (command && !strcmp(command, "exit"))
> repeat = false;
> free(command);
> }
> }
>
> int main(void)
> {
> loop();
> return 0;
> }
>
> [1]: *** glibc detected *** ./a.out: realloc(): invalid next size:
> 0x0804b008 ***
/* BEGIN read_line_test.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
void loop(void);
int read_line(char **lineptr,
size_t *n,
FILE *stream,
size_t *size);
int
main(void)
{
loop();
return 0;
}
void
loop(void)
{
int rc;
char *command = NULL;
size_t n = 0;
do {
printf("Enter> ");
rc = read_line(&command, &n, stdin, NULL);
} while (rc > -1 && strcmp(command, "exit"));
free(command);
}
int
read_line(char **lineptr, size_t *n, FILE *stream, size_t *size)
{
int rc;
size_t count = 0;
size_t min = 2;
while ((rc = getc(stream)) != EOF
|| !feof(stream) && !ferror(stream))
{
if (++count == (size_t)-2) {
if (rc != '\n') {
(*lineptr)[count - 1] = (char)rc;
(*lineptr)[count++] = '\0';
} else {
(*lineptr)[count - 1] = '\0';
}
break;
}
if (++min > *n) {
void *p;
size_t max;
size_t next;
for (next = min ^ min - 1; min > next; ++next) {
next *= 2;
}
do {
max = next;
next = (max - min) / 2 + min;
p = realloc(*lineptr, max);
} while (p == NULL && max != min);
if (p == NULL) {
if (*n > count) {
if (rc != '\n') {
(*lineptr)[count - 1] = (char)rc;
(*lineptr)[count++] = '\0';
} else {
(*lineptr)[count - 1] = '\0';
}
} else {
ungetc(rc, stream);
--count;
if (*n != 0) {
(*lineptr)[count++] = '\0';
}
}
if (size != NULL) {
*size = count;
}
count = 0;
break;
} else {
*lineptr = p;
*n = max;
}
}
if (rc != '\n') {
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
break;
}
}
if (rc != EOF || !feof(stream) && !ferror(stream)) {
rc = INT_MAX > count ? count : INT_MAX;
} else {
if (*n > count) {
(*lineptr)[count++] = '\0';
}
}
if (size != NULL && rc != 0) {
*size = count;
}
return rc;
}
/* END read_line_test.c */
--
pete
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Analogue of ReadLine() in C Максим Фомин <maxim@maxim-fomin.ru> - 2011-10-19 12:26 -0700
Re: Analogue of ReadLine() in C China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-19 13:05 -0700
Re: Analogue of ReadLine() in C China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-19 20:50 -0700
Re: Analogue of ReadLine() in C Seebs <usenet-nospam@seebs.net> - 2011-10-20 07:42 +0000
Re: Analogue of ReadLine() in C China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-20 02:51 -0700
Re: Analogue of ReadLine() in C gazelle@shell.xmission.com (Kenny McCormack) - 2011-10-20 12:52 +0000
Re: Analogue of ReadLine() in C "BartC" <bc@freeuk.com> - 2011-10-20 14:31 +0100
Re: Analogue of ReadLine() in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-10-20 08:05 -0700
Re: Analogue of ReadLine() in C gazelle@shell.xmission.com (Kenny McCormack) - 2011-10-20 15:15 +0000
Re: Analogue of ReadLine() in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-20 16:50 +0100
Re: Analogue of ReadLine() in C "BartC" <bc@freeuk.com> - 2011-10-20 23:23 +0100
Re: Analogue of ReadLine() in C Keith Thompson <kst-u@mib.org> - 2011-10-20 15:34 -0700
Re: Analogue of ReadLine() in C Ian Collins <ian-news@hotmail.com> - 2011-10-21 14:10 +1300
Re: Analogue of ReadLine() in C "BartC" <bc@freeuk.com> - 2011-10-21 10:30 +0100
Re: Analogue of ReadLine() in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-10-21 05:59 -0700
Re: Analogue of ReadLine() in C Keith Thompson <kst-u@mib.org> - 2011-10-21 10:58 -0700
Re: Analogue of ReadLine() in C "BartC" <bc@freeuk.com> - 2011-10-22 20:00 +0100
Re: Analogue of ReadLine() in C Ian Collins <ian-news@hotmail.com> - 2011-10-23 08:09 +1300
Re: Analogue of ReadLine() in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-10-22 23:55 -0700
Re: Analogue of ReadLine() in C Ian Collins <ian-news@hotmail.com> - 2011-10-21 08:34 +1300
Re: Analogue of ReadLine() in C Seebs <usenet-nospam@seebs.net> - 2011-10-20 17:57 +0000
Re: Analogue of ReadLine() in C Markus Wichmann <nullplan@gmx.net> - 2011-10-23 23:03 +0200
Re: Analogue of ReadLine() in C Acid Washed China Blue Jeans <chine.bleu@yahoo.com> - 2011-10-24 00:31 -0700
Re: Analogue of ReadLine() in C Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-24 08:10 -0400
Re: Analogue of ReadLine() in C 88888 Dihedral <dihedral88888@googlemail.com> - 2011-10-19 14:04 -0700
Re: Analogue of ReadLine() in C Kaz Kylheku <kaz@kylheku.com> - 2011-10-19 21:52 +0000
Re: Analogue of ReadLine() in C Ike Naar <ike@iceland.freeshell.org> - 2011-10-19 22:34 +0000
Re: Analogue of ReadLine() in C pete <pfiland@mindspring.com> - 2011-10-19 19:02 -0400
Re: Analogue of ReadLine() in C pete <pfiland@mindspring.com> - 2011-10-20 00:36 -0400
Re: Analogue of ReadLine() in C Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-10-20 00:11 -0700
Re: Analogue of ReadLine() in C Максим Фомин <maxim@maxim-fomin.ru> - 2011-10-20 01:59 -0700
Re: Analogue of ReadLine() in C James Kuyper <jameskuyper@verizon.net> - 2011-10-20 06:57 -0400
Re: Analogue of ReadLine() in C Markus Wichmann <nullplan@gmx.net> - 2011-10-20 18:40 +0200
Re: Analogue of ReadLine() in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-20 18:20 +0100
Re: Analogue of ReadLine() in C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-21 08:58 +0000
Re: Analogue of ReadLine() in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-21 19:12 +0100
Re: Analogue of ReadLine() in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-21 19:35 +0100
Re: Analogue of ReadLine() in C "io_x" <a@b.c.invalid> - 2011-10-22 18:58 +0200
Re: Analogue of ReadLine() in C David Thompson <dave.thompson2@verizon.net> - 2011-10-28 01:34 -0400
Re: Analogue of ReadLine() in C Michael Angelo Ravera <maravera@prodigy.net> - 2011-10-20 13:32 -0700
Re: Analogue of ReadLine() in C James Kuyper <jameskuyper@verizon.net> - 2011-10-20 16:42 -0400
Re: Analogue of ReadLine() in C "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-10-20 16:47 -0400
Re: Analogue of ReadLine() in C Shao Miller <sha0.miller@gmail.com> - 2012-01-07 14:37 -0500
csiph-web