#import <CoreServices/CoreServices.h>

/*
gcc -g -Wall -framework CoreServices -o mime2uti mime2uti.m
*/

int main (int argc, const char * argv[]) {
    
    if (argc != 2) {
        printf("usage: %s <MIME type>\n", argv[0]);
        return(1);
    }

    CFStringRef mime_type = CFStringCreateWithCString(
        kCFAllocatorDefault,
        argv[1],
        kCFStringEncodingUTF8);
    CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(
        kUTTagClassMIMEType,
        mime_type,
        NULL);

    int length = CFStringGetLength(uti);
	int size = sizeof(UniChar)*(length+1);
	char* buffer = (char*)NewPtrClear(size);
    if (CFStringGetCString(uti, buffer, size, kCFStringEncodingUTF8)) {
        printf("%s\n", buffer);
    }

    return (0);
}
