doc: fix typo
[dracut.git] / skipcpio / skipcpio.c
blob445d7f6509429a9ce3024e2cb8a7204bf9230610
1 /* dracut-install.c -- install files and executables
3 Copyright (C) 2012 Harald Hoyer
4 Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 This program is free software: you can redistribute it and/or modify
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program; If not, see <http://www.gnu.org/licenses/>.
20 #define PROGRAM_VERSION_STRING "1"
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #define CPIO_END "TRAILER!!!"
33 #define CPIO_ENDLEN (sizeof(CPIO_END)-1)
35 static char buf[CPIO_ENDLEN * 2 + 1];
37 int main(int argc, char **argv)
39 FILE *f;
40 size_t s;
42 if (argc != 2) {
43 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
44 exit(1);
47 f = fopen(argv[1], "r");
49 if (f == NULL) {
50 fprintf(stderr, "Cannot open file '%s'\n", argv[1]);
51 exit(1);
54 s = fread(buf, 6, 1, f);
55 if (s <= 0) {
56 fprintf(stderr, "Read error from file '%s'\n", argv[1]);
57 fclose(f);
58 exit(1);
60 fseek(f, 0, SEEK_SET);
62 /* check, if this is a cpio archive */
63 if ((buf[0] == 0x71 && buf[1] == 0xc7)
64 || (buf[0] == '0' && buf[1] == '7' && buf[2] == '0' && buf[3] == '7' && buf[4] == '0' && buf[5] == '1')) {
65 long pos = 0;
67 /* Search for CPIO_END */
68 do {
69 char *h;
70 fseek(f, pos, SEEK_SET);
71 buf[sizeof(buf) - 1] = 0;
72 s = fread(buf, CPIO_ENDLEN, 2, f);
73 if (s <= 0)
74 break;
76 h = strstr(buf, CPIO_END);
77 if (h) {
78 pos = (h - buf) + pos + CPIO_ENDLEN;
79 fseek(f, pos, SEEK_SET);
80 break;
82 pos += CPIO_ENDLEN;
83 } while (!feof(f));
85 if (feof(f)) {
86 /* CPIO_END not found, just cat the whole file */
87 fseek(f, 0, SEEK_SET);
88 } else {
89 /* skip zeros */
90 while (!feof(f)) {
91 size_t i;
93 buf[sizeof(buf) - 1] = 0;
94 s = fread(buf, 1, sizeof(buf) - 1, f);
95 if (s <= 0)
96 break;
98 for (i = 0; (i < s) && (buf[i] == 0); i++) ;
100 if (buf[i] != 0) {
101 pos += i;
102 fseek(f, pos, SEEK_SET);
103 break;
106 pos += s;
110 /* cat out the rest */
111 while (!feof(f)) {
112 s = fread(buf, 1, sizeof(buf), f);
113 if (s <= 0)
114 break;
116 s = fwrite(buf, 1, s, stdout);
117 if (s <= 0)
118 break;
120 fclose(f);
122 return EXIT_SUCCESS;