1"""Provides some Mac / Darwin based utility functions for xonsh."""
2from ctypes import c_uint, byref, create_string_buffer
3
4from xonsh.platform import LIBC
5
6
7def sysctlbyname(name, return_str=True):
8    """Gets a sysctl value by name. If return_str is true, this will return
9    a string representation, else it will return the raw value.
10    """
11    # forked from https://gist.github.com/pudquick/581a71425439f2cf8f09
12    size = c_uint(0)
13    # Find out how big our buffer will be
14    LIBC.sysctlbyname(name, None, byref(size), None, 0)
15    # Make the buffer
16    buf = create_string_buffer(size.value)
17    # Re-run, but provide the buffer
18    LIBC.sysctlbyname(name, buf, byref(size), None, 0)
19    if return_str:
20        return buf.value
21    else:
22        return buf.raw
23