Created
October 21, 2010 14:07
-
-
Save kosh04/638543 to your computer and use it in GitHub Desktop.
[newLISP]文字列ポインタを扱う際のメモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* $ gcc -fPIC -shared environ.c -o environ.so && strip environ.so | |
* or | |
* $ gcc -DMAIN environ.c | |
* $ ./a.out | |
*/ | |
extern char **environ; | |
char **envp() { | |
return environ; | |
} | |
#ifdef MAIN | |
#include <stdio.h> | |
int main(void) | |
{ | |
char **env; | |
env = envp(); | |
while (*env) | |
puts(*env++); | |
return 0; | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/newlisp | |
;; 共有ライブラリからの返り値が文字列のポインタだったりすると | |
;; アーキテクチャによって型のサイズが違うので処理が面倒だねという話。 | |
;; len_void* == sizeof(void *); | |
(cond | |
;; 64-bit | |
((= 256 (& 256 (sys-info 9))) | |
(define get-ptr get-long) | |
(define len_void* 8)) | |
;; 32-bit | |
(true | |
(define get-ptr get-int) | |
(define len_void* 4))) | |
(define NULL 0) | |
(import "./environ.so" "envp") ; 付属のenviron.cからshared-objectファイルを作成しておく | |
(import "libc.so.6" "puts") | |
(import "libc.so.6" "printf") | |
(setf environ (envp)) | |
(while (!= (get-ptr environ) NULL) | |
(printf "%s\n" (get-ptr environ)) | |
(++ environ len_void*) | |
) | |
(exit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment