A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/Neved4/asprintf below:

Neved4/asprintf: 🛠️ One true asprintf, vasprintf!

asprintf - One True asprintf, vasprintf! 🛠️

Robust, portable implementation of asprintf(), vasprintf(). Thoroughly tested.

Wanted to have a version of asprintf that is simple, robust and just works everywhere. Needed it to be backward-compatible with existing libc libraries, consistent across diverse platforms and systems. It should also focus on correctness, minimize unexpected behavior, be thoroughly tested, be easy to read and stick to both C99 and POSIX.1-2024.

This process also gave me the opportunity to review existing implementations and deepen my understanding of pointers and systems programming.

Important

Both asprintf() and vasprintf() are now included in POSIX.1-2024, see Austin Group Bug #1496. You can check the current specification here. For systems supporting POSIX.1-2024 or later, Neved4/asprintf is no longer necessary. For systems limited to POSIX.1-2017 or under, it offers a practical solution.

If you are building asprintf, ensure you have:

If you have clib installed, run:

$ clib install Neved4/asprintf

If you don't have the above, start by cloning the repository:

$ git clone https://github.com/Neved4/asprintf

Once you've cloned the repository, build by executing:

Alternatively, if you have zig:

$ zig cc asprintf.c -t asprintf
// asprintf, vasprintf - print to allocated string

#include "asprintf.h"

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

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

Consider a getconf() function to retrieve a config specified path, that supports both XDG_CONFIG_HOME and fallbacks:

char *getconf() {
    const char *file = "tz.conf", *home = getenv("HOME"),
        *xdg_config_home = getenv("XDG_CONFIG_HOME");
    char *config = NULL;

    // Path building logic

    return config;
}

After which we'll have to lay down our path building logic.

if (access("tz.conf", F_OK) != -1) {
    config = strdup("tz.conf");
} else if (xdg_config_home) {
    size_t len = strlen(xdg_config_home) + strlen("twc") + strlen(file) + 3;
    config = (char *)malloc(len);
    if (config != NULL) {
        snprintf(config, len, "%s/%s/%s", xdg_config_home, "twc", file);
    }
} else if (home) {
    size_t len = strlen(home) + strlen(".config/twc") + strlen(file) + 3;
    config = (char *)malloc(len);
    if (config != NULL) {
        snprintf(config, len, "%s/%s/%s", home, ".config/twc", file);
    }
}
if (access("tz.conf", F_OK) != -1) {
    config = strdup("tz.conf");
} else if (xdg_config_home) {
    asprintf(&config, "%s/%s/%s", xdg_config_home, "twc", file);
} else if (home) {
    asprintf(&config, "%s/%s/%s", home, ".config/twc", file);
}

To run all the tests against asprintf execute the following command:

cc test.c asprintf.c -o test && ./test

To use your system's default asprintf(3), run this instead:

cc test.c -o test && ./test

To link it against other asprintf implementations, run:

cc test.c /path/to/asprintf.c -o test && ./test

Any of the above will output something like:

 Ok "Memory allocation"
 Ok "Empty string as input"
 Ok "String formatting variations"
 Ok "Special characters in format string"
Err "Boundary cases for integers"

// More tests

-----------
Passing: 4
 Failed: 1
  Total: 5

To compile the binary inside a Docker image, run:

Runs on Linux, macOS and *BSD systems on both x86_64 and arm64. Builds with clang, gcc, tcc, zig and any other compiler that supports C99 or later.

Should be compatible with glibc, GLib, FreeBSD libc, musl libc asprintf.

asprintf is compatible with both POSIX.1-2024,1 and C99.2

asprintf is licensed under the terms of the MIT License.

See the LICENSE file for details.

Other libc implementations Other asprintf implementations
  1. IEEE Std 1003.1-2024: Standard for Information Technology — Portable Operating System Interface (POSIX®),
    ISO/IEC DIS 9945. URL: https://pubs.opengroup.org/onlinepubs/9799919799/

  2. ISO/IEC 9899: Standard for Information Technology — Programming languages — C, ISO/IEC 9899:2023.
    URL: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4