Saturday, November 10, 2007

Give windows a “bash”

How many of us, who love the power of command line feel handicapped when it comes to windows, with its pathetic cmd.exe or command.exe, I was missing the auto-completion, multiple tabs, and most importantly the commands, pipes, redirection, tee, I am not sure of the windows equivalents of these, so I was looking for some way to Linux-ify my widows.

Being a TA required me to compile few softwares for windows (skyeye, softgun, arm-elf-gprof etc...), so I had to choose between MinGW and Cygwin, first I started with MinGW and found it quite inadequate, since Cygwin gives a complete Linux environment, I settled for it, but I still miss the tabbed terminals, I was searching for “tabbed command prompt” in google and found a few links but none were open source, some how I stumbled upon a blog where they have mentioned about Console2, an open source one currently under development, so I downloaded it and am pretty happy about it, now i'll give a step by step procedure how I got a almost working gnome-terminal(or Konsole) alike...

  • download and install cygwin
  • download and extract console2
  • open console2
  • press ctrl+s to bring settings dialog
  • go to tabs option, click add change title to you something you want, select cygwin.bat from CYGWIN_HOME, press ok
  • now if you want a cygwin terminal press ctrl+f2 (assuming the cygwin appears below console2)
If you dont want cygwin then you can use gnuwin32 package, and add its wbin folder to path and just use console2 with all the power of unix

Console2: http://sourceforge.net/projects/console

Blogged with Flock

Tuesday, November 6, 2007

infront or infront of back

sounds crazy right, i could not come up with a sexy title for (pre or post increment operators in c), anyways today the issue of pre/post increment operators in c, sagar and i had a good 15 minute discussion how this works so he results are here

int i=5;
printf("%d,%d,%d",++i, i, i++);
/*
output: 7,7,5
*/

how, we understood that printf evaluates from right to left, so it'll do i++ first and return '5' and update value of i i.e. 6 in stack, the middle i is just i, the first ++i, will increment value of i in stack i.e. it becomes 7, now it'll examine the value of stack finds i=7, therefore it substitutes 7 as first and second argument


int i=5;
printf("%d,%d,%d",++i, i, ++i);
/*
output: 7,7,7
*/


how, we understood that printf evaluates from right to left, so it'll do ++i in stack i.e. 6 in stack, the middle i is just i, the first ++i, will increment value of i in stack i.e. it becomes 7, now it'll examine the value of stack finds i=7, therefore it substitutes 7 as first and second argument

because in post increment operator, the assignment happens before increment, therefore in case one 5 is assigned or returned, and get incremented on stack.
fair enough understanding i think.

refer:-

Blogged with Flock

Monday, November 5, 2007

Nurses or NCurses

the first time i saw this package was during Fedora Core 1 installation, a nurses package... what a funny name or is it really for nurses, but i forgot about it.
next comes kernel compilation for Advanced OS class, where make menuconfig reports some error in ubuntu.. after some digging someone found out that we have to install libncurses-dev package for it to work, and i remembered that funny incident that happened 3 years before.

the assignment questions seemed simple
  1. Observing “Linux“ behavior“ by writing a program to use the  /proc  mechanism to inspect various kernel values
  2. Learn how to write a UNIX—style shell program
what big deal, half the program is in book complete it and finish the assignment, my "proc" will intelligently display the status of the program if you give PID, and as an unexpected move Prof SN was completely "satisfied" with our work so we have to redo it.
so i want to add interactivity to my shell , that is history, command completion etc.. , so i have to capture keystrokes, but what shit, ours is not a complete shell, so the shell that we run on didn't give us the keycodes that we expect .. after a lot of blah blah .. nurses or ncurses came to resuce, there is a getch() function will capture all keys if we set keypad(stdsrc,TRUE), done...

worked for three more days to "port".. when every thing is working .. Hmm this is not what i wanted SEGMENTATION FAULT, why the hell!!, Hmm if do chdir() there is a seg fault, i got bugged of curses so had to revert back to non-interactive shell, but i can upgrade my proc code to work as a task manager (like top command) again 2 days of work, i could implement killing a process provided you have sufficient access rights, ya i know it has lot of flaws but i believe it is a "proof of concept" , now ask me do something in ncurses, i can do it with a bit of struggle.
next is a file manager in queue .. (long live ncurses ).

food for thought: try "tpp" the ncurses based based presentation program, ultimate, so decided to torture people with "tpp"  (available in ubuntu/debian repos)

Blogged with Flock

Friday, November 2, 2007

MFC

MFC project problem statement is out...
in simple words identify a state machine which will accept the given string of alphabets (full 2 months), god only knows how to do this..        

Blogged with Flock

Friday, October 19, 2007

cookbook

have a look at http://pleac.sourceforge.net/, thinking/working hard to contribute (Java & C++)  

Blogged with Flock

Lock "Stock"

after getting bugged by the internet speed, response time of nseindia.com & bseindia.com, java in firefox and the pain in maintaining a portfolio, i decided to build my own portfolio manager, here are the specs
1. Database: Postgresql
2. Programming Languages: Python(to populate database in 15minutes interval, using ystockquote module), JSP
3. TOMCAT (Obviously)

currently i have finished the database part, i think it's working fine. i am now in the process of building the front end, so i am going to use eclipse-europa-jee for that, wish me luck.
i'll keep posting about my progress and the details

Blogged with Flock

Wednesday, October 10, 2007

How do i Flush!!

Most programs i write suffer from either stdout or stdin not getting empty when it is supposed to (when using Eclipse CDT, explicit flush id required for me to print anything).
while i can flush stdout using fflush(stdout);, i cant do the same thing with stdin, this was creating a major headache for me, and i came across a site which gave a code snippet to clear the buffer
   int ch;
  while( (ch = fgetc(fp)) != EOF && ch != '\n' )
   /* null body */;


Blogged with Flock

Where is my Pointer

being a TA, its my job to help the students when they encounter any problem in implementation or tools used for their projects, when Joseph said that  his program is receiving SIGSEGV, i thought it must be some simple problem either the index of array is not proper or memory not allocated, so i started to debug and Voila! the program works just fine giving correct output.
we all were wondering what is wrong, and eclipse(CDT) was the first target, but debugging in eclipse also worked fine, and now we decided to go manual line by line checking so few things are observed
  • they wanted a 2D array for which they know that the column size is two and row size should be dynamically allocated
  • Array of pointers were used
so i said to them, "fine, but i think we should convert those array of pointers to double pointers", and they agreed, so the next run gave the same SIGSEGV error.
now its time for more digging, so i prefer to user -Wall option to clean and make my code perfect, there i noticed one warning, function returns reference to local pointer, voila got it... so changed the function a bit, instead of returning a pointer we passed a pointer to the function and worked on it, and the result SUCCESS.
it shouldn't have taken much time for me to find out, being an OS student, we always deal with functions(system calls) which take pointer as an argument instead of returning them, so there must be something wrong/bad in returning the reference to local pointer, the memory gets lost!!!. but why not in debugging, the only reason that came to my mind is when debugging the memory areas used by the program is locked till it is freed or the program ends.

Blogged with Flock

Wednesday, October 3, 2007

SciTE, Compile and Run

Lately SciTE is becoming my favorite text editor, (VIm rules always), i find the menu options Tools->Compile,Tools -> Go useful for beginners who are new to Linux/Programming,  but i found that even though it compiles properly, it does not execute it(i.e. run  ), Why?

few minutes of digging gave me these results
First: umask for files is 022, i.e. files created by user will be read/write for the user and read only for group and others
Second: compilers will know that they are creating executables and set executable bit when they produce .o/a.out files
Third: if "." is in your path, then the executables are searched in current directory as well

Now the solution:
SciTE uses gcc -c <filename.c> -o filename.o to compile a file, and filename.o to execute, i got the error "bash - access denied", i opened a terminal and tried to execute it by myself, still an error,
why?
when we compile with -c filename.c, we ask the compiler to just compile and don't link it, i.e. it creates an object file and not executable object file, so if you run, it'll produce the access denied error message.
so open cpp properties and remove that -c option, add "./" before executable i.e. ./filename.o

Now the more interesting points:
  1. SciTE reads config files when it loads, so any change you make you'll have to restart it
  2. also all but user config files should be edited as root
Disclaimer:- i dont know the impact of removing -c option, it might be useful in large or multi file programs, so please consult your manual for exact details.

Blogged with Flock

Stupid Me, System.currentTimeMills()

for(int j=0;j<100;j++)
{
    java.util.Random rnd = new java.util.Random(System.currentTimeMillis());
    for(int i=0;i<100;i++)
    {
        System.out.print(rnd.nextInt(10)+"");
    }
    System.out.println();
}

please don't expect the above program to give 100 different sets of 100 random numbers, there will be repetitions like anything. why?
First: for a particular seed a Random number generator should give same set of numbers, when ever it is run,
Second: our computers are fast enough to finish execution in micro seconds

so the generation of 100 numbers takes few micro seconds and the System.currentTimeMills() has resolution in milliseconds so, a week of stupid searching and debugging, here is a post.

how to fix it:-
use new Random(); forget about seed, this produces 100 different sets of 100 random numbers.

Blogged with Flock

Monday, October 1, 2007

Hello world!

class HelloWorld {
public static void main(String[] a)
{
System.out.println("this is my new blog, here i'll be posting things that i encounter while dealing with computers and programming. ");
}
}