def stringize(contents):
    """Convert contents into a valid C string, escaped if necessary"""
    contents = str(contents).rstrip().lstrip().replace('\\', '\\\\')
    contents = contents.replace('"', '\\"')
    return contents