home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
2
/
2032
/
getline.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-12-28
|
2KB
|
92 lines
/*
* this was mercilessly stolen from rn
*/
#include <stdio.h>
#ifndef AIX /* AIX doesn't have malloc.h. Oh well... */
#include <malloc.h>
#endif
typedef unsigned int MEM_SIZE;
#ifdef NOLINEBUF
#define FLUSH ,fflush(stdout)
#else
#define FLUSH
#endif
/* just like fgets but will make bigger buffer as necessary */
char *
get_a_line(original_buffer, buffer_length, fp)
char *original_buffer;
register int buffer_length;
FILE *fp;
{
register int bufix = 0;
register int nextch;
register char *some_buffer_or_other = original_buffer;
char *safemalloc(), *saferealloc();
do {
if (bufix >= buffer_length) {
buffer_length *= 2;
if (some_buffer_or_other == original_buffer) {
/* currently static? */
some_buffer_or_other = safemalloc((MEM_SIZE) buffer_length + 1);
strncpy(some_buffer_or_other, original_buffer, buffer_length / 2);
/* so we must copy it */
} else { /* just grow in place, if possible */
some_buffer_or_other = saferealloc(some_buffer_or_other,
(MEM_SIZE) buffer_length + 1);
}
}
if ((nextch = getc(fp)) == EOF)
return ((char *) NULL);
some_buffer_or_other[bufix++] = (char) nextch;
} while (nextch && nextch != '\n');
some_buffer_or_other[bufix] = '\0';
return some_buffer_or_other;
}
static char nomem[] = "annejones: out of memory!\n";
/* paranoid version of safemalloc */
char *
safemalloc(size)
MEM_SIZE size;
{
char *ptr;
extern char *malloc();
ptr = malloc(size ? size : 1); /* safemalloc(0) is NASTY on our
system */
if (ptr != (char *) NULL)
return ptr;
else {
fputs(nomem, stdout) FLUSH;
exit(0);
}
/* NOTREACHED */
}
/* paranoid version of realloc */
char *
saferealloc(where, size)
char *where;
MEM_SIZE size;
{
char *ptr;
extern char *realloc();
ptr = realloc(where, size ? size : 1);
if (ptr != (char *) NULL)
return ptr;
else {
fputs(nomem, stdout) FLUSH;
exit(0);
}
/* NOTREACHED */
}