第四章 Linux环境

程序参数

int main(int argc , char *argv[])

argc是程序参数的个数,argv是代表参数的字符串数组。

以下对参数检查:

//args.c
#include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]){ int arg; for(arg=0;arg<argc;arg++){ if(argv[arg][0] == '-'){ printf("option:%s ",argv[arg]+1); }else{ printf("argument %d: %s ",arg,argv[arg]); } } exit(0); }

带参数执行

wuchao@:~/linux_program/CH04$ ./args -i -lr 'hi there' -f abc
argument 0: ./args
option:i
option:lr
argument 3: hi there
option:f
argument 5: abc

getopt

#include<unistd.h>
int getopt(int agrc,char *const argv[],const char *optstring)
extern char *optarg;
extern int optind,opterr,optopt;

该函数将传递给程序的main函数的argc和argv作为参数,同时接收一个字符串optstring,该字符串告诉getopt哪些选项可用,以及它们是否有关联值。

optstring是一个字符列表,每个字符代表一个单字符选项,如果字符后面跟一个冒号,则说明该选项有一个关联值作为下一个参数。

函数解释:

循环调用getopt可以依次获得所有选项

1. 如果选项有一个关联值,则外部变量optarg指向这个值

2. 如果选项处理结束,getopt返回-1,特殊参数--将使getopt停止扫描选项

3. 若遇到无法识别的选项,getopt返回“?”,并保存到optopt中

4. 如果选项要求有关联值,但用户未提供,getopt返回"?"。如果选项字符串第一个字符为冒号,那么getopt在用户为提供值的情况下返回冒号而不是问号。

5. optind保存待处理参数的索引

以下程序,设选项为‘-i’,'-l','-r','-f',其中f选项后面需要一个参数,并设置未提供参数时,返回“:”

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc,char *argv[]){
    int opt;
    while((opt = getopt(argc,argv,":if:lr")) !=-1 ){
        switch(opt){
            case 'i':
            case 'l':
            case 'r':
                printf("option:%c
",opt);
                break;
            case 'f':
                printf("filename:%s
",optarg);
                break;
            case ':':
                printf("option needs a value
");
                break;
            case '?':
                printf("unknown option:%c
",optopt);
                break;        
        }
    }
    for(;optind<argc;optind++){
        printf("argument:%s
",argv[optind]);
    }
    exit(0);
}

执行

wuchao@:~/linux_program/CH04$ ./argopt -i -lr 'hi here' -f fred.c -q
option:i
option:l
option:r
filename:fred.c
unknown option:q
argument:hi here

环境变量

#include<stdlib.h>
char *getenv(const char *name);
int putenv(const char *string);

getenv函数以给定的名字搜索,返回字符串。如果变量不存在,返回null,如果存在但无关联值,返回空字符串。

putenv参数以“名=值”的字符串给定。失败返回-1。

时间

#include<time.h>
time_t time(time_t *tloc);

如果tloc不是空指针,time还会把返回值写入tloc。

#include<time.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
    time_t timeNow = time((time_t *)0);
    printf("Current time is %ld;
",timeNow);
}

执行

wuchao@:~/linux_program/CH04$ ./time
Current time is 1476760726;

用户信息

#include<sys/types.h>
#include<unistd.h>
uid_t getuid(void);
char *getlogin(void);

getuid返回程序关联的UID,一般为当前用户的UID。

getlogin返回当前用户的登录名。

 

#include<sys/types.h>
#include<pwd.h>

struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);

passwd结构体如下:

char *pw_name:用户登录名

uid_t pw_uid:UID

gid_T pw_gid:GID

char *pw_dir:用户家目录

char *pw_gecos:用户全名

char *pw_shell:用户默认shell

主机信息

#include<unistd.h>
int gethostname(char *name,size_t namelen);

把机器的网络名写入name中,该字符串至少有namelen个字符长。

 

 

原文地址:https://www.cnblogs.com/wuchaodzxx/p/5972699.html