]>
git.proxmox.com Git - libxdgmime-perl.git/blob - xdgmime-source/src/print-mime-data.c
1 /* print-mime-data.c: debug tests for the mime implementation
3 * More info can be found at http://www.freedesktop.org/standards/
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>
9 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
16 #include <sys/types.h>
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");
31 test_by_name (const char *filename
)
35 mt
= xdg_mime_get_mime_type_from_file_name (filename
);
37 printf ("\tname: %s\n", mt
);
41 test_by_data (const char *filename
)
50 file
= fopen (filename
, "r");
54 printf ("Could not open %s\n", filename
);
58 max_extent
= xdg_mime_get_max_buffer_extents ();
59 data
= malloc (max_extent
);
63 printf ("Failed to allocate memory for file %s\n", filename
);
68 bytes_read
= fread (data
, 1, max_extent
, file
);
72 printf ("Error reading file %s\n", filename
);
80 mt
= xdg_mime_get_mime_type_for_data (data
, bytes_read
, &result_prio
);
85 printf ("\tdata: %s\n", mt
);
89 test_by_file (const char *filename
)
93 mt
= xdg_mime_get_mime_type_for_file (filename
, NULL
);
95 printf ("\tfile: %s\n", mt
);
99 is_regular (const char *filename
)
103 if (stat (filename
, &s
) == 0)
104 if (S_ISREG (s
.st_mode
))
111 process_file (const char *dir
, const char *filename
)
115 snprintf (path
, 1024, "%s/%s", dir
, filename
);
117 if (!is_regular (path
))
120 printf ("%s:\n", filename
);
122 test_by_name (filename
);
130 read_from_dir (const char *path
)
133 struct dirent
*entry
;
135 dir
= opendir (path
);
137 printf ("Could not open dir '%s'\n", path
);
141 entry
= readdir (dir
);
142 while (entry
!= NULL
) {
143 if (entry
->d_name
== NULL
)
145 if (strcmp (entry
->d_name
, ".") == 0 ||
146 strcmp (entry
->d_name
, "..") == 0)
148 process_file (path
, entry
->d_name
);
151 entry
= readdir (dir
);
158 main (int argc
, char *argv
[])
165 for (i
= 1; i
< argc
; i
++)
167 read_from_dir (argv
[i
]);