Skip to content

Commit

Permalink
Include asprintf for Windows
Browse files Browse the repository at this point in the history
And formatting
  • Loading branch information
Sapd committed Feb 20, 2024
1 parent 3ba8f0d commit 8860a8d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,7 @@ int main(int argc, char* argv[])
return 1;
}
break;
case 'o':
{
case 'o': {
bool output_specified = true;

if (OPTIONAL_ARGUMENT_IS_PRESENT) {
Expand All @@ -611,7 +610,7 @@ int main(int argc, char* argv[])
} else {
output_specified = false;
}

if (output_specified == false) {
// short not listed because deprecated
fprintf(stderr, "Usage: %s -o JSON|YAML|ENV|STANDARD\n", argv[0]);
Expand Down Expand Up @@ -699,7 +698,7 @@ int main(int argc, char* argv[])
}
}
// fall through
} else if(strcmp(opts[option_index].name, "readme-helper") == 0) {
} else if (strcmp(opts[option_index].name, "readme-helper") == 0) {
print_readmetable();
return 0;
} else if (strcmp(opts[option_index].name, "help-all") == 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const char* battery_status_to_string(enum battery_status status)

/**
* @brief Adds an error to the HeadsetInfo struct
*
*
* @param info Headsetinfo struct
* @param source Error source/key as string
* @param message Error message
Expand All @@ -60,7 +60,7 @@ static void addError(HeadsetInfo* info, const char* source, const char* message)

/**
* @brief Adds an action to the HeadsetInfo struct
*
*
* @param info Headsetinfo struct
* @param capbability Capability as enum
* @param device Device name as string
Expand Down Expand Up @@ -160,7 +160,7 @@ void initializeHeadsetInfo(HeadsetInfo* info, struct device* device)

/**
* @brief Iterates through all requested features and processes their responses
*
*
* @param info struct with headset information to fill
* @param featureRequests struct with all feature requests
* @param size size of featureRequests
Expand Down
2 changes: 1 addition & 1 deletion src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ typedef struct {

/**
* @brief Main output function
*
*
* @param deviceList List of devices
* @param print_capabilities If the user wanted to print capabilities
* @param output Output type
Expand Down
94 changes: 93 additions & 1 deletion src/utility.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -119,4 +121,94 @@ int get_float_data_from_parameter(char* input, float* dest, size_t len)
}

return i;
}
}

// ----------------- asprintf / vasprintf -----------------
/*
* Copyright (c) 2004 Darren Tucker.
*
* Based originally on asprintf.c from OpenBSD:
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef VA_COPY
#ifdef HAVE_VA_COPY
#define VA_COPY(dest, src) va_copy(dest, src)
#else
#ifdef HAVE___VA_COPY
#define VA_COPY(dest, src) __va_copy(dest, src)
#else
#define VA_COPY(dest, src) (dest) = (src)
#endif
#endif
#endif

#define INIT_SZ 128

int vasprintf(char** str, const char* fmt, va_list ap)
{
int ret;
va_list ap2;
char *string, *newstr;
size_t len;

if ((string = malloc(INIT_SZ)) == NULL)
goto fail;

VA_COPY(ap2, ap);

Check failure on line 170 in src/utility.c

View workflow job for this annotation

GitHub Actions / macos-latest-compile

array type 'va_list' (aka '__builtin_va_list') is not assignable
ret = vsnprintf(string, INIT_SZ, fmt, ap2);
va_end(ap2);
if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
*str = string;
} else if (ret == INT_MAX || ret < 0) { /* Bad length */
free(string);
goto fail;
} else { /* bigger than initial, realloc allowing for nul */
len = (size_t)ret + 1;
if ((newstr = realloc(string, len)) == NULL) {
free(string);
goto fail;
}
VA_COPY(ap2, ap);

Check failure on line 184 in src/utility.c

View workflow job for this annotation

GitHub Actions / macos-latest-compile

array type 'va_list' (aka '__builtin_va_list') is not assignable
ret = vsnprintf(newstr, len, fmt, ap2);
va_end(ap2);
if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */
free(newstr);
goto fail;
}
*str = newstr;
}
return (ret);

fail:
*str = NULL;
errno = ENOMEM;
return (-1);
}

int asprintf(char** str, const char* fmt, ...)
{
va_list ap;
int ret;

*str = NULL;
va_start(ap, fmt);
ret = vasprintf(str, fmt, ap);
va_end(ap);

return ret;
}

// ----------------- -------------------- -----------------
6 changes: 5 additions & 1 deletion src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ int get_byte_data_from_parameter(char* input, unsigned char* dest, size_t len);
* @param len max dest length
* @return int amount of data converted
*/
int get_float_data_from_parameter(char* input, float* dest, size_t len);
int get_float_data_from_parameter(char* input, float* dest, size_t len);

int vasprintf(char** str, const char* fmt, va_list ap);

Check failure on line 90 in src/utility.h

View workflow job for this annotation

GitHub Actions / ubuntu-latest-compile

unknown type name ‘va_list’

int asprintf(char** str, const char* fmt, ...);

0 comments on commit 8860a8d

Please sign in to comment.