Author Archives: andreaslindell

Two useful links

Have been working the last weeks with a very hard to find bug and during my work I’ve stumbled upon a number of very useful web resources.

Two of the most useful ones I’d like to keep for the future:

Command line PuTTY

Just stumbled upon a new (to me) tool named Plink. Plink is a command-line interface to the PuTTY back ends and can for example be used when writing batch scripts in Windows.

For example the following one-liner could be used to start a script on a remote machine:

plink username@192.168.2.10 -pw password /folder/script.sh 

Plink can be downloaded from the regular PuTTY download page.

Regular expressions in notepad++

One of the first tools I always download when I’m face with a new Windows computer is notepad++. This clean and simple, yet very powerful, text editor is in my opinion an indispensible tool when working on a windows computer.

Except for the very useful “Find in Files” feature, which I’ve found to be light-years better than Windows’ built-in search feature, the regular expressions-type search (and replace) feature is also very useful.

Here’s an example on how to use this powerful feature, as I always tend to forget the exact syntax:

If I want to replace each line starting with either “#” or “;” with an empty line, or to completely remove the line, here’s how to do it:

Find:
^[#;].*

Replace:
(leave this emtpy.)

The ^ indicates the start of a line, the [#;] is a character class to match either # or ;, and .* matches anything else in the line.

In versions of Notepad++ before 6.0, you won’t be able to actually remove the lines due to a limitation in its regex engine; the replacement results in blank lines for each line matched. In other words, this:

# foo
; bar
statement;

Will turn into:

statement;

However, the replacement will work in Notepad++ 6.0 if you add \r, \n or \r\n to the end of the pattern, depending on which line ending your file is using, resulting in:
statement;

Be sure to select “Search Mode” -> “Regular Expression”

For example ^[#;].*\r\n in “Find what” and “” in Replace with will remove the entire line if it starts with # or ;!!

Pointers and the const keyword

One thing I remember from my early days as a C-programmer as all too easy to mess up is the usage of the const keyword in the context of pointers.

Given the following lines of code, which pointer is a pointer to a constant value, and which is a constant pointer to a value?


int v1, v2;
const int * p1 = &v1;
int * const p2 = &v2;

The simple rule of thumb to decipher this type of declaration, I soon learned, was to read the declaration from the very end.

So, p1 is a pointer to an int that is constant, and p2 is a constant pointer to an int.

Thus, neither of the two expressions would be allowed:


*p1 = 2;
p2 = &v1;

But both these would:


p1 = &v2;
*p2 = 1;

The ‘new’ auto keyword in C++11

Reading up on some of the new C++11-features, and stumbled upon the redefinition of the ‘auto’ keyword.

Though I don’t particularly like the choice to redefine the meaning of an already existing keyword, I do however see some good uses of this new keyword, especially when working with lots of externally produced and poorly documented libraries – something I’m all too familiar with.

Long live the ‘new’ auto keyword!

Time.h and epoch

I recently stumbled upon a time.h implementation from a hardware/software supplier that did not have its epoch time starting at midnight, Coordinated Universal Time (UTC), 1 January 1970 as expected.

Having always believed that this was standardized as a part of the C-standard I was surprised that this part was in fact not the case. A brief scan online showed that most, but not all, time.h implementations does in fact start it “time” in 1970.

This particular hardware/software supplier had instead chosen to have its epoch start at midnight UTC-6 Jan 1, 1900.

As I was working with a trace log implementation at the time, where I had logs on different types of processors being gathered on one processor it was important for me to have each trace entry’s time stamp be correct at least down to the second, to be able to match these trace logs between the processors correctly.

Some more searching on the matter showed that RFC 868 has a part defining the difference, in seconds, between these two times, giving to hand that 2,208,988,800 seconds have passed between these dates. Now of course, this would not be too hard to calculate by hand, but it’s always nice to be able to give a good reference to magical values like this that you leave behind you in your code.

On a side note, this particular implementation would wrap somewhere around 2036. While it is unlikely that any code we deliver today would still be running in some 23 years time – it’s still not that far away. Also, choosing a different epoch that most time.h implementations out there is just begging for problems – and error reports from your customers.

Simplified compiling

During my years as an embedded software consultant I’ve spent my fair share of time in projects with enormous build systems, outputting 1000’s of lines of messages when being built. The chance of spotting what could potentially be hazardous compiler warnings and remarks are then slim to nothing.

Of course, should I have a compiler that supports it, I could always turn on the gcc “-Werror” flag, treat all warnings as errors and then go about and fix all of these. However, sometimes that may not be a viable option, or perhaps I’d just like to get an overview of all warnings and solve them when time so permits.

The other day a colleague of mine introduced me to the simple, yet clever, tool “hilite”. This tiny piece of software will highlight anything printed to stderr, making it the perfect tool to use with your Linux/Unix-based build environment of choice.

Running a build with hilite, anything printed to stderr by the compiler, such as errors, warnings and remarks, will show up with bold red text on my screen – making these messages so much easier to spot.

Hilite can be found on sourceforge at the hilite page.

Finding the escape character

I find myself working in different terminal environments with different types of targets, something that sometimes gives me problems when trying to leave the target environment – how do I make that Escape character?

More often than not, I have opened a telnet session towards a target and has been greeted with the following message:

“Escape character is ‘^]’.”

I have found at least two different ways of making this escape character, depending on the environment I’m working in:

  • Ctrl+5
  • Alt+9
  • When I find more weird ways of producing this escape character, this blog post shall be updated.

    Diff labels in ClearCase

    Many customers seem to be using the ClearCase revision control system. While ClearCase is very powerful and useful in many ways, I have struggled to find a simple command to show the difference between two labels, i.e. which files that have been change between the application of these two labels.

    I have finally found how this is done:

    cleartool find -all -element ‘{lbtype_sub(LABEL1) && lbtype_sub(LABEL2)}’ -ver ‘{lbtype(LABEL1) && ! lbtype(LABEL2)}’ -nxname -print

    Just replace LABEL1 and LABEL2 with the labels of choice, and off you go. This will only show files which have both labels, files recently created that only has one of the labels on them will not be found – however since the above command would should the verified folder (where the file was added) those files are easilly spotted as well.

    Remembering people’s names

    Professionally, one of my biggest problems is that I just can not seem to remember people’s names. This of course does not happen with friends and family, nor with colleagues that I work with on a day-to-day basis, but when I meet new people I tend to forget their names before even having told them my name.

    I ran across this short, yet brilliant, blog by Michael Ray Hopkin discussing exactly this problem, and presenting five simple steps to take to start remembering people’s names.