ºìÁªLinuxÃÅ»§
Linux°ïÖú

Linux³£ÓÃCº¯Êý--Êý¾Ý½á¹¹¼°Ë㷨ƪ

·¢²¼Ê±¼ä:2006-02-02 09:07:14À´Ô´:ºìÁª×÷Õß:ABC
crypt£¨½«ÃÜÂë»òÊý¾Ý±àÂ룩
Ïà¹Øº¯Êý getpass

±íÍ·Îļþ #define _XOPEN_SOURCE
#include

¶¨Ò庯Êý char * crypt (const char *key,const char * salt);

º¯Êý˵Ã÷ crypt()½«Ê¹ÓÃData Encryption Standard(DES)ÑÝËã·¨½«²ÎÊýkeyËùÖ¸µÄ×Ö·û´®¼ÓÒÔ±àÂ룬key×Ö·û´®³¤¶È½öÈ¡Ç°8¸ö×Ö·û£¬³¬¹ý´Ë³¤¶ÈµÄ×Ö·ûûÓÐÒâÒå¡£²ÎÊýsaltΪÁ½¸ö×Ö·û×é³ÉµÄ×Ö·û´®£¬ÓÉa-z¡¢A-Z¡¢0-9£¬¡°.¡±ºÍ¡°/¡±Ëù×é³É£¬ÓÃÀ´¾ö¶¨Ê¹ÓÃ4096 ÖÖ²»Í¬ÄÚ½¨±í¸ñµÄÄÄÒ»¸ö¡£º¯ÊýÖ´Ðгɹ¦ºó»á·µ»ØÖ¸Ïò±àÂë¹ýµÄ×Ö·û´®Ö¸Õ룬²ÎÊýkey ËùÖ¸µÄ×Ö·û´®²»»áÓÐËù¸ü¶¯¡£±àÂë¹ýµÄ×Ö·û´®³¤¶ÈΪ13 ¸ö×Ö·û£¬Ç°Á½¸ö×Ö·ûΪ²ÎÊýsalt´ú±íµÄ×Ö·û´®¡£

·µ»ØÖµ ·µ»ØÒ»¸öÖ¸ÏòÒÔNULL½áβµÄÃÜÂë×Ö·û´®¡£

¸½¼Ó˵Ã÷ ʹÓÃGCC±àÒëʱÐè¼Ó-lcrypt¡£

·¶Àý #include
main()
{
char passwd[13];
char *key;
char slat[2];
key= getpass(¡°Input First Password:¡±);
slat[0]=key[0];
slat[1]=key[1];
strcpy(passwd,crypt(key slat));
key=getpass(¡°Input Second Password:¡±);
slat[0]=passwd[0];
slat[1]=passwd[1];
printf(¡°After crypt(),1st passwd :%s\n¡±,passwd);
printf(¡°After crypt(),2nd passwd:%s \n¡±,crypt(key slat));
}

Ö´ÐÐ Input First Password: /* ÊäÈëtest£¬±àÂëºó´æÓÚpasswd[ ] */
Input Second Password /*ÊäÈëtest£¬ÃÜÂëÏàͬ±àÂëºóÒ²»áÏàͬ*/
After crypt () 1st Passwd : teH0wLIpW0gyQ
After crypt () 2nd Passwd : teH0wLIpW0gyQ

¡¡




bsearch£¨¶þÔªËÑË÷£©
Ïà¹Øº¯Êý qsort

±íÍ·Îļþ #include

¶¨Ò庯Êý void *bsearch(const void *key,const void *base,size_t nmemb,size_tsize,int (*compar) (const void*,const void*));

º¯Êý˵Ã÷ bsearch()ÀûÓöþÔªËÑË÷´ÓÅÅÐòºÃµÄÊý×éÖвéÕÒÊý¾Ý¡£²ÎÊýkeyÖ¸ÏòÓû²éÕҵĹؼüÊý¾Ý£¬²ÎÊýbaseÖ¸ÏòÒª±»ËÑË÷µÄÊý×鿪ͷµØÖ·£¬²ÎÊýnmemb ´ú±íÊý×éÖеÄÔªËØÊýÁ¿£¬Ã¿Ò»ÔªËصĴóСÔòÓɲÎÊýsize¾ö¶¨£¬×îºóÒ»Ïî²ÎÊýcompar Ϊһº¯ÊýÖ¸Õ룬Õâ¸öº¯ÊýÓÃÀ´ÅжÏÁ½¸öÔªËØÖ®¼äµÄ´óС¹Øϵ£¬Èô´«¸øcompar µÄµÚÒ»¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾Ý´óÓÚµÚ¶þ¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾ÝÔò±ØÐë»Ø´«´óÓÚ0 µÄÖµ£¬Á½¸öÔªËØÊý¾ÝÏàµÈÔò»Ø´«0¡£

¸½¼Ó˵Ã÷ ÕÒµ½¹Ø¼üÊý¾ÝÔò·µ»ØÕÒµ½µÄµØÖ·£¬Èç¹ûÔÚÊý×éÖÐÕÒ²»µ½¹Ø¼üÊý¾ÝÔò·µ»ØNULL¡£

·¶Àý #include
#include
#define NMEMB 5
#define SIZE 10
int compar(const void *a,const void *b)
{
return (strcmp((char *)a,(char *)b));
}
main()
{
char data[50][size]={¡°linux¡±,¡±freebsd¡±,¡±solaris¡±,¡±sunos¡±,¡±windows¡±};
char key[80],*base ,*offset;
int i, nmemb=NMEMB,size=SIZE;
while(1){
printf(¡°>¡±);
fgets(key,sizeof(key),stdin);
key[strlen(key)-1]=¡¯\0¡¯;
if(!strcmp(key,¡±exit¡±))break;
if(!strcmp(key,¡±list¡±)){
for(i=0;iprintf(¡°%s\n¡±,data);
continue;
}
base = data[0];
qsort(base,nmemb,size,compar);
offset = (char *) bsearch(key,base,nmemb,size,compar);
if( offset = =NULL){
printf(¡°%s not found!\n¡±,key);
strcpy(data[nmemb++],key);
printf(¡°Add %s to data array\n¡±,key);
}else{
printf(¡°found: %s \n¡±,offset);
}
}
}

Ö´ÐÐ >hello /*ÊäÈëhello×Ö·û´®*/
hello not found! /*ÕÒ²»µ½hello ×Ö·û´®*/
add hello to data array /*½«hello×Ö·û´®¼ÓÈë*/
>.list /*ÁгöËùÓÐÊý¾Ý*/
freebsd
linux
solaris
sunos
windows
hello
>hello
found: hello

¡¡




lfind£¨ÏßÐÔËÑË÷£©
Ïà¹Øº¯Êý lsearch

±íÍ·Îļþ #include

¶¨Ò庯Êý void *lfind (const void *key,const void *base,size_t *nmemb,size_t
size,int(* compar) (const void * ,const void *));

º¯Êý˵Ã÷ lfind()ÀûÓÃÏßÐÔËÑË÷ÔÚÊý×éÖдÓÍ·ÖÁβһÏîÏî²éÕÒÊý¾Ý¡£²ÎÊýkeyÖ¸ÏòÓû²éÕҵĹؼüÊý¾Ý£¬²ÎÊýbaseÖ¸ÏòÒª±»ËÑË÷µÄÊý×鿪ͷµØÖ·£¬²ÎÊýnmemb´ú±íÊý×éÖеÄÔªËØÊýÁ¿£¬Ã¿Ò»ÔªËصĴóСÔòÓɲÎÊýsize¾ö¶¨£¬×îºóÒ»Ïî²ÎÊýcomparΪһº¯ÊýÖ¸Õ룬Õâ¸öº¯ÊýÓÃÀ´ÅжÏÁ½¸öÔªËØÊÇ·ñÏàͬ£¬Èô´«¸øcompar µÄÒìµØ¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾ÝºÍµÚ¶þ¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾ÝÏàͬʱÔò·µ»Ø0£¬Á½¸öÔªËØÊý¾Ý²»ÏàͬÔò·µ»Ø·Ç0Öµ¡£Lfind()Óëlsearch()²»Í¬µãÔÚÓÚ£¬µ±ÕÒ²»µ½¹Ø¼üÊý¾Ýʱlfind()½ö»á·µ»ØNULL£¬¶ø²»»áÖ÷¶¯°Ñ¸Ã±ÊÊý¾Ý¼ÓÈëÊý×éβ¶Ë¡£

·µ»ØÖµ ÕÒµ½¹Ø¼üÊý¾ÝÔò·µ»ØÕÒµ½µÄ¸Ã±ÊÔªËصĵØÖ·£¬Èç¹ûÔÚÊý×éÖÐÕÒ²»µ½¹Ø¼üÊý¾ÝÔò·µ»Ø¿ÕÖ¸Õë(NULL)¡£

·¶Àý ²Î¿¼lsearch()¡£

¡¡




lsearch£¨ÏßÐÔËÑË÷£©
Ïà¹Øº¯Êý lfind

±íÍ·Îļþ #include

¶¨Ò庯Êý void *lsearch(const void * key ,const void * base ,size_t * nmemb,size_t size, int ( * compar) (const void * ,const void *));

º¯Êý˵Ã÷ lsearch()ÀûÓÃÏßÐÔËÑË÷ÔÚÊý×éÖдÓÍ·ÖÁβһÏîÏî²éÕÒÊý¾Ý¡£²ÎÊýkeyÖ¸ÏòÓû²éÕҵĹؼüÊý¾Ý£¬²ÎÊýbaseÖ¸ÏòÒª±»ËÑË÷µÄÊý×鿪ͷµØÖ·£¬²ÎÊýnmemb ´ú±íÊý×éÖеÄÔªËØÊýÁ¿£¬Ã¿Ò»ÔªËصĴóСÔòÓɲÎÊýsize ¾ö¶¨£¬×îºóÒ»Ïî²ÎÊýcompar Ϊһº¯ÊýÖ¸Õ룬Õâ¸öº¯ÊýÓÃÀ´ÅжÏÁ½¸öÔªËØÊÇ·ñÏàͬ£¬Èô´«¸øcompar µÄµÚÒ»¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾ÝºÍµÚ¶þ¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾ÝÏàͬʱÔò·µ»Ø0£¬Á½¸öÔªËØÊý¾Ý²»ÏàͬÔò·µ»Ø·Ç0 Öµ¡£Èç¹ûlsearch£¨£©ÕÒ²»µ½¹Ø¼üÊý¾Ýʱ»áÖ÷¶¯°Ñ¸ÃÏîÊý¾Ý¼ÓÈëÊý×éÀï¡£

·µ»ØÖµ ÕÒµ½¹Ø¼üÊý¾ÝÔò·µ»ØÕÒµ½µÄ¸Ã±ÊÔªËصÄËÄÖ«£¬Èç¹ûÔÚÊý×éÖÐÕÒ²»µ½¹Ø¼üÊý¾ÝÔò½«´Ë¹Ø¼üÊý¾Ý¼ÓÈëÊý×飬ÔٰѼÓÈëÊý×éºóµÄµØÖ··µ»Ø¡£

·¶Àý #include
#include
#define NMEMB 50
#define SIZE 10
int compar (comst void *a,const void *b)
{
return (strcmp((char *) a, (char *) b));
}
main()
{
char data[NMEMB][SIZE]={¡°Linux¡±,¡±freebsd¡±,¡±solzris¡±,¡±sunos¡±,¡±windows¡±};
char key[80],*base,*offset;
int i, nmemb=NMEMB,size=SIZE;
for(i=1;i<5;i++){
fgets(key,sizeof9key),stdin);
key[strlen(key)-1]=¡¯\0¡¯;
base = data[0];
offset = (char *)lfind(key,base,&nmemb,size,compar);
if(offset ==NULL){
printf(¡°%s not found!\n¡±,key);
offset=(char *) lsearch(key,base,&nmemb,size,compar);
printf(¡°Add %s to data array\n¡±,offset);
}else{
printf(¡°found : %s \n¡±,offset);
}
}
}

Ö´ÐÐ linux
found:linux
os/2
os/2 not found!
add os/2 to data array
os/2
found:os/2

¡¡




qsort£¨ÀûÓÿìËÙÅÅÐò·¨ÅÅÁÐÊý×飩
Ïà¹Øº¯Êý bsearch

±íÍ·Îļþ #include

¶¨Ò庯Êý void qsort(void * base,size_t nmemb,size_t size,int ( * compar)(const void *, const void *));

º¯Êý˵Ã÷ ²ÎÊýbaseÖ¸ÏòÓûÅÅÐòµÄÊý×鿪ͷµØÖ·£¬²ÎÊýnmemb´ú±íÊý×éÖеÄÔªËØÊýÁ¿£¬Ã¿Ò»ÔªËصĴóСÔòÓɲÎÊýsize¾ö¶¨£¬×îºóÒ»Ïî²ÎÊýcomparΪһº¯ÊýÖ¸Õ룬Õâ¸öº¯ÊýÓÃÀ´ÅжÏÁ½¸öÔªËؼäµÄ´óС¹Øϵ£¬Èô´«¸øcomparµÄµÚÒ»¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾Ý´óÓÚµÚ¶þ¸ö²ÎÊýËùÖ¸µÄÔªËØÊý¾ÝÔò±ØÐë»Ø´«´óÓÚÁãµÄÖµ£¬Á½¸öÔªËØÊý¾ÝÏàµÈÔò»Ø´«0¡£

·µ»ØÖµ

¸½¼Ó˵Ã÷

·¶Àý #define nmemb 7
#include
int compar (const void *a ,const void *b)
{
int *aa=(int * ) a,*bb = (int * )b;
if( * aa >* bb)return 1;
if( * aa == * bb) return 0;
if( * aa < *bb) return -1;
}
main( )
{
int base[nmemb]={ 3,102,5,-2,98,52,18};
int i;
for ( i=0; iprintf(¡°%d ¡°,base);
printf(¡°\n¡±);
qsort(base,nmemb,sizeof(int),compar);
for(i=0;iprintf(¡°%d¡±base);
printf(¡°\n¡±);
}

Ö´ÐÐ 3 102 5 -2 98 52 18
-2 3 5 18 52 98 102

¡¡




rand£¨²úÉúËæ»úÊý£©
Ïà¹Øº¯Êý srand£¬random£¬srandom

±íÍ·Îļþ #include

¶¨Ò庯Êý int rand(void)

º¯Êý˵Ã÷ rand()»á·µ»ØÒ»Ëæ»úÊýÖµ£¬·¶Î§ÔÚ0ÖÁRAND_MAX ¼ä¡£ÔÚµ÷Óô˺¯Êý²úÉúËæ»úÊýÇ°£¬±ØÐëÏÈÀûÓÃsrand()ÉèºÃËæ»úÊýÖÖ×Ó£¬Èç¹ûδÉèËæ»úÊýÖÖ×Ó£¬rand()ÔÚµ÷ÓÃʱ»á×Ô¶¯ÉèËæ»úÊýÖÖ×ÓΪ1¡£¹ØÓÚËæ»úÊýÖÖ×ÓÇë²Î¿¼srand()¡£

·µ»ØÖµ ·µ»Ø0ÖÁRAND_MAXÖ®¼äµÄËæ»úÊýÖµ£¬RAND_MAX¶¨ÒåÔÚstdlib.h£¬ÆäֵΪ2147483647¡£

·¶Àý /* ²úÉú½éÓÚ1 µ½10 ¼äµÄËæ»úÊýÖµ£¬´Ë·¶ÀýδÉèËæ»úÊýÖÖ×Ó£¬ÍêÕûµÄËæ»úÊý²úÉúÇë²Î¿¼
srand£¨£©*/
#include
main()
{
int i,j;
for(i=0;i<10;i++)
{
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
printf(¡°%d ¡°,j);
}
}

Ö´ÐÐ 9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6

¡¡




srand£¨ÉèÖÃËæ»úÊýÖÖ×Ó£©
Ïà¹Øº¯Êý rand£¬random srandom

±íÍ·Îļþ #include

¶¨Ò庯Êý void srand (unsigned int seed);

º¯Êý˵Ã÷ srand()ÓÃÀ´ÉèÖÃrand()²úÉúËæ»úÊýʱµÄËæ»úÊýÖÖ×Ó¡£²ÎÊýseed±ØÐëÊǸöÕûÊý£¬Í¨³£¿ÉÒÔÀûÓÃgeypid()»òtime(0)µÄ·µ»ØÖµÀ´µ±×öseed¡£Èç¹ûÿ´Îseed¶¼ÉèÏàֵͬ£¬rand()Ëù²úÉúµÄËæ»úÊýֵÿ´Î¾Í»áÒ»Ñù¡£

·µ»ØÖµ

·¶Àý /* ²úÉú½éÓÚ1 µ½10 ¼äµÄËæ»úÊýÖµ£¬´Ë·¶ÀýÓëÖ´Ðнá¹û¿ÉÓërand£¨£©²ÎÕÕ*/
#include
#include
main()
{
int i,j;
srand((int)time(0));
for(i=0;i<10;i++)
{
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
printf(¡° %d ¡°,j);
}
}

Ö´ÐÐ 5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 3 ÌõÆÀÂÛ

  1. oio-c ÓÚ 2006-08-06 08:15:31·¢±í:

    ½øÀ´´ÕÈÈÄÖ!¸Õ½øÀ´²»ÈÃÎÒÏÂÔØ£¬»Ø¸öÌû׬µãÍþÍû£¡²»ÒªËµÎÒ¹àË®£¡£¡

  2. heshang ÓÚ 2006-08-04 11:05:48·¢±í:

    ºÃÌù

  3. chg ÓÚ 2006-07-28 00:35:12·¢±í:

    good