Saturday, June 1, 2013

How to use vim to substitute from current line

Most folks use the command
:%s/SEARCHSTRING/REPLACESTRING/gc
to search for the string SEARCHSTRING and replace it with REPLACESTRING. However, the problem with this command is that it starts from the beginning of the file and replaces right through. How do we do this if we do not want to start from the beginning of the file, but only from the current line to the end of file.
Replace the %, which is short hand for 1,$ meaning the entire file. To go from the current line to the end you use .,$. The period means current line and $ means the last line. So the command would be:
:.,$s/SEARCHSTRING/REPLACESTRING/gc
But the . or current line can be assumed therefore can be removed:
:,$s/SEARCHSTRING/REPLACESTRING/gc
For more help see
:h range

Monday, May 27, 2013

How to use IS_ERR and PTR_ERR? What do they mean?

From the kernel definition there are three macros: 
IS_ERR - used to check,
- Returns non-0 value if the ptr is an error. Otherwise 0 if it's not an error
PTR_ERR - used to print.
- Current value of the pointer.
Used as follows- if ptr is the pointer you want to check then use it as follows:
if (IS_ERR(ptr))
     printk("Error here: %ld", PTR_ERR(ptr));
Code definitions:

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

static inline void * __must_check ERR_PTR(long error)
{
    return (void *) error;
}

static inline long __must_check PTR_ERR(const void *ptr)
{
    return (long) ptr;
}

static inline long __must_check IS_ERR(const void *ptr)
{
    return IS_ERR_VALUE((unsigned long)ptr);
}

static inline long __must_check IS_ERR_OR_NULL(const void *ptr)
{
    return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}