# Problem locating unix executables #MG5

**URL:** <https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [December 15, 2025, 3:26pm UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660 "2025-12-15T15:26:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![salmon-571](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/salmon-571/32/32_2.png) [@salmon-571](https://forum.codecrafters.io/u/salmon-571)\
**Post date:** [December 15, 2025, 3:26pm UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/1 "2025-12-15T15:26:57Z")

</div>

It seems to work from my Windows machine:  
`$ type java`  
`java is C:\Program Files\Eclipse Adoptium\jdk-21.0.6.7-hotspot\bin\java.exe`  
`$`

But doesn’t work with the tester’s Linux machine:

```auto
remote: [tester::#MG5] Running ./your_program.sh
remote: [your-program] $ type cat
remote: [your-program] cat: not found
remote: [tester::#MG5] ^ Line does not match expected value.
remote: [tester::#MG5] Expected: "cat is /bin/cat"
remote: [tester::#MG5] Received: "cat: not found"
remote: [your-program] $
remote: [tester::#MG5] Assertion failed.
remote: [tester::#MG5] Test failed

```

Code:

```csharp
// Builtin.cs

public void Type(object[] module_name)
{
    string name = module_name[0].ToString();
    if (ValidateBuiltinName(name))
    {
        Console.WriteLine($"{name} is a shell builtin");
        return;
    }

    string path = Environment.GetEnvironmentVariable("PATH");
    IEnumerable<string> folders = path.Split(Path.PathSeparator);

    foreach (string folder in folders)
    {
        DirectoryInfo directory = new DirectoryInfo(folder);
        try
        {
            foreach (FileInfo file in directory.GetFiles())
            {
                if (file.Name.StartsWith(name) && u.IsFileExecutable(file.FullName))
                {
                    Console.WriteLine($"{name} is {file.FullName}");
                    return;
                }
            }
        } catch (DirectoryNotFoundException ex) { }

        
    }

    Console.WriteLine($"{module_name[0]}: not found");
}

```

```csharp
// Utils.cs

public bool IsFileExecutable(string filePath)
{

    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
        if (new string[] { ".dll" }.Contains(new FileInfo(filePath).Extension))
        {
            return false;
        }

        byte[] firstBytes = new byte[2];

        try
        {
            using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                if (fs.Length >= 2)
                {
                    fs.Read(firstBytes, 0, 2);
                }
            }
        }
        catch
        {
            return false;
        }

        return Encoding.UTF8.GetString(firstBytes) == "MZ"; 
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { 
        UnixFileMode mode = File.GetUnixFileMode(filePath);
        if (mode == UnixFileMode.OtherExecute || mode == UnixFileMode.UserExecute || mode == UnixFileMode.GroupExecute)
        {
            return true;
        }
    }

    return false;
}

```

I don’t have a Linux machine to test on so this is rather inconvenient 😕

---

<div class="post-metadata">

**Author:** ![senevoldsen](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/senevoldsen/32/6460_2.png) [@senevoldsen](https://forum.codecrafters.io/u/senevoldsen)\
**Post date:** [December 15, 2025, 8:49pm UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/3 "2025-12-15T20:49:21Z")

</div>

Hmm. Maybe investigate `File.GetUnixFileMode`. It looks like it returns a bitmask of capabilities but your comparison is checking 3 particular exact bitmask matches `(1, 8, or 64)`. Would your check ever pass if the owner has `UserRead`(256) permission?

---

<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 15, 2025, 11:04pm UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/5 "2025-12-15T23:04:38Z")

</div>

Hey @salmon-571, you’ll want to use **bitwise checks** rather than equality comparisons on UnixFileMode.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/5/2/52d69062e021385bc03fee30521bb64fe54b5a70.png)

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/1/7/17d9f38514bfa7a7d83a5b73b7c565ea3763e2d4.png)

> <https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Private.CoreLib/src/System/IO/UnixFileMode.cs#L8-%23L8>

I’d recommend using [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) if you’re testing this locally,

Let me know if you’d like further clarification!

---

<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 15, 2025, 11:53pm UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/7 "2025-12-15T23:53:16Z")

</div>

> [@salmon-571](#):
>
> java is C:\Program Files\Eclipse Adoptium\jdk-21.0.6.7-hotspot\bin\java.exe

@salmon-571 Just curious, were you able to run C# code using Java? 🤔

---

<div class="post-metadata">

**Author:** ![salmon-571](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/salmon-571/32/32_2.png) [@salmon-571](https://forum.codecrafters.io/u/salmon-571)\
**Post date:** [December 16, 2025, 5:12am UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/8 "2025-12-16T05:12:22Z")

</div>

Thank you for your help, I’ll have to try this when I get home.

Also, no I was not able to do that. I just used `java` as an example of a program on my PATH because I don’t have `cat`.

---

<div class="post-metadata">

**Author:** ![salmon-571](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/salmon-571/32/32_2.png) [@salmon-571](https://forum.codecrafters.io/u/salmon-571)\
**Post date:** [December 16, 2025, 5:16am UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/9 "2025-12-16T05:16:29Z")

</div>

No, I forgot to add this check to my if statement. Thanks for reminding me 😅

---

<div class="post-metadata">

**Author:** ![salmon-571](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/salmon-571/32/32_2.png) [@salmon-571](https://forum.codecrafters.io/u/salmon-571)\
**Post date:** [December 16, 2025, 11:46am UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/10 "2025-12-16T11:46:07Z")

</div>

Here is the function fixed for those that are wondering:

```csharp
public bool IsFileExecutable(string filePath)
{

    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
        if (new string[] { ".dll" }.Contains(new FileInfo(filePath).Extension))
        {
            return false;
        }

        byte[] firstBytes = new byte[2];

        try
        {
            using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                if (fs.Length >= 2)
                {
                    fs.Read(firstBytes, 0, 2);
                }
            }
        }
        catch
        {
            return false;
        }

        return Encoding.UTF8.GetString(firstBytes) == "MZ"; 
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { 
        UnixFileMode mode = File.GetUnixFileMode(filePath);
        UnixFileMode perms = UnixFileMode.UserExecute | UnixFileMode.UserRead;
        return (mode & perms) == perms;
    }

    return false;
}

```

---

<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:** [December 21, 2025, 11:46am UTC](https://forum.codecrafters.io/t/problem-locating-unix-executables-mg5/15660/11 "2025-12-21T11:46:54Z")

</div>

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