segunda-feira, 4 de abril de 2011

Pickshit game

Pickshit is a pointless game where you need to clean the shits that apear
around. Isn't actually game because there is no objective to complete. It's just an exercise in games developing know as pick stick.

The "game" was develop using allegro library so you need to have it installed to compile the game. I'm working on a better prototype.

You can download the game code/files here, and compile using this:
$ gcc -o pickshit pickshit.c -lalleg

Controls:
Arrows to move
ESC to exit

screenshot:




See you...

Cheers

terça-feira, 8 de março de 2011

Boot hello world

; Tell the compiler that this is the 0 address
[ORG 0] 
    jmp 07C0h:start   ; Jump to start

    msg db "Hello boot world ", 2, 0 ; My string
         ; the 2 byte should provide some fun

start: 

    ; Setting up the segment registers
    
    mov ax, cs
    mov ds, ax
    mov es, ax
    
    
    ; Writing the string 

    mov si, msg
print:    lodsb
    cmp al, 0
    je hang
    mov ah, 0Eh
    mov bx, 7
    int 010h    ; BIOS video service interrupt
    
    jmp print
    

hang:     
    jmp hang


    times 510-($-$$) db 0
    dw 0AA55h

.

I saw this here, so is not my merit..

to run:
copy this to a .asm file, e.g. b.asm, then assemble it with
$ nasm -o b.bin b.asm
and finaly write it to some pendrive with
dd if=b.bin of=/dev/YOURPENDRIVE
e.g $ dd if=b.bin of=/dev/sdc
Boot the pendrive and you should get a funny message on screen =)
if you have qemu installed you can boot directly the binary
$ qemu -hda b.bin

domingo, 9 de janeiro de 2011

Revised tree

I have to make some changes in tree.c and tree.h, so del_branch
can be implemented easily!

Code:

tree.h
#ifndef GKO_TREE_H
#define GKO_TREE_H

#include "stack.h"
#include "common.h"

struct branch {
    char *name;
    char *data;
    struct branch *cont;
    struct branch *next;
} branch;
    

void init_tree (struct branch *,  char *,  char *) ;
void set_branch (struct branch *,  char *,  char *);
struct branch * get_branch (struct branch *,  char *);
void del_branch (struct branch *);

#endif


tree.c
#include "tree.h"

/* Initialize a Tree, set the root node */
void
init_tree (struct branch *t, char *name, char *data) 
{
    cpystr (&t->name, name);
    cpystr (&t->data, data);
    t->cont = NULL;
    t->next = NULL;
}

/* Create a new struct branch or edit an existent one */
void
set_branch (struct branch *t, char *name, char *data)
{
    struct branch *b;
    struct branch *prior;

    if (!t || !name || !data)
        pexit ("set_branch: NULL argument call\n");

    for (b = t->cont, prior = NULL; b; prior = b, b = b->next) {
        if (! strcmp (b->name, name)) {
            free (b->data);
            cpystr (&b->data, data);
            return;
        }
    }

    b = xmalloc (sizeof (struct branch));
    init_tree (b, name, data);
    if (!prior) /* if there is no content in branch *t, the prior remains NULL */
        t->cont = b;
    else
        prior->next = b;
}

/* Return the address of a struct branch */
struct branch *
get_branch (struct branch *t, char *name)
{
    struct branch *b;
    
    if (!t || !name)
        pexit ("get_brach: NULL argument call\n");

    for (b = t->cont; b; b = b->next)
        if (! strcmp (b->name, name))
            return b;
    
    return NULL;
}


/* Delete a struct branch an everything below that node */
void
del_branch (struct branch *t)
{
    if (t->cont) 
        del_branch (t->cont);

    if  (t->next)
        del_branch (t->next);
    
    free (t->name);
    free (t->data);
    free (t);
}


common.c
#include "common.h"

void
pexit (const char *msg)
{
    perror (msg);
    exit (EXIT_FAILURE);
} 
    
void *
xmalloc (size_t siz)
{
    void *n = malloc (siz);
    if (!n) 
        pexit ("malloc");
    return n;
}

void
cpystr (char **dst, char *src)
{
    int len = strlen (src) + 1;
    *dst = xmalloc (len);
    strncpy (*dst, src, len); 
}


test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tree.h"

main ()
{
    struct branch *myt = xmalloc (sizeof (struct branch));
    struct branch *buf;

    init_tree (myt, "root", "root_data");
    set_branch (myt, "etc", "etc_data");
    set_branch (myt, "usr", "usr_data");
    buf = get_branch (myt, "etc");
    set_branch (buf, "rc.conf", "rc.conf data");
    set_branch (myt, "etc", "etc_edited_data");

    del_branch (myt);
    return 0;
}

sexta-feira, 7 de janeiro de 2011

Middleware manager..

Yeap, I got a job. Intern on middleware management area.. I really like it.. I have
close contact with commercial aplications and linux/unix environments... and the warm/pressure of the businesses.. There are nice guys there.. some times looks like an arena.. but with little fat funny guys instead of big muscle losers.. :-) 

by the way.. an tree prototype...


tree.h
#ifndef GKO_TREE_H
#define GKO_TREE_H

#include "stack.h"
#include "ll.h"
#include "common.h"

typedef struct branch {
    char *name;
    char *data;
    ll cont;
    struct branch *next;
} branch;
    
void init_tree (branch *,  char *,  char *) ;
void set_branch (branch *,  char *,  char *);
branch *get_branch (branch *, char *);

#endif


tree.c
#include "tree.h"

/* Initialize a Tree, set the root node */
void
init_tree (branch *t, char *name, char *data) 
{
    cpystr (&t->name, name);
    cpystr (&t->data, data);
    init_ll (&t->cont);
    t->next = NULL;
}

/* Create a new branch or edit an existent one */
void
set_branch (branch *t, char *name, char *data)
{
    lln **node = search_lln (&t->cont, name);
    branch *b;

    if (node) { /* edit branch */
        b = node[0]->data; /* the address of found branch */
        free (b->data);
        cpystr (&b->data, data);
    } else { /* create a brand new branch */
        b = xmalloc (sizeof (branch));
        init_tree (b, name, data);
    }
    
    add_lln (&t->cont, name, b); /* tree contains branch */
}

/* Return the address of a branch */
branch *
get_branch (branch *t, char *name)
{
    lln **node = search_lln (&t->cont, name);
    if (node)
        return node[0]->data; /* return void * don't need casts */
    return NULL;
}

/* Delete a branch an everything below that node */
/* @TODO */
void
del_branch ()
{
}



driver
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tree.h"

main ()
{
    branch myt;
    branch *buf;

    init_tree (&myt, "root", "root_data");
    set_branch (&myt, "etc", "etc_data");
    set_branch (&myt, "usr", "usr_data");

    buf = get_branch (&myt, "etc");
    printf ("Searched %s\n", buf->data);
    
    set_branch (buf, "rc.conf", "rc.conf data");
    buf = get_branch (buf, "rc.conf");
    printf ("Searched %s\n", buf->data);

    buf = get_branch (&myt, "usr");
    printf ("User data? %s\n", buf->data); 

    set_branch (&myt, "etc", "etc_edited_data");
    buf = get_branch (&myt, "etc");
    printf ("Searched %s\n", buf->data);

    return 0;
}


MAYBE I work more on this..

segunda-feira, 22 de novembro de 2010

Prototype maker 2.0

There is no patter alignment anymore .. !

# Create the prototypes for all functions
# on a given C source file
use strict;
use warnings;

my @ctypes = qw(
    void
    unsigned 
    signed 
    long 
    short 
    int 
    float 
    double 
    struct 
    char
    static
    const
);

my @contents = <>;

for my $line (0 .. $#contents) {
    my $lref = \$contents[$line];
    my $prot = '';
    for my $type (@ctypes) {
        if ($$lref =~ /^$type/) {
             while ($contents[$line] !~ /\{/) { # seek for {
                $prot .= "$contents[$line++] ";
            }
            chop $prot;            # the last space
            $prot =~ s/\n//go;     # remove LFs 
            $prot =~ s/\w*,/, /go; # remove argument intentifiers
            $prot =~ s/\w*\)/)/go; # remove the last argument 
            $prot .= ';';           # append a semicolon 
            print $prot, "\n";
            next;
        }
    }
}

sábado, 20 de novembro de 2010

Perl script to generate basic prototypes..

Typedefs are not supported and you need to code your
functions with follow patter:
type
function_indetifier (list_of_parameters)
{ ... hereafter makes no difference

usage: cat souce.c | perl prot.pl > prots.txt
then copy and paste where you want...




# Create the prototypes for all functions
# on a given C source file
use strict;
use warnings;

my @ctypes = qw(
    unsigned 
    signed 
    long 
    short 
    int 
    float 
    double 
    struct 
    char
    static
    const
);

my @contents = <>;

for my $line (0 .. $#contents) {
    my $lref = \$contents[$line];
    my $prot = '';
    for (@ctypes) {
        if ($$lref =~ /^$_/) {    
            my $func = $contents[++$line];
            $func =~ s/\w*,/,/g;
            $func =~ s/\w*\)/)/g;
            $prot = "$$lref $func;";
            $prot =~ s/\n//g;
            print "$prot\n";
            next;
        }
    }
}
           
test: http://ideone.com/5tE0F
note: I will coment that hash implementation soon..

quarta-feira, 10 de novembro de 2010

Mini web server in perl (really small han?)

use strict;
use warnings;
use LWP;
use HTTP::Daemon;

# the parameters are 
# the user/group user to restrict permissions, the port (default 80)
# the local addres (default locahost)
# respectivaly
my ($userg, $port, $laddres) = @ARGV;
$userg or die 'You need to suply a nonroot user/group as first parameter'; 
my ($user, $group) = split ('/', $userg);

$port = 80 unless $port;
$laddres = 'localhost' unless $laddres;

my $server = new HTTP::Daemon (
    LocalAddr => $laddres,
    LocalPort => $port,
    Listen => 5,
    Reuse => 1,
) || die $!;

$> = getpwnam $user || die $!; 
$) = getgrnam $group || die $!; 


while (my $con = $server->accept()) {
    my $pid = fork();
    if (!$pid) { 
        my $req = $con->get_request();
        print $req->as_string();
        my $file = $req->url();
        $file =~ s/^.//g;    
        $con->send_file_response($file);
    } elsif ($pid) { next;  
    } else { print STDERR "fork: $!\n" };
}