Saturday, August 22, 2009

A simple example of strtok

Tokenizing a string is always a cumbersome process and most of times results in a buggy code.
Here by presents a very simple example of strtok,


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

int main() {

char *token;
char *lasts;
char buf[50] ;

strcpy(buf,"3/4/5/**6*7");
printf("\ntokenizing %s with strtok_r():\n", buf);

if ((token = strtok_r(buf, "*/", &lasts)) != NULL) {

printf("token = %s\n", token);
while ((token = strtok_r(NULL, "*/", &lasts)) != NULL) {
printf("token = %s\n", token);

}
}
}

Output:

tokenizing 3/4/5/**6*7 with strtok_r()

token = 3

token = 4


token = 5


token = 6


token = 7

The
strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.

Friday, August 14, 2009

Recover deleted data on Linux

////////////// IF YOU HAPPEN TO READ THIS ////////////////////////////////

Q: How can I recover (undelete) deleted files from my ext3 partition?

Actually, you can't! This is what one of the developers, Andreas Dilger, said about it:

In order to ensure that ext3 can safely resume an unlink after a crash, it actually zeros out the block pointers in the inode, whereas ext2 just marks these blocks as unused in the block bitmaps and marks the inode as "deleted" and leaves the block pointers alone.

Your only hope is to "grep" for parts of your files that have been deleted and hope for the best.

////////////////////////// THIS IS NOT TRUE ///////////////////////////////////

You can get your deleted data from linux machine also. I have done
$rm -rf folder/
but this program recovered the complete data back.

ext3grep: http://code.google.com/p/ext3grep/

This is an excellent tool, and more importantly it really works.
Its working instructions can be found here.

http://www.xs4all.nl/~carlo17/howto/undelete_ext3.html

Thursday, August 13, 2009

A Simple Thread Example


#include <pthread.h>
#include <stdio.h>

#define NUM_THREADS 5

void *PrintHello(void* threadid)
{
int tid;
tid = (int)threadid;

printf("#####Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];

int rc;
int t;

for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);

//rc = pthread_create(&threads[t], NULL, PrintHello, t);

if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}