8000 minix/commands/ash/mkinit.c at v3.1.4 · lx00700/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
executable file
·
500 lines (428 loc) · 10.7 KB

mkinit.c

File metadata and controls

executable file
·
500 lines (428 loc) · 10.7 KB
fprintf(fp, "}\n");
}
fclose(fp);
}
/*
* A text structure is simply a block of text that is kept in memory.
* Addstr appends a string to the text struct, and addchar appends a single
* character.
*/
static void
addstr(char *s, struct text *text)
{
while (*s) {
if (--text->nleft < 0)
addchar(*s++, text);
else
*text->nextc++ = *s++;
}
}
static void
addchar(int c, struct text *text)
{
struct block *bp;
if (--text->nleft < 0) {
bp = ckmalloc(sizeof *bp);
if (text->start == NULL)
text->start = bp;
else
text->last->next = bp;
text->last = bp;
text->nextc = bp->text;
text->nleft = BLOCKSIZE - 1;
}
*text->nextc++ = c;
}
/*
* Write the contents of a text structure to a file.
*/
static void
writetext(struct text *text, FILE *fp)
{
struct block *bp;
if (text->start != NULL) {
for (bp = text->start ; bp != text->last ; bp = bp->next)
fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
}
}
static FILE *
ckfopen(char *file, char *mode)
{
FILE *fp;
if ((fp = fopen(file, mode)) == NULL) {
fprintf(stderr, "Can't open %s: %s\n", file, strerror(errno));
exit(2);
}
return fp;
}
static void *
ckmalloc(int nbytes)
{
char *p;
if ((p = malloc(nbytes)) == NULL)
error("Out of space");
return p;
}
static char *
savestr(char *s)
{
char *p;
p = ckmalloc(strlen(s) + 1);
strcpy(p, s);
return p;
}
static void
error(char *msg)
{
if (curfile != NULL)
fprintf(stderr, "%s:%d: ", curfile, linno);
fprintf(stderr, "%s\n", msg);
exit(2);
}
/*
* $PchId: mkinit.c,v 1.6 2006/05/22 12:16:50 philip Exp $
*/
0