1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: gethostname.lisp
6 ;;;; Purpose: UFFI Example file to get hostname of system
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
12 ;;;; *************************************************************************
14 (in-package #:uffi-tests)
17 ;;; This example is inspired by the example on the CL-Cookbook web site
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20 (uffi:def-function ("gethostname" c-gethostname)
21 ((name (* :unsigned-char))
26 "Returns the hostname"
27 (let* ((name (uffi:allocate-foreign-string 256))
28 (result-code (c-gethostname name 256))
29 (hostname (when (zerop result-code)
30 (uffi:convert-from-foreign-string name))))
31 (uffi:free-foreign-object name)
32 (unless (zerop result-code)
33 (error "gethostname() failed."))
36 (defun gethostname2 ()
37 "Returns the hostname"
38 (uffi:with-foreign-object (name '(:array :unsigned-char 256))
39 (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
40 (uffi:convert-from-foreign-string name)
41 (error "gethostname() failed.")))))
43 (deftest :gethostname.1 (stringp (gethostname)) t)
44 (deftest :gethostname.2 (stringp (gethostname2)) t)
45 (deftest :gethostname.3 (plusp (length (gethostname))) t)
46 (deftest :gethostname.4 (plusp (length (gethostname2))) t)
47 (deftest :gethostname.5 (string= (gethostname) (gethostname2)) t)