File: hostname.c

package info (click to toggle)
sn 0.3.8-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 976 kB
  • sloc: ansic: 9,255; sh: 467; makefile: 210
file content (54 lines) | stat: -rw-r--r-- 1,134 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright � 1998-2000 Harold Tay.
 * Copyright � 2000- Patrik R�dman.
 */

#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include "config.h"

static const char ver_ctrl_id[] = "$Id: hostname.c 29 2004-04-24 23:02:38Z patrik $";

char *myname (void)
{
   int fd;
   char *host = NULL;
   char buf[256];
   int count;

   if ((fd = open(".me", O_RDONLY)) > -1)
   {
      if ((count = read(fd, buf, sizeof (buf) - 1)) > 0)
      {
         char *cp;

         buf[count] = '\0';
         if ((cp = strchr(buf, '\n')))
            *cp = '\0';
         if ((cp = strchr(buf, '\r')))
            *cp = '\0';
         host = strdup(buf);
      }
      close(fd);
   }

   if (!host)
   {
      struct hostent *hp;

      if (0 == gethostname(buf, sizeof (buf)))
      {
         hp = gethostbyname(buf);
         if (hp)
            host = (char *) hp->h_name; /* silence cc, sometimes */
      }
   }
   if (NULL == host)
      host = strdup("localhost");
   return host;
}