Warning: Undefined array key "rss_show_deleted" in /var/www/wiki/dw/inc/Feed/FeedCreatorOptions.php on line 86
Warning: Cannot modify header information - headers already sent by (output started at /var/www/wiki/dw/inc/Feed/FeedCreatorOptions.php:86) in /var/www/wiki/dw/feed.php on line 53
Warning: Cannot modify header information - headers already sent by (output started at /var/www/wiki/dw/inc/Feed/FeedCreatorOptions.php:86) in /var/www/wiki/dw/feed.php on line 54
Warning: Cannot modify header information - headers already sent by (output started at /var/www/wiki/dw/inc/Feed/FeedCreatorOptions.php:86) in /var/www/wiki/dw/feed.php on line 55
Warning: Cannot modify header information - headers already sent by (output started at /var/www/wiki/dw/inc/Feed/FeedCreatorOptions.php:86) in /var/www/wiki/dw/feed.php on line 56
Warning: Cannot modify header information - headers already sent by (output started at /var/www/wiki/dw/inc/Feed/FeedCreatorOptions.php:86) in /var/www/wiki/dw/inc/httputils.php on line 32
Warning: Cannot modify header information - headers already sent by (output started at /var/www/wiki/dw/inc/Feed/FeedCreatorOptions.php:86) in /var/www/wiki/dw/inc/httputils.php on line 33 wiki.unix7.org - c
https://wiki.unix7.org/
Fri, 24 Jul 2026 22:47:42 +0000FeedCreator 1.8https://wiki.unix7.org/lib/tpl/unix7/images/favicon.icowiki.unix7.org
https://wiki.unix7.org/
Simple pseudo-monotic id generator
https://wiki.unix7.org/c/genid
c unix
Simple pseudo-monotic id generator
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#ifdef __linux__
#include <endian.h>
#else
#include <sys/endian.h>
#endif
int64_t getid(void) {
uint64_t id = 0;
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
uint64_t nsec = (uint64_t)(now.tv_sec * 1000000) + (uint64_t)now.tv_nsec;
uint64_t r = (uint64_t)(rand() % 0xFFFF);
id …anonymous@undisclosed.example.com (Anonymous)Fri, 27 Jan 2023 11:19:58 +0000Argument string analyzer
https://wiki.unix7.org/c/getlex
c unix
Argument string analyzer
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define RES_OK 0
#define RES_ERR -1
typedef struct {
size_t rsize;
size_t wsize;
size_t capa;
uint8_t* data;
} bstream_t;
#define STREAM_INITCAPA 64
int bstream_init(bstream_t * stream) {
stream->data = malloc(STREAM_INITCAPA);
stream->wsize = 0;
stre…anonymous@undisclosed.example.com (Anonymous)Tue, 14 Feb 2023 14:04:34 +0000Getopt but better
https://wiki.unix7.org/c/getopt
c unix
Getopt but better
Основание: меня еще 10 лет назад утомили мелкие глюки и разное поведение различных libc getopt. =)
Note: не все проверки сделаны
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
static bool isprefixed(char* str, char* prefix) {
if ((str == NULL) || (prefix == NULL)) {
return false;
}
if (strncmp(str, prefix, strlen(prefix)) == 0) {
…anonymous@undisclosed.example.com (Anonymous)Wed, 11 Jan 2023 08:48:27 +0000Go-like channel on ring buffer
https://wiki.unix7.org/c/go-channel
c go
Go-like channel on ring buffer
Behavior:
* If the channel is full, channel_write() returns -1
* Calling channel_read() results in waiting for an element
More complex behavior (wait with timeout, non-blocking read) is easily implemented.
Поведение:anonymous@undisclosed.example.com (Anonymous)Sat, 31 Dec 2022 09:23:48 +0000Corountine in Golang: Origin
https://wiki.unix7.org/c/go-origin
c go
Corountine in Golang: Origin
This is an example of implementation of cooperative multitasking on a single thread.
I used library makecontext() and company.
Это пример реализации кооперативной многозадачности на одном потоке.anonymous@undisclosed.example.com (Anonymous)Thu, 02 Jul 2026 20:15:11 +0000Go-like dynamic array
https://wiki.unix7.org/c/go-slice
Go-like dynamic array
“Go-like”, хаха =) На самом деле алгоритм динамического массива используеться повсеместно лет 50, наверное. Поведение массива можно адаптировать под свои цели.anonymous@undisclosed.example.com (Anonymous)Wed, 04 Jan 2023 07:44:57 +0000Go-like channel on unlimited queue
https://wiki.unix7.org/c/go-uchannel
c go
Go-like channel on unlimited queue
Note: the code w/o some check of return values.
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <semaphore.h>
#include <pthread.h>
typedef struct cell cell_t;
struct cell {
cell_t* next;
int data;
};
cell_t* new_cell(int data) {
cell_t* cell = malloc(sizeof(cell_t));
cell->data = data;
cell->next = NULL;
return cell;
}
void cell_free(cell_t* …anonymous@undisclosed.example.com (Anonymous)Sat, 31 Dec 2022 09:24:05 +0000Embeded key-value database
https://wiki.unix7.org/c/hwkeyval
Embeded key-value database
Пример key-value хранилища для встраиваемых систем. Например, для хранения конфигурации.
Рассчитана на малый объем используемой оперативной памяти.
Кроме дескриптора базы данных, все записи хранятся в устройстве периферийной памяти. Естественно, это увеличивает время доступа. Для меньшего времени доступа возможно использовать верхний слой-надстройку в оперативной памяти.…anonymous@undisclosed.example.com (Anonymous)Mon, 09 Jan 2023 08:25:01 +0000Lexical and grammatical analyzer
https://wiki.unix7.org/c/lexer01
c unix
Lexical and grammatical analyzer
A simple and grammatical analyzer for simple syntax:
?? key = val # comment ??
Starts with the UNDEF context and ends with the ENDFL context. With more contexts it might make more sense to write a binary array of context transitions by event/characters.anonymous@undisclosed.example.com (Anonymous)Tue, 14 Feb 2023 14:05:54 +0000Map of values
https://wiki.unix7.org/c/mapper
c unix
Map of values
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
static char* copystr(char* src) {
size_t srcsize = strlen(src) + 1;
char* dest = malloc(srcsize);
memset(dest, '\0', srcsize);
strcpy(dest, src);
return dest;
}
typedef struct {
char* name;
int type;
void* vptr;
} mlink_t;
#define MAPPER_STRING 1
#define MAPPER_INTEGER 2
#define MAPPER_B…anonymous@undisclosed.example.com (Anonymous)Mon, 06 Feb 2023 21:11:26 +0000Cancel thread & waiting with semaphores
https://wiki.unix7.org/c/thread-cancel
c go
Cancel thread & waiting with semaphores
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h>
#include <signal.h>
#include <stdatomic.h>
typedef struct {
int ident;
char* message;
sem_t execsem;
sem_t exitsem;
sem_t donesem;
atomic_int doexit;
pthread_t thread;
} worker_t;
vo…anonymous@undisclosed.example.com (Anonymous)Fri, 30 Dec 2022 08:12:37 +0000Sync detached thread with semaphore
https://wiki.unix7.org/c/thread-sync
Sync detached thread with semaphore
Another decision is waitgroup.
Note: the code w/o check of return values.
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h>
typedef struct {
sem_t sem;
int ident;
int state;
} hello_t;
hello_t* new_hello(int ident) {
hello_t* hello = malloc(sizeof(hello_t));
hello->ident = ident;
…anonymous@undisclosed.example.com (Anonymous)Fri, 30 Dec 2022 05:06:05 +0000Go-like waitgroup
https://wiki.unix7.org/c/waitgroup
c go
Go-like waitgroup
Note: w/o check of return values.
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
typedef struct {
sem_t sem;
atomic_int num;
} wg_t;
wg_t* new_gw() {
wg_t* wg = malloc(sizeof(wg_t));
wg->num = 0;
sem_init(&(wg->sem), 1, 0);
return wg;
}
void wg_add(wg_t* wg) {
wg->num++;
}
void wg_done(wg_t* w…anonymous@undisclosed.example.com (Anonymous)Fri, 30 Dec 2022 05:41:33 +0000Thread pool with unlimited channel
https://wiki.unix7.org/c/worker-pool
Thread pool with unlimited channel
Пул работников с одной общей “неограниченной” очередью, мягкой остановкой, ожиданием остановки всей группы.
Note: the code w/o some check of return values.anonymous@undisclosed.example.com (Anonymous)Mon, 09 Jan 2023 09:57:45 +0000