2 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3 * Distributed under the terms of the MIT License.
10 #include <AppFileInfo.h>
13 static const char *kCommandName
= "settype";
16 static const char *const *kArgv
;
20 "Usage: %s <options> <file> ...\n"
22 "Sets the MIME type, signature, and/or preferred application of one or more\n"
27 " - Print this help text and exit.\n"
28 " -preferredAppSig <signature>\n"
29 " - Set the preferred application of the given files to\n"
32 " - Set the application signature of the given files to\n"
35 " - Set the MIME type of the given files to <type>.\n"
40 print_usage(bool error
)
43 const char *commandName
= NULL
;
45 if (const char *lastSlash
= strchr(kArgv
[0], '/'))
46 commandName
= lastSlash
+ 1;
48 commandName
= kArgv
[0];
51 if (!commandName
|| strlen(commandName
) == 0)
52 commandName
= kCommandName
;
55 fprintf((error
? stderr
: stdout
), kUsage
, commandName
, commandName
,
59 // print_usage_and_exit
61 print_usage_and_exit(bool error
)
69 next_arg(int &argi
, bool optional
= false)
73 print_usage_and_exit(true);
82 check_mime_type(const char *type
)
86 if (!BMimeType::IsValid(type
)) {
87 fprintf(stderr
, "\"%s\" is no valid MIME type.\n", type
);
96 main(int argc
, const char *const *argv
)
102 const char **files
= new const char*[argc
];
104 const char *type
= NULL
;
105 const char *signature
= NULL
;
106 const char *preferredApp
= NULL
;
108 // parse the arguments
109 for (int argi
= 1; argi
< argc
; ) {
110 const char *arg
= argv
[argi
++];
112 if (strcmp(arg
, "-h") == 0 || strcmp(arg
, "--help") == 0) {
113 print_usage_and_exit(false);
115 } else if (strcmp(arg
, "-preferredAppSig") == 0) {
116 preferredApp
= next_arg(argi
);
118 } else if (strcmp(arg
, "-s") == 0) {
119 signature
= next_arg(argi
);
121 } else if (strcmp(arg
, "-t") == 0) {
122 type
= next_arg(argi
);
125 fprintf(stderr
, "Error: Invalid option: \"%s\"\n", arg
);
126 print_usage_and_exit(true);
130 files
[fileCount
++] = arg
;
135 if (!preferredApp
&& !signature
&& !type
) {
136 fprintf(stderr
, "Error: At least one option of \"-preferredAppSig\", "
137 "\"-s\", and \"-t\" must be given.\n");
138 print_usage_and_exit(true);
141 if (fileCount
== 0) {
142 fprintf(stderr
, "Error: No file specified.\n");
143 print_usage_and_exit(true);
146 // check for valid MIME types
147 check_mime_type(preferredApp
);
148 check_mime_type(type
);
149 check_mime_type(signature
);
151 // iterate through the files
152 for (int i
= 0; i
< fileCount
; i
++) {
153 const char *fileName
= files
[i
];
155 // check, whether the file exists
157 status_t error
= entry
.SetTo(fileName
, false);
159 fprintf(stderr
, "Error: Can't access file \"%s\": %s\n",
160 fileName
, strerror(error
));
165 if (!entry
.Exists()) {
166 fprintf(stderr
, "Error: \"%s\": No such file or directory.\n",
172 // ... and has the right type
173 if (signature
&& !entry
.IsFile()) {
174 fprintf(stderr
, "Error: \"%s\" is not a file. Signatures can only "
175 "be set for executable files.\n", fileName
);
183 BNode
&node
= (signature
? file
: _node
);
184 error
= (signature
? file
.SetTo(fileName
, B_READ_WRITE
)
185 : node
.SetTo(fileName
));
187 fprintf(stderr
, "Error: Failed to open file \"%s\": %s\n",
188 fileName
, strerror(error
));
193 // prepare an node/app info object
194 BAppFileInfo appInfo
;
196 BNodeInfo
&nodeInfo
= (signature
? appInfo
: _nodeInfo
);
197 error
= (signature
? appInfo
.SetTo(&file
) : nodeInfo
.SetTo(&node
));
199 fprintf(stderr
, "Error: Failed to open file \"%s\": %s\n",
200 fileName
, strerror(error
));
207 error
= nodeInfo
.SetPreferredApp(preferredApp
);
209 fprintf(stderr
, "Error: Failed to set the preferred "
210 "application of file \"%s\": %s\n", fileName
,
219 error
= nodeInfo
.SetType(type
);
221 fprintf(stderr
, "Error: Failed to set the MIME type of file "
222 "\"%s\": %s\n", fileName
, strerror(error
));
230 error
= appInfo
.SetSignature(signature
);
232 fprintf(stderr
, "Error: Failed to set the signature of file "
233 "\"%s\": %s\n", fileName
, strerror(error
));