Linux 程式編寫之快速建立命令列引數

2021-10-19 17:38:10 字數 3940 閱讀 3944

全域性變數optarg

示例程式

linux中,程式在命令列中經常需要新增引數執行,自己編寫進行引數解析比較麻煩,可以使用gnu c提供的函式getoptgetopt_longgetopt_long_only來快速解析命令列引數

命令列引數可以分為兩類:

兩者後面都可選擇性新增額外引數。比如--block-size=size,size便是額外的引數。

getopt函式只能處理短選項,而getopt_long函式兩者都可以,函式宣告如下:

#include

extern

char

*optarg;

extern

int optind, opterr, optopt;

#include

intgetopt

(int argc,

char

*const ar**,

const

char

*optstring)

;int

getopt_long

(int argc,

char

*const ar**,

const

char

*optstring,

const

struct option *longopts,

int*longindex)

;int

getopt_long_only

(int argc,

char

*const ar**,

const

char

*optstring,

const

struct option *longopts,

int*longindex)

;

以上三個函式都適用

argc和ar**和main函式的兩個引數一致。

optstring: 表示短選項字串,形式如a:b:c:d:,分別表示程式支援的命令列短選項有-a、-b、-c、-d,冒號含義如下: 乙個字元,後接乙個冒號——表示選項後面帶乙個引數,如-a 100longopts:表示長選項結構體。結構如下:

struct option

;

4.longindex:一般為null,非空時記錄當前找到引數在longopts中的下標值。

如果短選項找到,那麼將返回短選項對應的字元。

如果長選項找到,如果flag為null,返回val。如果flag不為空,返回0

如果遇到乙個選項沒有在短字元、長字元裡面。或者在長字元裡面存在二義性的,返回「?」

如果解析完所有字元沒有找到(一般是輸入命令引數格式錯誤,eg: 連斜槓都沒有加的選項),返回「-1」

如果選項需要引數,忘了新增引數。返回值取決於optstring,如果其第乙個字元是「:」,則返回「:」,否則返回「?」。

注意:longopts的最後乙個元素必須是全0填充,否則會報段錯誤短選項中每個選項都是唯一的。而長選項如果簡寫,也需要保持唯一性。

為了學習程序間通訊之訊息佇列,下面以建立訊息佇列、傳送資料到訊息佇列、讀取訊息佇列資料三個程序為例進行示例:

編譯:gcc -o make_msgqueue make_msgqueue.c

執行:

./creat_msg_queue 

message queue key: 67201.

message queue id: 0.

#include

#include

#include

intmain()

printf

("message queue key: %d.\n"

, key)

;//建立訊息佇列if(

(messagequeueid =

msgget

(key, ipc_creat|

0777))

==-1)

printf

("message queue id: %d.\n"

, messagequeueid)

;}

編譯:gcc -o send send_msg.c

執行:./send -i 0 -t 123 -m "hello world"

可以看到後面跟了很多引數,需要快速解析出來

/*

下面的這段程式,getopt_long、do-while 迴圈以及 switch,是用來解析命令列引數的。

optarg變數為全域性變數,用來承載解析後的引數

命令列引數的格式定義在 long_options 裡面。

每一項的第乙個成員「id」「type」「message」是引數選項的全稱,第二個成員都為 1,表示引數選項後面要跟引數,最後乙個成員』i』『t』'm』是引數選項的簡稱。

*/#include

#include

#include

#include

#include

//訊息型別

struct msg_buffer

;int

main

(int argc,

char

*ar**)

,,,//最後一項必須全0,否則段錯誤};

int messagequeueid =-1

;//訊息佇列id

struct msg_buffer buffer;

buffer.mtype =-1

;int len =-1

;char

* message =

null;do

memcpy

(buffer.mtext, message, len)

;break

;default

:break;}

}while

(next_option !=-1

);if(messagequeueid !=-1

&& buffer.mtype !=-1

&& len !=-1

&& message !=

null)}

else

return0;

}

編譯:gcc -o recv recv_msg.c

執行:

./recv -i 0 -t 123

received message type : 123, text: hello world.

#include

#include

#include

#include

#include

struct msg_buffer

;int

main

(int argc,

char

*ar**)

,,};

int messagequeueid =-1

;struct msg_buffer buffer;

long type =-1

;do}while

(next_option !=-1

);if(messagequeueid !=-1

&& type !=-1

)printf

("received message type : %d, text: %s."

, buffer.mtype, buffer.mtext);}

else

return0;

}

linux 編寫服務程式

以前我們在看 unix環境高階程式設計 的時候,有專門的整章詳細介紹如何編寫乙個後台daemon程式 精靈程式 主要涉及到建立會話組,切換工作目錄,設定檔案遮蔽字,關閉不必要的描述符等多個操作。這些操作對於每乙個後台程式來說都是類似的。在linux中專門提供了乙個函式來完成這個daemon化的過程,...

快速建立控制面板程式

delphi 5快速建立控制面板程式 2007 09 05 18 51 轉貼自 一 關於控制面板程式 hwndcpl thandle identifies the main umsg dword specifies the message lparam1,lparam2 longint specif...

linux之檔案編寫

vi 打卡對應檔案後 開啟或新建檔案,並將游標至於第一行首 root centos6 vi etc my.cnf 開啟檔案,並將游標移至最後一行行首 root centos6 vi etc my.cnf 開啟檔案,並將游標置於第n行首 root centos6 vi n etc my.cnf 開啟檔...