hostname (empty) → 1.0
raw patch · 4 files changed
+96/−0 lines, 4 filesdep +Win32dep +basesetup-changed
Dependencies added: Win32, base
Files
- LICENSE +22/−0
- Network/HostName.hs +48/−0
- Setup.lhs +4/−0
- hostname.cabal +22/−0
+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2008, Maximilian Bolingbroke+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted+provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright notice, this list of+ conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright notice, this list of+ conditions and the following disclaimer in the documentation and/or other materials+ provided with the distribution.+ * Neither the name of Maximilian Bolingbroke nor the names of other contributors may be used to+ endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Network/HostName.hs view
@@ -0,0 +1,48 @@+module Network.HostName (+ HostName, getHostName+ ) where++import Foreign.C.Error+import Foreign.C.String+import Foreign.C.Types+import Foreign.Marshal.Array++#ifdef WINDOWS++import Foreign.Marshal.Utils+import Foreign.Ptr+import Foreign.Storable++import System.Win32.Types++foreign import stdcall unsafe "windows.h GetComputerNameExW" getComputerNameEx :: COMPUTER_NAME_FORMAT -> LPTSTR -> LPDWORD -> IO BOOL++type COMPUTER_NAME_FORMAT = CInt++computerNamePhysicalDnsHostname :: COMPUTER_NAME_FORMAT+computerNamePhysicalDnsHostname = 5++getHostName :: IO HostName+getHostName = with 0 $ \p_charcount -> do+ -- On the first run, determine the character count and ignore any error we get+ _ <- getComputerNameEx computerNamePhysicalDnsHostname nullPtr p_charcount+ charcount <- peek p_charcount+ + -- The second time around, use the correct character count to retrieve the data+ withTString (replicate (fromIntegral charcount) ' ') $ \name -> do+ failIfFalse_ "GetComputerNameExW" $ getComputerNameEx computerNamePhysicalDnsHostname name p_charcount+ peekTString name++#else++foreign import ccall unsafe "gethostname" gethostname :: CString -> CSize -> IO CInt++getHostName :: IO HostName+getHostName = allocaArray0 size $ \cstr -> do+ throwErrnoIfMinus1_ "getHostName" $ gethostname cstr (fromIntegral size)+ peekCString cstr+ where size = 256++#endif++type HostName = String
+ Setup.lhs view
@@ -0,0 +1,4 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> main = defaultMain
+ hostname.cabal view
@@ -0,0 +1,22 @@+Name: hostname+Version: 1.0+Cabal-Version: >= 1.2+Category: Network+Synopsis: A very simple package providing a cross-platform means of determining the hostname+License: BSD3+License-File: LICENSE+Author: Max Bolingbroke <batterseapower@hotmail.com>+Maintainer: Max Bolingbroke <batterseapower@hotmail.com>+Build-Type: Simple++Library+ Exposed-Modules: Network.HostName+ + Build-Depends: base >= 3 && < 5+ + Extensions: CPP, ForeignFunctionInterface+ + if os(windows)+ Build-Depends: Win32 >= 2.0+ Cpp-Options: -DWINDOWS+ Extra-Libraries: "kernel32"