# Help about #MG5 stage

**URL:** <https://forum.codecrafters.io/t/help-about-mg5-stage/3541>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [December 19, 2024, 5:50am UTC](https://forum.codecrafters.io/t/help-about-mg5-stage/3541 "2024-12-19T05:50:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![diego-dev018](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/diego-dev018/32/3575_2.png) [@diego-dev018](https://forum.codecrafters.io/u/diego-dev018)\
**Post date:** [December 19, 2024, 5:50am UTC](https://forum.codecrafters.io/t/help-about-mg5-stage/3541/1 "2024-12-19T05:50:35Z")

</div>

I could fix this, closed

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 19, 2024, 9:45am UTC](https://forum.codecrafters.io/t/help-about-mg5-stage/3541/2 "2024-12-19T09:45:05Z")

</div>

Hi @diego-dev018, could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

---

<div class="post-metadata">

**Author:** ![D10f](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/d10f/32/3530_2.png) [@D10f](https://forum.codecrafters.io/u/D10f)\
**Post date:** [December 19, 2024, 10:33am UTC](https://forum.codecrafters.io/t/help-about-mg5-stage/3541/4 "2024-12-19T10:33:17Z")

</div>

I think this is very similar to what happened to me [here](https://forum.codecrafters.io/t/tests-wont-pass-on-mg5/3511/9).

The call to `strtok` is destructive, meaning that it modifies the string it acts upon in place. If you run your code in your local machine and try to run `type ls` for example, it will work once but if you run the same again it will fail. That’s because it’s chopping the PATH string constantly.

You’d have to run `strndup` to duplicate it first, and use that.

```diff
char *pathenv = getenv("PATH");
+ char *pathdup = strndup(pathenv, strlen(pathenv));
char *delimit = ":";
char *token;
DIR *dir;
struct dirent *dp;
- token = strtok(pathenv, delimit);
+ token = strtok(pathdup, delimit);

```

---

<div class="post-metadata">

**Author:** ![D10f](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/d10f/32/3530_2.png) [@D10f](https://forum.codecrafters.io/u/D10f)\
**Post date:** [December 19, 2024, 11:07am UTC](https://forum.codecrafters.io/t/help-about-mg5-stage/3541/6 "2024-12-19T11:07:12Z")

</div>

That’s a _different_ issue. You can see from the thread that I created that there’s a `/bin` folder in the path:

> [@Tests won't pass on #MG5](https://forum.codecrafters.io/t/tests-wont-pass-on-mg5/3511/8):
>
> Right, that’s what I understood as well. I’m running a function separately on every loop iteration, not preloading anything. When I print the contents of the PATH variable I get this: /tmp/banana/banana/grape:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin For readability: /tmp/banana/banana/grape /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin The temporary directory is created and placed at the beginning of the PATH. It should found the my\_exe executable there. I…

In my case, the test is expecting to find `cp` inside `/bin` but, as per the order of the directories in the path variable, it find it sooner inside `/usr/bin` which is what is causing my tests to fail.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 28, 2024, 12:20am UTC](https://forum.codecrafters.io/t/help-about-mg5-stage/3541/7 "2024-12-28T00:20:02Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [January 2, 2025, 12:20am UTC](https://forum.codecrafters.io/t/help-about-mg5-stage/3541/8 "2025-01-02T00:20:13Z")

</div>

Closed on January 2, 2025, 12:20am UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
