-2

i tried to debug my code in vscode and it worked as i wanted but when i put it in vagrant it dosnt work for somereason and gives me nothing im an alx student and trying to learn coding so it may be obvious for u but rly i spent hours on this and it didn't work in vagrant and my tasks have to work in vagrant not vscode

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdbool.h>

int main(int argc, char *v, char **env)
{
        int i, j;

        char *c = "PATH";
        char *ptr;
        j = 0;
        i = 4;
        while(*env)
        {
                if (**env == *c)
                {
                        ptr = *env;
                        while (*ptr == *c)
                        {
                                j++;
                                ptr++;
                                c++;
                                if (j == i)
                                {
                                        printf("%s", ptr);
                                }
                                if (*c != *ptr)
                                {
                                        j = 0;
                                        break;
                                }
                        }
                }     
        env++;
        }
}
  • Is there an error message? If not, the issue may be that you need to flush the terminal at the end of the program - try adding `fflush(stdout);` – CoderMuffin May 17 '23 at 18:46
  • Welcome to StackOverflow! Please take the [tour] to learn how this site works, and read "[ask]". Then come back and [edit] your question, telling us what "_does not work_" means. – the busybee May 17 '23 at 18:50
  • In addition, your main function looks a little off - see this post https://stackoverflow.com/questions/10321435/is-char-envp-as-a-third-argument-to-main-portable – CoderMuffin May 17 '23 at 18:58
  • Maybe define what it means for this code to "work" or not. Does it fail to compile in one case? Fail with an error at runtime? Just produce results different from those you expect? – John Bollinger May 17 '23 at 19:07
  • 2
    I'm assuming you're trying to do string comparison with things like `if (**env == *c)` and `while (*ptr == *c)` looking for the `PATH` environment variable? If so, that's not correct, you need to use [`strcmp`](https://man7.org/linux/man-pages/man3/strcmp.3.html) instead. If you do intend to compare single characters, then nevermind. – yano May 17 '23 at 19:07
  • 1
    That third argument to `main`, `char **env`, is common but nonstandard. Your problem may be that Vagrant doesn't supply it. – Steve Summit May 17 '23 at 19:08
  • 1
    Why not fetch an environment variable using the standard `getenv` function? – Steve Summit May 17 '23 at 19:10
  • 1
    If the environment happens to contain an environment variable other than `PATH` whose name starts with `P`, and that variable happens to appear earlier in the list than `PATH`, then this program loses the initial value of `c`, and therefore will never be able to find the `PATH` variable. – John Bollinger May 17 '23 at 19:14

1 Answers1

0

Your program prints PATH only if it is the first environment variable with a 'P' as first the letter. As soon as there is another environment variable beginning with 'P', it cannot find PATH.

Why is this?

If this other environment variable is checked, you increment c. But when the mismatch is detected, you do not reset it again.

Remove the statement c++ and replace the two occurrences of *c with c[j].

So, why did it work in Windows, but not in Vagrant? Most probably because in Windows PATH is found first, and in Vagrant not. Check the environment in both cases.

the busybee
  • 10,755
  • 3
  • 13
  • 30