I need to install a package such as Curl on Linux, but the problem is that when it comes to installing it, I don’t know how to enter the superuser password when prompted.
Here is the function I’m using.
Assuming you are making a cli program, you probably just want to let sudo ask the user for the password. sudo should automatically ask for the password on the tty through /dev/tty even when stdin and stdout are redirected.
Yes invoking sudo is the correct design here if you want the user to run something with elevated privileges. For a note, do not try and read the password and shuffle that to sudo. Let sudo have exclusive access to the terminal temporarily or something like that. Besides the obvious security reasons, a lot of people (like me) don't use passwords for sudo. They might use USB keys or fingerprint sensors, things like that. There is no guarantee your program has the ability to collect data to elevate privileges on a computer.
For installing packages specifically you may also try using PackageKit. This is an abstraction over the system package manager that comes with polkit integration to have your desktop environment show a popup to authenticate. Not everyone has PackageKit installed though, so you may still want a sudo fallback. Various other admin tasks also have ways to have authorization mediated by polkit.