~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to pith/osdep/hostname.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !defined(lint) && !defined(DOS)
 
2
static char rcsid[] = "$Id: hostname.c 312 2006-12-11 18:06:32Z hubert@u.washington.edu $";
 
3
#endif
 
4
 
 
5
/*
 
6
 * ========================================================================
 
7
 * Copyright 2006 University of Washington
 
8
 *
 
9
 * Licensed under the Apache License, Version 2.0 (the "License");
 
10
 * you may not use this file except in compliance with the License.
 
11
 * You may obtain a copy of the License at
 
12
 *
 
13
 *     http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 * ========================================================================
 
16
 */
 
17
 
 
18
#include <system.h>
 
19
 
 
20
#if     HAVE_GETHOSTNAME
 
21
 
 
22
#elif   HAVE_UNAME
 
23
 
 
24
#include <sys/utsname.h>
 
25
 
 
26
#elif   defined(SYSTEMID)
 
27
 
 
28
#elif   defined(XENIX)
 
29
 
 
30
#endif
 
31
 
 
32
#include "hostname.h"
 
33
 
 
34
 
 
35
 
 
36
/*----------------------------------------------------------------------
 
37
       Call system gethostname
 
38
 
 
39
  Args: hostname -- buffer to return host name in 
 
40
        size     -- Size of buffer hostname is to be returned in
 
41
 
 
42
 Result: returns 0 if the hostname is correctly set,
 
43
         -1 if not (and errno is set).
 
44
 ----*/
 
45
int
 
46
hostname(char *hostname, int size)
 
47
{
 
48
#if     HAVE_GETHOSTNAME
 
49
 
 
50
    return(gethostname(hostname, size));
 
51
 
 
52
#elif   HAVE_UNAME
 
53
 
 
54
    /** This routine compliments of Scott McGregor at the HP
 
55
            Corporate Computing Center **/
 
56
     
 
57
    int uname(struct utsname *);
 
58
    struct utsname name;
 
59
 
 
60
    (void)uname(&name);
 
61
    (void)strncpy(hostname,name.nodename,size-1);
 
62
 
 
63
    hostname[size - 1] = '\0';
 
64
    return 0;
 
65
 
 
66
#elif   defined(SYSTEMID)
 
67
        char    buf[32];
 
68
        FILE    *fp;
 
69
        char    *p;
 
70
 
 
71
        if ((fp = our_fopen("/etc/systemid", "rb")) != 0) {
 
72
          fgets(buf, sizeof(buf) - 1, fp);
 
73
          fclose(fp);
 
74
          if ((p = strindex(buf, '\n')) != NULL)
 
75
            *p = '\0';
 
76
          (void) strncpy(hostname, buf, size - 1);
 
77
          hostname[size - 1] = '\0';
 
78
          return 0;
 
79
        }
 
80
 
 
81
#elif   defined(XENIX)
 
82
 
 
83
#ifdef DOUNAME
 
84
        /** This routine compliments of Scott McGregor at the HP
 
85
            Corporate Computing Center **/
 
86
     
 
87
        int uname();
 
88
        struct utsname name;
 
89
 
 
90
        (void) uname(&name);
 
91
        (void) strncpy(hostname,name.nodename,size-1);
 
92
#else
 
93
        (void) strncpy(hostname, HOSTNAME, size-1);
 
94
#endif  /* DOUNAME */
 
95
 
 
96
        hostname[size - 1] = '\0';
 
97
        return 0;
 
98
#else
 
99
        /* We shouldn't get here except for the windows
 
100
         * case, which currently doesn't use this (as
 
101
         * it appears nothing else does as well)
 
102
         */
 
103
        return -1;
 
104
 
 
105
#endif
 
106
}
 
107
 
 
108