Linux环境
1. 程序参数
1.1 main 函数的形式
int main () {/* ... */} // 为C++的标准
int main(int argc, char *argv[]) {/* ... */} // 其中argc是程序参数的个数,argv代表参数自身的字符串数组。
main() {/* ... */} // C90标准允许这种形式,但是C99标准不允许。因此即使你当前的编译器允许,也不要这么写。
1.2 getopt
#include <unistd.h>
int getopt(int argc, char *const arv[], const char *optsrting);
extern char *optarg;
extern int optind, opterr, optopt;
/*************************************************************************
> File Name: argopt.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年04月30日 星期三 19时14分02秒
************************************************************************/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int opt;
while((opt = getopt(argc,argv, ":qif:lr")) != -1){
printf("optind:%d, opterr:%d, optopt:%c\n",optind,opterr,optopt);
switch(opt){
case ‘i‘:
case ‘l‘:
case ‘r‘:
case ‘q‘:
printf("option: %c\n",opt);
break;
case ‘f‘:
printf("filename: %s\n", optarg);
break;
case ‘:‘:
printf("option needs a value\n");
break;
case ‘?‘:
printf("unknown option: %c\n",optopt);
break;
}
}
int result ;
for(result = 0; result < argc; result ++) // 重写了argv数组,所有的非选项参数都集中在一起。
printf("argv[%d] = %s ",result, argv[result]);
printf("\n");
for(;optind < argc; optind++)
printf("argv[%d]=argument: %s\n", optind,argv[optind]);
exit(0);
}
运行结果:$ ./argopt -i -lr ‘hi there‘ sjdf -f fred.c -q sfjkdjsfoptind:2, opterr:1, optopt:
option: i
optind:2, opterr:1, optopt:
option: l
optind:3, opterr:1, optopt:
option: r
optind:7, opterr:1, optopt:
filename: fred.c
optind:8, opterr:1, optopt:
option: q
argv[0] = ./argopt argv[1] = -i argv[2] = -lr argv[3] = -f argv[4] = fred.c argv[5] = -q argv[6] = hi there argv[7] = sjdf argv[8] = sfjkdjsf
argv[6]=argument: hi there
argv[7]=argument: sjdf
argv[8]=argument: sfjkdjsf
1.3 . getopt_long
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
struct option {
const char *name; // 长选项名字
int has_arg; // 该选项是否带参数。0不带参数,1必须带一个参数,2有一个可选的参数
int *flag; // 设置为NULL表示当找到该选项时,getopt_long返回在val里的值,否则getopt_long返回0,并将val的值写入flag指向的变量
int val; // getopt_long为该选项返回的值。
}; // 此数组最后一组数据必须全部填写0
/*************************************************************************
> File Name: argopt2.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年05月01日 星期四 09时36分40秒
************************************************************************/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#define _GNU_SOURCE
#include<getopt.h>
int main(int argc, char *argv[])
{
int opt;
int list_test;
struct option longopts[]= {
{"initl",0,&list_test,‘i‘},
{"file",1,&list_test,‘f‘},
{"list",0,&list_test,‘l‘},
{"restart",0,&list_test,‘r‘},
{0,0,0,0}
};
int long_optind;
while((opt = getopt_long(argc,argv,":if:lr", longopts, &long_optind)) != -1) {
switch(opt){
case 0: // 如果返回0,说明把值写入list_test中。比且通过long_optind索引访问对于长选项数组。
printf("option: %c\n",list_test);
printf("longopts: %s %d %c %c\n"
,longopts[long_optind].name,longopts[long_optind].has_arg,
*longopts[long_optind].flag,longopts[long_optind].val
);
break;
case ‘i‘:
case ‘l‘:
case ‘r‘:
printf("option: %c\n",opt);
break;
case ‘f‘:
printf("filename: %s\n",optarg);
break;
case ‘:‘:
printf("option needs a value\n");
break;
case ‘?‘:
printf("unknow option: %c\n",optopt);
break;
}
}
int i;
for(i=0;i<argc;i++)
printf("argv[%d] = %s ", i, argv[i]);
puts("");
}
运行结果:./argopt2 one --initl two -l --file=fred.c -f three -l -qoption: i
longopts: initl 0 i i
option: l
option: f
longopts: file 1 f f
filename: three
option: l
unknow option: q
argv[0] = ./argopt2 argv[1] = --initl argv[2] = -l argv[3] = --file=fred.c argv[4] = -f argv[5] = three argv[6] = -l argv[7] = -q argv[8] = one argv[9] = two
2. 环境变量
2.1 配置环境变量
#include<stdlib.h>
char *getenv(const char *name);// 查找环境中给定名字的环境变量的值,如果变量不存在或变量无关联值,返回NULL,PS:getenv返回的字符串是存储在getenv提供的静态空间中,如果想进一步使用它,就需将字符串复制到另一个字符串中,以免被后续的getenv调用覆盖。
int putenv(const char *string); //“名字=值”形式设置值 The putenv() function returns zero on success, or nonzero if an error occurs.
/*************************************************************************
> File Name: environ.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年05月01日 星期四 10时56分20秒
************************************************************************/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
char *var, *value;
if(argc == 1 || argc > 3){
fprintf(stderr, "usage: environ var [value]\n");
exit(1);
}
var = argv[1];
value = getenv(var);
if(value) // 如果var有值,则输出其值
printf("Variable %s has value %s\n",var,value);
else
printf("Variable %s has no value\n",var);
if(argc == 3){
char *string;
value = argv[2];
string = malloc(strlen(var)+strlen(value)+2);
if(!string) {
fprintf(stderr, "out of memory\n");
exit(1);
}
strcpy(string,var);
strcat(string,"=");
strcat(string,value);
printf("Calling putenv with: %s\n",string);
if(putenv(string) != 0) {
fprintf(stderr, "putev failed\n");
free(string);
exit(1);
}
value = getenv(var);
if(value) // 如果var有值,则输出其值
printf("Variable %s has value %s\n",var,value);
else
printf("Variable %s has no value\n",var);
free(string);
}
exit(0);
}
2.2 时间和日期
#include<time.h>
time_t time(time_t *tloc) //返回从纪元(1970.1.1,0点)至今的秒数 time_t 为长整型long
double difftime(time_t time1, time_t time2); // time1 - time2, 由于可移植性,最好使用difftime
struct tm *gmtime(const time_t *timeval); // 把时间分解为格式struct tm 返回GMT时间
struct tm *localtime(const time_t *timeval); // 返回当地时间
time_t mktime(struct tm *timeptr); // 把struct tm结构时间转换为time_t时间
char *asctime(const struct tm *timeptr); // 格式化为易读的时间格式:Thu May 1 14:52:43 2014\n\0
char *ctime(const time_t *timeval); // 同上
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) 允许临时闰秒或双闰秒*/
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] 0表示1月*/
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] 0表示周日*/
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1] 是否为夏令时*/
#ifdef __USE_BSD
long int tm_gmtoff; /* Seconds east of UTC. */
__const char *tm_zone; /* Timezone abbreviation. */
#else
long int __tm_gmtoff; /* Seconds east of UTC. */
__const char *__tm_zone; /* Timezone abbreviation. */
#endif
};
/*************************************************************************
> File Name: gmtime.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年05月01日 星期四 14时30分01秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
struct tm *tm_ptr;
time_t now_time;
(void) time(&now_time);
//tm_ptr = gmtime(&now_time); // 按照GMT(格林尼治标准时间)返回时间,可能与本地日期不相符。
tm_ptr = localtime(&now_time); //当地时间
printf("The date is : %s | %s | %s",ctime(&now_time),asctime(tm_ptr),asctime(gmtime(&now_time)));
printf("Now time is %ld\n",now_time);
printf("gmtime give:\n");
printf("date: %02d/%02d/%02d\n",
tm_ptr->tm_year,tm_ptr->tm_mon,tm_ptr->tm_mday);
printf("time: %02d:%02d:%02d\n",
tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
exit(0);
}
/*
运行结果:
The date is : Thu May 1 14:56:52 2014
| Thu May 1 14:56:52 2014
| Thu May 1 14:56:52 2014
Now time is 1398927412
gmtime give:
date: 114/04/01
time: 14:56:52
*/
#include <time.h>
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);//针对时间的类似springf函数,通过format字符串格式化时间
#define _XOPEN_SOURCE
#include <time.h>
char *strptime(const char *s, const char *format, struct tm *tm); //类似sscanf通过format把字符串s格式化为struct tm的时间
3. 临时文件
#include <stdio.h>
char *tmpnam(char *s);//返回一个不与任何已存在文件同名的有效文件名,如果s不为NULL,文件也会写入它,它的长度至少为L_tmpnam(通常为20),此函数最多调用TMP_MAX次(至少为几千次)。应尽可能快地打开它以减小另一个程序用同样的名字打开文件的风险
FILE *tmpfile(void); //同时创建和打开一个临时文件,文件以读写的方式打开(w+),当对它的所有引用全部关闭时,该文件自动删除,如果出错返回NULL并设置errno的值。 #include<stdlib.h> char *mktmep(char *template) // The last six characters of template must be XXXXXX and these are replaced with a string that makes the filename unique. 返回一个唯一的文件名 int mkstemp(char *template) /* The mkstemp() function generates a unique temporary filename from tem‐
plate, creates and opens the file, and returns an open file descriptor
for the file.
The last six characters of template must be "XXXXXX" and these are
replaced with a string that makes the filename unique. Since it will
be modified, template must not be a string constant, but should be
declared as a character array.
The file is created with permissions 0600, that is, read plus write for
owner only. */
/*************************************************************************
> File Name: tmpnam.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年05月01日 星期四 16时22分20秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
int main()
{
char tmpname[L_tmpnam];
char *filename;
FILE *tmpfp;
filename = tmpnam(tmpname);
printf("Temporary file name is: %s\n",filename);
tmpfp = tmpfile();
if(tmpfp != NULL)
printf("Opened a temporary file OK\n");
else
perror("tmpfile");
exit(0);
}
/*
运行结果:
Temporary file name is: /tmp/file5zGzSJ
Opened a temporary file OK
*/
4. 用户信息
#include<sys/types.h>
#include<unistd.h>
uid_t getuid(void); //returns the real user ID of the calling process.
uid_t geteuid(void);//returns the effective user ID of the calling process. 两者区别:http://www.cppblog.com/converse/archive/2007/12/20/39166.html
char *getlogin(void); /*returns a pointer to a string containing the name of the user logged in on the controlling terminal of the process, or a NULL pointer if this information cannot be determined.*/
#include<pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name); //出错时返回NULL,并设置errno
struct passwd {
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
}; struct passwd *getpwent(void);/*依次扫描密码文件返回每个用户的信息数据,当到达文件尾时,返回NULL,可以用endpwent函数终止,setpwent重置读取指针到密码文件开头。*/ void endpwent(void); void setpwent(void);
5. 主机信息
#include<unistd.h>
int gethostname(char *name, size_t namelen); // 把机器的网络名写入name,且该字符串至少有namelen长。成功返回0,失败返回-1
#include<sys/utsname.h>
int uname(struct utsname *name); //成功返回非负整数,否则返回-1并设置errno。
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined
network" */
char release[]; /* OS release (e.g., "2.6.28") */
char version[]; /* OS version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
}; long gethostid(void) //获取机器的唯一标识符
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。