summaryrefslogtreecommitdiff
path: root/src/print-mime-data.c
blob: 1244655952bcfeac34a87b6535fb79c0861f00c3 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* print-mime-data.c: debug tests for the mime implementation
 *
 * More info can be found at http://www.freedesktop.org/standards/
 * 
 * Copyright (C) 2005  Red Hat, Inc.
 * Copyright (C) 2005  Matthias Clasen <mclasen@redhat.com>
 * Copyright (C) 2012  Bastien Nocera <hadess@hadess.net>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <sys/types.h>
#include <dirent.h>

#include "xdgmime.h"

static void
usage (void)
{
  printf ("usage: print-mime-data <DIR>\n\n");
  printf ("Prints the mime-type of every file in <DIR>, detected in various ways.\n");

  exit (1);
}

static void
test_by_name (const char *filename)
{
  const char *mt;

  mt = xdg_mime_get_mime_type_from_file_name (filename);

  printf ("\tname: %s\n", mt);
}

static void
test_by_data (const char *filename)
{
  FILE  *file;
  const char *mt;
  int max_extent;
  char *data;
  int bytes_read;
  int result_prio;

  file = fopen (filename, "r");

  if (file == NULL)
    {
      printf ("Could not open %s\n", filename);
      return;
    }

  max_extent = xdg_mime_get_max_buffer_extents ();
  data = malloc (max_extent);

  if (data == NULL)
    {
      printf ("Failed to allocate memory for file %s\n", filename);
      fclose (file);
      return;
    }

  bytes_read = fread (data, 1, max_extent, file);
  
  if (ferror (file))
    {
      printf ("Error reading file %s\n", filename);

      free (data);
      fclose (file);
      
     return;
    }

  mt = xdg_mime_get_mime_type_for_data (data, bytes_read, &result_prio);
  
  free (data);
  fclose (file);

  printf ("\tdata: %s\n", mt);
}

static void
test_by_file (const char *filename)
{
  const char *mt;

  mt = xdg_mime_get_mime_type_for_file (filename, NULL);

  printf ("\tfile: %s\n", mt);
}

static int
is_regular (const char *filename)
{
  struct stat s;

  if (stat (filename, &s) == 0)
    if (S_ISREG (s.st_mode))
      return 1;

  return 0;
}

static void
process_file (const char *dir, const char *filename)
{
  char path[1024];

  snprintf (path, 1024, "%s/%s", dir, filename);

  if (!is_regular (path))
    return;

  printf ("%s:\n", filename);

  test_by_name (filename);
  test_by_data (path);
  test_by_file (path);

  printf ("\n");
}

static void
read_from_dir (const char *path)
{
  DIR *dir;
  struct dirent *entry;

  dir = opendir (path);
  if (!dir) {
    printf ("Could not open dir '%s'\n", path);
    return;
  }

  entry = readdir (dir);
  while (entry != NULL) {
    if (strcmp (entry->d_name, ".") == 0 ||
        strcmp (entry->d_name, "..") == 0)
      goto next;
    process_file (path, entry->d_name);

next:
    entry = readdir (dir);
  }

  closedir (dir);
}

int
main (int argc, char *argv[])
{
  int i;

  if (argc < 2)
    usage ();
  
  for (i = 1; i < argc; i++)
    {
      read_from_dir (argv[i]);
    }

  return 0;
}