# Getpwnam()?

**URL:** <https://discourse.julialang.org/t/getpwnam/31554>\
**Category:** General Usage\
**Created:** [November 26, 2019, 10:04pm UTC](https://discourse.julialang.org/t/getpwnam/31554 "2019-11-26T22:04:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleisher](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mleisher](https://discourse.julialang.org/u/mleisher)\
**Post date:** [November 26, 2019, 10:04pm UTC](https://discourse.julialang.org/t/getpwnam/31554/1 "2019-11-26T22:04:17Z")

</div>

Before I try to write one, does a julia version already exist?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [November 26, 2019, 10:36pm UTC](https://discourse.julialang.org/t/getpwnam/31554/2 "2019-11-26T22:36:35Z")

</div>

based on the documentation, there isn’t one in the official library. maybe calling that directly? [Running External Programs · The Julia Language](https://docs.julialang.org/en/v1/manual/running-external-programs/)

---

<div class="post-metadata">

**Author:** ![mleisher](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mleisher](https://discourse.julialang.org/u/mleisher)\
**Post date:** [December 6, 2019, 9:38pm UTC](https://discourse.julialang.org/t/getpwnam/31554/3 "2019-12-06T21:38:17Z")

</div>

Finally got time to implement:

```julia
#
# Simple module to call the getpwnam() function from libc and
# return the results.
#
# Example:
# using Getpwnam
# p = getpwnam(ENV["USER"])
# @show(p)
#
# Mark Leisher <mleisher@cs.nmsu.edu>
# 06 December 2019
#

module Getpwnam

export getpwnam, Passwd

struct Passwd
    name::String
    pswd::String
    uid::UInt32
    gid::UInt32
    gecos::String
    dir::String
    shell::String
end

#
# Internal structure that gets converted to Passwd before being passed
# back from getpwnam.
#
struct passwd
    name::Ptr{UInt8}
    pswd::Ptr{UInt8}
    uid::Cuint
    gid::Cuint
    gecos::Ptr{UInt8}
    dir::Ptr{UInt8}
    shell::Ptr{UInt8}
end

function getpwnam(name::AbstractString)
    pptr = ccall(:getpwnam, Ptr{Ptr{Cvoid}}, (Cstring,), name)
    if pptr == C_NULL
        return nothing
    end
    p = unsafe_load(convert(Ptr{passwd}, pptr))
    #
    # Return the structure.
    #
    Passwd(unsafe_string(p.name),
           unsafe_string(p.pswd),
           p.uid,
           p.gid,
           unsafe_string(p.gecos),
           unsafe_string(p.dir),
           unsafe_string(p.shell))
end

end

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 16, 2022, 9:09am UTC](https://discourse.julialang.org/t/getpwnam/31554/4 "2022-02-16T09:09:04Z")

</div>

> [@mleisher](#):
>
> `pptr = ccall(:getpwnam, Ptr{Ptr{Cvoid}}, (Cstring,), name)`

I get the error:

```julia
could not load symbol "getpwnam":

```

I saw that there are some symbols defined with the prefix “`jl_`”, like e.g. “`jl_getuid`”  
The reason why I tried to use the code of @mleisher is the fact that I do not know how to determine information regarding the group ID and the user ID of the current user?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 16, 2022, 9:19am UTC](https://discourse.julialang.org/t/getpwnam/31554/5 "2022-02-16T09:19:54Z")

</div>

Why not just:

```julia
id=read(`id`,String)

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 16, 2022, 11:37am UTC](https://discourse.julialang.org/t/getpwnam/31554/6 "2022-02-16T11:37:53Z")

</div>

Thanks for the proposal! Could you be more explicit? E.g. in form of an example?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 16, 2022, 11:49am UTC](https://discourse.julialang.org/t/getpwnam/31554/7 "2022-02-16T11:49:58Z")

</div>

Perhaps it needs some fine tuning for robustness:

```julia
julia> id=read(`id`,String)
"uid=1000(oheil) gid=1000(oheil) groups=1000(oheil),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev)\n"

julia> m=match(r"uid=(\d*)",id)
RegexMatch("uid=1000", 1="1000")

julia> uid=m.captures
1-element Vector{Union{Nothing, SubString{String}}}:
 "1000"

julia> m=match(r"gid=(\d*)",id)
RegexMatch("gid=1000", 1="1000")

julia> gid=m.captures
1-element Vector{Union{Nothing, SubString{String}}}:
 "1000"

```

And of course, as you try to use `getpwnam`, I assume that you are on a linux like OS.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 16, 2022, 9:18pm UTC](https://discourse.julialang.org/t/getpwnam/31554/8 "2022-02-16T21:18:16Z")

</div>

> [@oheil](#):
>
> `id=read(`id`,String)`

@oheil Thanks for your snippet!  
Ok, it is OS dependent, on linux it works, on MS Windows not.  
It is the same with `pptr = ccall(:getpwnam, Ptr{Ptr{Cvoid}}, (Cstring,), ENV["USER"])`,  
on linux it is possible to execute the command on Windows not ☹ .  
Meanwhile, I found the function: “`Base.Libc.getpwuid()`”, is there a chance to call this function?  
Under Julia v1.7.1 on a “MS Windows 10”-system the call fails with the error message:  
`operation not supported on socket (ENOTSUP)`.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 16, 2022, 10:30pm UTC](https://discourse.julialang.org/t/getpwnam/31554/9 "2022-02-16T22:30:50Z")

</div>

No, it’s OS specific. Windows has no UID and GID like Unix, and there is no getpwnam in Windows.

What is your real problem?  
It sounds strange that you are on a Windows OS and looking for user ID and group ID. There are user names, principals, group names, and so on, but not an ID.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 17, 2022, 7:18am UTC](https://discourse.julialang.org/t/getpwnam/31554/10 "2022-02-17T07:18:29Z")

</div>

To be honest I’am not familiar with the internals of different OS.  
Does other functions in Julia exist, to figure out, what the rights are for a specific person on a MS WIN system?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 17, 2022, 8:33am UTC](https://discourse.julialang.org/t/getpwnam/31554/11 "2022-02-17T08:33:51Z")

</div>

I don’t know about any Julia functions to get windows user rights.  
For Windows it’s probably best to run some powershell commands. I think you have to do some research for your specific task, because I don’t have too much experience with powershell.  
This one I have just googled:

```julia
julia> run(`powershell '[System.Security.Principal.WindowsIdentity]::GetCurrent().Name'`)
WORKSTATION\Oheil
Process(`powershell '[System.Security.Principal.WindowsIdentity]::GetCurrent().Name'`, ProcessExited(0))

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 17, 2022, 8:45am UTC](https://discourse.julialang.org/t/getpwnam/31554/12 "2022-02-17T08:45:04Z")

</div>

> [@oheil](#):
>
> `run(`powershell ‘[System.Security.Principal.WindowsIdentity]::GetCurrent().Name’`)`

Thanks again! Ok, I guess we have to wait a while, before such OS-specific thinks are available in Julia out-of-the box. Maybe,the Python-community was more active in this domain and we can use Python functions inside Julia.

---

<div class="post-metadata">

**Author:** ![mleisher](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mleisher](https://discourse.julialang.org/u/mleisher)\
**Post date:** [February 21, 2022, 11:01pm UTC](https://discourse.julialang.org/t/getpwnam/31554/13 "2022-02-21T23:01:16Z")

</div>

@ellocco, download [https://www.cs.nmsu.edu/~mleisher/julia/Getpwnam.jl](https://www.cs.nmsu.edu/~mleisher/julia/Getpwnam.jl)

I’m looking into a pipe error on Windows now, but it may work for you. It worked the last time I used it three years ago.

---

<div class="post-metadata">

**Author:** ![mleisher](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mleisher](https://discourse.julialang.org/u/mleisher)\
**Post date:** [February 22, 2022, 3:31am UTC](https://discourse.julialang.org/t/getpwnam/31554/14 "2022-02-22T03:31:46Z")

</div>

It turns out there is an issue with using ‘break’ inside a ‘for line in eachline(filehandle)’ loop in julia 1.7.2 on Windows (but not on Linux or Mac OS). The getpwnam code at [https://www.cs.nmsu.edu/~mleisher/julia/Getpwnam.jl](https://www.cs.nmsu.edu/~mleisher/julia/Getpwnam.jl) should run with most versions of julia from 1.6 up on Windows now.
