blind-read-head.c (991B)
1 /* See LICENSE file for copyright and license details. */ 2 #include "common.h" 3 4 USAGE("") 5 6 int 7 main(int argc, char *argv[]) 8 { 9 char buf[STREAM_HEAD_MAX]; 10 char magic[] = {'\0', 'u', 'i', 'v', 'f'}; 11 char b, *p; 12 size_t i, ptr; 13 14 UNOFLAGS(argc); 15 16 for (ptr = 0; ptr < sizeof(buf);) { 17 if (!eread(STDIN_FILENO, buf + ptr, 1, "<stdin>")) 18 goto bad_format; 19 if (buf[ptr++] == '\n') 20 break; 21 } 22 if (ptr == sizeof(buf)) 23 goto bad_format; 24 25 p = buf; 26 for (i = 0; i < ELEMENTSOF(magic); i++) 27 if (!eread(STDIN_FILENO, &b, 1, "<stdin>") || b != magic[i]) 28 goto bad_format; 29 30 for (i = 0; i < 3; i++) { 31 if (!isdigit(*p)) 32 goto bad_format; 33 while (isdigit(*p)) p++; 34 if (*p++ != ' ') 35 goto bad_format; 36 } 37 while (isalnum(*p) || *p == ' ') { 38 if (p[0] == ' ' && p[-1] == ' ') 39 goto bad_format; 40 p++; 41 } 42 if (p[-1] == ' ' || p[0] != '\n') 43 goto bad_format; 44 45 ewriteall(STDOUT_FILENO, buf, ptr, "<stdout>"); 46 47 return 0; 48 bad_format: 49 eprintf("<stdin>: file format not supported\n"); 50 }