C语言网

 找回密码
 加入社区!

QQ登录

只需一步,快速开始

楼主: jiangbowen

Linux C 函数参考   [复制链接]

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:23:06 |显示全部楼层
ctime(将时间和日期以字符串格式表示)

相关函数        time,asctime,gmtime,localtime
表头文件        #include<time.h>
定义函数        char *ctime(const time_t *timep);
函数说明        ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值        返回一字符串表示目前当地的时间日期。

范例       
#include<time.h>
main()
{
  time_t timep;
  time (&timep);
  printf(“%s”,ctime(&timep));
}
执行       
Sat Oct 28 10 : 12 : 05 2000

gettimeofday(取得目前的时间)

相关函数         time,ctime,ftime,settimeofday
表头文件         #include <sys/time.h>
#include <unistd.h>
定义函数         int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明         gettimeofday () 会把目前的时间有tv 所指的结构返回,当地时区的信息则放到tz所指的结构中。timeval结构定义为:
struct timeval{
  long tv_sec;        /*秒*/
long tv_usec;           /*微秒*/
   };
  timezone 结构定义为:
struct timezone{
  int tz_minuteswest;    /*和Greenwich时间差了多少分钟*/
  int tz_dsttime;        /*日光节约时间的状态*/
};
   上述两个结构都定义在 /usr/include/sys/time.h。tz_dsttime 所代表的状态如下:
DST_NONE   /*不使用*/
DST_USA     /*美国*/
DST_AUST    /*澳洲*/
DST_WET     /*西欧*/
DST_MET     /*中欧*/
DST_EET      /*东欧*/
DST_CAN      /*加拿大*/
DST_GB       /*大不列颠*/
DST_RUM      /*罗马尼亚*/
DST_TUR       /*土耳其*/
DST_AUSTALT  /*澳洲(1986年以后)*/
返回值         成功则返回0,失败返回-1,错误代码存于errno。
附加说明         EFAULT指针tv和tz所指的内存空间超出存取权限。

范例       
#include<sys/time.h>
#include<unistd.h>
main(){
  struct timeval tv;
  struct timezone tz;
  gettimeofday (&tv , &tz);
  printf(“tv_sec; %d\n”, tv,.tv_sec) ;
  printf(“tv_usec; %d\n”,tv.tv_usec);
  printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
  printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
}
执行       
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:23:23 |显示全部楼层
gmtime(取得目前时间和日期)

相关函数         time,asctime,ctime,localtime
表头文件         #include<time.h>
定义函数         struct tm*gmtime(const time_t*timep);
函数说明        gmtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义为:
   struct tm
{
  int tm_sec;
  int tm_min;
  int tm_hour;
  int tm_mday;
  int tm_mon;
  int tm_year;
  int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
  int tm_min 代表目前分数,范围0-59
  int tm_hour 从午夜算起的时数,范围为0-23
  int tm_mday 目前月份的日数,范围01-31
  int tm_mon 代表目前月份,从一月算起,范围从0-11
  int tm_year 从1900年算起至今的年数
  int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。

返回值         返回结构tm代表目前UTC时间
范例       
#include <time.h>
main(){
   char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat” };
   time_t timep;
   struct tm *p;
   time(&timep);
   p=gmtime(&timep);
   printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
   printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行       
2000/10/28 Sat 8:15:38

localtime(取得当地目前时间和日期)

相关函数        time, asctime, ctime, gmtime
表头文件        #include<time.h>
定义函数         struct tm *localtime(const time_t * timep);
函数说明         localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回值        返回结构tm代表目前的当地时间。

范例       
#include<time.h>
main(){
  char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
  time_t timep;
  struct tm *p;
  time(&timep);
  p=localtime(&timep); /*取得当地时间*/
  printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
  printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行       
2000/10/28 Sat 11:12:22

mktime(将时间结构数据转换成经过的秒数)

相关函数        time,asctime,gmtime,localtime
表头文件        #include<time.h>
定义函数        time_t mktime(strcut tm * timeptr);
函数说明        mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0秒算起至今的UTC时间所经过的秒数。
返回值        返回经过的秒数。

范例       
/* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数 */
#include<time.h>
main()
{
  time_t timep;
  strcut tm *p;
  time(&timep);
  printf(“time() : %d \n”,timep);
  p=localtime(&timep);
  timep = mktime(p);
  printf(“time()->localtime()->mktime():%d\n”,timep);
}
执行       
time(): 974943297
time()->localtime()->mktime():974943297


settimeofday(设置目前时间)

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:23:38 |显示全部楼层
相关函数        time,ctime,ftime,gettimeofday
表头文件        #include<sys/time.h>
#include<unistd.h>
定义函数        int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函数说明        settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值        成功则返回0,失败返回-1,错误代码存于errno。
错误代码        EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。


time(取得目前的时间)

相关函数        ctime,ftime,gettimeofday
表头文件        #include<time.h>
定义函数        time_t time(time_t *t);
函数说明        此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值        成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。

范例       
#include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
执行       
972699100


bcmp(比较内存内容)

相关函数        bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
表头文件        #include<string.h>
定义函数        int bcmp ( const void *s1,const void * s2,int n);
函数说明        bcmp()用来比较s1和s2所指的内存区间前n个字节,若参数n为0,则返回0。
返回值        若参数s1和s2所指的内存内容都完全相同则返回0值,否则返回非零值。
附加说明        建议使用memcmp()取代。

范例       
参考memcmp()。

bcopy(拷贝内存内容)

相关函数        memccpy,memcpy,memmove,strcpy,ctrncpy
表头文件        #include <string.h>
定义函数        void bcopy ( const void *src,void *dest ,int n);
函数说明        bcopy()与memcpy()一样都是用来拷贝src所指的内存内容前n个字节到dest所指的地址,不过参数src与dest在传给函数时是相反的位置
返回值        无
附加说明        建议使用memcpy()取代

范例       
#include<string.h>
main()
{
char dest[30]=”string(a)”;
char src[30]=”string\0string”;
int i;
bcopy(src,dest,30);/* src指针放在前*/
printf(bcopy(): “)
for(i=0;i<30;i++)
   printf(“%c”,dest[i]);
memcpy(dest src,30); /*dest指针放在钱*/
printf(‘\nmemcpy() : “);
for(i=0;i<30;i++)
  printf(“%c”,dest[i]);
执行       
bcopy() : string string
memcpy() :string sring

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:24:14 |显示全部楼层
bzero(将一段内存内容全清为零)

相关函数        memset,swab
表头文件        #include<string.h>
定义函数        void bzero(void *s,int n);
函数说明        bzero()会将参数s所指的内存区域前n个字节,全部设为零值。相当于调用memset((void *)s,0,size_t n);
返回值        无
附加说明        建议使用memset取代

范例       
参考memset()。




index(查找字符串中第一个出现的指定字符)

相关函数        rindex,srechr,strrchr
表头文件        #include<string.h>
定义函数        char * index( const char *s, int c);
函数说明        index()用来找出参数s字符串中第一个出现的参数c地址,然后将该字符出现的地址返回。字符串结束字符(NULL)也视为字符串一部分。
返回值        如果找到指定的字符则返回该字符所在地址,否则返回0。

范例       
#include<string.h>
main()
{
   char *s =”0123456789012345678901234567890”;
   char *p;
   p =index(s,’5’);
   printf(%s\n”,p);
}
执行       
56789012345678901234567890

memccpy(拷贝内存内容)

相关函数        bcopy,memcpy,memmove,strcpy,strncpy
表头文件        #include<string.h>
定义函数        void * memccpy(void *dest, const void * src, int c,size_t n);
函数说明        memccpy()用来拷贝src所指的内存内容前n个字节到dest所指的地址上。与memcpy()不同的是,memccpy()会在复制时检查参数c是否出现,若是则返回dest中值为c的下一个字节地址。
返回值        返回指向dest中值为c的下一个字节指针。返回值为0表示在src所指内存前n个字节中没有值为c的字节。

范例       
#include<string.h>
main()
{
char a[]=”string[a]”;
char b[]=”string(b)”l
memccpy(a,b,’B’,sizeof(b));
printf(memccpy():%s\n”,a);
}
执行       
memccpy():string(b)

memchr(在某一内存范围中查找一特定字符)

相关函数        index,rindex,strchr,strpbrk,strrchr,strsep,strspn,strstr
表头文件        #include<string.h>
定义函数        void * memchr(const void *s,int c,size_t n);
函数说明        memchr()从头开始搜寻s所指的内存内容前n个字节,直到发现第一个值为c的字节,则返回指向该字节的指针。
返回值        如果找到指定的字节则返回该字节的指针,否则返回0。

范例       
#include <string.h>
main()
{
char *s=”0123456789012345678901234567890”;
char *p;
p=memchr(s,’5’,10);
printf(“%s\n”,p);
}
执行       
56789012345678901234567890

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:24:39 |显示全部楼层
memcmp(比较内存内容)

相关函数        bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
表头文件        #include<string.h>
定义函数        int memcmp (const void *s1,const void *s2,size_t n);
函数说明        memcmp()用来比较s1和s2所指的内存区间前n个字符。字符串大小的比较是以ASCII码表上的顺序来决定,次顺序亦为字符的值。memcmp()首先将s1第一个字符值减去s2第一个字符的值,若差为0则再继续比较下个字符,若差值不为0则将差值返回。例如,字符串“Ac”和“ba”比较则会返回字符‘A’(65)和‘b’(98)的差值(-33)。
返回值        若参数s1和s2所指的内存内容都完全相同则返回0值。s1若大于s2则返回大于0的值。s1若小于s2则返回小于0的值。

范例       
#include<string.h>
main()
{
char *a =”aBcDeF”;
char *b=”AbCdEf”;
char *c=”aacdef”;
char *d=”aBcDeF”;
printf(“memcmp(a,b):%d\n”,memcmp((void*)a,(void*) b,6));
printf(“memcmp(a,c):%d\n”,memcmp((void*)a,(void*) c,6));
printf(“memcmp(a,d):%d\n”,memcmp((void*)a,(void*) d,6));
执行       
memcmp(a,b):1   /*字符串a>字符串b,返回1*/
memcmp(a,c):-1  /* 字符串a<字符串c,返回-1*/
memcmp(a,d):0  /*字符串a=字符串d,返回0*/


memcpy(拷贝内存内容)

相关函数        bcopy,memccpy,memcpy,memmove,strcpy,strncpy
表头文件        #include<string.h>
定义函数        void * memcpy (void * dest ,const void *src, size_t n);
函数说明        memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束‘\0‘而结束。
返回值        返回指向dest的指针。
附加说明        指针src和dest所指的内存区域不可重叠。

范例       
#include<string.h>
main()
{
char a[30]=”string (a)”;
char b[30]=”string\0string”;
int i;
strcpy(a,b);
printf(strcpy():”);
  for(i=0;i<30;i++)
printf(“%c”,a[i]);
  memcpy(a,b,30);
  printf(“\nmemcpy() :”);
  for(i=0;i<30;i++)
printf(“%c”,a[i]);
}
执行       
strcpy() : string a )
memcpy() : string string





memmove(拷贝内存内容)

相关函数        bcopy,memccpy,memcpy,strcpy,strncpy
表头文件        #include<string.h>
定义函数        void * memmove(void *dest,const void *src,size_t n);
函数说明        memmove()与memcpy()一样都是用来拷贝src所指的内存内容前n个字节到dest所指的地址上。不同的是,当src和dest所指的内存区域重叠时,memmove()仍然可以正确的处理,不过执行效率上会比使用memcpy()略慢些。
返回值        返回指向dest的指针。
附加说明        指针src和dest所指的内存区域可以重叠。

范例       
参考memcpy()。

memset(将一段内存空间填入某值)

相关函数        bzero,swab
表头文件        #include<string.h>
定义函数        void * memset (void *s ,int c, size_t n);
函数说明        memset()会将参数s所指的内存区域前n个字节以参数c填入,然后返回指向s的指针。在编写程序时,若需要将某一数组作初始化,memset()会相当方便。
返回值        返回指向s的指针。
附加说明        参数c虽声明为int, 但必须是unsigned char ,所以范围在0到255之间。

范例       
#include <string.h>
main()
{
  char s[30];
memset (s,’A’,sizeof(s));
s[30]=’\0’;
printf(“%s\n”,s);
}
执行       
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

rindex(查找字符串中最后一个出现的指定字符)

相关函数        index,memchr,strchr,strrchr
表头文件        #include<string.h>
定义函数        char * rindex( const char *s,int c);
函数说明        rindex()用来找出参数s字符串中最后一个出现的参数c 地址,然后将该字符出现的地址返回。字符串结束字符(NULL)也视为字符串一部分。
返回值        如果找到指定的字符则返回该字符所在的地址,否则返回0。

范例       
#include <string.h>
mian()
{
char *s =”0123456789012345678901234567890”;
char *p;
p= rindex(s,’5’);
printf(“%s\n”,p);
}
执行       
567890
strcasecmp(忽略大小写比较字符串)

相关函数        bcmp,memcmp,strcmp,strcoll,strncmp
表头文件        #include<string.h>
定义函数        int strcasecmp ( const char *s1, const char *s2);
函数说明        strcasecmp()用来比较参数s1和s2字符串,比较时会自动忽略大小写的差异。
返回值        若参数s1和s2字符串相同则返回0。s1长度大于s2长度则返回大于0的值,s1长度若小于s2长度则返回小于0的值。

范例       
#include <string.h>
main()
{
char *a=”aBcDeF”;
char *b= “AbCdEf”;
if(!strcasecmp(a,b))
   printf(“%s=%s\n”,a,b);
}
执行       
aBcDeF=AbCdEf

strcat(连接两字符串)

相关函数        bcopy,memccpy,memcpy,strcpy,strncpy
表头文件        #include <string.h>
定义函数        char *strcat (char *dest,const char *src);
函数说明        strcat()会将参数src字符串拷贝到参数dest所指的字符串尾。第一个参数dest要有足够的空间来容纳要拷贝的字符串。
返回值        返回参数dest的字符串起始地址

范例       
#include <string.h.>
main()
{
   char a[30]=”string(1)”;
   char b[]=”string(2)”;
   printf(“before strcat() : %s\n”,a);
   printf(“after strcat() : %s\n”,strcat(a,b));
}
执行       
before strcat () : string(1)
after strcat () : string(1)string(2)

strchr(查找字符串中第一个出现的指定字符)

相关函数        index,memchr,rinex,strbrk,strsep,strspn,strstr,strtok
表头文件        #include<string.h>
定义函数        char * strchr (const char *s,int c);
函数说明        strchr()用来找出参数s字符串中第一个出现的参数c地址,然后将该字符出现的地址返回。
返回值        如果找到指定的字符则返回该字符所在地址,否则返回0。

范例       
#include<string.h>
main()
{
char *s=0123456789012345678901234567890”;
char *p;
p=strchr(s,’5’);
printf(“%s\n”,p);
}
执行       
56789012345678901234567890

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:24:53 |显示全部楼层
strcmp(比较字符串)

相关函数        bcmp,memcmp,strcasecmp,strncasecmp,strcoll
表头文件        #include<string.h>
定义函数        int strcmp(const char *s1,const char *s2);
函数说明        strcmp()用来比较参数s1和s2字符串。字符串大小的比较是以ASCII码表上的顺序来决定,此顺序亦为字符的值。strcmp()首先将s1第一个字符值减去s2第一个字符值,若差值为0则再继续比较下个字符,若差值不为0则将差值返回。例如字符串“Ac”和“ba”比较则会返回字符“A”(65)和‘b’(98)的差值(-33)。
返回值        若参数s1和s2字符串相同则返回0。s1若大于s2则返回大于0的值。s1若小于s2则返回小于0的值。

范例       
#include<string.h>
main()
{
  char *a=”aBcDeF”;
  char *b=”AbCdEf”;
  char *c=”aacdef”;
  char *d=”aBcDeF”;

printf(“strcmp(a,b) : %d\n”,strcmp(a,b));
printf(“strcmp(a,c) : %d\n”,strcmp(a,c));
printf(“strcmp(a,d) : %d\n”,strcmp(a,d));
}
执行       
strcmp(a,b) : 32
strcmp(a,c) :-31
strcmp(a,d) : 0


strcoll(采用目前区域的字符排列次序来比较字符串)

相关函数        strcmp,bcmp,memcmp,strcasecmp,strncasecmp
表头文件        #include<string.h>
定义函数        int strcoll( const char *s1, const char *s2);
函数说明        strcoll()会依环境变量LC_COLLATE所指定的文字排列次序来比较s1和s2字符串。
返回值        若参数s1和s2字符串相同则返回0。s1若大于s2则返回大于0的值。s1若小于s2则返回小于0的值。
附加说明        若LC_COLLATE为“POSIX”或“C”,则strcoll()与strcmp()作用完全相同。

范例       
参考strcmp()。

strcpy(拷贝字符串)

相关函数        bcopy,memcpy,memccpy,memmove
表头文件        #include<string.h>
定义函数        char *strcpy(char *dest,const char *src);
函数说明        strcpy()会将参数src字符串拷贝至参数dest所指的地址。
返回值        返回参数dest的字符串起始地址。
附加说明        如果参数dest所指的内存空间不够大,可能会造成缓冲溢出(buffer Overflow)的错误情况,在编写程序时请特别留意,或者用strncpy()来取代。

范例       
#include<string.h>
main()
{
char a[30]=”string(1)”;
char b[]=”string(2)”;
printf(“before strcpy() :%s\n”,a);
printf(“after strcpy() :%s\n”,strcpy(a,b));
}
执行       
before strcpy() :string(1)
after strcpy() :string(2)

strcspn(返回字符串中连续不含指定字符串内容的字符数)

相关函数        strspn
表头文件        #inclued<string.h>
定义函数        size_t strcspn ( const char *s,const char * reject);
函数说明        strcspn()从参数s字符串的开头计算连续的字符,而这些字符都完全不在参数reject所指的字符串中。简单地说,若strcspn()返回的数值为n,则代表字符串s开头连续有n个字符都不含字符串reject内的字符。
返回值        返回字符串s开头连续不含字符串reject内的字符数目。

范例       
#include <string.h>
main()
{
  char *str=”Linux was first developed for 386/486-based pcs.”;
  printf(“%d\n”,strcspn(str,” “));
  printf(“%d\n”,strcspn(str,”/-“));
  printf(“%d\n”,strcspn(str,”1234567890”));
}
执行       
5        /*只计算到“ ”的出现,所以返回“Linux”的长度*/
33 /*计算到出现“/”或“-”,所以返回到“6”的长度*/
30 /* 计算到出现数字字符为止,所以返回“3”出现前的长度*/


strdup(复制字符串)

相关函数        calloc,malloc,realloc,free
表头文件        #include<string.h>
定义函数        char * strdup( const char *s);
函数说明        strdup()会先用maolloc()配置与参数s字符串相同的空间大小,然后将参数s字符串的内容复制到该内存地址,然后把该地址返回。该地址最后可以利用free()来释放。
返回值        返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL表示内存不足。

范例       
#include<string.h>
main()
{
char a[]=”strdup”;
char *b;
b=strdup(a);
printf(“b[ ]=\”%s\”\n”,b);
}
执行       
b[ ]=”strdup”

strlen(返回字符串长度)

相关函数        无
表头文件        #include<string.h>
定义函数        size_t strlen ( const char *s);
函数说明        strlen()用来计算指定的字符串s的长度,不包括结束字符“\0”。
返回值        返回字符串s的字符数。

范例       
/*取得字符串str的长度*/
#include<string.h>
main()
{
char *str =”12345678”;
printf(“str length = %d\n”,strlen(str));
}
执行       
str length = 8

strncasecmp(忽略大小写比较字符串)

相关函数        bcmp,memcmp,strcmp,strcoll,strncmp
表头文件        #include<string.h>
定义函数        int strncasecmp(const char *s1,const char *s2,size_t n);
函数说明        strncasecmp()用来比较参数s1和s2字符串前n个字符,比较时会自动忽略大小写的差异。
返回值        若参数s1和s2字符串相同则返回0。s1若大于s2则返回大于0的值,s1若小于s2则返回小于0的值。

范例       
#include<string.h>
main()
{
  char *a=”aBcDeF”;
  char *b=”AbCdEf”;
if(!strncasecmp(a,b))
   printf(“%s =%s\n”,a,b);
}
执行       
aBcDef=AbCdEf



strncat(连接两字符串)

相关函数        bcopy,memccpy,memecpy,strcpy,strncpy
表头文件        #inclue <string.h>
定义函数        char * strncat(char *dest,const char *src,size_t n);
函数说明        strncat()会将参数src字符串拷贝n个字符到参数dest所指的字符串尾。第一个参数dest要有足够的空间来容纳要拷贝的字符串。
返回值        返回参数dest的字符串起始地址。

范例       
#include <string.h>
main()
{
  char a[30]=”string(1)”;
  char b[]=”string(2)”;
  printf(“before strnact() :%s\n”,a);
  printf(“after strncat() :%s\n”,strncat(a,b,6));
}
执行       
before strnact() : string(1)
after strncat() : string(1) string

strncpy(拷贝字符串)

相关函数        bcopy,memccpy,memcpy,memmove
表头文件        #include<string.h>
定义函数        char * strncpy(char *dest,const char *src,size_t n);
函数说明        strncpy()会将参数src字符串拷贝前n个字符至参数dest所指的地址。
返回值        返回参数dest的字符串起始地址。

范例       
#inclue <string.h>
main()
{
char a[30]=”string(1)”;
char b[]=”string(2)”;
printf(“before strncpy() : %s\n”,a);
printf(after strncpy() : %s\n”,strncpy(a,b,6));
}

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:25:09 |显示全部楼层
执行       
before strncpy() : string(1)
after strncpy() : string(1)

strpbrk(查找字符串中第一个出现的指定字符)

相关函数        index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
表头文件        #include <include.h>
定义函数        char *strpbrk(const char *s,const char *accept);
函数说明        strpbrk()用来找出参数s字符串中最先出现存在参数accept字符串中的任意字符。
返回值        如果找到指定的字符则返回该字符所在地址,否则返回0。

范例       
#include <string.h>
main()
{
char *s=”0123456789012345678901234567890”;
char *p;
p=strpbrk(s,”a1 839”); /*1会最先在s字符串中找到*/
printf(“%s\n”,p);
p=strprk(s,”4398”);/*3会最先在s字符串中找到*/
printf(“%s\n”,p);

执行       
123456789012345678901234567890
3456789012345678901234567890

strrchr(查找字符串中最后出现的指定字符)

相关函数        index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
表头文件        #include<string.h>
定义函数        char * strrchr(const char *s, int c);
函数说明        strrchr()用来找出参数s字符串中最后一个出现的参数c地址,然后将该字符出现的地址返回。
返回值        如果找到指定的字符则返回该字符所在地址,否则返回0。

范例       
#include<string.h>
main()
{
char *s=”0123456789012345678901234567890”;
char *p;
p=strrchr(s,’5’);
printf(“%s\n”,p);
}
执行       
567890

strspn(返回字符串中连续不含指定字符串内容的字符数)

相关函数        strcspn,strchr,strpbrk,strsep,strstr
表头文件        #include<string.h>
定义函数        size_t strspn (const char *s,const char * accept);
函数说明        strspn()从参数s字符串的开头计算连续的字符,而这些字符都完全是accept所指字符串中的字符。简单的说,若strspn()返回的数值为n,则代表字符串s开头连续有n个字符都是属于字符串accept内的字符。
返回值        返回字符串s开头连续包含字符串accept内的字符数目。

范例       
#include<string.h>
main()
{
char *str=”Linux was first developed for 386/486-based PCs.”;
char *t1=”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
printf(“%d\n”,strspn(str,t1));
}
执行       
5 /*计算大小写字母。不包含“ ”,所以返回Linux的长度。*/

strstr(在一字符串中查找指定的字符串)

相关函数        index,memchr,rindex,strchr,strpbrk,strsep,strspn,strtok
表头文件        #include<string.h>
定义函数        char *strstr(const char *haystack,const char *needle);
函数说明        strstr()会从字符串haystack中搜寻字符串needle,并将第一次出现的地址返回。
返回值        返回指定字符串第一次出现的地址,否则返回0。

范例       
#include<string.h>
main()
{
char * s=”012345678901234567890123456789”;
char *p;
p= strstr(s,”901”);
printf(“%s\n”,p);
}
执行       
9012345678901234567890

strtok(分割字符串)

相关函数        index,memchr,rindex,strpbrk,strsep,strspn,strstr
表头文件        #include<string.h>
定义函数        char * strtok(char *s,const char *delim);
函数说明        strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时则会将该字符改为\0字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回下一个分割后的字符串指针。
返回值        返回下一个分割后的字符串指针,如果已无从分割则返回NULL。

范例       
#include<string.h>
main()
{
char s[]=”ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z”;
char *delim=”-: “;
char *p;
printf(%s “;strtok(s,delim));
while((p=strtok(NULL,delim)))printf(“%s   ”,p);
printf(“\n”);
}
执行       
ab  cd  ef;gh  i  jkl;mnop;qrs  tu  vwx  y;z  /*-与:字符已经被\0字符取代*/




abs(计算整型数的绝对值)

相关函数         labs, fabs
表头文件         #include<stdlib.h>
定义函数         int abs (int j)
函数说明         abs ()  用来计算参数j的绝对值,然后将结果返回。
返回值         返回参数j的绝对值结果。

范例       
  #ingclude <stdlib.h>
  main(){
  int ansert;
  answer = abs(-12);
  printf(“|-12| = %d\n” , answer);
}
执行       
|-12| = 12


acos(取反余弦函数数值)

相关函数         asin , atan , atan2 , cos , sin , tan
表头文件         #include <math.h>
定义函数         double acos (double x);
函数说明         acos () 用来计算参数x的反余弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围则会失败。
返回值         返回0至 PI之间的计算结果,单位为弧度,在函数库中角度均以弧度来表示。
错误代码         EDOM参数x超出范围。
附加说明         使用GCC编译时请加入-lm。

范例       
  #include <math.h>
  main (){
  double angle;
  angle = acos(0.5);
  printf (“angle = %f\n” , angle);
}
执行       
  angle = 1.047198


asin(取反正弦函数值)

相关函数         acos , atan , atan2 , cos , sin , tan
表头文件         #include <math.h>
定义函数         double asin (double x)
函数说明         asin () 用来计算参数x的反正弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围则会失败。
返回值         返回-PI/2之PI/2之间的计算结果。
错误代码         EDOM参数x超出范围
附加说明        使用GCC编译时请加入 -lm

范例       
#include<math.h>
main()
{
double angle;
angle = asin (0.5);
printf(“angle = %f\n”,angle);
}
执行       
angle = 0.523599


atan(取反正切函数值)

相关函数        acos,asin,atan2,cos,sin,tan
表头文件        #include<math.h>
定义函数        double atan(double x);
函数说明        atan()用来计算参数x的反正切值,然后将结果返回。
返回值        返回-PI/2至PI/2之间的计算结果。
附加说明        使用GCC编译时请加入-lm

范例       
#include<math.h>
main()
{
double angle;
angle =atan(1);
printf(“angle = %f\n”,angle);
}
执行       
angle = 1.570796

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:25:25 |显示全部楼层
atan2(取得反正切函数值)

相关函数        acos,asin,atan,cos,sin,tan
表头文件        #include<math.h>
定义函数        double atan2(double y,double x);
函数说明        atan2()用来计算参数y/x的反正切值,然后将结果返回。
返回值        返回-PI/2至PI/2之间的计算结果。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
  double angle;
  angle = atan2(1,2);
  printf(“ angle = %f\n”,angle);
}
执行       
angle = 0.463648


ceil(取不小于参数的最小整型数)

相关函数        fabs
表头文件        #include <math.h>
定义函数        double ceil (double x);
函数说明        ceil()会返回不小于参数x的最小整数值,结果以double形态返回。
返回值        返回不小于参数x的最小整数值。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
double value[ ]={4.8,1.12,-2.2,0};
int i;
for (i=0;value[i]!=0;i++)
printf(“%f=>%f\n”,value[i],ceil(value[i]));
}
执行       
4.800000=>5.000000
1.120000=>2.000000
-2.200000=>-2.000000


cos(取余玄函数值)

相关函数        acos,asin,atan,atan2,sin,tan
表头文件        #include<math.h>
定义函数        double cos(double x);
函数说明        cos()用来计算参数x的余玄值,然后将结果返回。
返回值        返回-1至1之间的计算结果。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
double answer = cos(0.5);
printf(“cos (0.5) = %f\n”,answer);
}
执行       
cos(0.5) = 0.877583


cosh(取双曲线余玄函数值)

相关函数        sinh,tanh
表头文件        #include<math.h>
定义函数        double cosh(double x);
函数说明        cosh()用来计算参数x的双曲线余玄值,然后将结果返回。数学定义式为:(exp(x)+exp(-x))/2。
返回值        返回参数x的双曲线余玄值。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
  double answer = cosh(0.5);
  printf(“cosh(0.5) = %f\n”,answer);
}
执行       
cosh(0.5) = 1.127626


exp(计算指数)

相关函数        log,log10,pow
表头文件        #include<math.h>
定义函数        double exp(double x);
函数说明        exp()用来计算以e为底的x次方值,即ex值,然后将结果返回。
返回值        返回e的x次方计算结果。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
  double answer;
  answer = exp (10);
  printf(“ e^10 =%f\n”,answer);
}
执行       
e^10 = 22026.465795


frexp(将浮点型数分为底数与指数)

相关函数        ldexp,modf
表头文件        #include<math.h>
定义函数        double frexp( double x, int *exp);
函数说明        frexp()用来将参数x的浮点型数切割成底数和指数。底数部分直接返回,指数部分则借参数exp指针返回,将返回值乘以2的exp次方即为x的值。
返回值        返回参数x的底数部分,指数部分则存于exp指针所指的地址。
附加说明        使用GCC编译时请加入-lm。

范例       
#include <math.h>
main()
{
int exp;
double fraction;
fraction = frexp (1024,&exp);
printf(“exp = %d\n”,exp);
printf(“fraction = %f\n”,fraction);
}
执行       
exp = 11
fraction = 0.500000 /* 0.5*(2^11)=1024*/


ldexp(计算2的次方值)

相关函数        frexp
表头文件        #include<math.h>
定义函数        double ldexp(double x,int exp);
函数说明        ldexp()用来将参数x乘上2的exp次方值,即x*2exp。
返回值        返回计算结果。
附加说明        使用GCC编译时请加入-lm。

范例       
/* 计算3*(2^2)=12 */
#include<math.h>
main()
{
int exp;
double x,answer;
answer = ldexp(3,2);
printf(“3*2^(2) = %f\n”,answer);
}
执行       
3*2^(2) = 12.000000


log(计算以e为底的对数值)

相关函数        exp,log10,pow
表头文件        #include <math.h>
定义函数        double log (double x);
函数说明        log()用来计算以e为底的x对数值,然后将结果返回。
返回值        返回参数x的自然对数值。
错误代码        EDOM  参数x为负数
ERANGE  参数x为零值,零的对数值无定义。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
double answer;
answer = log (100);
printf(“log(100) = %f\n”,answer);
}
执行       
log(100) = 4.605170

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:25:45 |显示全部楼层
log10(计算以10为底的对数值)

相关函数        exp,log,pow
表头文件        #include<math.h>
定义函数        double log10(double x);
函数说明        log10()用来计算以10为底的x对数值,然后将结果返回。
返回值        返回参数x以10为底的对数值。
错误代码        EDOM 参数x为负数。
RANGE 参数x为零值,零的对数值无定义。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h.
main()
{
double answer;
answer = log10(100);
printf(“log10(100) = %f\n”,answer);
}
执行       
log10(100) = 2.000000


pow(计算次方值)

相关函数        exp,log,log10
表头文件        #include<math.h>
定义函数        double pow(double x,double y);
函数说明        pow()用来计算以x为底的y次方值,即xy值,然后将结果返回。
返回值        返回x的y次方计算结果。
错误代码        EDOM 参数x为负数且参数y不是整数。
附加说明        使用GCC编译时请加入-lm。

范例       
#include <math.h>
main()
{
double answer;
answer =pow(2,10);
printf(“2^10 = %f\n”,answer);
}
执行       
2^10 = 1024.000000


sin(取正玄函数值)

相关函数        acos,asin,atan,atan2,cos,tan
表头文件        #include<math.h>
定义函数        double sin(double x);
函数说明        sin()用来计算参数x的正玄值,然后将结果返回。
返回值        返回-1至1之间的计算结果。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
double answer = sin (0.5);
printf(“sin(0.5) = %f\n”,answer);
}
执行       
sin(0.5) = 0.479426


sinh(取双曲线正玄函数值)

相关函数        cosh,tanh
表头文件        #include<math.h>
定义函数        double sinh( double x);
函数说明        sinh()用来计算参数x的双曲线正玄值,然后将结果返回。数学定义式为:(exp(x)-exp(-x))/2。
返回值        返回参数x的双曲线正玄值。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
double answer = sinh (0.5);
printf(“sinh(0.5) = %f\n”,answer);
}
执行       
sinh(0.5) = 0.521095
sqrt(计算平方根值)

相关函数        hypotq
表头文件        #include<math.h>
定义函数        double sqrt(double x);
函数说明        sqrt()用来计算参数x的平方根,然后将结果返回。参数x必须为正数。
返回值        返回参数x的平方根值。
错误代码        EDOM 参数x为负数。
附加说明        使用GCC编译时请加入-lm。

范例       
/* 计算200的平方根值*/
#include<math.h>
main()
{
double root;
root = sqrt (200);
printf(“answer is %f\n”,root);
}
执行       
answer is 14.142136


tan(取正切函数值)

相关函数        atan,atan2,cos,sin
表头文件        #include <math.h>
定义函数        double tan(double x);
函数说明        tan()用来计算参数x的正切值,然后将结果返回。
返回值        返回参数x的正切值。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
double answer = tan(0.5);
printf(“tan (0.5) = %f\n”,answer);
}
执行       
tan(0.5) = 0.546302


tanh(取双曲线正切函数值)

相关函数        cosh,sinh
表头文件        #include<math.h>
定义函数        double tanh(double x);
函数说明        tanh()用来计算参数x的双曲线正切值,然后将结果返回。数学定义式为:sinh(x)/cosh(x)。
返回值        返回参数x的双曲线正切值。
附加说明        使用GCC编译时请加入-lm。

范例       
#include<math.h>
main()
{
double answer = tanh(0.5);
printf(“ tanh(0.5) = %f\n”,answer);
}
执行       
tanh(0.5) = 0.462117


endgrent(关闭组文件)

相关函数        getgrent,setgrent
表头文件        #include<grp.h>
#include<sys/types.h>
定义函数        void endgrent(void);
函数说明        endgrent()用来关闭由getgrent()所打开的密码文件。
返回值        无
附加说明        无

范例       
请参考getgrent()与setgrent()。


endpwent(关闭密码文件)

相关函数        getpwent,setpwent
表头文件        #include<pwd.h>
#include<sys/types.h>
定义函数        void endpwent(void);
函数说明        endpwent()用来关闭由getpwent()所打开的密码文件。
返回值        无
附加说明        无

范例       
请参考getpwent()与setpwent()。


endutent(关闭utmp文件)

相关函数        getutent,setutent
表头文件        #include<utmp.h>
定义函数        void endutent(void);
函数说明        endutent()用来关闭由getutent所打开的utmp文件。
返回值        无
附加说明        无

范例       
请参考getutent()。


fgetgrent(从指定的文件来读取组格式)

相关函数        fgetpwent
表头文件        #include<grp.h>
#include<stdio.h>
#include<sys/types.h>
定义函数        struct group * getgrent(FILE * stream);
函数说明        fgetgrent()会从参数stream指定的文件读取一行数据,然后以group结构将该数据返回。参数stream所指定的文件必须和、etc/group相同的格式。group结构定义请参考getgrent()。
返回值        返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例       
#include <grp.h>
#include<sys/types.h>
#include<stdio.h>
main()
{
struct group *data;
FILE *stream;
int i;
stream = fopen(“/etc/group”,”r”);
while((data = fgetgrent(stream))!=0){
i=0;
printf(“ %s :%s:%d :”,data->gr_name,data->gr_passwd,data->gr_gid);
while (data->gr_mem[i])printf(“%s,”,data->gr_mem[i++]);
printf(“\n”);
}
fclose(stream);
}
执行       
root:x:0:root,
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
tty:x:5
disk:x:6:root
lp:x:7:daemon,lp
mem:x:8
kmem:x:9
wheel:x:10:root
mail:x:12:mail
news:x:13:news
uucp:x:14:uucp
man:x:15
games:x:20
gopher:x:30
dip:x:40:
ftp:x:50
nobody:x:99:

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:26:05 |显示全部楼层
fgetpwent(从指定的文件来读取密码格式)

相关函数        fgetgrent
表头文件        #include<pwd.h>
#include<stdio.h>
#include<sys/types.h>
定义函数        struct passwd * fgetpwent(FILE *stream);
函数说明        fgetpwent()会从参数stream指定的文件读取一行数据,然后以passwd结构将该数据返回。参数stream所指定的文件必须和/etc/passwd相同的格式。passwd结构定义请参考getpwent()。
返回值        返回passwd结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例       
#include<pwd.h>
#include<sys/types.h>
main()
{
struct passwd *user;
FILE *stream;
  stream = fopen(“/etc/passwd”,”r”);
while((user = fgetpwent(stream))!=0){
printf(“%s:%d:%d:%s:%s:%s\n”,user->pw_name,user->pw_uid,user->pw_gid,
user->pw_gecos,user->pw_dir,user->pw_shell);
}
}
执行       
root:0:0:root:/root:/bin/bash
bin:1:1:bin:/bin:
daemon:2:2:daemon:/sbin:
adm:3:4:adm:/var/adm:
lp:4:7:lp:/var/spool/lpd:
sync:5:0:sync:/sbin:/bin/sync
shutdown:6:0:shutdown:/sbin:/sbin/shutdown
halt:7:0:halt:/sbin:/sbin/halt
mail:8:12:mail:/var/spool/mail:
news:9:13:news:var/spool/news
uucp:10:14:uucp:/var/spool/uucp:
operator:11:0perator :/root:
games:12:100:games:/usr/games:
gopher:13:30:gopher:/usr/lib/gopher-data:
ftp:14:50:FTP User:/home/ftp:
nobody:99:99:Nobody:/:
xfs:100:101:X Font Server: /etc/Xll/fs:/bin/false
gdm:42:42:/home/gdm:/bin/bash
kids:500:500: : /home/kids:/bin/bash


getegid(取得有效的组识别码)

相关函数        getgid,setgid,setregid
表头文件        #include<unistd.h>
#include<sys/types.h>
定义函数        gid_t getegid(void);
函数说明        getegid()用来取得执行目前进程有效组识别码。有效的组识别码用来决定进程执行时组的权限。
返回值        返回有效的组识别码。

范例       
main()
{
printf(“egid is %d\n”,getegid());
}

执行       
egid is 0 /*当使用root身份执行范例程序时*/


geteuid(取得有效的用户识别码)

相关函数        getuid,setreuid,setuid
表头文件        #include<unistd.h>
#include<sys/types.h>
定义函数        uid_t geteuid(void)
函数说明        geteuid()用来取得执行目前进程有效的用户识别码。有效的用户识别码用来决定进程执行的权限,借由此改变此值,进程可以获得额外的权限。倘若执行文件的setID位已被设置,该文件执行时,其进程的euid值便会设成该文件所有者的uid。例如,执行文件/usr/bin/passwd的权限为-r-s--x--x,其s位即为setID(SUID)位,而当任何用户在执行passwd时其有效的用户识别码会被设成passwd所有者的uid值,即root的uid值(0)。
返回值        返回有效的用户识别码。

范例       
main()
{
printf (“euid is %d \n”,geteuid());
}
执行       
euid is 0 /*当使用root身份执行范例程序时*/


getgid(取得真实的组识别码)

相关函数        getegid,setregid,setgid
表头文件        #include<unistd.h>
#include<sys/types.h>
定义函数        gid_t getgid(void);
函数说明        getgid()用来取得执行目前进程的组识别码。
返回值        返回组识别码

范例       
main()
{
printf(“gid is %d\n”,getgid());
}
执行       
gid is 0 /*当使用root身份执行范例程序时*/


getgrent(从组文件中取得账号的数据)

相关函数        setgrent,endgrent
表头文件        #include<grp.h>
#include <sys/types.h>
定义函数        struct group *getgrent(void);
函数说明        getgrent()用来从组文件(/etc/group)中读取一项组数据,该数据以group结构返回。第一次调用时会取得第一项组数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。
struct group{
  char *gr_name;     /*组名称*/
  char *gr_passwd;   /* 组密码*/
  gid_t gr_gid;      /*组识别码*/
  char **gr_mem;   /*组成员账号*/
}
返回值        返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。
附加说明        getgrent()在第一次调用时会打开组文件,读取数据完毕后可使用endgrent()来关闭该组文件。
错误代码        ENOMEM 内存不足,无法配置group结构。

范例       
#include<grp.h>
#include<sys/types.h>
main()
{
struct group *data;
int i;
while((data= getgrent())!=0){
  i=0;
  printf(“%s:%s:%d:”,data->gr_name,data->gr_passwd,data->gr_gid);
  while(data->gr_mem[i])printf(“%s,”,data->gr_mem[i++]);
  printf(“\n”);
}
endgrent();
}
执行       
root:x:0:root,
bin:x:1:root,bin,daemon,
daemon:x:2:root,bin,daemon,
sys:x:3:root,bin,adm,
adm:x:4:root,adm,daemon
tty:x:5
disk:x:6:root
lp:x:7:daemon,lp
mem:x:8
kmem:x:9:
wheel:x:10:root
mail:x:12:mail
news:x:13:news
uucp:x:14:uucp
man:x:15:
games:x:20
gopher:x:30
dip:x:40
ftp:x:50
nobody:x:99




getgrgid(从组文件中取得指定gid的数据)

相关函数        fgetgrent,getgrent,getgrnam
表头文件        #include<grp.h>
#include<sys/types.h>
定义函数        strcut group * getgrgid(gid_t gid);
函数说明        getgrgid()用来依参数gid指定的组识别码逐一搜索组文件,找到时便将该组的数据以group结构返回。group结构请参考getgrent()。
返回值        返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例       
/* 取得gid=3的组数据*/
#include<grp.h>
#include<sys/types.h>
main()
{
strcut group *data;
int i=0;
data = getgrgid(3);
printf(“%s:%s:%d:”,data->gr_name,data->gr_passwd,data->gr_gid);
while(data->gr_mem[i])printf(“%s ,”,data->mem[i++]);
printf(“\n”);
}
执行       
sys:x:3:root,bin,adm


getgrnam(从组文件中取得指定组的数据)

相关函数        fgetgrent,getrent,getgruid
表头文件        #include<grp.h>
#include<sys/types.h>
定义函数        strcut group * getgrnam(const char * name);
函数说明        getgrnam()用来逐一搜索参数那么指定的组名称,找到时便将该组的数据以group结构返回。group结构请参考getgrent()。
返回值        返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例       
/* 取得adm的组数据*/
#include<grp.h>
#include<sys/types.h>
main()
{
strcut group * data;
int i=0;
data = getgrnam(“adm”);
printf(“%s:%s:%d:”,data->gr_name,data->gr_passwd,data->gr_gid);
while(data->gr_mem[i])printf(“%s,”,data->gr_mem[i++]);
printf(“\n”);
}
执行       
adm:x:4:root,adm,daemon


getgroups(取得组代码)

相关函数        initgroups,setgroup,getgid,setgid
表头文件        #include<unistd.h>
#include<sys/types.h>
定义函数        int getgroups(int size,gid_t list[]);
函数说明        getgroup()用来取得目前用户所属的组代码。参数size为list〔〕所能容纳的gid_t数目。如果参数size值为零,此函数仅会返回用户所属的组数。
返回值        返回组识别码,如有错误则返回-1。
错误代码        EFAULT 参数list数组地址不合法。
EINVAL 参数size值不足以容纳所有的组

范例       
#include<unistd.h>
#include<sys/types.h>
main()
{
gid_t list[500];
int x,i;
x = getgroups(0.list);
getgroups(x,list);
for(i=0;i<x;i++)
printf(“%d:%d\n”,i,list[i]);
}
执行       
0:0
1:1
2:2
3:3
4:4
5:6
6:10


getpw(取得指定用户的密码文件数据)

相关函数        getpwent
表头文件        #include<pwd.h>
#include<sys/types.h>
定义函数        int getpw(uid_t uid,char *buf);
函数说明        getpw()会从/etc/passwd中查找符合参数uid所指定的用户账号数据,找不到相关数据就返回-1。所返回的buf字符串格式如下:
账号:密码:用户识别码(uid):组识别码(gid):全名:根目录:shell
返回值        返回0表示成功,有错误发生时返回-1。
附加说明        1.        getpw()会有潜在的安全性问题,请尽量使用别的函数取代。
2.        使用shadow的系统已把用户密码抽出/etc/passwd,因此使用getpw()取得的密码将为“x”。

范例       
#include<pwd.h>
#include<sys/types.h>
main()
{
char buffer[80];
getpw(0,buffer);
printf(“%s\n”,buffer);
}
执行       
root:x:0:0:root:/root:/bin/bash


getpwent(从密码文件中取得账号的数据)

相关函数        getpw,fgetpwent,getpwnam,getpwuid,setpwent,endpwent
表头文件        #include<pwd.h>
#include<sys/types.h>
定义函数        strcut passwd * getpwent(void);
函数说明        getpwent()用来从密码文件(/etc/passwd)中读取一项用户数据,该用户的数据以passwd结构返回。第一次调用时会取得第一位用户数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。
passwd结构定义如下:
struct passwd{
  char * pw_name;   /*用户账号*/
  char * pw_passwd;  /*用户密码*/
  uid_t pw_uid;      /*用户识别码*/
  gid_t pw_gid;      /*组识别码*/
  char * pw_gecos;   /*用户全名*/
  char * pw_dir;     /*家目录*/
  char * pw_shell;    /* 所使用的shell路径*/
};
返回值        返回passwd结构数据,如果返回NULL则表示已无数据,或有错误发生。
附加说明        getpwent()在第一次调用时会打开密码文件,读取数据完毕后可使用endpwent()来关闭该密码文件。
错误代码        ENOMEM 内存不足,无法配置passwd结构。

范例       
#include<pwd.h>
#include<sys/types.h>
main()
{
  struct passwd *user;
  while((user = getpwent())!=0){
   printf(“%s:%d:%d:%s:%s:%s\n”,user->pw_name,user->pw_uid,user->pw_gid,
   user->pw_gecos,user->pw_dir,user->pw_shell);
}
endpwent();
}
执行       
root:0:0:root:/root:/bin/bash
bin:1:1:bin:/bin:
daemon:2:2:daemon:/sbin:
adm:3:4:adm:/var/adm:
lp:4:7:lp:/var/spool/lpd:
sync:5:0:sync:/sbin:/bin/sync
shutdown:6:0:shutdown:/sbin:/sbin/shutdown
halt:7:0:halt:/sbin:/sbin/halt
mail:8:12:mail:/var/spool/mail:
news:9:13:news:var/spool/news
uucp:10:14:uucp:/var/spool/uucp:
operator:11:0:operator :/root:
games:12:100:games:/usr/games:
gopher:13:30:gopher:/usr/lib/gopher-data:
ftp:14:50:FTP User:/home/ftp:
nobody:99:99:Nobody:/:
xfs:100:101:X Font Server: /etc/Xll/fs:/bin/false
gdm:42:42:/home/gdm:/bin/bash
kids:500:500: : /home/kids:/bin/bash
您需要登录后才可以回帖 登录 | 加入社区!

C语言 ( 粤ICP备11042647号-2 )

GMT+8, 2012-2-7 22:40

©2009-2011 cyuyan.com.cn

回顶部