-1

I'd like to find if the program less is installed on the system. I'm not allowed to use a direct system("which less") because of my requirements. So I'm going to try the PATHs in the PATH variable and do stat on the files to see if less is installed. But how do I append less to each of my PATHs? I started with this code

pathValue = getenv ("PATH");
if (! pathValue) {
    printf ("'%s' is not set.\n", "PATH");
}
else {
    printf ("'%s' is set to %s.\n", "PATH", pathValue);
}

Now it correctly prints my PATH but I don't know how to proceed. Can you help me? I think that I must tokenize my pathValue, how can I do that?

$ ./a.out 'PATH' is set to /home/developer/google-cloud-sdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • google [C string append](https://www.google.fr/search?q=C%20string%20append) have you not? – Eregrith May 19 '15 at 07:03
  • possible duplicate of [C string append](http://stackoverflow.com/questions/5901181/c-string-append) – Eregrith May 19 '15 at 07:03
  • @Eregrith Yes but I must tokenize the PATH, I think, – Niklas Rosencrantz May 19 '15 at 07:03
  • 1
    Well, have you googled "C tokenize string"? And by the way, if you cannot come up with the faintest idea about how you could tokenize a string nothing we tell you will help you in any way. – Peter - Reinstate Monica May 19 '15 at 07:05
  • 1
    By the way, the question surprisingly *does* have an interesting aspect, namely that a directory name in *nix-like OSs can contain any character but '/' and '\0'; in particular it can contain colons. While only pathological minds would have such directories it is not immediately clear to me whether -- and if: how -- you or `which` or a shell would recognize them if they were in the PATH. (Well, a shell could always keep the PATH elements in an array and have PATH be a read-only variable conatenated from the array elements. But then scripts (and programs) trying to set PATH would fail.) – Peter - Reinstate Monica May 19 '15 at 07:15
  • @PeterSchneider I think you have yourself a nice question here :) ask it! – Eregrith May 19 '15 at 07:50
  • @Eregrith I don't think there is a solution. The "keep-it-simple-stupid" text paradigm in unix just bites us sometimes. – Peter - Reinstate Monica May 19 '15 at 08:03
  • @PeterSchneider Hmm I don't know, this seems like a pretty big caveat to me. A path containing a directory with `:` in its name is also a big security flaw I think... – Eregrith May 19 '15 at 08:09
  • @PeterSchneider out of curiosity I looked for it and [here is an answer](http://stackoverflow.com/a/29213487/1151654) – Eregrith May 22 '15 at 09:22

1 Answers1

2

I believe, the getenv ("PATH"); returns a pointer to the string like,

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Clearly, we can see, the different directory paths are : delimited. So,

  1. You can start tokenizing the pathValue using strtok() and : as demiliter. Each resulting token should act as a directory path to check for the file.
  2. Check the existance using stat().
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261