<?xml version="1.0" encoding="UTF-8" ?>
<feed xml:lang="ja" xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0">
  <title type="text">とある現場の組込みエンジニア</title>
  <subtitle type="html">組込み現場の若手エンジニアが無責任な情報を垂れ流します！
</subtitle>
  <link rel="self" type="application/atom+xml" href="https://nex2t.blog.shinobi.jp/atom"/>
  <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/"/>
  <updated>2012-02-24T20:18:36+09:00</updated>
  <author><name>nex2t</name></author>
  <generator uri="//www.ninja.co.jp/blog/" version="0.9">忍者ブログ</generator>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" />
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/33</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/linux/apt-get%E3%81%AE%E3%82%A4%E3%83%BC%E3%82%B9%E3%82%BF%E3%83%BC%E3%82%A8%E3%83%83%E3%82%B0" />
    <published>2013-08-21T22:41:42+09:00</published> 
    <updated>2013-08-21T22:41:42+09:00</updated> 
    <category term="Linux" label="Linux" />
    <title>apt-getのイースターエッグ</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[イースターエッグって知ってますか？<br />
ソフトウェア開発者が遊び心でプログラムに入れる機能ですね。<br />
特定のボタン同時押しすると、シグナルが飛んで行ったりして、<br />
LEDを一定時間クリスマスツリーのようにチカチカさせるとかね。<br />
(まぁ遊び機能を付け加えるほど、立派でないし、余裕もないし、その力を開発に入れるべきですね。。)<br />
<br />
最近イースターエッグのあるプログラムを知ったので書いてみる。<br />
apt-getに 「moo」をつけると、&darr;が出ます。<br />
<br />
<a href="//nex2t.blog.shinobi.jp/File/apt-get-moo.png" target="_blank"><img alt="" border="0" src="//nex2t.blog.shinobi.jp/Img/1377092261/" /></a><br />
<br />
牛www<br />
<br />
<br />
aptitudeコマンドにもあるみたいです、そっちは探すの苦戦するかもです笑<br />
ヒントはaptitude -h！<br />
終わり]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/32</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/%E3%82%B3%E3%83%9F%E3%82%B182/%E4%BC%9A%E5%A0%B4%E3%81%AE%E6%A7%98%E5%AD%90" />
    <published>2013-08-21T01:00:28+09:00</published> 
    <updated>2013-08-21T01:00:28+09:00</updated> 
    <category term="コミケ82" label="コミケ82" />
    <title>会場の様子</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[1年放置してしまったので、再開していきたいと思います(葬式のようなテンションでこの文を書いています)<br />
この1年はDDRばかり、ほんとうにDDRばっかりしていました。(これからも続ける所存です)<br />
DDRのことはこちら&rarr;<a href="https://twitter.com/nex2t_ddr">https://twitter.com/nex2t_ddr</a><br />
<br />
とりあえず前回の記事からの続きとして会場の様子などupしてみます。<br />
<br />
ちょっと斜めから全体を。<br />
<a href="//nex2t.blog.shinobi.jp/File/a9d16d97.jpeg" target="_blank"><img alt="" border="0" src="//nex2t.blog.shinobi.jp/Img/1377014150/" /></a><br />
<br />
<br />
サーバ(デーモン)に接続して、現在ログイン中のユーザーをGUIでリスト表示し、<br />
相手に対戦を申し込む画面。ネットワークにつながっていないので、ローカルホスト内で<br />
サーバもクライアントも動いています。<br />
<a href="//nex2t.blog.shinobi.jp/File/IMG_2559.JPG" target="_blank"><img alt="" border="0" src="//nex2t.blog.shinobi.jp/Img/1377014149/" /></a><br />
<br />
<br />
電源がなかったため、マルチディスプレイとかもできず、印刷したものを置いてるww<br />
<a href="//nex2t.blog.shinobi.jp/File/IMG_2558.JPG" target="_blank"><img alt="" border="0" src="//nex2t.blog.shinobi.jp/Img/1377014148/" /></a><br />
<br />
<br />
正面 (ちゃっちいですね)<br />
<a href="//nex2t.blog.shinobi.jp/File/IMG_2557.JPG" target="_blank"><img alt="" border="0" src="//nex2t.blog.shinobi.jp/Img/1377014147/" /></a><br />
<br />
人が(珍しく)来たら、PCの画面上で実際に、対戦開始して、駒が勝手に更新して動いていくのを<br />
ディスプレイしてました。<br />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/31</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/nex2t%20shogi/nex2t%20shogi%20%E3%81%AE%E3%83%9A%E3%83%BC%E3%82%B8" />
    <published>2012-08-20T07:05:35+09:00</published> 
    <updated>2012-08-20T07:05:35+09:00</updated> 
    <category term="nex2t shogi" label="nex2t shogi" />
    <title>nex2t shogi のページ</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[8/20<br />
C82でブースに来ていただいた方、ありがとうございました。<br />
C82で領布させていただいたnex2t shogi(&beta;版)ですが、<br />
今日月曜日修正したものをアップする予定だったのですが、<br />
本業のほうが少々忙しく進みが芳しくないので、<br />
ちゃんとしたものをアップできるのは、9月半ばごろになりそうです。<br />
(きちんと終わらせるってのは大変ですね。。)<br />
<br />
とりあえず領布したものを公開したいと思います。<br />
<a href="//nex2t.blog.shinobi.jp/File/nex2t_shogi.zip" target="_blank">領布したもの(zip)</a><br />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/29</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/%E3%82%B3%E3%83%9F%E3%82%B182/%E3%80%90%E3%82%B3%E3%83%9F%E3%82%B1%E5%B0%82%E7%94%A8%E3%83%9A%E3%83%BC%E3%82%B8%E3%80%91linux%E3%83%8D%E3%83%83%E3%83%88%E5%AF%BE%E6%88%A6%E5%B0%86%E6%A3%8B%E3%82%B2%E3%83%BC%E3%83%A0" />
    <published>2012-08-01T05:33:33+09:00</published> 
    <updated>2012-08-01T05:33:33+09:00</updated> 
    <category term="コミケ82" label="コミケ82" />
    <title>【コミケ専用ページ】Linuxネット対戦将棋ゲーム</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[コミケC82でゲームを出します。<br />
参加日は8/11日(土）、場所は「西は09b」です。<br />
サークル名はハンネと同じnex2tです。<br />
<br />
出し物はネット対戦できる将棋ゲームです。<br />
まだまだ機能を付けきれていないので、<br />
&beta;版としてCD-ROMを無料配布させていただきます。<br />
(&beta;版じゃなくてもずっと無料です)<br />
将棋・Linux・GTK+・ネットワークプログラミング・C言語など<br />
が好きな方興味がある方、ぜひもらっていってください！<br />
<br />
将棋のゲーム作り中画面&darr;です。<br />
<hr />
<a href="//nex2t.blog.shinobi.jp/File/Screenshot-3.png" target="_blank"><img alt="" border="0" src="//nex2t.blog.shinobi.jp/Img/1335192805/" /></a>
<hr /><br />
<br />
以下ソフトウェア説明。<br />
★は現在制限機能<br />
ソフトウェア名：nex2t shogi<br />
概要：普通の将棋に無いカオスフルなルールで対戦できる将棋ソフト<br />
動作環境：Linux (現状Windowsでは動きません)<br />
主な機能<br />
・ネットワーク対戦可能 (世界中の人とできます)<br />
・ユーザ情報登録可(戦歴や通信切れなどが記憶されます)(メアドなどわずらわしい登録は無いです。)<br />
・CPU対戦モード有★<br />
・チャット可★<br />
・棋譜記録・再生・出力機能★<br />
・対戦見学★<br />
・カオスフルなルール★<br />
ワープ (指定した駒を好きな場所へ移動できる)<br />
地雷&nbsp;&nbsp;&nbsp;&nbsp; (指定した場所を踏んだ駒を消滅させられる)<br />
消滅&nbsp;&nbsp;&nbsp;&nbsp; (指定した駒を消滅させられる)<br />
合体&nbsp;&nbsp;&nbsp;&nbsp; (駒と駒を合体できる) (駒の効きを単純に合体させるか、足し算にするかは検討中)<br />
伏兵&nbsp;&nbsp;&nbsp;&nbsp; (指定したタイミングであらかじめ配置しておいた駒を出現させることができる)<br />
ランダムスタート (初期配置をランダムにできる)<br />
場所入れ替え&nbsp;&nbsp;&nbsp;&nbsp; (盤上の2つの味方駒を入れ替えできる)<br />
貫通&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (1マスのみ壁を乗り越えさせて効かせられる)<br />
裏切り&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (盤上の相手の歩・桂・香を味方に変更できる)<br />
二歩あり&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (二歩で駒を置ける)<br />
etc..<br />
～カオスフルルールについて～<br />
カオスフルなルールは、対戦開始時に使用したいカオス機能をカオスフルゲージの範囲<br />
内で好きなように選択して対戦できるものです。<br />
有益なカオス機能であればあるほどゲージを消費するためカオス機能によっては1つしか<br />
カオス機能を選択できない<br />
<br />
みたいなゲームです。<br />
<br />
ということでして、上記制限事項のように配布する際はカオスなルールで将棋できないのですが、<br />
普通に将棋はできるとこまでしていきたいと思います。(ロビーに入って対戦を始める部分がまだ未完成です。。)<br />
<br />
よろしくお願いいたします。<br />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/28</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/c%E8%A8%80%E8%AA%9E/strncpy%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E5%AE%89%E5%85%A8%E3%81%AA%E6%96%87%E5%AD%97%E5%88%97%E3%82%B3%E3%83%94%E3%83%BC-c%E8%A8%80%E8%AA%9E-" />
    <published>2012-06-30T13:06:37+09:00</published> 
    <updated>2012-06-30T13:06:37+09:00</updated> 
    <category term="C言語" label="C言語" />
    <title>strncpyを使った安全な文字列コピー(C言語)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<pre>
コピー元の文字列の長さが不明な時用のメモリ破壊を起こさない
文字列コピーの方法について、strncpyの動作を見ながら確認したメモ
結論から言うと
strncpy(dst, src, DST_BUFF_LEN);
dst[DST_BUFF_LEN-1] = &#39;\0&#39;;
すれば破壊は起きない、ただsrcをコピーしきれないことがある。
下記のソースを試したコンパイラバージョンは
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)です。
</pre>
<hr />
<h3>
	ソースコード</h3>
<pre class="brush: c">
/* nex2t_strncpy.c */
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define EX_STR &quot;0123456789abcdefghij&quot;
#define EX_STR2 &quot;01234&quot;
#define BUF_LEN     16
#define DEBUG_LINE &quot;---------------------------------------------------------\n&quot;

int main(void)
{
	int i;
	char buf[BUF_LEN];

	/* -------------------------------------------------------------- */
	printf(DEBUG_LINE);
	printf(&quot;use buffer buf[BUF_LEN] BUF_LEN=%d\n&quot;, BUF_LEN);
	printf(DEBUG_LINE);
	printf(&quot;EX_STR:%s [strlen(EX_STR):%ld, sizeof(EX_STR):%ld]\n&quot;, EX_STR,
	       strlen(EX_STR), sizeof(EX_STR));
	printf(&quot;       (over buf[BUF_LEN] size!!)\n&quot;);
	printf(DEBUG_LINE);
	printf(&quot;EX_STR2:%s [strlen(EX_STR2):%ld, sizeof(EX_STR2):%ld]\n&quot;,
	       EX_STR2, strlen(EX_STR2), sizeof(EX_STR2));
	printf(&quot;       (not over buf[BUF_LEN] size)\n&quot;);
	printf(DEBUG_LINE &quot;\n&quot;);


	/* ---- Init after ----------------------------------------------- */
	printf(&quot;Init after\n&quot;);
	printf(DEBUG_LINE);
	for (i = 0; i &lt; BUF_LEN; i++) {
		buf[i] = &#39;\0&#39;;
	}
	for (i = 0; i &lt; BUF_LEN; i++) {
		printf(&quot;buf[%d] = %d\n&quot;, i, buf[i]);
	}
	printf(&quot;buf[BUF_LEN] = %d  (secretly watch.)\n\n&quot;, buf[BUF_LEN]);


	/* ---- Normal_1 ------------------------------------------------ */
	printf(&quot;Normal_1\n&quot;);
	printf(DEBUG_LINE);
	strncpy(buf, EX_STR, 5);
	printf(&quot;strncpy(buf, EX_STR, 5);\n&quot;);
	printf(&quot;result:string:%s [strlen(buf):%ld, sizeof(buf):%ld]\n&quot;, buf,
	       strlen(buf), sizeof(buf));
	for (i = 0; i &lt; BUF_LEN; i++) {
		printf(&quot;buf[%d] = %d\n&quot;, i, buf[i]);
	}
	printf(&quot;buf[BUF_LEN] = %d  (secretly watch.)\n\n&quot;, buf[BUF_LEN]);
	/* init */
	for (i = 0; i &lt; BUF_LEN; i++) {
		buf[i] = &#39;\0&#39;;
	}

	/* ---- Normal_2 ----------------------------------------------- */
	printf(&quot;Normal_2\n&quot;);
	printf(DEBUG_LINE);
	strncpy(buf, EX_STR, BUF_LEN);
	printf(&quot;strncpy(buf, EX_STR, BUF_LEN);\n&quot;);
	printf(&quot;result:string:%s [strlen(buf):%ld, sizeof(buf):%ld]\n&quot;, buf,
	       strlen(buf), sizeof(buf));
	for (i = 0; i &lt; BUF_LEN; i++) {
		printf(&quot;buf[%d] = %d\n&quot;, i, buf[i]);
	}
	printf(&quot;buf[BUF_LEN] = %d  (secretly watch.)\n\n&quot;, buf[BUF_LEN]);
	printf(&quot;***Not write &#39;\\0&#39; automatically!! (buf[%d] = %d)\n&quot;,
	       BUF_LEN-1, buf[BUF_LEN-1]);
	printf(&quot;***Not write buf[BUF_LEN] (buf[%d] = %d)\n\n&quot;,
	       BUF_LEN, buf[BUF_LEN]);
	/* not init */
	/*
	for (i = 0; i &lt; BUF_LEN; i++) {
		buf[i] = &#39;\0&#39;;
	}
	*/

	/* ---- Normal_3 ----------------------------------------------- */
	printf(&quot;Normal_3\n&quot;);
	printf(DEBUG_LINE);
	strncpy(buf, EX_STR2, BUF_LEN);
	printf(&quot;strncpy(buf, EX_STR2, BUF_LEN);\n&quot;);
	printf(&quot;result:string:%s [strlen(buf):%ld, sizeof(buf):%ld]\n&quot;, buf,
	       strlen(buf), sizeof(buf));
	for (i = 0; i &lt; BUF_LEN; i++) {
		printf(&quot;buf[%d] = %d\n&quot;, i, buf[i]);
	}
	printf(&quot;buf[BUF_LEN] = %d  (secretly watch.)\n\n&quot;, buf[BUF_LEN]);
	printf(&quot;***automatically write &#39;\\0&#39; to buf[15]\n&quot;);

	/* ---- Extra ------------------------------------------------- */
	printf(&quot;\nExtra\n&quot;);
	printf(DEBUG_LINE);
	strncpy(buf, EX_STR, 0);
	printf(&quot;strncpy(buf, EX_STR, 0);\n&quot;);
	printf(&quot;result:string:%s [strlen(buf):%ld, sizeof(buf):%ld]\n&quot;, buf,
	       strlen(buf), sizeof(buf));
	for (i = 0; i &lt; BUF_LEN; i++) {
		printf(&quot;buf[%d] = %d\n&quot;, i, buf[i]);
	}
	printf(&quot;buf[BUF_LEN] = %d  (secretly watch.)\n\n&quot;, buf[BUF_LEN]);
	printf(&quot;Not write any.\n\n&quot;);


	/* ---- Right usage ------------------------------------------- */
	printf(&quot;Right usage\n&quot;);
	printf(DEBUG_LINE);
	strncpy(buf, EX_STR, BUF_LEN);
	buf[BUF_LEN-1] = &#39;\0&#39;;
	for (i = 0; i &lt; BUF_LEN; i++) {
		printf(&quot;buf[%d] = %d\n&quot;, i, buf[i]);
	}
	printf(&quot;buf[BUF_LEN] = %d  (secretly watch.)\n\n&quot;, buf[BUF_LEN]);

	printf(&quot;strncpy(buf, EX_STR, BUF_LEN); after buf[BUF_LEN-1] = &#39;\\0&#39;;&quot;);

	return 0;

}
</pre>
<hr />
<h3>
	コンパイル例</h3>
<pre class="brush: bash">
gcc -W -Wall -o nex2t_strncpy nex2t_strncpy.c
</pre>
<hr />
<h3>
	実行結果</h3>
<pre class="brush: bash">
./strncpy
---------------------------------------------------------
use buffer buf[BUF_LEN] BUF_LEN=16
---------------------------------------------------------
EX_STR:0123456789abcdefghij [strlen(EX_STR):20, sizeof(EX_STR):21]
       (over buf[BUF_LEN] size!!)
---------------------------------------------------------
EX_STR2:01234 [strlen(EX_STR2):5, sizeof(EX_STR2):6]
       (not over buf[BUF_LEN] size)
---------------------------------------------------------

Init after
---------------------------------------------------------
buf[0] = 0
buf[1] = 0
buf[2] = 0
buf[3] = 0
buf[4] = 0
buf[5] = 0
buf[6] = 0
buf[7] = 0
buf[8] = 0
buf[9] = 0
buf[10] = 0
buf[11] = 0
buf[12] = 0
buf[13] = 0
buf[14] = 0
buf[15] = 0
buf[BUF_LEN] = 0  (secretly watch.)

Normal_1
---------------------------------------------------------
strncpy(buf, EX_STR, 5);
result:string:01234 [strlen(buf):5, sizeof(buf):16]
buf[0] = 48
buf[1] = 49
buf[2] = 50
buf[3] = 51
buf[4] = 52
buf[5] = 0
buf[6] = 0
buf[7] = 0
buf[8] = 0
buf[9] = 0
buf[10] = 0
buf[11] = 0
buf[12] = 0
buf[13] = 0
buf[14] = 0
buf[15] = 0
buf[BUF_LEN] = 0  (secretly watch.)

Normal_2
---------------------------------------------------------
strncpy(buf, EX_STR, BUF_LEN);
result:string:0123456789abcdef [strlen(buf):16, sizeof(buf):16]
buf[0] = 48
buf[1] = 49
buf[2] = 50
buf[3] = 51
buf[4] = 52
buf[5] = 53
buf[6] = 54
buf[7] = 55
buf[8] = 56
buf[9] = 57
buf[10] = 97
buf[11] = 98
buf[12] = 99
buf[13] = 100
buf[14] = 101
buf[15] = 102
buf[BUF_LEN] = 0  (secretly watch.)

***Not write &#39;\0&#39; automatically!! (buf[15] = 102)
***Not write buf[BUF_LEN] (buf[16] = 0)

Normal_3
---------------------------------------------------------
strncpy(buf, EX_STR2, BUF_LEN);
result:string:01234 [strlen(buf):5, sizeof(buf):16]
buf[0] = 48
buf[1] = 49
buf[2] = 50
buf[3] = 51
buf[4] = 52
buf[5] = 0
buf[6] = 0
buf[7] = 0
buf[8] = 0
buf[9] = 0
buf[10] = 0
buf[11] = 0
buf[12] = 0
buf[13] = 0
buf[14] = 0
buf[15] = 0
buf[BUF_LEN] = 0  (secretly watch.)

***automatically write &#39;\0&#39; to buf[15]

Extra
---------------------------------------------------------
strncpy(buf, EX_STR, 0);
result:string:01234 [strlen(buf):5, sizeof(buf):16]
buf[0] = 48
buf[1] = 49
buf[2] = 50
buf[3] = 51
buf[4] = 52
buf[5] = 0
buf[6] = 0
buf[7] = 0
buf[8] = 0
buf[9] = 0
buf[10] = 0
buf[11] = 0
buf[12] = 0
buf[13] = 0
buf[14] = 0
buf[15] = 0
buf[BUF_LEN] = 0  (secretly watch.)

Not write any.

Right usage
---------------------------------------------------------
buf[0] = 48
buf[1] = 49
buf[2] = 50
buf[3] = 51
buf[4] = 52
buf[5] = 53
buf[6] = 54
buf[7] = 55
buf[8] = 56
buf[9] = 57
buf[10] = 97
buf[11] = 98
buf[12] = 99
buf[13] = 100
buf[14] = 101
buf[15] = 0
buf[BUF_LEN] = 0  (secretly watch.)

strncpy(buf, EX_STR, BUF_LEN); after buf[BUF_LEN-1] = &#39;\0&#39;;
</pre>
<hr />
<pre>
検索用タグ　C言語　strncpy 安全
</pre>
<hr />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/27</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/c%E8%A8%80%E8%AA%9E/%E8%87%AA%E5%B7%B1%E5%8F%82%E7%85%A7%E5%9E%8B%E5%8F%8C%E6%96%B9%E5%90%91%E3%83%AA%E3%82%B9%E3%83%88-c%E8%A8%80%E8%AA%9E-" />
    <published>2012-06-24T22:27:34+09:00</published> 
    <updated>2012-06-24T22:27:34+09:00</updated> 
    <category term="C言語" label="C言語" />
    <title>自己参照型双方向リスト(C言語)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<pre>
現在ゲーム中のユーザ管理するのにリスト構造使おうとちょっと作ったので置いておく。
自己参照型双方向リストです。
</pre>
<hr />
<h3>
	ソースコード</h3>
<pre class="brush: c">
/* nex2t_list.c */
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;netdb.h&gt;

#define RET_SUCCESS        0
#define RET_NO_ENTRY       1
#define RET_NO_MATCH_ENTRY 2

#define RET_NO_EXIST       0
#define RET_EXIST          1

#define DEBUG_LINE         &quot;------------------------------------------\n&quot;

typedef struct client_info {
	pid_t pid;
	char ip[NI_MAXHOST];
	char port[NI_MAXSERV];
} cli_info_t;

typedef struct client_info_node {
	struct client_info_node *next;
	struct client_info_node *prev;
	cli_info_t ent;
} cli_info_node_t;

typedef struct client_info_list {
	struct client_info_node *head;
	struct client_info_node *tail;
} cli_info_list_t;

void test(void);
void create_info(cli_info_t *info, pid_t pid, const char ip[],
                 const char port[]);
void list_init(cli_info_list_t *list);
void list_exit(cli_info_list_t *list);
void list_add(cli_info_list_t *list, const cli_info_t *info);
int list_del_head(cli_info_list_t *list);
int list_del_tail(cli_info_list_t *list);
int list_del_match_pid(cli_info_list_t *list, pid_t pid);
int list_del_match_ip(cli_info_list_t *list, const char ip[]);
int list_del_match_port(cli_info_list_t *list, const char port[]);
int list_del_match(cli_info_list_t *list, const cli_info_t *info);
int is_exist_cli_info_pid(const cli_info_list_t *list, pid_t pid);
int is_exist_cli_info_ip(const cli_info_list_t *list, const char ip[]);
int is_exist_cli_info_port(const cli_info_list_t *list, const char port[]);
int is_exist_cli_info(const cli_info_list_t *list, const cli_info_t *info);
void list_show(const cli_info_list_t *list);
void list_show_match_pid(const cli_info_list_t *list, pid_t pid);
void list_show_match_ip(const cli_info_list_t *list, const char ip[]);
void list_show_match_port(const cli_info_list_t *list, const char port[]);
void list_show_match(const cli_info_list_t *list, const cli_info_t *info);

int main(void)
{
	test();
	return 0;
}

void create_info(cli_info_t *info, pid_t pid, const char ip[],
                 const char port[])
{
	info-&gt;pid = pid;
	strcpy(info-&gt;ip, ip);
	strcpy(info-&gt;port, port);
}

void list_init(cli_info_list_t *list)
{
	list-&gt;head = NULL;
	list-&gt;tail = NULL;
}

void list_add(cli_info_list_t *list, const cli_info_t *info)
{
	cli_info_node_t *node;
	cli_info_node_t *node_tmp;

	node = (cli_info_node_t *)malloc(sizeof(cli_info_node_t));
	node-&gt;ent.pid = info-&gt;pid;
	strcpy(node-&gt;ent.ip, info-&gt;ip);
	strcpy(node-&gt;ent.port, info-&gt;port);

	if (list-&gt;head == NULL) {
		/* first add */
		list-&gt;head = node;
		list-&gt;tail = node;
		node-&gt;prev = NULL;
		node-&gt;next = NULL;
	} else {
		node_tmp = list-&gt;head;
		while (node_tmp-&gt;next != NULL) {
			node_tmp = node_tmp-&gt;next;
		}
		node_tmp-&gt;next = node;
		node-&gt;next = NULL;
		node-&gt;prev = node_tmp;
		list-&gt;tail = node;
	}
}

int list_del_head(cli_info_list_t *list)
{
	cli_info_node_t *node_next_keep;

	if (list-&gt;head == NULL) {
		return RET_NO_ENTRY;
	} else if (list-&gt;head-&gt;next == NULL) {
		free(list-&gt;head);
		list-&gt;head = NULL;
		list-&gt;tail = NULL;
	} else {
		node_next_keep = list-&gt;head-&gt;next;
		free(node_next_keep-&gt;prev);
		list-&gt;head = node_next_keep;
		list-&gt;head-&gt;prev = NULL;
	}
	return RET_SUCCESS;
}

int list_del_tail(cli_info_list_t *list)
{
	cli_info_node_t *node_prev_keep;

	if (list-&gt;tail == NULL) {
		return RET_NO_ENTRY;
	} else if (list-&gt;tail-&gt;prev == NULL) {
		free(list-&gt;tail);
		list-&gt;head = NULL;
		list-&gt;tail = NULL;
	} else {
		node_prev_keep = list-&gt;tail-&gt;prev;
		free(list-&gt;tail);
		list-&gt;tail = node_prev_keep;
		list-&gt;tail-&gt;next = NULL;
	}
	return RET_SUCCESS;
}

int list_del_match_pid(cli_info_list_t *list, pid_t pid)
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_ENTRY;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (node-&gt;ent.pid == pid) {
			if ((node-&gt;prev == NULL) &amp;&amp; (node-&gt;next == NULL)) {
				list-&gt;head = NULL;
				list-&gt;tail = NULL;
			} else if (node-&gt;prev == NULL) {
				list-&gt;head = node-&gt;next;
				node-&gt;next-&gt;prev = NULL;
			} else if (node-&gt;next == NULL) {
				list-&gt;tail = node-&gt;prev;
				node-&gt;prev-&gt;next = NULL;
			} else {
				node-&gt;prev-&gt;next = node-&gt;next;
				node-&gt;next-&gt;prev = node-&gt;prev;
			}
			free(node);
			return RET_SUCCESS;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_MATCH_ENTRY;
}

int list_del_match_ip(cli_info_list_t *list, const char ip[])
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_ENTRY;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (!strcmp(node-&gt;ent.ip, ip)) {
			if ((node-&gt;prev == NULL) &amp;&amp; (node-&gt;next == NULL)) {
				list-&gt;head = NULL;
				list-&gt;tail = NULL;
			} else if (node-&gt;prev == NULL) {
				list-&gt;head = node-&gt;next;
				node-&gt;next-&gt;prev = NULL;
			} else if (node-&gt;next == NULL) {
				list-&gt;tail = node-&gt;prev;
				node-&gt;prev-&gt;next = NULL;
			} else {
				node-&gt;prev-&gt;next = node-&gt;next;
				node-&gt;next-&gt;prev = node-&gt;prev;
			}
			free(node);
			return RET_SUCCESS;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_MATCH_ENTRY;
}

int list_del_match_port(cli_info_list_t *list, const char port[])
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_ENTRY;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (!strcmp(node-&gt;ent.port, port)) {
			if ((node-&gt;prev == NULL) &amp;&amp; (node-&gt;next == NULL)) {
				list-&gt;head = NULL;
				list-&gt;tail = NULL;
			} else if (node-&gt;prev == NULL) {
				list-&gt;head = node-&gt;next;
				node-&gt;next-&gt;prev = NULL;
			} else if (node-&gt;next == NULL) {
				list-&gt;tail = node-&gt;prev;
				node-&gt;prev-&gt;next = NULL;
			} else {
				node-&gt;prev-&gt;next = node-&gt;next;
				node-&gt;next-&gt;prev = node-&gt;prev;
			}
			free(node);
			return RET_SUCCESS;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_MATCH_ENTRY;
}

int list_del_match(cli_info_list_t *list, const cli_info_t *info)
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_ENTRY;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if ((node-&gt;ent.pid == info-&gt;pid)
		    &amp;&amp; !strcmp(node-&gt;ent.ip, info-&gt;ip)
		    &amp;&amp; (!strcmp(node-&gt;ent.port, info-&gt;port))) {
			if ((node-&gt;prev == NULL) &amp;&amp; (node-&gt;next == NULL)) {
				list-&gt;head = NULL;
				list-&gt;tail = NULL;
			} else if (node-&gt;prev == NULL) {
				list-&gt;head = node-&gt;next;
				node-&gt;next-&gt;prev = NULL;
			} else if (node-&gt;next == NULL) {
				list-&gt;tail = node-&gt;prev;
				node-&gt;prev-&gt;next = NULL;
			} else {
				node-&gt;prev-&gt;next = node-&gt;next;
				node-&gt;next-&gt;prev = node-&gt;prev;
			}
			free(node);
			return RET_SUCCESS;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_MATCH_ENTRY;
}

void list_show(const cli_info_list_t *list)
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		printf(&quot;NO ENTRY.\n&quot;);
		return;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		printf(&quot;[%d] pid:%d, ip:%s, port:%s\n&quot;, i++, node-&gt;ent.pid,
		       node-&gt;ent.ip, node-&gt;ent.port);
	} while (node-&gt;next != NULL);
}

void list_show_match_pid(const cli_info_list_t *list, pid_t pid)
{
	int i = 1, match_flag = 0;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		printf(&quot;NO_ENTRY.\n&quot;);
		return;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (node-&gt;ent.pid == pid) {
			printf(&quot;[%d] pid:%d, ip:%s, port:%s\n&quot;, i,
			       node-&gt;ent.pid, node-&gt;ent.ip, node-&gt;ent.port);
			match_flag = 1;
		}
		i++;
	} while (node-&gt;next != NULL);

	if (!match_flag) {
		printf(&quot;NO MATCH ENTRY pid:%d\n&quot;, pid);
	}
}

void list_show_match_ip(const cli_info_list_t *list, const char ip[])
{
	int i = 1, match_flag = 0;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		printf(&quot;NO_ENTRY.\n&quot;);
		return;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (!strcmp(node-&gt;ent.ip, ip)) {
			printf(&quot;[%d] pid:%d, ip:%s, port:%s\n&quot;, i,
			       node-&gt;ent.pid, node-&gt;ent.ip, node-&gt;ent.port);
			match_flag = 1;
		}
		i++;
	} while (node-&gt;next != NULL);

	if (!match_flag) {
		printf(&quot;NO MATCH ENTRY ip:%s\n&quot;, ip);
	}
}

void list_show_match_port(const cli_info_list_t *list, const char port[])
{
	int i = 1, match_flag = 0;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		printf(&quot;NO ENTRY.\n&quot;);
		return;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (!strcmp(node-&gt;ent.port, port)) {
			printf(&quot;[%d] pid:%d, ip:%s, port:%s\n&quot;, i,
			       node-&gt;ent.pid, node-&gt;ent.ip, node-&gt;ent.port);
			match_flag = 1;
		}
		i++;
	} while (node-&gt;next != NULL);

	if (!match_flag) {
		printf(&quot;NO MATCH ENTRY port:%s\n&quot;, port);
	}
}

void list_show_match(const cli_info_list_t *list, const cli_info_t *info)
{
	int i = 1, match_flag = 0;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		printf(&quot;NO ENTRY\n&quot;);
		return;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if ((node-&gt;ent.pid == info-&gt;pid)
		    &amp;&amp; !strcmp(node-&gt;ent.ip, info-&gt;ip)
		    &amp;&amp; (!strcmp(node-&gt;ent.port, info-&gt;port))) {
			printf(&quot;[%d] pid:%d, ip:%s, port:%s\n&quot;, i,
			       node-&gt;ent.pid, node-&gt;ent.ip, node-&gt;ent.port);
			match_flag = 1;
		}
		i++;
	} while (node-&gt;next != NULL);

	if (!match_flag) {
		printf(&quot;NO MATCH ENTRY pid:%d, ip:%s, port:%s\n&quot;,
		       info-&gt;pid, info-&gt;ip, info-&gt;port);
	}
}

int is_exist_cli_info_pid(const cli_info_list_t *list, pid_t pid)
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_EXIST;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (node-&gt;ent.pid == pid) {
			return RET_EXIST;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_EXIST;
}

int is_exist_cli_info_ip(const cli_info_list_t *list, const char ip[])
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_EXIST;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (!strcmp(node-&gt;ent.ip, ip)) {
			return RET_EXIST;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_EXIST;
}

int is_exist_cli_info_port(const cli_info_list_t *list, const char port[])
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_EXIST;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if (!strcmp(node-&gt;ent.port, port)) {
			return RET_EXIST;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_EXIST;
}

int is_exist_cli_info(const cli_info_list_t *list, const cli_info_t *info)
{
	int i = 1;
	cli_info_node_t *node;

	if (list-&gt;head == NULL) {
		return RET_NO_EXIST;
	}
	node = list-&gt;head;
	do {
		if (i != 1) {
			node = node-&gt;next;
		}
		if ((node-&gt;ent.pid == info-&gt;pid)
		    &amp;&amp; !strcmp(node-&gt;ent.ip, info-&gt;ip)
		    &amp;&amp; (!strcmp(node-&gt;ent.port, info-&gt;port))) {
			return RET_EXIST;
		}
		i++;
	} while (node-&gt;next != NULL);

	return RET_NO_EXIST;
}

void list_exit(cli_info_list_t *list)
{
	cli_info_node_t *node_tmp;
	cli_info_node_t *node_keep;

	if (list-&gt;head == NULL) {
		return; /* no entry */
	}
	node_tmp = list-&gt;head;
	do {
		node_keep = node_tmp-&gt;next;
		free(node_tmp);
		node_tmp = node_keep;
	} while (node_tmp != NULL);
}

void test(void)
{
	cli_info_list_t list;
	cli_info_t info;

	list_init(&amp;list);

	printf(&quot;first list state.\n&quot;);
	create_info(&amp;info, 1003, &quot;192.168.1.3&quot;, &quot;10003&quot;);
	list_add(&amp;list, &amp;info);
	create_info(&amp;info, 1004, &quot;192.168.1.4&quot;, &quot;10004&quot;);
	list_add(&amp;list, &amp;info);
	create_info(&amp;info, 1005, &quot;192.168.1.5&quot;, &quot;10005&quot;);
	list_add(&amp;list, &amp;info);
	create_info(&amp;info, 1006, &quot;192.168.1.6&quot;, &quot;10006&quot;);
	list_add(&amp;list, &amp;info);

	printf(DEBUG_LINE);
	list_show(&amp;list);
	printf(DEBUG_LINE &quot;\n&quot;);

	printf(&quot;list_show_match test\n&quot;);
	printf(DEBUG_LINE);
	list_show_match_pid(&amp;list, 1005);
	list_show_match_pid(&amp;list, 1007);
	list_show_match_ip(&amp;list, &quot;192.168.1.10&quot;);
	list_show_match_ip(&amp;list, &quot;192.168.1.6&quot;);
	list_show_match_port(&amp;list, &quot;10009&quot;);
	list_show_match_port(&amp;list, &quot;10003&quot;);
	create_info(&amp;info, 1003, &quot;192.168.1.5&quot;, &quot;10003&quot;);
	list_show_match(&amp;list, &amp;info);
	create_info(&amp;info, 1003, &quot;192.168.1.3&quot;, &quot;10003&quot;);
	list_show_match(&amp;list, &amp;info);
	printf(DEBUG_LINE &quot;\n&quot;);

	printf(&quot;is_exist_cli_info test (equal param list_show_match test)\n&quot;);
	printf(DEBUG_LINE);
	printf(&quot;%d\n&quot;, is_exist_cli_info_pid(&amp;list, 1005));
	printf(&quot;%d\n&quot;, is_exist_cli_info_pid(&amp;list, 1007));
	printf(&quot;%d\n&quot;, is_exist_cli_info_ip(&amp;list, &quot;192.168.1.10&quot;));
	printf(&quot;%d\n&quot;, is_exist_cli_info_ip(&amp;list, &quot;192.168.1.6&quot;));
	printf(&quot;%d\n&quot;, is_exist_cli_info_port(&amp;list, &quot;10009&quot;));
	printf(&quot;%d\n&quot;, is_exist_cli_info_port(&amp;list, &quot;10003&quot;));
	create_info(&amp;info, 1003, &quot;192.168.1.5&quot;, &quot;10003&quot;);
	printf(&quot;%d\n&quot;, is_exist_cli_info(&amp;list, &amp;info));
	create_info(&amp;info, 1003, &quot;192.168.1.3&quot;, &quot;10003&quot;);
	printf(&quot;%d\n&quot;, is_exist_cli_info(&amp;list, &amp;info));
	printf(DEBUG_LINE &quot;\n&quot;);

	printf(&quot;list_add test\n&quot;);
	printf(DEBUG_LINE);
	create_info(&amp;info, 2003, &quot;192.168.2.3&quot;, &quot;20003&quot;);
	list_add(&amp;list, &amp;info);
	create_info(&amp;info, 2004, &quot;192.168.2.4&quot;, &quot;20004&quot;);
	list_add(&amp;list, &amp;info);
	list_show(&amp;list);
	printf(DEBUG_LINE &quot;\n&quot;);

	printf(&quot;list_del test\n&quot;);
	printf(DEBUG_LINE);
	list_del_head(&amp;list);
	list_del_tail(&amp;list);
	list_show(&amp;list);
	printf(DEBUG_LINE);
	list_del_match_pid(&amp;list, 1003);
	list_del_match_pid(&amp;list, 1004); /* del */
	list_show(&amp;list);
	printf(DEBUG_LINE);
	list_del_match_ip(&amp;list, &quot;192.168.1.5&quot;); /* del */
	list_del_match_ip(&amp;list, &quot;192.168.1.7&quot;);
	list_show(&amp;list);
	printf(DEBUG_LINE);
	list_del_match_port(&amp;list, &quot;10006&quot;); /* del */
	list_del_match_port(&amp;list, &quot;10005&quot;);
	list_show(&amp;list);
	printf(DEBUG_LINE);
	create_info(&amp;info, 1003, &quot;192.168.1.5&quot;, &quot;10003&quot;);
	list_del_match(&amp;list, &amp;info);
	create_info(&amp;info, 2003, &quot;192.168.2.3&quot;, &quot;20003&quot;); /* del */
	list_del_match(&amp;list, &amp;info);
	list_show(&amp;list);
	printf(DEBUG_LINE);

	list_exit(&amp;list);
}
</pre>
<hr />
<h3>
	コンパイル例</h3>
<pre class="brush: bash">
gcc -W -Wall -o nex2t_list nex2t_list.c
</pre>
<hr />
<h3>
	実行結果</h3>
<pre class="brush: bash">
./list
first list state.
------------------------------------------
[1] pid:1003, ip:192.168.1.3, port:10003
[2] pid:1004, ip:192.168.1.4, port:10004
[3] pid:1005, ip:192.168.1.5, port:10005
[4] pid:1006, ip:192.168.1.6, port:10006
------------------------------------------

list_show_match test
------------------------------------------
[3] pid:1005, ip:192.168.1.5, port:10005
NO MATCH ENTRY pid:1007
NO MATCH ENTRY ip:192.168.1.10
[4] pid:1006, ip:192.168.1.6, port:10006
NO MATCH ENTRY port:10009
[1] pid:1003, ip:192.168.1.3, port:10003
NO MATCH ENTRY pid:1003, ip:192.168.1.5, port:10003
[1] pid:1003, ip:192.168.1.3, port:10003
------------------------------------------

is_exist_cli_info test (equal param list_show_match test)
------------------------------------------
1
0
0
1
0
1
0
1
------------------------------------------

list_add test
------------------------------------------
[1] pid:1003, ip:192.168.1.3, port:10003
[2] pid:1004, ip:192.168.1.4, port:10004
[3] pid:1005, ip:192.168.1.5, port:10005
[4] pid:1006, ip:192.168.1.6, port:10006
[5] pid:2003, ip:192.168.2.3, port:20003
[6] pid:2004, ip:192.168.2.4, port:20004
------------------------------------------

list_del test
------------------------------------------
[1] pid:1004, ip:192.168.1.4, port:10004
[2] pid:1005, ip:192.168.1.5, port:10005
[3] pid:1006, ip:192.168.1.6, port:10006
[4] pid:2003, ip:192.168.2.3, port:20003
------------------------------------------
[1] pid:1005, ip:192.168.1.5, port:10005
[2] pid:1006, ip:192.168.1.6, port:10006
[3] pid:2003, ip:192.168.2.3, port:20003
------------------------------------------
[1] pid:1006, ip:192.168.1.6, port:10006
[2] pid:2003, ip:192.168.2.3, port:20003
------------------------------------------
[1] pid:2003, ip:192.168.2.3, port:20003
------------------------------------------
NO ENTRY.
------------------------------------------
</pre>
<hr />
<pre>
検索用タグ　C言語　双方向リスト　自己参照型
</pre>
<hr />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/26</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/c%E8%A8%80%E8%AA%9E/%E5%8F%AF%E5%A4%89%E9%95%B7%E5%BC%95%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%83%AD%E3%82%B0%E3%83%A1%E3%83%83%E3%82%BB%E3%83%BC%E3%82%B8%E4%BD%9C%E6%88%90-c%E8%A8%80%E8%AA%9E-" />
    <published>2012-05-30T00:04:42+09:00</published> 
    <updated>2012-05-30T00:04:42+09:00</updated> 
    <category term="C言語" label="C言語" />
    <title>可変長引数を使ってログメッセージ作成(C言語)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<pre>
syslogに出力するのに、可変長引数使ってログメッセージ出力してやろうと
思って少しやったのでメモ。以下ソースはエラー処理とか細かいところはやってません
(C言語でsyslog()に出力するのはまた別の記事でやろうかと思います)
</pre>
<hr />
<h3>ソースコード</h3>
<pre class="brush: c">
/* nex2t_syslog.c */
#include &lt;stdio.h&gt;
#include &lt;stdarg.h&gt;

#define LOG_MESSAGE1 "[20000]: system info\n"
#define LOG_MESSAGE2 "[20001]: system error(%d, %d)\n"

#define LOG_MAIN 1 /* where?         */

void nex2t_syslog(char *fmt, ...);

int main(void)
{
	nex2t_syslog(LOG_MESSAGE1);
	nex2t_syslog(LOG_MESSAGE2, LOG_MAIN, 0);
	/* ～hogehoge～ */
	nex2t_syslog(LOG_MESSAGE2, LOG_MAIN, 7);

	return 0;
}

void nex2t_syslog(char *fmt, ...)
{
	char *p;
	int ival;
	va_list ap;

	va_start(ap, fmt);
	for (p = fmt; *p; p++) {
		if (*p != '%') {
			putchar(*p);
			continue;
		}
		p++;
		ival = va_arg(ap, int);
		printf("%d", ival);
	}
	va_end(ap);
}
</pre>
<hr />
<h3>コンパイル例</h3>
<pre class="brush: bash">
gcc -W -Wall -o nex2t_syslog nex2t_syslog.c
</pre>
<hr />
<h3>実行結果</h3>
<pre class="brush: bash">
./nex2t_syslog
[20000]: system info
[20001]: system error(1, 0)
[20001]: system error(1, 7)
</pre>
<hr />
<pre>
検索用タグ　C言語　可変長引数
</pre>
<hr />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/25</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/linux/initrd%E3%81%AE%E5%B1%95%E9%96%8B%E3%83%BB%E7%B7%A8%E9%9B%86%E3%83%BB%E5%86%8D%E6%A7%8B%E7%AF%89" />
    <published>2012-05-28T23:13:48+09:00</published> 
    <updated>2012-05-28T23:13:48+09:00</updated> 
    <category term="Linux" label="Linux" />
    <title>initrdの展開・編集・再構築</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<pre>
「ubuntuのLANドライバ変更してー」と言われ、e1000eの最新ドライバをmake, make installみたいな
よくあるやり方で入れたつもりで再起動したが変更できてない!ってことが起きました。
どうやらinitrdをいじる必要があるようです。。ということでまとめ。

※initrd はLinuxカーネルのブート時によく使われる一時的なファイルシステムの一種。
&quot;initial ramdisk&quot; の略。
真のルートファイルシステムをマウントできるようになる前にファイルシステムを
必要とする場面で使用される。
</pre>
<hr />
<h3>
	initrdの展開</h3>
<pre class="brush: bash">
su                                     # とりあえずrootになる
cd
mkdir sagyou                           # /root/sagyouを作る
cp /boot/initrd.img～ /root/sagyou/    # コピーしてくる
gunzip &lt; initrd.img～ | cpio -i        # &larr;ここが肝
rm initrd.img～                        # 展開した元のファイルは削除
# 以上でinitrdは展開されました。
</pre>
<hr />
<h3>
	initrdの編集</h3>
<pre class="brush: bash">
# たとえば、e1000eのドライバを変えるなら
/root/sagyou/lib/modules/`uname -r`/kernel/drivers/net/e1000e/
の中のe1000e.koを変更するとかします。
</pre>
<hr />
<h3>
	initrdの再構築</h3>
<pre class="brush: bash">
find . | cpio -o -H newc | gzip -c &gt; ./initrd-new
# initrd-newの部分は自由
(※再構築したinitrdで立ち上げたいなら、展開時と同じファイル名にして、
/boot/に置いてあげればOK)
</pre>
<hr />
<pre>
検索用タグ initrd 展開　編集　再構築
</pre>
<hr />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/24</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/c%E8%A8%80%E8%AA%9E/%E3%83%91%E3%82%A4%E3%83%97%E3%81%A7%E5%8F%8C%E6%96%B9%E5%90%91%E3%83%97%E3%83%AD%E3%82%BB%E3%82%B9%E9%96%93%E9%80%9A%E4%BF%A1-c%E8%A8%80%E8%AA%9E-" />
    <published>2012-05-27T23:42:30+09:00</published> 
    <updated>2012-05-27T23:42:30+09:00</updated> 
    <category term="C言語" label="C言語" />
    <title>パイプで双方向プロセス間通信(C言語)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<pre>
ファイル整理してたら出てきたのでメモ
</pre>
<h3>
	ソースコード</h3>
<pre class="brush: c">
/* pipe.c */
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;

#define BUF_LEN 256

int main(void)
{
	int fd[2], ret;
	char buf[BUF_LEN];
	pid_t pid;

	if ((ret = pipe(fd)) == -1) {
		perror(&quot;pipe&quot;);
		return -1;
	}
	if ((ret = fork()) == -1) {
		perror(&quot;fork&quot;);
		return -1;
	} else if (ret == 0) {
		pid = getpid();
		printf(&quot;[pid:%d] I&#39;m child\n&quot;, (int)pid);
		write(fd[1], &quot;child wrote&quot;, 13);
		read(fd[0], buf, BUF_LEN);
		printf(&quot;[pid:%d] from parent:[%s]\n&quot;, (int)pid, buf);
	} else {
		pid = getpid();
		printf(&quot;[pid:%d] I&#39;m parent\n&quot;, (int)pid);
		write(fd[1], &quot;parent wrote&quot;, 14);
		read(fd[0], buf, BUF_LEN);
		printf(&quot;[pid:%d] from child:[%s]\n&quot;, (int)pid, buf);
		wait(&amp;ret);
	}
	return 0;
}
</pre>
<hr />
<h3>
	実行結果</h3>
<pre class="brush: bash">
./pipe
[pid:13330] I&#39;m parent
[pid:13330] from child:[parent wrote]
[pid:13331] I&#39;m child
[pid:13331] from parent:[child wrote]
</pre>
<hr />
<pre>
検索用タグ　C言語 プロセス間通信
</pre>
<hr />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
  <entry>
    <id>nex2t.blog.shinobi.jp://entry/23</id>
    <link rel="alternate" type="text/html" href="https://nex2t.blog.shinobi.jp/%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88/%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%A7%E6%8C%87%E5%AE%9A%E5%9B%9E%E6%95%B0%E5%AE%9F%E8%A1%8C" />
    <published>2012-05-27T23:14:51+09:00</published> 
    <updated>2012-05-27T23:14:51+09:00</updated> 
    <category term="シェルスクリプト" label="シェルスクリプト" />
    <title>シェルスクリプトで指定回数実行</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<pre>
指定回数実行するシェルスクリプトのテンプレをメモ
とりあえずいくつかのパターンを、
最後に指定回数Verを
</pre>
<hr />
<h3>
	for文 Ver1 (10回)</h3>
<pre class="brush: bash">
#!/bin/sh

for i in `seq 1 1 10`
do
	echo &quot;$i&quot;
done
exit 0
</pre>
<hr />
<h3>
	for文 Ver2 (10回)</h3>
<pre class="brush: bash">
(下記の書き方だと、bashじゃないとダメだった)
#!/bin/bash

for ((i=1;i<=10;i++))
do
	echo $i
done

exit 0
</pre>
<hr />
<h3>
	while文 Ver (10回)</h3>
<pre class="brush: bash">
#!/bin/sh

i=1
while [ $i -le 10 ];
do
	echo &quot;$i&quot;
	i=`expr $i + 1`
done

exit 0
</pre>
<hr />
<h3>
	実行結果</h3>
<pre class="brush: bash">
1
2
3
4
5
6
7
8
9
10
</pre>
<hr />
<h3>
	指定回数実行 テンプレ</h3>
<pre class="brush: bash">
#!/bin/sh

if [ $# -lt 1 ]
then
	echo &quot;usage: $0 [trial_num]&quot;
	exit 0
fi

for i in `seq 1 1 $1`
do
	echo $i
done

exit 0
</pre>
<hr />
<pre>
検索用タグ シェルスクリプト
</pre>
<hr />
]]> 
    </content>
    <author>
            <name>nex2t</name>
        </author>
  </entry>
</feed>