vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.67k stars 2.15k forks source link

os.expand_tilde_to_home doesn't expand homes of other users #17261

Closed gergelyk closed 1 year ago

gergelyk commented 1 year ago

Describe the feature

~/foobar is expanded to /home/myusername/foobar ~myusername/foobar should also be expanded to /home/myusername/foobar ~otheruser/foobar should be expanded to /home/otheruser/foobar

of course in a typical linux configuration.

Use Case

Providing paths from CLI.

Proposed Solution

No response

Other Information

No response

Acknowledgements

Version used

V 0.3.2 865c0ea

Environment details (OS name and version, etc.)

MacOS

JalonSolov commented 1 year ago

Not sure this is possible for other users than your own. The only way to expand ~ is by using the $HOME env var, which will only be set for you, not other users.

bogen85 commented 1 year ago

~ expansion is typically done in a command line shell, not in a language environment. @gergelyk Are you aware of another language environment that is not a command line shell has this feature?

As @JalonSolov pointed out for the given user this can be done by replacing ~ with the $HOME env var.

But a one does not have access to what another's $HOME env var would be. Short of parsing /etc/passwd on Linux and BSD (would this work on MacOS?) which would really be a feature request for general user information. (One would have to parse a system file or query some system utility for that, as user's home directories might not be in standard places).

One could query bash (by running it and capturing the results) from V for that information, but that is kind of kludgy.

gergelyk commented 1 year ago

I don't think that it would be reliable to rely on bash. In some environments it may even be absent.

The other environment which does such expansion is Python3. Try:

python3 -c 'import os; print(os.path.expanduser("~root"))'

It may be worth to have a look in Python sources.

bogen85 commented 1 year ago

Looks like glibc has the ability to look up the home directory of another user.

#include <stdio.h>
#include <pwd.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <username>\n", argv[0]);
        return 1;
    }

    struct passwd *pw = getpwnam(argv[1]);
    if (pw == NULL) {
        printf("User '%s' not found\n", argv[1]);
        return 1;
    }

    printf("Home directory for user '%s': %s\n", argv[1], pw->pw_dir);
    return 0;
}
bogen85 commented 1 year ago

Well, I just asked OpenAI how do I in C on linux look up the directory for another user? Show an example C program where the user name is passed in on the command line. and it gave me the above sample program. (which I confirmed does work).

gergelyk commented 1 year ago

Excelent :) I have found the same in Python sources one minute later. Just for the record: https://man7.org/linux/man-pages/man3/getpwnam.3.html

bogen85 commented 1 year ago

https://www.unix.com/man-page/osx/3/getpwnam/ is on MacOS as well. https://www.unix.com/man-page/FreeBSD/3/getpwnam/

On Windows it is not as simple.

JalonSolov commented 1 year ago

And all of this is fine for the C backend, but what about JavaScript and WASM?

bogen85 commented 1 year ago

And all of this is fine for the C backend, but what about JavaScript and WASM?

In the context of running in a web page are any user's home directories typically referenced directly, including the user that is running the web browser?

JalonSolov commented 1 year ago

You can run both JavaScript and WASM from the command line.

bogen85 commented 1 year ago

Do v libraries like term work in the JavaScript and WASM backends? https://github.com/vlang/v/tree/master/vlib/term

bogen85 commented 1 year ago

Or picoev? https://github.com/vlang/v/tree/master/vlib/picoev

gergelyk commented 1 year ago

If this cannot be implemented for JS and WASM, then we may need to check what design decisions have been made in vlang project. Namely whether stdlib should support only common set of features provided by each platform (to ensure portability), or should it support some of the features that are available selectively.

bogen85 commented 1 year ago

https://vlang.io/ V is said to be a systems programming language on it's main web page (the categories on what is listed be meant for).

On POSIX compliant operating systems this would seem to imply it is on par with with other systems programming languages that also run on the same POSIX compliant operating systems.

Otherwise in my opinion the systems programming language category that V is said to be in is questionable in my opinion.

bogen85 commented 1 year ago

That being said, not every POSIX function would need to be in a V library per-se, just easy access to them would be what is needed.

gergelyk commented 1 year ago

OK, this makes sense.

JalonSolov commented 1 year ago

Actually, the home pages specifically says

"Despite being simple, V gives a lot of power to the developer and can be used in pretty much every field, including systems programming, webdev, gamedev, GUI, mobile, science, embedded, tooling, etc."

No, V is not a "systems programming language". It is a general purpose programming language, which can be used for systems programming as well as pretty much any other type of programming.

bogen85 commented 1 year ago

Yeah, and interfacing with C functions is not difficult. I was just trying to make the point that not everything available to standard C needs to be available to V in a V library.