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: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);
}
@mortymacs
mortymacs / sample.go
Created August 18, 2024 09:09
Go decorator pattern
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type IFeature interface {
GetPrice() int
}
@mortymacs
mortymacs / sample.go
Created August 13, 2024 14:21
BBolt sample in Go
package main
import (
"fmt"
"log"
"go.etcd.io/bbolt"
)
func main() {
add_newline = false
format = "$sudo$username[](bg:#D81E5B fg:#82264f)$directory[](bg:#E23E58 fg:#D81E5B)$git_branch$git_commit$git_state$git_metrics[](fg:#E23E58 bg:#0E131F)$fill[](bg:#0E131F fg:#18212b)$nix_shell[](bg:#18212b fg:#2e294e)$status[](bg:#2e294e fg:#633359)$cmd_duration[](bg:#633359 fg:#973c64)$jobs[](bg:#973c64 fg:#E23E58)$git_status$line_break$character"
[character]
error_symbol = "[ ](red)"
format = "$symbol"
success_symbol = "[ ](purple)"
vimcmd_symbol = "[ ](green)"
[cmd_duration]
@mortymacs
mortymacs / a.c
Created April 11, 2024 16:45
Release heap memory when it goes out of scope in C
// gcc a.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void clean_name(char **name) {
printf("clean clean!");
free(*name);
}