2021-12-29 03:16:43 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/hostname/hostname_main.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-03 22:57:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_usage
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void show_usage(FAR const char *progname)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [<hostname>|-F <file>]\n", progname);
|
|
|
|
}
|
|
|
|
|
2021-12-29 03:16:43 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
int ret;
|
2022-07-03 22:57:21 +02:00
|
|
|
bool set = false;
|
|
|
|
FAR FILE *f;
|
|
|
|
char hostname[HOST_NAME_MAX + 2];
|
|
|
|
|
|
|
|
while ((ret = getopt(argc, argv, ":F:h")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case 'F':
|
|
|
|
set = true;
|
|
|
|
f = fopen(optarg, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to open '%s': %s\n", optarg,
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fgets(hostname, sizeof(hostname), f) == NULL)
|
|
|
|
{
|
|
|
|
if (errno != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to read '%s': %s\n",
|
|
|
|
optarg, strerror(errno));
|
|
|
|
fclose(f);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*strchrnul(hostname, '\n') = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "ERROR: Option needs a value: '%c'\n", optopt);
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "ERROR: Unrecognized option: '%c'\n", optopt);
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc && !set)
|
|
|
|
{
|
|
|
|
set = true;
|
|
|
|
strlcpy(hostname, argv[optind++], sizeof(hostname));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Too many arguments\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-12-29 03:16:43 +01:00
|
|
|
|
2022-07-03 22:57:21 +02:00
|
|
|
if (set)
|
2021-12-29 03:16:43 +01:00
|
|
|
{
|
2022-07-03 22:57:21 +02:00
|
|
|
if (strlen(hostname) == 0 || strlen(hostname) > HOST_NAME_MAX)
|
2021-12-31 15:58:44 +01:00
|
|
|
{
|
2022-07-03 22:57:21 +02:00
|
|
|
fprintf(stderr, "ERROR: The specified hostname is invalid\n");
|
2021-12-31 15:58:44 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-07-03 22:57:21 +02:00
|
|
|
ret = sethostname(hostname, strlen(hostname));
|
2021-12-29 03:16:43 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-07-03 22:57:21 +02:00
|
|
|
fprintf(stderr, "sethostname() returned %d\n", ret);
|
2021-12-29 03:16:43 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = gethostname(hostname, HOST_NAME_MAX);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-07-03 22:57:21 +02:00
|
|
|
fprintf(stderr, "gethostname() returned %d\n", ret);
|
2021-12-29 03:16:43 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s\n", hostname);
|
|
|
|
}
|
|
|
|
|
2021-12-31 15:58:44 +01:00
|
|
|
return EXIT_SUCCESS;
|
2021-12-29 03:16:43 +01:00
|
|
|
}
|