User Tools

Site Tools


c:start

Programming

Some things I do. Little snippets of peoples homework and such are here: http://scottn.us/code/

awk

I've been messing with awk a lot lately. Some is in the code directory, but the big one is my twitter client. odd, huh? :)

irc bot

lot of stuffs to learn in that. http://scottn.us/bot.php

Modules example

Makefile
all: main module

main: main.c
        $(CC) $(CFLAGS) -o main main.c -ldl

module: module.c
        $(CC) $(CFLAGS) -shared -o module.o module.c

clean:
        -rm -f main module.o
common.h
struct s_cmd {
        char *text;
        void (*func)();
};
main.c
#include <stdio.h>
#include <dlfcn.h>
 
#include "common.h"
 
int main(int argc, char **argv)
{
        void *dll;
        struct s_cmd *c;
        void (*func)();
 
        dll = dlopen("./module.o", RTLD_LAZY);
        if (!dll) {
                fputs(dlerror(), stderr);
                return -1;
        }
 
        c = dlsym(dll, "__exports");
 
        if (!c) {
                fputs("No __exports?\n", stderr);
                return -1;
        }
 
        while (c->text)
        {
                printf("my_command = [%s]\n", c->text);
 
                c->func();
                c++;
        }
        return 0;
}
module.c
#include <stdio.h>
#include "common.h"
 
void cmd_test();
void cmd_again();
 
struct s_cmd __exports[] = {
        { "test", &cmd_test },
        { "again", &cmd_again },
        NULL
};
 
void cmd_test()
{
        printf("This is test1.\n");
}
 
void cmd_again()
{
        printf("And this is another test\n");
}
c/start.txt · Last modified: 2023/11/04 22:30 by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki