1 /* /////////////////////////////////////////////////////////////////////////
2  * File:    hostname.c
3  *
4  * Purpose: Implementation of the gethostname() function.
5  *
6  * Created: 20th April 2008
7  * Updated: 12th August 2010
8  *
9  * Home:    http://synesis.com.au/software/
10  *
11  * Copyright (c) 2008-2010, Matthew Wilson and Synesis Software
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met:
17  *
18  * - Redistributions of source code must retain the above copyright notice,
19  *   this list of conditions and the following disclaimer.
20  * - Redistributions in binary form must reproduce the above copyright
21  *   notice, this list of conditions and the following disclaimer in the
22  *   documentation and/or other materials provided with the distribution.
23  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
24  *   names of any contributors may be used to endorse or promote products
25  *   derived from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  * ////////////////////////////////////////////////////////////////////// */
40 
41 
42 #ifndef UNIXEM_DOCUMENTATION_SKIP_SECTION
43 # define _SYNSOFT_VER_C_HOSTNAME_MAJOR      2
44 # define _SYNSOFT_VER_C_HOSTNAME_MINOR      0
45 # define _SYNSOFT_VER_C_HOSTNAME_REVISION   1
46 # define _SYNSOFT_VER_C_HOSTNAME_EDIT       7
47 #endif /* !UNIXEM_DOCUMENTATION_SKIP_SECTION */
48 
49 /* /////////////////////////////////////////////////////////////////////////
50  * Includes
51  */
52 
53 #include <unixem/unistd.h>
54 
55 #include <unixem/unixem.h>
56 #include <unixem/internal/safestr.h>
57 #include <unixem/internal/util.h>
58 
59 #include <assert.h>
60 #include <errno.h>
61 #include <stdlib.h>
62 #include <windows.h>
63 
64 /* /////////////////////////////////////////////////////////////////////////
65  * API functions
66  */
67 
unixem_gethostname(char * name,size_t cchName)68 int __stdcall unixem_gethostname(
69     char*   name
70 ,   size_t  cchName
71 )
72 {
73     DWORD   cchName_;
74 
75     assert(0 == cchName || NULL != name);
76 
77     cchName_ = (DWORD)cchName;
78 
79     if(!GetComputerNameA(name, &cchName_))
80     {
81         if(ERROR_BUFFER_OVERFLOW != GetLastError())
82         {
83             errno = unixem_internal_errno_from_Win32(GetLastError());
84 
85             return -1;
86         }
87         else
88         {
89             errno = ENAMETOOLONG;
90         }
91     }
92 #if 0
93     else
94     {
95         errno = 0;
96     }
97 #endif /* 0 */
98 
99     return 0;
100 }
101 
102 /* ///////////////////////////// end of file //////////////////////////// */
103