1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 106 107 108 109 110 111 112 113 114 115 116 117 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
|
/*
* © Copyright 1996-2012 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <ctype.h>
#include <sys/types.h>
#include "mars.h"
static char* scan_file(const char* file) {
FILE* f = fopen(file, "r");
int len;
int max;
unsigned char c = ' ';
union {
char c;
short s;
long l;
char ch[8];
} buf;
if (f == NULL)
return "BAD";
memset(&buf, 0, sizeof(buf));
if ((len = fread((char*)&buf, 1, sizeof(buf), f)) < 0) {
fclose(f);
marslog(LOG_DBUG, "Bad len '%d' while reading '%s'", len, file);
return "BAD";
}
if (strncmp(buf.ch, "%!", 2) == 0) {
fclose(f);
return "PSFILE";
}
if (strncmp(buf.ch, "#!", 2) == 0) {
fclose(f);
return "SHELL";
}
if (strncmp(buf.ch, "GRIB", 4) == 0) {
fclose(f);
return "GRIB";
}
if (strncmp(buf.ch, "TIDE", 4) == 0) {
fclose(f);
return "GRIB";
}
if (strncmp(buf.ch, "BUDG", 4) == 0) {
fclose(f);
return "GRIB";
}
if (strncmp(buf.ch, "BUFR", 4) == 0) {
fclose(f);
return "BUFR";
}
if (strncmp(buf.ch, "%!", 2) == 0) {
fclose(f);
return "POSTSCRIPT";
}
if (strncmp(buf.ch, "#GEO", 4) == 0) {
fclose(f);
return "GEOPOINTS";
}
if (strncmp(buf.ch, "#LLM", 4) == 0) {
fclose(f);
return "LLMATRIX";
}
if (strncmp(buf.ch + 2, "GRIB", 4) == 0) {
fclose(f);
return "GRIB";
}
if (strncmp(buf.ch + 4, "GRIB", 4) == 0) {
fclose(f);
return "GRIB";
}
if (strncmp(buf.ch + 2, "BUFR", 4) == 0) {
fclose(f);
return "BUFR";
}
if (strncmp(buf.ch + 4, "BUFR", 4) == 0) {
fclose(f);
return "BUFR";
}
if (strncmp(buf.ch, "CDF", 3) == 0) {
fclose(f);
return "NETCDF";
}
rewind(f);
max = 4096; /* 4 K */
while (max-- > 0 && !feof(f)) {
fread((char*)&c, 1, 1, f);
if (!isprint(c) && !isspace(c)) {
fclose(f);
return "BINARY";
}
}
fclose(f);
return "NOTE";
}
char* guess_class(const char* file) {
struct stat buf;
if (stat(file, &buf) < 0)
return "BAD";
switch (buf.st_mode & S_IFMT) {
case S_IFDIR:
return "FOLDER";
case S_IFREG:
return scan_file(file);
default:
return "SPECIAL";
}
}
|