Multicall Binary

Jika anda pernah menggunakan busybox, pasti anda paham apa itu multicall binary. Busybox adalah contoh dari multicall binary, mencakup hampir semua perintah dasar shell. Definisi kasarnya adalah sebuah binary yang dapat dipanggil dengan berbagai nama. Hal ini dapat dilakukan dengan memanipulasi argv[0] yang mana menunjuk pada program name. Misalkan:

int main(int argc, char **argv)
{
        printf("nama program: %s\n", argv[0]);
        return 0;
}

Kemudian kita panggil binary dari kode di atas:

./a.out
nama program: ./a.out

Sama halnya dengan:

ln -s a.out app
./app
nama program: ./app

Saya mencoba untuk membuat sebuah multicall binary sederhana dengan konsep di atas, yaitu dengan memanipulasi argv[0]. Setiap kali ditemukan argv[0] yang sesuai maka fungsi bersangkutan akan dipanggil. Untuk melakukannya kita gunakan array yang berisi pointer to function yang nantinya dipanggil menyesuaikan dengan isi argv[0].

/* Applet struct */
struct applet {
        int id;
        char *name;
        void (*app) (void);
};
/* Array of pointer to function */
static void (*applet_callback[]) (void) = { applet0, applet1, applet2 };

/* Array of applet name */
const char *applets[] = { "applet0", "applet1", "applet2" };

Tinggal kita terapkan apakah isi argv[0] sama dengan salah satu dari isi array applets[], dapatkan indexnya untuk mengisi applet->app dengan applet_callback[index]. Jangan lupa implementasikan masing2 fungsi dalam applet_callback[].

if (strcmp(applets[asize], foo->name) == 0) {
       appid = asize;
       foo->app = applet_callback[asize];
       return appid;
}

Tinggal tambahkan perintah untuk menjalankan:

       foo->app();

Untuk mengetesnya:

gcc applet.c -o applet
ln -s applet applet0
ln -s applet applet1
ln -s applet applet2
./applet2
calling: ./applet2
get applet2 at 2
Applet2 is running!

Kode tersedia di:
HTTP: https://github.com/ruckuus/multi
Git: git://github.com/ruckuus/multi.git

Advertisements

Lindent – amazing script for lazy C coder

For you a lazy C coder, sometimes indentation could be a big mess when you only think about code’s performance and not so into a cosmetic and style. Anyway, I was in that time too, I never thought about the importance of style when I wrote a code until I have to review someone’s code. It’s crappy and PITA, it ended up by me writing the whole code from scratch.

Thanks to kernel source script: Lindent now that cosmetic stuff is over, you can just write the code your way, and in the end call this script to make your code better and readable, and the most important is: it conforms Linux CodingStyle ;-).

Here we go, you can get it from Linux source in script directory: linux-source/scripts/Lindent. Or you can make a leftover from the snippet below:

1#!/bin/sh
   2PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
   3RES=`indent --version`
   4V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
   5V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
   6V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
   7if [ $V1 -gt 2 ]; then
   8  PARAM="$PARAM -il0"
   9elif [ $V1 -eq 2 ]; then
  10  if [ $V2 -gt 2 ]; then
  11    PARAM="$PARAM -il0";
  12  elif [ $V2 -eq 2 ]; then
  13    if [ $V3 -ge 10 ]; then
  14      PARAM="$PARAM -il0"
  15    fi
  16  fi
  17fi
  18indent $PARAM "$@"
  19

You can use it as follow:

$ /path/to/Lindent source.c

Consider following images:

1. before

2. After

Lindent uses the features of GNU indent. A tool to change the appearance of a C program by inserting or deleting whitespace.

Vim – entering special character

It’s been a question for me for years, but usually I ignore it. Sometimes, I want to put a special character on my code, for example; a copyright symbol (©) or registered sign (®) or a latin character, or other. Actually it is quiet simple to do using Vim.

First thing to know is RFC1345: Character Mnemonics and Character Sets. You need to know the character mnemonic, then how to put it on your code. Vim provides :digraph. Simply open Vim and type :digraph, you should find a table of special characters which are available in Vim. There are several way of using :digraph, in INSERT mode you can use CTRL+K followed by _two letter combination_, or :set digraph (:set dg), or by entering character value after pressing CTRL+V.

Consider this image:

Vim :digraph

There are three columns described as follow:

1. magenta block (#1): SH, D1, Nb, NH, TS ….

2. green block (#2): ^A, ^Q, #, , , …

3. orange block (#3): 1, 17, 35, 131, 147 …

The first column (#1) represent two letter combination to show the special character in digraph after pressing CTRL+K

The second column (#2) is the special character you want to type

The third columnd (#3) is the character’s decimal value based on RFC1345

See what are the value you need related with REGISTERED SIGN from :digraph extract

Rg  ® 174

So, you can proceed by:

1. CTRL+K Rg

2. CTRL+V 174

3. R <BS> g

For option #3, this can only work when you :set digraph (or :set dg)

<BS> is CTRL+H in Vim

For complete reference, you can see Vim: digraph documentation

PECL and twitter oauth

Currently, I am trying to make batbet to be able to communicate with twitter API. Twitter uses oauth as authentication method. The first thing comes in mind is to use liboauth for C (available here). In fact I am not so familiar and I am studying how twitter’s oauth works. I got clue from PECL sample (available here).

So, the first thing to do is how to make PECL works then port it to C. Why C? because this is the only language I know 😛

How to install PECL oauth extension?

$ pecl search oauth

Retrieving data…0%Matched packages, channel pecl.php.net:
=======================================
Package Stable/(Latest) Local
oauth   0.99.9 (beta)   0.99.9 oauth consumer extension

then

$ sudo pecl install oauth-0.99.9

Now PECL oauth should be installed to your machine.

or you can also build manually the extension using phpize.

download oauth-0.99.9, extract then go inside oauth directory, invoke phpize

oauth-0.99.9 $ phpize

oauth-0.99.9 $ ./configure; make

oauth-0.99.9 $ sudo make install

To make your new extension works you should adjust your php.ini. On ubuntu there is /etc/php5/cli/php.ini then add following:

extension=oauth.so

Once you’re done, go to twitter example of oauth PECL package.

$ php updateStatus.php

you’ll see …

I was wondering how does the twitter authentication method go, from the beginning to the end. I might be wrong but it works for me, following is the workflow:

1. After you get consumer key and consumer secret, you need to request_token(). For instance, using PECL liboauth:

$request_token_info = $oauth->getRequestToken(https://twitter.com/oauth/request_token);

then you will get “oauth_token” reply.

2. You need to ask authorization from user, by giving a link to:

https://twitter.com/oauth/authorize?oauth_token=oauth_token -> you get this on the step #1

User will have to allow the access.

3. Right after authorization is granted by user, you need to request a new oauth token by accessing:

https://twitter/oauth/access_token

You will get oauth_token and oauth_token_secret to be used to call twitter REST API. This oauth_token and oauth_token_secret is persistent. I save these variables to a plaintext to be used as future request, so user does not need to do step #2 anymore.

4. Use Twitter REST API to do anything you want.

NOTE: I am open to any suggestion.