0

I would like to know hot to parse a string like this "hello world" into "helloworld" using the strstrip kernel function. I am developing a Linux Kernel char device and this functions causes me a Kernel Panic (or Kernel Opss).

The way I'm using this function is the following:

char result[100];

strcpy(result, "hello world");
strstrip(result);
strstrip(&result); //Also tried this
strstrip("100+200"); //Also tried this

The Kernel error is caused as soon as the strstrip line gets executed. What is the proper way to call this function?

4

2 に答える 2

1

srtstrip() は、最新のカーネルの strim() ( http://lxr.linux.no/linux+v3.11.2/lib/string.c#L361 ) のラッパー関数です。文字列自体を変更しようとするため、3 回目の試行のように静的文字列で呼び出すことはできません。

2 番目の試みは、ポインターでもある配列変数にポインターを渡すことです。したがって、上記のリンクを見ると正しくないことがわかる char** を渡しています。

最初の試行でカーネル エラーが発生することはありませんが、ローカル変数で戻り値を受け取っていないようです。どのようなエラーが表示されますか? その情報を提供できる場合は、この回答を更新します。

結局のところ、Balamurugan A が指摘しているように、この機能はあなたが思っているようには機能しません。strsep() ( http://lxr.linux.no/linux+v3.11.2/lib/string.c#L485 ) がここで役立つかもしれませんが、すべてのスペースを削除する足がかりにすぎません。いわば単に「メモリの内容をシフトする」方法がないため、実際には文字列を新しいバッファに単語ごとにコピーする必要があります。

于 2013-09-27T15:43:03.277 に答える
1

実際、strstrip は、先頭の空白を削除するのに役立ちます。文字列内のすべての空白を削除するわけではありません。以下の例を見てください。

char result[100];
strcpy(result, "     hello world from stack exchange");
printk("\n before: %s",result);
strcpy(result, strstrip((char*)result));
printk("\n after: %s",result);

それが役に立てば幸い。

于 2013-09-27T14:09:49.427 に答える