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
|
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <assert.h>
#include <stdlib.h>
#define DBG 0
#if DBG
static void display (unsigned long const hist[])
{
size_t i;
for (i = 0; i < 256; i++)
{
unsigned long n = hist[i];
if (n > 0)
{
printf ("%3d : %5lu\n", (int) i, n);
}
}
}
#endif
int is_bin (char const *path, char const *fname)
{
int bin = -1;
size_t const size = strlen (path) + 1 + strlen (fname) + 1;
char *fullname = malloc (size);
if (fullname != NULL)
{
sprintf (fullname, "%s/%s", path, fname);
printf ("%-40s", fullname);
{
FILE *fp = fopen (fullname, "rb");
if (fp != NULL)
{
unsigned long hist[256] = { 0 };
int c;
while ((c = fgetc (fp)) != EOF)
{
size_t i = (size_t) (unsigned char) c;
assert (i < 256);
hist[i]++;
/* a developper */
}
#if DBG
display (hist);
#endif
fclose (fp);
bin = hist[0] != 0;
}
free (fullname);
}
}
return bin;
}
int main (void)
{
char const *path = "/util";
DIR *dir = opendir (path);
if (dir != NULL)
{
struct dirent *lecture;
while ((lecture = readdir (dir)) != NULL)
{
char const *fname = lecture->d_name;
printf ("%-20s", fname);
int bin = is_bin (path, fname);
if (bin == -1)
{
printf ("DIR");
}
else if (bin == 1)
{
printf ("BIN");
}
else
{
printf ("TXT");
}
printf ("\n");
}
closedir (dir);
}
return 0;
} |
Partager