Blame view

usr/gen_init_cpio.c 13.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <stdio.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <string.h>
  #include <unistd.h>
  #include <time.h>
  #include <fcntl.h>
  #include <errno.h>
  #include <ctype.h>
  #include <limits.h>
  
  /*
   * Original work by Jeff Garzik
   *
   * External file lists, symlink, pipe and fifo support by Thayne Harbaugh
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
17
   * Hard link support by Luciano Rocha
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
23
24
   */
  
  #define xstr(s) #s
  #define str(s) xstr(s)
  
  static unsigned int offset;
  static unsigned int ino = 721;
a8b8017c3   Michal Marek   initramfs: Use KB...
25
  static time_t default_mtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  
  struct file_handler {
  	const char *type;
  	int (*handler)(const char *line);
  };
  
  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;
  	unsigned int tmp_ofs;
  
  	fputs(name, stdout);
  	putchar(0);
  	offset += name_len;
  
  	tmp_ofs = name_len + 110;
  	while (tmp_ofs & 3) {
  		putchar(0);
  		offset++;
  		tmp_ofs++;
  	}
  }
  
  static void push_hdr(const char *s)
  {
  	fputs(s, stdout);
  	offset += 110;
  }
  
  static void cpio_trailer(void)
  {
  	char s[256];
  	const char name[] = "TRAILER!!!";
  
  	sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX"
  	       "%08X%08X%08X%08X%08X%08X%08X",
  		"070701",		/* magic */
  		0,			/* ino */
  		0,			/* mode */
  		(long) 0,		/* uid */
  		(long) 0,		/* gid */
  		1,			/* nlink */
  		(long) 0,		/* mtime */
  		0,			/* filesize */
  		0,			/* major */
  		0,			/* minor */
  		0,			/* rmajor */
  		0,			/* rminor */
  		(unsigned)strlen(name)+1, /* namesize */
  		0);			/* chksum */
  	push_hdr(s);
  	push_rest(name);
  
  	while (offset % 512) {
  		putchar(0);
  		offset++;
  	}
  }
  
  static int cpio_mkslink(const char *name, const char *target,
  			 unsigned int mode, uid_t uid, gid_t gid)
  {
  	char s[256];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106

43f901fbc   Thomas Chou   gen_init_cpio: re...
107
108
  	if (name[0] == '/')
  		name++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
115
116
  	sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  	       "%08X%08X%08X%08X%08X%08X%08X",
  		"070701",		/* magic */
  		ino++,			/* ino */
  		S_IFLNK | mode,		/* mode */
  		(long) uid,		/* uid */
  		(long) gid,		/* gid */
  		1,			/* nlink */
a8b8017c3   Michal Marek   initramfs: Use KB...
117
  		(long) default_mtime,	/* mtime */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  		(unsigned)strlen(target)+1, /* filesize */
  		3,			/* major */
  		1,			/* minor */
  		0,			/* rmajor */
  		0,			/* rminor */
  		(unsigned)strlen(name) + 1,/* namesize */
  		0);			/* chksum */
  	push_hdr(s);
  	push_string(name);
  	push_pad();
  	push_string(target);
  	push_pad();
  	return 0;
  }
  
  static int cpio_mkslink_line(const char *line)
  {
  	char name[PATH_MAX + 1];
  	char target[PATH_MAX + 1];
  	unsigned int mode;
  	int uid;
  	int gid;
  	int rc = -1;
  
  	if (5 != sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX) "s %o %d %d", name, target, &mode, &uid, &gid)) {
  		fprintf(stderr, "Unrecognized dir format '%s'", line);
  		goto fail;
  	}
  	rc = cpio_mkslink(name, target, mode, uid, gid);
   fail:
  	return rc;
  }
  
  static int cpio_mkgeneric(const char *name, unsigned int mode,
  		       uid_t uid, gid_t gid)
  {
  	char s[256];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155

43f901fbc   Thomas Chou   gen_init_cpio: re...
156
157
  	if (name[0] == '/')
  		name++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
162
163
164
165
  	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 */
  		2,			/* nlink */
a8b8017c3   Michal Marek   initramfs: Use KB...
166
  		(long) default_mtime,	/* mtime */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  		0,			/* filesize */
  		3,			/* major */
  		1,			/* minor */
  		0,			/* rmajor */
  		0,			/* rminor */
  		(unsigned)strlen(name) + 1,/* namesize */
  		0);			/* chksum */
  	push_hdr(s);
  	push_rest(name);
  	return 0;
  }
  
  enum generic_types {
  	GT_DIR,
  	GT_PIPE,
  	GT_SOCK
  };
  
  struct generic_type {
  	const char *type;
  	mode_t mode;
  };
  
  static struct generic_type generic_type_table[] = {
  	[GT_DIR] = {
  		.type = "dir",
  		.mode = S_IFDIR
  	},
  	[GT_PIPE] = {
  		.type = "pipe",
  		.mode = S_IFIFO
  	},
  	[GT_SOCK] = {
  		.type = "sock",
  		.mode = S_IFSOCK
  	}
  };
  
  static int cpio_mkgeneric_line(const char *line, enum generic_types gt)
  {
  	char name[PATH_MAX + 1];
  	unsigned int mode;
  	int uid;
  	int gid;
  	int rc = -1;
  
  	if (4 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d", name, &mode, &uid, &gid)) {
  		fprintf(stderr, "Unrecognized %s format '%s'",
  			line, generic_type_table[gt].type);
  		goto fail;
  	}
  	mode |= generic_type_table[gt].mode;
  	rc = cpio_mkgeneric(name, mode, uid, gid);
   fail:
  	return rc;
  }
  
  static int cpio_mkdir_line(const char *line)
  {
  	return cpio_mkgeneric_line(line, GT_DIR);
  }
  
  static int cpio_mkpipe_line(const char *line)
  {
  	return cpio_mkgeneric_line(line, GT_PIPE);
  }
  
  static int cpio_mksock_line(const char *line)
  {
  	return cpio_mkgeneric_line(line, GT_SOCK);
  }
  
  static int cpio_mknod(const char *name, unsigned int mode,
  		       uid_t uid, gid_t gid, char dev_type,
  		       unsigned int maj, unsigned int min)
  {
  	char s[256];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
  
  	if (dev_type == 'b')
  		mode |= S_IFBLK;
  	else
  		mode |= S_IFCHR;
43f901fbc   Thomas Chou   gen_init_cpio: re...
249
250
  	if (name[0] == '/')
  		name++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
252
253
254
255
256
257
258
  	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 */
a8b8017c3   Michal Marek   initramfs: Use KB...
259
  		(long) default_mtime,	/* mtime */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  		0,			/* filesize */
  		3,			/* major */
  		1,			/* minor */
  		maj,			/* rmajor */
  		min,			/* rminor */
  		(unsigned)strlen(name) + 1,/* namesize */
  		0);			/* chksum */
  	push_hdr(s);
  	push_rest(name);
  	return 0;
  }
  
  static int cpio_mknod_line(const char *line)
  {
  	char name[PATH_MAX + 1];
  	unsigned int mode;
  	int uid;
  	int gid;
  	char dev_type;
  	unsigned int maj;
  	unsigned int min;
  	int rc = -1;
  
  	if (7 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d %c %u %u",
  			 name, &mode, &uid, &gid, &dev_type, &maj, &min)) {
  		fprintf(stderr, "Unrecognized nod format '%s'", line);
  		goto fail;
  	}
  	rc = cpio_mknod(name, mode, uid, gid, dev_type, maj, min);
   fail:
  	return rc;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
  static int cpio_mkfile(const char *name, const char *location,
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
293
294
  			unsigned int mode, uid_t uid, gid_t gid,
  			unsigned int nlinks)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
298
  {
  	char s[256];
  	char *filebuf = NULL;
  	struct stat buf;
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
299
  	long size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
300
301
302
  	int file = -1;
  	int retval;
  	int rc = -1;
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
303
304
  	int namesize;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
  
  	mode |= S_IFREG;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
312
  	file = open (location, O_RDONLY);
  	if (file < 0) {
  		fprintf (stderr, "File %s could not be opened for reading
  ", location);
  		goto error;
  	}
a3c888fcd   Andrew Morton   gen_init_cpio: ch...
313
  	retval = fstat(file, &buf);
96aebafa6   Jesper Juhl   gen_init_cpio: Av...
314
  	if (retval) {
a3c888fcd   Andrew Morton   gen_init_cpio: ch...
315
316
  		fprintf(stderr, "File %s could not be stat()'ed
  ", location);
96aebafa6   Jesper Juhl   gen_init_cpio: Av...
317
318
  		goto error;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
323
324
325
326
327
328
329
330
331
  	filebuf = malloc(buf.st_size);
  	if (!filebuf) {
  		fprintf (stderr, "out of memory
  ");
  		goto error;
  	}
  
  	retval = read (file, filebuf, buf.st_size);
  	if (retval < 0) {
  		fprintf (stderr, "Can not read %s file
  ", location);
  		goto error;
  	}
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
332
333
334
335
  	size = 0;
  	for (i = 1; i <= nlinks; i++) {
  		/* data goes on last link */
  		if (i == nlinks) size = buf.st_size;
43f901fbc   Thomas Chou   gen_init_cpio: re...
336
337
  		if (name[0] == '/')
  			name++;
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
  		namesize = strlen(name) + 1;
  		sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  		       "%08lX%08X%08X%08X%08X%08X%08X",
  			"070701",		/* magic */
  			ino,			/* ino */
  			mode,			/* mode */
  			(long) uid,		/* uid */
  			(long) gid,		/* gid */
  			nlinks,			/* nlink */
  			(long) buf.st_mtime,	/* mtime */
  			size,			/* filesize */
  			3,			/* major */
  			1,			/* minor */
  			0,			/* rmajor */
  			0,			/* rminor */
  			namesize,		/* namesize */
  			0);			/* chksum */
  		push_hdr(s);
  		push_string(name);
  		push_pad();
  
  		if (size) {
6d87fea4d   Mike Frysinger   gen_init_cpio: fi...
360
361
362
363
364
  			if (fwrite(filebuf, size, 1, stdout) != 1) {
  				fprintf(stderr, "writing filebuf failed
  ");
  				goto error;
  			}
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
365
366
367
368
369
370
371
  			offset += size;
  			push_pad();
  		}
  
  		name += namesize;
  	}
  	ino++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
373
374
375
376
377
378
  	rc = 0;
  	
  error:
  	if (filebuf) free(filebuf);
  	if (file >= 0) close(file);
  	return rc;
  }
3b1ec9fb8   Sally, Gene   kbuild: gen_init_...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  static char *cpio_replace_env(char *new_location)
  {
         char expanded[PATH_MAX + 1];
         char env_var[PATH_MAX + 1];
         char *start;
         char *end;
  
         for (start = NULL; (start = strstr(new_location, "${")); ) {
                 end = strchr(start, '}');
                 if (start < end) {
                         *env_var = *expanded = '\0';
                         strncat(env_var, start + 2, end - start - 2);
                         strncat(expanded, new_location, start - new_location);
                         strncat(expanded, getenv(env_var), PATH_MAX);
                         strncat(expanded, end + 1, PATH_MAX);
                         strncpy(new_location, expanded, PATH_MAX);
                 } else
                         break;
         }
  
         return new_location;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401
402
403
  static int cpio_mkfile_line(const char *line)
  {
  	char name[PATH_MAX + 1];
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
404
  	char *dname = NULL; /* malloc'ed buffer for hard links */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405
406
407
408
  	char location[PATH_MAX + 1];
  	unsigned int mode;
  	int uid;
  	int gid;
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
409
410
  	int nlinks = 1;
  	int end = 0, dname_len = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
  	int rc = -1;
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
412
413
414
  	if (5 > sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX)
  				"s %o %d %d %n",
  				name, location, &mode, &uid, &gid, &end)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
  		fprintf(stderr, "Unrecognized file format '%s'", line);
  		goto fail;
  	}
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  	if (end && isgraph(line[end])) {
  		int len;
  		int nend;
  
  		dname = malloc(strlen(line));
  		if (!dname) {
  			fprintf (stderr, "out of memory (%d)
  ", dname_len);
  			goto fail;
  		}
  
  		dname_len = strlen(name) + 1;
  		memcpy(dname, name, dname_len);
  
  		do {
  			nend = 0;
  			if (sscanf(line + end, "%" str(PATH_MAX) "s %n",
  					name, &nend) < 1)
  				break;
  			len = strlen(name) + 1;
  			memcpy(dname + dname_len, name, len);
  			dname_len += len;
  			nlinks++;
  			end += nend;
  		} while (isgraph(line[end]));
  	} else {
  		dname = name;
  	}
3b1ec9fb8   Sally, Gene   kbuild: gen_init_...
446
447
  	rc = cpio_mkfile(dname, cpio_replace_env(location),
  	                 mode, uid, gid, nlinks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
   fail:
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
449
  	if (dname_len) free(dname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
  	return rc;
  }
5c7251384   Trevor Keith   Fix all -Wmissing...
452
  static void usage(const char *prog)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
454
455
  {
  	fprintf(stderr, "Usage:
  "
a8b8017c3   Michal Marek   initramfs: Use KB...
456
457
  		"\t%s [-t <timestamp>] <cpio_list>
  "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
461
462
463
464
465
466
467
  		"
  "
  		"<cpio_list> is a file containing newline separated entries that
  "
  		"describe the files to be included in the initramfs archive:
  "
  		"
  "
  		"# a comment
  "
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
468
469
  		"file <name> <location> <mode> <uid> <gid> [<hard links>]
  "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
474
475
476
477
478
479
480
481
  		"dir <name> <mode> <uid> <gid>
  "
  		"nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>
  "
  		"slink <name> <target> <mode> <uid> <gid>
  "
  		"pipe <name> <mode> <uid> <gid>
  "
  		"sock <name> <mode> <uid> <gid>
  "
  		"
  "
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
482
483
484
485
  		"<name>       name of the file/dir/nod/etc in the archive
  "
  		"<location>   location of the file in the current filesystem
  "
3b1ec9fb8   Sally, Gene   kbuild: gen_init_...
486
487
  		"             expands shell variables quoted with ${}
  "
24fa50961   Luciano Rocha   [PATCH] usr/gen_i...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
  		"<target>     link target
  "
  		"<mode>       mode/permissions of the file
  "
  		"<uid>        user id (0=root)
  "
  		"<gid>        group id (0=root)
  "
  		"<dev_type>   device type (b=block, c=character)
  "
  		"<maj>        major number of nod
  "
  		"<min>        minor number of nod
  "
  		"<hard links> space separated list of other links to file
  "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  		"
  "
  		"example:
  "
  		"# A simple initramfs
  "
  		"dir /dev 0755 0 0
  "
  		"nod /dev/console 0600 0 0 c 5 1
  "
  		"dir /root 0700 0 0
  "
  		"dir /sbin 0755 0 0
  "
a8b8017c3   Michal Marek   initramfs: Use KB...
518
519
520
521
522
523
524
525
526
527
  		"file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0
  "
  		"
  "
  		"<timestamp> is time in seconds since Epoch that will be used
  "
  		"as mtime for symlinks, special files and directories. The default
  "
  		"is to use the current time for these entries.
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  		prog);
  }
  
  struct file_handler file_handler_table[] = {
  	{
  		.type    = "file",
  		.handler = cpio_mkfile_line,
  	}, {
  		.type    = "nod",
  		.handler = cpio_mknod_line,
  	}, {
  		.type    = "dir",
  		.handler = cpio_mkdir_line,
  	}, {
  		.type    = "slink",
  		.handler = cpio_mkslink_line,
  	}, {
  		.type    = "pipe",
  		.handler = cpio_mkpipe_line,
  	}, {
  		.type    = "sock",
  		.handler = cpio_mksock_line,
  	}, {
  		.type    = NULL,
  		.handler = NULL,
  	}
  };
  
  #define LINE_SIZE (2 * PATH_MAX + 50)
  
  int main (int argc, char *argv[])
  {
  	FILE *cpio_list;
  	char line[LINE_SIZE];
  	char *args, *type;
  	int ec = 0;
  	int line_nr = 0;
a8b8017c3   Michal Marek   initramfs: Use KB...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
  	const char *filename;
  
  	default_mtime = time(NULL);
  	while (1) {
  		int opt = getopt(argc, argv, "t:h");
  		char *invalid;
  
  		if (opt == -1)
  			break;
  		switch (opt) {
  		case 't':
  			default_mtime = strtol(optarg, &invalid, 10);
  			if (!*optarg || *invalid) {
  				fprintf(stderr, "Invalid timestamp: %s
  ",
  						optarg);
  				usage(argv[0]);
  				exit(1);
  			}
  			break;
  		case 'h':
  		case '?':
  			usage(argv[0]);
  			exit(opt == 'h' ? 0 : 1);
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591

a8b8017c3   Michal Marek   initramfs: Use KB...
592
  	if (argc - optind != 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
594
595
  		usage(argv[0]);
  		exit(1);
  	}
a8b8017c3   Michal Marek   initramfs: Use KB...
596
597
  	filename = argv[optind];
  	if (!strcmp(filename, "-"))
f2434ec1e   Mike Frysinger   kbuild: add suppo...
598
  		cpio_list = stdin;
a8b8017c3   Michal Marek   initramfs: Use KB...
599
  	else if (!(cpio_list = fopen(filename, "r"))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
601
602
  		fprintf(stderr, "ERROR: unable to open '%s': %s
  
  ",
a8b8017c3   Michal Marek   initramfs: Use KB...
603
  			filename, strerror(errno));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  		usage(argv[0]);
  		exit(1);
  	}
  
  	while (fgets(line, LINE_SIZE, cpio_list)) {
  		int type_idx;
  		size_t slen = strlen(line);
  
  		line_nr++;
  
  		if ('#' == *line) {
  			/* comment - skip to next line */
  			continue;
  		}
  
  		if (! (type = strtok(line, " \t"))) {
  			fprintf(stderr,
  				"ERROR: incorrect format, could not locate file type line %d: '%s'
  ",
  				line_nr, line);
  			ec = -1;
aa1e816fc   Jesper Juhl   [PATCH] Fix poten...
625
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
  		}
  
  		if ('
  ' == *type) {
  			/* a blank line */
  			continue;
  		}
  
  		if (slen == strlen(type)) {
  			/* must be an empty line */
  			continue;
  		}
  
  		if (! (args = strtok(NULL, "
  "))) {
  			fprintf(stderr,
  				"ERROR: incorrect format, newline required line %d: '%s'
  ",
  				line_nr, line);
  			ec = -1;
  		}
  
  		for (type_idx = 0; file_handler_table[type_idx].type; type_idx++) {
  			int rc;
  			if (! strcmp(line, file_handler_table[type_idx].type)) {
  				if ((rc = file_handler_table[type_idx].handler(args))) {
  					ec = rc;
  					fprintf(stderr, " line %d
  ", line_nr);
  				}
  				break;
  			}
  		}
  
  		if (NULL == file_handler_table[type_idx].type) {
  			fprintf(stderr, "unknown file type line %d: '%s'
  ",
  				line_nr, line);
  		}
  	}
aa1e816fc   Jesper Juhl   [PATCH] Fix poten...
666
667
  	if (ec == 0)
  		cpio_trailer();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
669
670
  
  	exit(ec);
  }