ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
version.c
Go to the documentation of this file.
1
6
7#include <ascii-chat/version.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <ctype.h>
12#include <limits.h>
13#include <errno.h>
14
15// GCC/Clang: Use __attribute__((section)) to place string in custom section
16// ELF (Linux): .section_name format
17// Mach-O (macOS): __SEGMENT,__section format
18#if defined(__GNUC__) || defined(__clang__)
19
20#if defined(__APPLE__)
21// macOS Mach-O format: use __TEXT segment for read-only data
22__attribute__((used, section("__TEXT,__ascii_chat"))) const char ascii_chat_custom_section[] = ASCII_CHAT_DOT_ASCII_CHAT_STRING;
23__attribute__((used, section("__TEXT,__comment"))) const char ascii_chat_comment_string[] = ASCII_CHAT_DOT_COMMENT_STRING;
24__attribute__((used, section("__TEXT,__version"))) const char ascii_chat_version_string[] = ASCII_CHAT_VERSION_FULL;
25#define ASCII_CHAT_VERSION_GETTERS
26
27#elif defined(__linux__)
28// Linux ELF format: standard .section_name format
29__attribute__((used, section(".ascii_chat"))) const char ascii_chat_custom_section[] = ASCII_CHAT_DOT_ASCII_CHAT_STRING;
30__attribute__((used, section(".comment"))) const char ascii_chat_comment_string[] = ASCII_CHAT_DOT_COMMENT_STRING;
31__attribute__((used, section(".version"))) const char ascii_chat_version_string[] = ASCII_CHAT_VERSION_FULL;
32#define ASCII_CHAT_VERSION_GETTERS
33#endif
34
35#ifdef ASCII_CHAT_VERSION_GETTERS
36// Provide functions to get version info at runtime
37const char *ascii_chat_get_version(void) {
38 return ascii_chat_version_string;
39}
40const char *ascii_chat_get_comment(void) {
41 return ascii_chat_comment_string;
42}
43#endif
44
45#endif
46
47semantic_version_t version_parse(const char *version_string) {
48 semantic_version_t result = {0, 0, 0, false};
49
50 if (!version_string || version_string[0] == '\0') {
51 return result;
52 }
53
54 const char *p = version_string;
55
56 // Optionally skip 'v' prefix if present
57 if (p[0] == 'v' || p[0] == 'V') {
58 p++; // Skip 'v'
59 }
60
61 // Parse major version
62 char *endptr = NULL;
63 errno = 0; // Reset errno before strtol
64 long major = strtol(p, &endptr, 10);
65 if (errno == ERANGE || endptr == p || major < 0 || major > INT_MAX) {
66 return result; // Invalid major version or overflow
67 }
68 result.major = (int)major;
69 p = endptr;
70
71 // Version must have all three components (major.minor.patch)
72 if (*p != '.') {
73 return result; // Invalid: missing minor version
74 }
75 p++; // Skip dot
76
77 // Parse minor version
78 errno = 0; // Reset errno before strtol
79 long minor = strtol(p, &endptr, 10);
80 if (errno == ERANGE || endptr == p || minor < 0 || minor > INT_MAX) {
81 return result; // Invalid minor version or overflow
82 }
83 result.minor = (int)minor;
84 p = endptr;
85
86 // Version must have patch component
87 if (*p != '.') {
88 return result; // Invalid: missing patch version
89 }
90 p++; // Skip dot
91
92 // Parse patch version
93 errno = 0; // Reset errno before strtol
94 long patch = strtol(p, &endptr, 10);
95 if (errno == ERANGE || endptr == p || patch < 0 || patch > INT_MAX) {
96 return result; // Invalid patch version or overflow
97 }
98 result.patch = (int)patch;
99 p = endptr;
100
101 // Reject prerelease tags (e.g., "v1.2.3-beta") - must end after patch number
102 if (*p != '\0') {
103 return result; // Invalid: has extra characters after version
104 }
105
106 result.valid = true;
107 return result;
108}
109
110int version_compare(semantic_version_t a, semantic_version_t b) {
111 // Compare major version
112 if (a.major < b.major) {
113 return -1;
114 }
115 if (a.major > b.major) {
116 return 1;
117 }
118
119 // Major versions equal, compare minor
120 if (a.minor < b.minor) {
121 return -1;
122 }
123 if (a.minor > b.minor) {
124 return 1;
125 }
126
127 // Minor versions equal, compare patch
128 if (a.patch < b.patch) {
129 return -1;
130 }
131 if (a.patch > b.patch) {
132 return 1;
133 }
134
135 // All components equal
136 return 0;
137}
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
int32_t vec_t __attribute__((vector_size(4 *4)))
SESSION_CREATED (PACKET_TYPE_ACIP_SESSION_CREATED) - Session created response.
Definition vector.h:42
🔤 String Manipulation and Shell Escaping Utilities
Network quality metrics for a single participant.
Definition metrics.h:36
int version_compare(semantic_version_t a, semantic_version_t b)
Definition version.c:110
semantic_version_t version_parse(const char *version_string)
Definition version.c:47