Skip to content

Instantly share code, notes, and snippets.

View mortymacs's full-sized avatar

Morteza NourelahiAlamdari mortymacs

View GitHub Profile
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:12
Sample Unittest in C
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
static void db_connection(void **state)
{
(void) state;
}
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:09
C Pointer playground
#include <stdio.h>
#include <stdlib.h>
int *x()
{
int* a = malloc(sizeof(int));
*a = 19;
return a;
}
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:06
Check file in C
#include <unistd.h>
#include <stdio.h>
int main()
{
if(access("a.sh", F_OK) != -1)
{
printf("Exists!\n");
}
else
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:04
Dictionary by Glib in C
//gcc dict.c `pkg-config --libs --cflags glib-2.0`
#include <glib.h>
#include <stdio.h>
int main()
{
GHashTable* hash = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(hash, "name", "Mort");
g_hash_table_insert(hash, "email", "hello@localhost");
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:03
Get GNU/Linux user group in C
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
struct passwd *pwd;
struct group *grp;
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:02
Temp file in C
#include <stdio.h>
int main()
{
char b[255];
FILE *f;
f = tmpfile();
fputs("hello mr dj", f);
@mortymacs
mortymacs / main.c
Created August 25, 2024 07:59
Get ENV in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *r = getenv("JAVA_HOME");
printf("java home: %s\n", r);
}
@mortymacs
mortymacs / main.c
Created August 25, 2024 07:58
Read dir in C
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
int main()
{
struct dirent *entry;
DIR *dir;
dir = opendir("/home/user1/");
@mortymacs
mortymacs / main.c
Created August 25, 2024 07:57
memcached sample C code
//compile: gcc a.c -lmemcached
#include <libmemcached/memcached.h>
#include <stdio.h>
#include <string.h>
int main()
{
memcached_server_st *servers = NULL;
memcached_st *memc;
memcached_return rc;
@mortymacs
mortymacs / main.c
Created August 22, 2024 21:19
C - How to check nonnull parameter during compile time
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void action(char *a, char *b) __attribute__((nonnull(2)));
void action(char *a, char *b) {
if (a != NULL) {
printf("%s\n", a);
}