How do you know if less is installed on the system?

I would like the following behavior without using system

int ret = system("less -V > /dev/null 2>&1");
if (ret == 0) {
    printf("The less executable was found.\n");
}

      

I need the variable ret

as 0 if less is set on the system, but no call system

. Can this be done with exec

or similar?

+3


source to share


1 answer


You can use system("which less")

to determine if installed less

. This avoids trying to call less

if it is not installed. If you don't want to use at all system

, you'll have to reimplement the functionality which

in your program: i.e. Use getenv

to get a variable PATH

and try adding /less

for each path and call stat

in the resulting file to determine if it exists and is executable.



+2


source







All Articles