diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3dafc56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store + +tag2uti \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..8d7c253 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +# +# Copyright (c) 2019 rokudogobu +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +tag2uti: main.c + clang -O2 -framework CoreFoundation -framework CoreServices -o $@ $^ diff --git a/README.md b/README.md index 3763971..51a7ea1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ + # tag2uti -Simple C program to get UTI (= Uniform Type Identifier) from extension or MIME on Mac. + +Simple C program to get UTI (= Uniform Type Identifier) from filename extension or MIME type on Mac. + +## Usage + + usage: tag2uti ... + +## Example + + $ tag2uti c text/html bash + public.c-source + public.html + public.bash-script com.apple.xcode.bash-script + +## License + +Copyright (c) 2019 rokudogobu. +Licensed under the Apache License, Version 2.0. +See LICENSE for details. diff --git a/main.c b/main.c new file mode 100755 index 0000000..200b8e8 --- /dev/null +++ b/main.c @@ -0,0 +1,44 @@ +/* + + Copyright (c) 2019 rokudogobu + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + */ + +#include +#include + +#define STRENC kCFStringEncodingASCII + +int main( int argc, const char * argv[] ) { + for ( int i = 1; i < argc; i++ ) { + CFStringRef tag = CFStringCreateWithCString( kCFAllocatorDefault, argv[i], STRENC ); + if ( tag ) { + CFRange range = CFStringFind( tag, CFSTR( "/" ), 0 ); + CFStringRef class = range.location == kCFNotFound ? kUTTagClassFilenameExtension: kUTTagClassMIMEType; + CFArrayRef types = UTTypeCreateAllIdentifiersForTag( class, tag, NULL ); + if ( types ) { + for ( int j = 0; j < CFArrayGetCount( types ); j++ ) { + printf( j == 0 ? "%s": " %s", CFStringGetCStringPtr( CFArrayGetValueAtIndex( types, j ), STRENC )); + } + CFRelease( types ); + } + printf( "\n" ); + CFRelease( tag ); + } + } + return 0; +} + +