1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/* library.c */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include "library.h"
extern char *fgetsz (char *s, int size, FILE *stream)
{
int c, wait;
char *p;
if (size <= 0) {
/* Oops... shouldn't happen */
abort ();
}
p = s;
while (1) {
if (p - s >= size - 1) {
*p = '\0';
return s;
}
wait = 0;
while (1) {
c = getc (stream);
if (c == EOF) { /* error */
*p = '\0';
if (ferror(stream)) { /* error on stream */
if (errno == EINTR) { /* if error is signal interrupt */
wait++;
if (wait < 30) { /* retry reading 30 times */
sleep(1);
continue;
}
}
else
return NULL; /* other errors are errors */
}
return (p == s) ? NULL : s; /* end of file */
}
else
break;
}
if (c == '\0') {
*p = '\0';
return s;
}
*p = c;
p++;
}
}
extern int fputsz (const char *s, FILE *stream)
{
if (fputs (s, stream) == EOF)
return EOF;
if (putc ('\0', stream) == EOF)
return EOF;
return 0;
}
extern void convert_to_printable (char *s)
{
for ( ; *s; s++)
if (! isprint (*s))
*s = '?';
}
|