Skip to content

Meta Tags Generator — C source

Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.

This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.

/* meta-tags-generator — build the ordered SEO head-tag set: title,
 * description, canonical, robots, Open Graph (og_title/og_description/og_url
 * falling back to the base fields, og:type website whenever the block is
 * present), Twitter card, theme-color. C port (canonical TS:
 * src/lib/meta-tags.ts; Go twin:
 * cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
 * fallbacks, same emission order, same warnings (title > 60,
 * description > 160) and the same hard errors (missing title/description,
 * non-absolute og:image, non-hex theme-color suppress emission entirely).
 * Caller frees the result with meta_tags_free(). */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MT_FIELD_CAP 2048

typedef struct {
    const char *title;
    const char *description;
    const char *canonical_url;
    const char *robots;
    const char *og_title;
    const char *og_description;
    const char *og_image;
    const char *og_url;
    const char *twitter_card;
    const char *twitter_site;
    const char *theme_color;
} MetaTagsInput;

typedef struct {
    int ok;
    char **errors;
    size_t error_count;
    char **warnings;
    size_t warning_count;
    char **tags;
    size_t tag_count;
} MetaTagsResult;

/* Copy src into dst with surrounding whitespace removed; NULL src -> "". */
static void mt_trim(const char *src, char *dst, size_t cap) {
    size_t len = 0;
    dst[0] = '\0';
    if (!src) return;
    while (*src && isspace((unsigned char)*src)) src++;
    len = strlen(src);
    while (len > 0 && isspace((unsigned char)src[len - 1])) len--;
    if (len >= cap) len = cap - 1;
    memcpy(dst, src, len);
    dst[len] = '\0';
}

static int mt_is_hex_color(const char *s) {
    size_t i;
    if (strlen(s) != 7 || s[0] != '#') return 0;
    for (i = 1; i < 7; i++) {
        if (!isxdigit((unsigned char)s[i])) return 0;
    }
    return 1;
}

static int mt_is_absolute_http(const char *s) {
    return strncmp(s, "http://", 7) == 0 || strncmp(s, "https://", 8) == 0;
}

static char *mt_dup(const char *s) {
    char *out = malloc(strlen(s) + 1);
    if (out) strcpy(out, s);
    return out;
}

static void mt_push(char ***arr, size_t *n, size_t *cap, const char *s) {
    if (*n >= *cap) {
        *cap = *cap ? *cap * 2 : 4;
        *arr = realloc(*arr, *cap * sizeof(char *));
    }
    (*arr)[(*n)++] = mt_dup(s);
}

/* printf a tag/warning line built from one string slot. */
static void mt_pushf(char ***arr, size_t *n, size_t *cap, const char *fmt, const char *s) {
    char buf[MT_FIELD_CAP * 2];
    snprintf(buf, sizeof buf, fmt, s);
    mt_push(arr, n, cap, buf);
}

MetaTagsResult build_meta_tags(const MetaTagsInput *in) {
    MetaTagsResult r;
    char t[MT_FIELD_CAP], d[MT_FIELD_CAP], canon[MT_FIELD_CAP], robot[MT_FIELD_CAP];
    char ot_in[MT_FIELD_CAP], od_in[MT_FIELD_CAP], oi[MT_FIELD_CAP], ou_in[MT_FIELD_CAP];
    char tc[MT_FIELD_CAP], ts[MT_FIELD_CAP], theme[MT_FIELD_CAP];
    char *ot, *od, *ou;
    char **errors = NULL, **warnings = NULL, **tags = NULL;
    size_t nerr = 0, cerr = 0, nwarn = 0, cwarn = 0, ntag = 0, ctag = 0;
    char buf[128];

    mt_trim(in->title, t, sizeof t);
    mt_trim(in->description, d, sizeof d);
    mt_trim(in->canonical_url, canon, sizeof canon);
    mt_trim(in->og_title, ot_in, sizeof ot_in);
    mt_trim(in->og_description, od_in, sizeof od_in);
    mt_trim(in->og_image, oi, sizeof oi);
    mt_trim(in->og_url, ou_in, sizeof ou_in);
    mt_trim(in->twitter_card, tc, sizeof tc);
    mt_trim(in->twitter_site, ts, sizeof ts);
    mt_trim(in->theme_color, theme, sizeof theme);
    ot = ot_in[0] ? ot_in : t;
    od = od_in[0] ? od_in : d;
    ou = ou_in[0] ? ou_in : canon;

    if (t[0] == '\0') mt_push(&errors, &nerr, &cerr, "Title is required.");
    if (d[0] == '\0') mt_push(&errors, &nerr, &cerr, "Description is required.");
    if (oi[0] != '\0' && !mt_is_absolute_http(oi))
        mt_push(&errors, &nerr, &cerr, "og:image must be an absolute http(s) URL.");
    if (theme[0] != '\0' && !mt_is_hex_color(theme))
        mt_push(&errors, &nerr, &cerr, "theme-color must be a #rrggbb hex value.");
    if (nerr > 0) {
        r.ok = 0;
        r.errors = errors;
        r.error_count = nerr;
        r.warnings = NULL;
        r.warning_count = 0;
        r.tags = NULL;
        r.tag_count = 0;
        return r;
    }

    /* Length warnings — never block emission. */
    if (strlen(t) > 60) {
        snprintf(buf, sizeof buf,
                 "Title is %zu characters — search results truncate around 60.", strlen(t));
        mt_push(&warnings, &nwarn, &cwarn, buf);
    }
    if (strlen(d) > 160) {
        snprintf(buf, sizeof buf,
                 "Description is %zu characters — search results truncate around 160.", strlen(d));
        mt_push(&warnings, &nwarn, &cwarn, buf);
    }

    mt_pushf(&tags, &ntag, &ctag, "<title>%s</title>", t);
    mt_pushf(&tags, &ntag, &ctag, "<meta name=\"description\" content=\"%s\" />", d);
    if (canon[0] != '\0') mt_pushf(&tags, &ntag, &ctag, "<link rel=\"canonical\" href=\"%s\" />", canon);
    mt_trim(in->robots, robot, sizeof robot);
    if (robot[0] != '\0') mt_pushf(&tags, &ntag, &ctag, "<meta name=\"robots\" content=\"%s\" />", robot);

    if (ot_in[0] != '\0' || od_in[0] != '\0' || oi[0] != '\0' || ou[0] != '\0') {
        mt_pushf(&tags, &ntag, &ctag, "<meta property=\"og:title\" content=\"%s\" />", ot);
        mt_pushf(&tags, &ntag, &ctag, "<meta property=\"og:description\" content=\"%s\" />", od);
        if (oi[0] != '\0') mt_pushf(&tags, &ntag, &ctag, "<meta property=\"og:image\" content=\"%s\" />", oi);
        if (ou[0] != '\0') mt_pushf(&tags, &ntag, &ctag, "<meta property=\"og:url\" content=\"%s\" />", ou);
        mt_push(&tags, &ntag, &ctag, "<meta property=\"og:type\" content=\"website\" />");
    }

    if (tc[0] != '\0') mt_pushf(&tags, &ntag, &ctag, "<meta name=\"twitter:card\" content=\"%s\" />", tc);
    if (ts[0] != '\0') mt_pushf(&tags, &ntag, &ctag, "<meta name=\"twitter:site\" content=\"%s\" />", ts);
    if (theme[0] != '\0') mt_pushf(&tags, &ntag, &ctag, "<meta name=\"theme-color\" content=\"%s\" />", theme);

    r.ok = 1;
    r.errors = NULL;
    r.error_count = 0;
    r.warnings = warnings;
    r.warning_count = nwarn;
    r.tags = tags;
    r.tag_count = ntag;
    return r;
}

void meta_tags_free(MetaTagsResult *r) {
    size_t i;
    for (i = 0; i < r->error_count; i++) free(r->errors[i]);
    free(r->errors);
    for (i = 0; i < r->warning_count; i++) free(r->warnings[i]);
    free(r->warnings);
    for (i = 0; i < r->tag_count; i++) free(r->tags[i]);
    free(r->tags);
    r->errors = NULL;
    r->warnings = NULL;
    r->tags = NULL;
    r->error_count = r->warning_count = r->tag_count = 0;
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →