Skip to content

Instantly share code, notes, and snippets.

View lesovsky's full-sized avatar
😉
Don't stop

Lesovsky Alexey lesovsky

😉
Don't stop
  • Postgres Pro
  • Yekaterinburg, Russian Federation
View GitHub Profile
@lesovsky
lesovsky / ncurses-get-key-code.c
Last active April 8, 2022 03:28
Get key code with ncurses.
#include <ncurses.h>
int main()
{
initscr();
keypad(stdscr, true); //Включаем режим чтения функциональных клавиш
noecho(); //Выключаем отображение вводимых символов, нужно для getch()
halfdelay(100); //Устанавливаем ограничение по времени ожидания getch() в 10 сек
printw("Press F2 to exit.\n");
@lesovsky
lesovsky / flags-and-bitwise-operations.c
Created July 25, 2015 19:51
Use composite set of flags with bitwise operations.
#include <stdio.h>
#include <string.h>
#define DO_ACTIVE 1 << 0
#define DO_IDLE 1 << 1
#define DO_IDLE_IN_XACT 1 << 2
#define DO_WAITING 1 << 3
#define DO_OTHER 1 << 4
int main(void)
@lesovsky
lesovsky / traffic-shaper.sh
Created July 27, 2015 18:19
Destination based traffic limiting with tc.
# add
# avpkt 1500 - MTU, bandwidth 1000mbit - interface speed, rate 20mbit - limit threshold
tc qdisc add dev eth1 root handle 1: cbq avpkt 1500 bandwidth 1000mbit
tc class add dev eth1 parent 1: classid 1:1 cbq rate 20mbit allot 1500 prio 5 bounded isolated
tc filter add dev eth1 parent 1: protocol ip prio 16 u32 match ip dst 10.0.0.33 flowid 1:1
# remove
tc filter del dev eth1 parent 1: protocol ip prio 16 u32 match ip dst 10.0.0.33 flowid 1:1
tc class del dev eth1 parent 1: classid 1:1 cbq rate 20mbit allot 1500 prio 5 bounded isolated
tc qdisc del dev eth1 root handle 1: cbq avpkt 1500 bandwidth 1000mbit
@lesovsky
lesovsky / signal-handling.c
Created July 30, 2015 10:54
Simple SIGINT handling.
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void sig_handler(int signo)
{
if (signo == SIGINT) {
printf("received SIGINT\n");
exit(0);
@lesovsky
lesovsky / substring-replace.c
Created August 1, 2015 09:58
Replace substring.
/**
****************************************************|
* String replace Program |
****************************************************|
* Takes three string input from the user
* Replaces all the occurances of the second string
* with the third string from the first string
* @author Swashata -- http://www.intechgrity.com/c-program-replacing-a-substring-from-a-string/
*/
@lesovsky
lesovsky / simple-tail.c
Last active August 29, 2015 14:26
Simple tail
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#define BUFSIZE 8192
@lesovsky
lesovsky / find-orphaned-temp-tables.sql
Last active September 15, 2015 14:28
Find orphaned temp tables.
SELECT n.nspname as "Schema",
c.relname as "Name",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner" FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','') AND c.relpersistence = 't'
ORDER BY 1,2;
@lesovsky
lesovsky / read-meminfo.c
Created October 20, 2015 10:57
Read /proc/meminfo
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *fp;
fp = fopen("/proc/meminfo", "r");
char buffer[121];
char key[80];
long int value;
@lesovsky
lesovsky / brokenconns.c
Last active October 29, 2015 08:27
Create broken conns to PostgreSQL.
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include "libpq-fe.h"
/* gcc -std=gnu99 -pedantic -I/usr/pgsql-9.4/include -L/usr/pgsql-9.4/lib -o broken-conns broken-conns.c -lpq */
#define CONNINFO "host = 127.0.0.1 port = 5433 dbname = hldemo user = postgres"
@lesovsky
lesovsky / simple-iostat.c
Created November 23, 2015 09:07
Simple iostat based on /proc/diskstats
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <ncurse