Shenghou Ma | 1e95429 | 2012-08-07 09:38:35 +0800 | [diff] [blame] | 1 | // skip |
| 2 | |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 3 | // Copyright 2009 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
| 7 | /* |
| 8 | A trivial example of wrapping a C library in Go. |
| 9 | For a more complex example and explanation, |
| 10 | see ../gmp/gmp.go. |
| 11 | */ |
| 12 | |
| 13 | package stdio |
| 14 | |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 15 | /* |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
Russ Cox | 0432f28 | 2010-07-14 17:17:53 -0700 | [diff] [blame] | 18 | #include <sys/stat.h> |
| 19 | #include <errno.h> |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 20 | |
Russ Cox | 0432f28 | 2010-07-14 17:17:53 -0700 | [diff] [blame] | 21 | char* greeting = "hello, world"; |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 22 | */ |
| 23 | import "C" |
| 24 | import "unsafe" |
| 25 | |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 26 | type File C.FILE |
| 27 | |
Russ Cox | 6c6d530 | 2010-12-17 11:37:11 -0800 | [diff] [blame] | 28 | // Test reference to library symbol. |
| 29 | // Stdout and stderr are too special to be a reliable test. |
Russ Cox | c3f4319 | 2012-03-06 23:27:30 -0500 | [diff] [blame] | 30 | //var = C.environ |
Russ Cox | 6c6d530 | 2010-12-17 11:37:11 -0800 | [diff] [blame] | 31 | |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 32 | func (f *File) WriteString(s string) { |
Robert Griesemer | 5a1d332 | 2009-12-15 15:33:31 -0800 | [diff] [blame] | 33 | p := C.CString(s) |
Russ Cox | 0432f28 | 2010-07-14 17:17:53 -0700 | [diff] [blame] | 34 | C.fputs(p, (*C.FILE)(f)) |
Robert Griesemer | 5a1d332 | 2009-12-15 15:33:31 -0800 | [diff] [blame] | 35 | C.free(unsafe.Pointer(p)) |
Russ Cox | 0432f28 | 2010-07-14 17:17:53 -0700 | [diff] [blame] | 36 | f.Flush() |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 37 | } |
Russ Cox | 0432f28 | 2010-07-14 17:17:53 -0700 | [diff] [blame] | 38 | |
| 39 | func (f *File) Flush() { |
| 40 | C.fflush((*C.FILE)(f)) |
| 41 | } |
| 42 | |
| 43 | var Greeting = C.GoString(C.greeting) |
Russ Cox | db9229d | 2011-07-28 12:39:50 -0400 | [diff] [blame] | 44 | var Gbytes = C.GoBytes(unsafe.Pointer(C.greeting), C.int(len(Greeting))) |