Functions with variable number of arguments

In C/C++, functions can be written to take variable no. of arguments. Examples are printf, sprintf, snprintf etc. The prototype of printf functions looks like int printf(const char *format, …). These functions internally use macros such as va_start, va_arg, va_end etc to get the passed parameters. But, I have observed that for every function like printf, there is another similarly defined function which takes va_list instead of … . For example, vprintf is similar to printf and has int vprintf(const char *format, va_list ap) prototype. In the same way, vsprintf and vsnprintf are also defined. Till now, I did not know the reason and used to think of it as an unnecessary wrapper.

For example, if a wrapper called myprintf needs to be written over printf

int myprintf(const char* format, …){

va_list ap;

va_start(ap,format);

???????

va_end(ap);

}

In the above code, I can not put ‘printf’ in place of ??????. Because printf is expecting variable arguments (like i, j , l) and not va_list dataype. So, in place of ??????, I should call vprintf which takes va_list.

So, I observed that it is a good practice to write 2 functions in case of variable number of arguments. One which takes variable no. of arguments as … and the other one taking va_list. An example is given below.

void vfunction (char * format, va_list ap){

/*This function does the actual work*/

}

void function(char* format, …){

/*This function just redirects to vfunction which does the work */

va_list ap;

va_start(ap,format);

vfunction(format, ap);

va_end(ap);

}

Plus addressing in GMail

I recently learnt that plus addressing is supported in GMail. I feel this is a nice feature. If plus addressing is supported, even if we send a mail to user+office@gmail.com, it will be delivered to user@gmail.com. But, for user, it will be easy to set filters based on the extra information (+office). User can give different email addresses (user+office, user+maths, user+studies etc.) to others for different purposes.

This feature (but as minus addressing) is supported in Yahoo mail also (only in Yahoo Mail Plus). You can read more information about plus addressing here.

Scheduling e-mails in web based mail services

In web based e-mail services like yahoo mail & gmail, I always feel 1 feature is missing. That is setting timer to send mails. Suppose, if tomorrow is my friend’s birthday, I will write an email and set a timer to deliver the email at night 12′ clock. I don’t have to be awake till 12′ clock to wish him.

In the view part, only change is “Drop down” should be provided at the send button to select the time.

I know we can achieve this using schedulers present in our machines. But, feature like this in web based mail services will reach more audience. If this option is provided in yahoo or gmail, that will be very nice.

Find & Replace non-printable characters in vim

When I converted a file from EBCDIC format to ASCII format using an online tool, there are many non-printable characters in the ASCII file. So, this ASCII file is completely unreadable. So, I followed the below steps to make it readable.

1) Find hexa value of that non-printable character. Move your cursor to that character and press ‘ga’.

2) In escape mode, execute this ‘:%s/\%x85/\r/gc’. In my case, hexadecimal value of that non-printable character is 85. I replaced that with ‘\r’.

Some characters like ‘Ctrl+M’ can be printed by ‘Ctrl+v’ + ‘Ctrl+M’. But, in my case, 0x85 is that non-printable character and could not print it using ‘Ctrl+v’.

some problems when commands are hashed

In Unix systems, commands are hashed for performance reasons [man hash]. Today, I got a problem related to this and wasted so much time for resolving. In my solaris machine, patch command is in /usr/bin/patch and its version is very old. So, I have installed a new patch version using pkgadd and it has installed patch command in /usr/local/bin/. In PATH variable, /usr/local/bin/ is before /usr/bin directory.  So, `which patch` command is showing /usr/local/bin/patch. But, when I execute patch on command prompt, it is executing old version of patch [/usr/bin/patch]. After some time, I tried type command `type patch` and it is showing /usr/bin/patch is hashed. Then, I came to know about hash command in solaris. After rehashing, every thing started working fine.

R6034 – C runtime error in Vista

Today, when trying to execute a test program on Vista, I got the above error. I have copied only executable and forgot to copy manifest file to the destination folder. So, I was getting the above error, when I tried to execute the executable. But, after copying manifest file also, this error did not go away. I have searched over the net for any clues, in many forums this issue is discussed, but all posts are talking about some manifest problem in Vista. But, somewhere I read that Vista caches the meta data. So, I touched the executable using touch command to change the last modified time[I use cygwin]. After that OS is able to recognize manifest file associated with executable and started working fine.

My first bug in OpenSolaris

Yesterday, while using getpass* functions & passwd command in solaris, I found a bug. Type passwd command and press ctrl-z to stop that process and bring that process to foreground using fg command. Now, you can see your password while typing it. Basically, tty are settings are reset when you stop the process and bring it to foreground.

when I sent a mail to Sun Security Coordination Team [secure@security.Eng.Sun.COM], they said it was found internally recently and already raised as a bug http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=6443857 in OpenSolaris bug database. By mistake, as it is noted as security vulnerability in bug description, so it is not visible outside.

New tricks that I have learned in using debuggers

In gdb/dbx, we can see the variable using addresses. Suppose a structure [struct Box * b] is not deallocated but we are setting it to NULL i.e. b = NULL. We can not access that structure using b. But if we know the address pointed by b [we can get it using print b], we can access it like *(struct Box *) (0x4f2b2448) [assuming 0x4f2b2448 is the address that b points]. Obviously this is a very simple thing. But, I have used it for the first time. Similarly, when setting watchpoints over variables like watch b->length, if the control goes to some other place where this variable b is not in scope, that watchpoint will be deleted. [Atleast this happens in old gdb versions, I'm using SuSE 8]. So, in those cases you can put watch point over address so that it is accessible from anywhere. watch ((struct Box *)(0x4f2b2448))->length works fine.

Projects in Solaris

When I was reading an interesting blog about prstat behavior, I read about projects in Solaris. After reading about projects, I came to know that they are mainly used for controlling resources used by process[es]. Suppose, if we want to control the CPU/disc/memory usages, then we create a project [using projadd, projmod] and in /etc/project, we have to modify resource control field. Then we can create/attach processes into this project [using newtask]. Once the process is created/attached in this project, all resource constraints will apply to that process. Suppose if the resource constraint name is process.max-file-descriptor and the value is 10, a process in that project can not have more that 10 file descriptors. Moreover, using prstat -J command, we can clearly see the resources used for projects. I felt this is a very good way to control resources used by specific processes, but I don’t know if the similar thing exists on other platforms like Linux. Some good links that I have referred are here & here.

Make vim to remember file cursor position : solaris

Some time back, I installed vim in Solaris after taking the package from sunfreeware site. I have configured colors also. But one main annoying thing is that it does not remember the cursor position of the same file opened previously. i.e. When I open a file and move to line no:1000 , quit and open the same file again, cursor will be placed at line no:0. After reading a few web pages [not with much help], I have read vim documentation [:help viminfo]. There I found that by typing `” [backquote & doublequote], cursor will be moved to the line where we left last time. But I dont want to type `” everytime I open a file. I want to automate this. After searching for `” pattern in my Linux vim config files, I found the following code snippet in /etc/vimrc in my SuSE.

if has(“autocmd”)
autocmd BufReadPost *
\ if line(“‘\””) > 0 && line(“‘\””) <= line(“$”) |
\ exe “normal g`\”” |
\ endif
endif

After I have added the above code in my solaris .vimrc, things began working properly. Actually I have sent a mail to vim mailing list and started working on this problem. When I checked my mails after solving the problem, I saw few good replies from vim mailing list pointing to these links tip#80 , yahoo_groups .

« Older entries

Follow

Get every new post delivered to your Inbox.