Remove old CVS $Id$ keyword
[uffi.git] / tests / gethostname.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          gethostname.lisp
6 ;;;; Purpose:       UFFI Example file to get hostname of system
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; *************************************************************************
13
14 (in-package #:uffi-tests)
15
16
17 ;;; This example is inspired by the example on the CL-Cookbook web site
18
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20   (uffi:def-function ("gethostname" c-gethostname)
21       ((name (* :unsigned-char))
22        (len :int))
23     :returning :int)
24
25   (defun gethostname ()
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."))
34       hostname))
35
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.")))))
42
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)
48
49
50