xref: /haiku/src/bin/mimeset.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 /*
2  * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <MimeType.h>
8 #include <Application.h>
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 
15 extern const char *__progname;
16 static const char *sProgramName = __progname;
17 
18 // options
19 bool gFiles = true;
20 bool gApps = false;
21 int gForce = 0;
22 
23 
24 void
25 usage(int status)
26 {
27 	printf("usage: %s [OPTION]... [PATH]...\n"
28 		"  -all\t\tcombine default action and the -apps option\n"
29 		"  -apps\t\tupdate 'app' and 'meta_mime' information\n"
30 		"  -f\t\tforce updating, even if previously updated\n"
31 		"  	\t  (will not overwrite the 'type' of a file)\n"
32 		"  -F\t\tforce updating, even if previously updated\n"
33 		"  	\t  (will overwrite the 'type' of a file)\n"
34 		"  --help\tdisplay this help information\n"
35 		"When PATH is @, file names are read from stdin\n\n",
36 		sProgramName);
37 
38 	exit(status);
39 }
40 
41 
42 void
43 process_file(const char *path)
44 {
45 	status_t status = B_OK;
46 
47 	BEntry entry(path);
48 	if (!entry.Exists())
49 		status = B_ENTRY_NOT_FOUND;
50 
51 	if (gFiles && status >= B_OK)
52 		status = update_mime_info(path, true, true, gForce);
53 	if (gApps && status >= B_OK)
54 		create_app_meta_mime(path, true, true, gForce);
55 
56 	if (status < B_OK) {
57 		fprintf(stderr, "%s: \"%s\": %s\n",
58 			sProgramName, path, strerror(status));
59 	}
60 }
61 
62 
63 int
64 main(int argc, char **argv)
65 {
66 	// parse arguments
67 
68 	if (argc < 2)
69 		usage(-1);
70 
71 	while (*++argv) {
72 		char *arg = *argv;
73 		if (*arg != '-')
74 			break;
75 
76 		if (!strcmp(arg, "-all"))
77 			gApps = true;
78 		else if (!strcmp(arg, "-apps")) {
79 			gApps = true;
80 			gFiles = false;
81 		} else if (!strcmp(arg, "-f"))
82 			gForce = 1;
83 		else if (!strcmp(arg, "-F"))
84 			gForce = 2;
85 		else if (!strcmp(arg, "--help"))
86 			usage(0);
87 		else {
88 			fprintf(stderr, "unknown  option \"%s\"\n", arg);
89 			usage(-1);
90 		}
91 	}
92 
93 	// process files
94 
95 	BApplication app("application/x-vnd.haiku.mimeset");
96 
97 	while (*argv) {
98 		char *arg = *argv++;
99 
100 		if (!strcmp(arg, "@")) {
101 			// read file names from stdin
102 			char name[B_PATH_NAME_LENGTH];
103 			while (fgets(name, sizeof(name), stdin) != NULL) {
104 				name[strlen(name) - 1] = '\0';
105 					// remove trailing '\n'
106 				process_file(name);
107 			}
108 		} else
109 			process_file(arg);
110 	}
111 
112 	return 0;
113 }
114 
115