Test command name

I'm very new to C and could use a little help.

I'm testing to make sure the command is running as it's proper name, if not then fail.
if (strcmp(argv[0], "xinit") != 0) {
fprintf(stdout, "name = %s length = %d\n",argv[0],l);
usage(0);
}

This works if the command is called as /sbin/xinit but fails if it is called as
/xinit. Id like it to also work if called as xinit , /sbin/xinit , or /xinit.

Thanks for any help

You have several choices:
easiest one is (also not completely bulletproof) -


if (strstr(argv[0], "xinit") == NULL)
{
       //  complain & exit
}

There are intrinsic difficulties in finding the real name of the executable -
for an example see the bottom of Perderabo's second post about arg 0 here:

Thanks for the quick reply.

That works as long as 'xinit' is not in any part of the string. I'm tring to also limit the executable from being executed from other locations. i.e. /usr/joe/xinit -> I would want that to fail.

Looks like a series of if-elseif statements is needed to strcmp argv[0] to the various pathnames from where xinit can be invoked.