]>
Commit | Line | Data |
---|---|---|
1 | /* print-mime-data.c: debug tests for the mime implementation | |
2 | * | |
3 | * More info can be found at http://www.freedesktop.org/standards/ | |
4 | * | |
5 | * Copyright (C) 2005 Red Hat, Inc. | |
6 | * Copyright (C) 2005 Matthias Clasen <mclasen@redhat.com> | |
7 | * Copyright (C) 2012 Bastien Nocera <hadess@hadess.net> | |
8 | * | |
9 | * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0 | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <libgen.h> | |
16 | #include <sys/types.h> | |
17 | #include <dirent.h> | |
18 | ||
19 | #include "xdgmime.h" | |
20 | ||
21 | static void | |
22 | usage (void) | |
23 | { | |
24 | printf ("usage: print-mime-data <DIR>\n\n"); | |
25 | printf ("Prints the mime-type of every file in <DIR>, detected in various ways.\n"); | |
26 | ||
27 | exit (1); | |
28 | } | |
29 | ||
30 | static void | |
31 | test_by_name (const char *filename) | |
32 | { | |
33 | const char *mt; | |
34 | ||
35 | mt = xdg_mime_get_mime_type_from_file_name (filename); | |
36 | ||
37 | printf ("\tname: %s\n", mt); | |
38 | } | |
39 | ||
40 | static void | |
41 | test_by_data (const char *filename) | |
42 | { | |
43 | FILE *file; | |
44 | const char *mt; | |
45 | int max_extent; | |
46 | char *data; | |
47 | int bytes_read; | |
48 | int result_prio; | |
49 | ||
50 | file = fopen (filename, "r"); | |
51 | ||
52 | if (file == NULL) | |
53 | { | |
54 | printf ("Could not open %s\n", filename); | |
55 | return; | |
56 | } | |
57 | ||
58 | max_extent = xdg_mime_get_max_buffer_extents (); | |
59 | data = malloc (max_extent); | |
60 | ||
61 | if (data == NULL) | |
62 | { | |
63 | printf ("Failed to allocate memory for file %s\n", filename); | |
64 | fclose (file); | |
65 | return; | |
66 | } | |
67 | ||
68 | bytes_read = fread (data, 1, max_extent, file); | |
69 | ||
70 | if (ferror (file)) | |
71 | { | |
72 | printf ("Error reading file %s\n", filename); | |
73 | ||
74 | free (data); | |
75 | fclose (file); | |
76 | ||
77 | return; | |
78 | } | |
79 | ||
80 | mt = xdg_mime_get_mime_type_for_data (data, bytes_read, &result_prio); | |
81 | ||
82 | free (data); | |
83 | fclose (file); | |
84 | ||
85 | printf ("\tdata: %s\n", mt); | |
86 | } | |
87 | ||
88 | static void | |
89 | test_by_file (const char *filename) | |
90 | { | |
91 | const char *mt; | |
92 | ||
93 | mt = xdg_mime_get_mime_type_for_file (filename, NULL); | |
94 | ||
95 | printf ("\tfile: %s\n", mt); | |
96 | } | |
97 | ||
98 | static int | |
99 | is_regular (const char *filename) | |
100 | { | |
101 | struct stat s; | |
102 | ||
103 | if (stat (filename, &s) == 0) | |
104 | if (S_ISREG (s.st_mode)) | |
105 | return 1; | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
110 | static void | |
111 | process_file (const char *dir, const char *filename) | |
112 | { | |
113 | char path[1024]; | |
114 | ||
115 | snprintf (path, 1024, "%s/%s", dir, filename); | |
116 | ||
117 | if (!is_regular (path)) | |
118 | return; | |
119 | ||
120 | printf ("%s:\n", filename); | |
121 | ||
122 | test_by_name (filename); | |
123 | test_by_data (path); | |
124 | test_by_file (path); | |
125 | ||
126 | printf ("\n"); | |
127 | } | |
128 | ||
129 | static void | |
130 | read_from_dir (const char *path) | |
131 | { | |
132 | DIR *dir; | |
133 | struct dirent *entry; | |
134 | ||
135 | dir = opendir (path); | |
136 | if (!dir) { | |
137 | printf ("Could not open dir '%s'\n", path); | |
138 | return; | |
139 | } | |
140 | ||
141 | entry = readdir (dir); | |
142 | while (entry != NULL) { | |
143 | if (entry->d_name == NULL) | |
144 | goto next; | |
145 | if (strcmp (entry->d_name, ".") == 0 || | |
146 | strcmp (entry->d_name, "..") == 0) | |
147 | goto next; | |
148 | process_file (path, entry->d_name); | |
149 | ||
150 | next: | |
151 | entry = readdir (dir); | |
152 | } | |
153 | ||
154 | closedir (dir); | |
155 | } | |
156 | ||
157 | int | |
158 | main (int argc, char *argv[]) | |
159 | { | |
160 | int i; | |
161 | ||
162 | if (argc < 2) | |
163 | usage (); | |
164 | ||
165 | for (i = 1; i < argc; i++) | |
166 | { | |
167 | read_from_dir (argv[i]); | |
168 | } | |
169 | ||
170 | return 0; | |
171 | } |