Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Appendix C Chapter 5 Solutions 915


ms->vsize = size;
}
fp = funopen(ms, mstream_read, mstream_write,
mstream_seek, mstream_close);
if (fp == NULL) {
if (ms->flags & MS_MYBUF)
free(ms->buf);
free(ms);
}
return(fp);
}
static int
type_to_flags(const char *__restrict type)
{
const char *cp;
int flags = 0;
for (cp = type; *cp != 0; cp++) {
switch (*cp) {
case ’r’:
if (flags != 0)
return(0); /* error */
flags |= MS_READ;
break;
case ’w’:
if (flags != 0)
return(0); /* error */
flags |= MS_WRITE|MS_TRUNCATE;
break;
case ’a’:
if (flags != 0)
return(0); /* error */
flags |= MS_APPEND;
break;
case ’+’:
if (flags == 0)
return(0); /* error */
flags |= MS_READ|MS_WRITE;
break;
case ’b’:
if (flags == 0)
return(0); /* error */
break;
default:
return(0); /* error */
}
}
Free download pdf