mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:47:35 +00:00
Userland: Use Core::ArgsParser for 'hostname'
This commit is contained in:
parent
718a45ef71
commit
31a4a2cc2a
1 changed files with 21 additions and 18 deletions
|
@ -24,6 +24,7 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -31,24 +32,26 @@
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (argc == 1) {
|
const char* hostname = nullptr;
|
||||||
char buffer[HOST_NAME_MAX];
|
|
||||||
int rc = gethostname(buffer, sizeof(buffer));
|
|
||||||
if (rc < 0) {
|
|
||||||
printf("gethostname() error: %s\n", strerror(errno));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printf("%s\n", buffer);
|
|
||||||
}
|
|
||||||
else if (argc == 2) {
|
|
||||||
if (strlen(argv[1]) >= HOST_NAME_MAX) {
|
|
||||||
printf("hostname must be less than %i characters\n", HOST_NAME_MAX);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sethostname(argv[1], strlen(argv[1]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.add_positional_argument(hostname, "Hostname to set", "hostname", Core::ArgsParser::Required::No);
|
||||||
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (!hostname) {
|
||||||
|
char buffer[HOST_NAME_MAX];
|
||||||
|
int rc = gethostname(buffer, sizeof(buffer));
|
||||||
|
if (rc < 0) {
|
||||||
|
perror("gethostname");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
} else {
|
||||||
|
if (strlen(hostname) >= HOST_NAME_MAX) {
|
||||||
|
fprintf(stderr, "Hostname must be less than %i characters\n", HOST_NAME_MAX);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
sethostname(hostname, strlen(hostname));
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue