1

Considering the following command:

sudo scutil
> list

I get the list of "pattern" or subkeys available:

subKey [0] = Plugin:...
subKey [1] = Setup:....
....

Programmatically in Objective-C, I am able to acces a specific key:

SCDynamicStoreRef ds = SCDynamicStoreCreate(nullptr, CFSTR("GetWorkgroup"), nullptr, nullptr);
NSLog(@"Config: %@", SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/IPv4")));

However I am struggling in getting all the list of available patterns.

Following program does NOT work:

SCDynamicStoreRef ds = SCDynamicStoreCreate(nullptr, CFSTR("GetWorkgroup"), nullptr, nullptr);

const auto  list = SCDynamicStoreCopyKeyList(ds, CFSTR("State:/Network/Service/"));
const size_t keysNum = CFArrayGetCount(list);

std::cout << keysNum << std::endl;
for (size_t i = 0; i< keysNum; ++i)
{
    auto property = static_cast<CFDictionaryRef>(SCDynamicStoreCopyValue(ds, static_cast<CFStringRef>(CFArrayGetValueAtIndex(list, i))));
    NSLog(@"Key: %@", property);
}

I tried many codes without success. Searching on internet for SCDynamicStore list subKey SCDynamicStore iterate patterns or similar give little results.

Also, documentation on developer.apple.com, as usual, is useless: no examples, documentation which relay on you already knowing all about it, no #include/#import indication, etc. etc..

Question

How to list all available patterns in SCDynamicStore ?

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85

1 Answers1

1

So, after some research, I found the SCDynamicStoreCopyMultiple which has a parameter of type pattern: This means it is possible to have regular expression to define the keys.

After discovering this, it was quite easy to implement a prove of concept.

SCDynamicStoreRef ds = SCDynamicStoreCreate(nullptr, CFSTR("test"), nullptr, nullptr);
    
const auto keys = CFArrayCreate(nullptr, nullptr, 0, nullptr);
const auto keyValue = CFSTR(".*");
const auto patterns = CFArrayCreate(nullptr, (const void**)&keyValue, 1, nullptr);
const CFDictionaryRef  list = SCDynamicStoreCopyMultiple(ds, keys, patterns);

const size_t num = CFDictionaryGetCount(list);
std::vector<CFTypeRef> valuesV(num);
std::vector<CFTypeRef> keysV(num);
CFDictionaryGetKeysAndValues(list, keysV.data(), valuesV.data());
    
for (size_t i = 0; i< num; ++i)
{
    NSLog(@"%@ = %@", keysV[i], valuesV[i]);
}

DISCLAIM: ok, this is not pure Objective-C, but it's trivial to convert it in case.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85