(root)/
coreutils-9.4/
gnulib-tests/
test-gethostname.c
       1  /*
       2   * Copyright (C) 2008-2023 Free Software Foundation, Inc.
       3   * Written by Simon Josefsson.
       4   *
       5   * This program is free software: you can redistribute it and/or modify
       6   * it under the terms of the GNU General Public License as published by
       7   * the Free Software Foundation, either version 3 of the License, or
       8   * (at your option) any later version.
       9   *
      10   * This program is distributed in the hope that it will be useful,
      11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13   * GNU General Public License for more details.
      14   *
      15   * You should have received a copy of the GNU General Public License
      16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <config.h>
      19  
      20  /* Get gethostname() declaration.  */
      21  #include <unistd.h>
      22  
      23  #include "signature.h"
      24  SIGNATURE_CHECK (gethostname, int, (char *, size_t));
      25  
      26  /* Get HOST_NAME_MAX definition.  */
      27  #include <limits.h>
      28  
      29  #include <stdio.h>
      30  #include <string.h>
      31  #include <errno.h>
      32  
      33  #define NOHOSTNAME "magic-gnulib-test-string"
      34  
      35  int
      36  main (int argc, _GL_UNUSED char *argv[])
      37  {
      38    char buf[HOST_NAME_MAX];
      39    int rc;
      40  
      41    if (strlen (NOHOSTNAME) >= HOST_NAME_MAX)
      42      {
      43        printf ("HOST_NAME_MAX impossibly small?! %d\n", HOST_NAME_MAX);
      44        return 2;
      45      }
      46  
      47    strcpy (buf, NOHOSTNAME);
      48  
      49    rc = gethostname (buf, sizeof (buf));
      50  
      51    if (rc != 0)
      52      {
      53        printf ("gethostname failed, rc %d errno %d\n", rc, errno);
      54        return 1;
      55      }
      56  
      57    if (strcmp (buf, NOHOSTNAME) == 0)
      58      {
      59        printf ("gethostname left buffer untouched.\n");
      60        return 1;
      61      }
      62  
      63    if (argc > 1)
      64      printf ("hostname: %s\n", buf);
      65  
      66    return 0;
      67  }