ChangeSet 1.914.157.7, 2003/02/15 23:21:47-06:00, greg@kroah.com klibc: add file support to gen_init_cpio.c diff -Nru a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c --- a/usr/gen_init_cpio.c Wed Feb 19 11:42:03 2003 +++ b/usr/gen_init_cpio.c Wed Feb 19 11:42:03 2003 @@ -5,10 +5,28 @@ #include #include #include +#include static unsigned int offset; static unsigned int ino = 721; +static void push_string(const char *name) +{ + unsigned int name_len = strlen(name) + 1; + + fputs(name, stdout); + putchar(0); + offset += name_len; +} + +static void push_pad (void) +{ + while (offset & 3) { + putchar(0); + offset++; + } +} + static void push_rest(const char *name) { unsigned int name_len = strlen(name) + 1; @@ -120,11 +138,83 @@ push_rest(name); } +static void cpio_mkfile(const char *filename, const char *location, + unsigned int mode, uid_t uid, gid_t gid) +{ + char s[256]; + char *filebuf; + struct stat buf; + int file; + int retval; + int i; + + mode |= S_IFREG; + + retval = stat (filename, &buf); + if (retval) { + fprintf (stderr, "Filename %s could not be located\n", filename); + goto error; + } + + file = open (filename, O_RDONLY); + if (file < 0) { + fprintf (stderr, "Filename %s could not be opened for reading\n", filename); + goto error; + } + + filebuf = malloc(buf.st_size); + if (!filebuf) { + fprintf (stderr, "out of memory\n"); + goto error_close; + } + + retval = read (file, filebuf, buf.st_size); + if (retval < 0) { + fprintf (stderr, "Can not read %s file\n", filename); + goto error_free; + } + + sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" + "%08X%08X%08X%08X%08X%08X%08X", + "070701", /* magic */ + ino++, /* ino */ + mode, /* mode */ + (long) uid, /* uid */ + (long) gid, /* gid */ + 1, /* nlink */ + (long) buf.st_mtime, /* mtime */ + (int) buf.st_size, /* filesize */ + 3, /* major */ + 1, /* minor */ + 0, /* rmajor */ + 0, /* rminor */ + strlen(location) + 1, /* namesize */ + 0); /* chksum */ + push_hdr(s); + push_string(location); + + for (i = 0; i < buf.st_size; ++i) + fputc(filebuf[i], stdout); + close(file); + free(filebuf); + push_pad(); + return; + +error_free: + free(filebuf); +error_close: + close(file); +error: + exit(-1); +} + int main (int argc, char *argv[]) { cpio_mkdir("/dev", 0700, 0, 0); cpio_mknod("/dev/console", 0600, 0, 0, 'c', 5, 1); cpio_mkdir("/root", 0700, 0, 0); + cpio_mkdir("/sbin", 0700, 0, 0); + cpio_mkfile("usr/hello_world/hello", "/sbin/hotplug", 0700, 0, 0); cpio_trailer(); exit(0);