Saturday, April 27, 2013

'\r' vs '\n'


I've been confused by the difference between \r and \n for a long time. Now I found a really wonderful answer from Stackoverflow!

In terms of ascii code, it's 3 -- since they're 10 and 13 respectively;-).
But seriously, there are many:
  • in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special
  • as a consequence, in C and most languages that somehow copy it (even remotely), \n is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)
  • in old Mac systems (pre-OS X), \r was the code for end-of-line instead
  • in Windows (and many old OSs), the code for end of line is 2 characters, `\r\n', in this order
  • as a (surprising;-) consequence (harking back to OSs much older than Windows),\r\n is the standard line-termination for text formats on the Internet
  • for electromechanical teletype-like "terminals", \r commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), \n commands the roller to roll up one line (a much faster operation) -- that's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards!-)
  • for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller;-)
In practice, in the modern context of writing to a text file, you should always use \n(the underlying runtime will translate that if you're on a weird OS, e.g., Windows;-). The only reason to use \r is if you're writing to a character terminal (or more likely a "console window" emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy "ascii animation" effects of e.g. progress bars) -- this is getting pretty obsolete in a world of GUIs, though;-).


PS: when I coded in Xinu, things work like this.
/***Without '\r'*****************/
for (int i = 0; i < 3; i++)
     kprintf ("Hello World\n");
/*******************************/
Then the output is:
Hello World
     Hello World
          Hello World

/*******With '\r'******************/
for (int i = 0; i < 3; i++)
     kprintf ("Hello World\r\n");
/*********************************/

Output is:
Hello World
Hello World
Hello World

So I guess we can understand like this. '\n' moves the cursor to the next line. '\r' moves cursor to the leftmost of the current line.

Wednesday, March 20, 2013

strace in Linux

I ran into a interesting blog about strace.

http://timetobleed.com/hello-world/

time command in Linux

When working on cs252 lab, I was confused by the meaning of the output of time command. By searching the web, something useful was found.

http://www.dirac.org/linux/time/

To summarize,

Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processed and time the process spends blocked (for example if it is waiting for I/O to complete).

User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CUP time used by the process.

The equation is
user + Sys will tell you how much actual CPU time your process used.

Saturday, March 2, 2013

Tutorial for regular expression

Find a very good tutorial for regular expression!

http://www.regular-expressions.info/quickstart.html

This is a quick start for regular expression. Within this article, there is also a detailed version.

Write script to login a remote machine

As a cs student, I have remotely login many times every day. The name of the remote machine is pretty long and my password is even longer. So I decided to do something.

Firstly, I wrote a scipt like this


#!/bin/bash
ssh username@machinename

Here, username stands for your login name and machinename represents the address of the remote machine.

By running this script, I saved a lot of time to type the long name of the machine. But, another question is I still have to enter the code after running the script. I searched the Internet, keygen seems to be a cool command for me.

http://www.debian-administration.org/articles/152

This website has detailed instructions about how to use keygen. For mac users, we have no ssh-copy-id command. I found an alternative way to copy the public key to the server. The command is

cat ~/cat ~/.ssh/id_rsa.pub | ssh username@machinename "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"


install Ctags and Taglist without root

Ctags and Taglist seem to be quite popular plugins for vim editor. Since I'm novice to Linux. It took me several hours to figure out how to configure them for my vim on the department machine. It's kind of tricky because I have no root right on that machine.

I mainly borrowed methods from the following three places,


http://www.thegeekstuff.com/2009/04/ctags-taglist-vi-vim-editor-as-sourece-code-browser/


This one intrigues my curiosity to try out Ctags and Taglist.


http://superuser.com/questions/66367/is-it-possible-to-install-ctags-without-root-privs


The above site introduces how to install Ctags without root.


http://vim-taglist.sourceforge.net/installation.html


The last one is for installing taglist without root.



The main idea of installing Ctags without root is to compile it by yourself and install it in your home directory.


First download Ctags from its webpage and type in the following commands.


$ tar zxf ctags-5.8.tar.gz

$ cd ctags-5.8
$ ./configure --prefix=$HOME
$ make && make install


This will compile and install ctags in your home directory. The resulting binary will be: $HOME/bin/ctags


That's all I did to make Ctags work in my vim. If it doesn't work for you. Just check the second webpage. I omitted several steps which seems not critical to me.

For the Taglist, just follow the instructions in the third page. I only finished the first step - download taglist.zip and uzip it to .vim folder and it works.

The first website contains detailed description about how to use Ctags and taglist in vim. Have fun!