Common Linux C functions-data structures and algorithms-general Linux technology-Linux programming and kernel information. For more information, see the following section. Crypt (password or data encoding)
Getpass
Header file # define _ XOPEN_SOURCE
# Include
Define the function char * crypt (const char * key, const char * salt );
Function Description: crypt () uses the Data Encryption Standard (DES) algorithm to encode the string referred to by the parameter key. The length of the key string is only the first 8 characters, the characters that exceed this length are meaningless. The salt parameter is a string consisting of A-z, a-Z, 0-9, ". "and"/"are used to determine which of the 4096 different built-in tables to use. After the function is successfully executed, a pointer to the encoded string is returned, and the string referred to by the parameter key is not changed. The encoded string is 13 characters long and the first two characters are strings represented by the salt parameter.
The Return Value Returns a string pointing to the password ending with NULL.
Run Input First Password:/* and enter test, which is encoded and stored in passwd [] */
Input Second Password/* enter test, and the Password will be the same after the same encoding */
After crypt () 1st Passwd: teH0wLIpW0gyQ
After crypt () 2nd Passwd: teH0wLIpW0gyQ
Bsearch)
Related Function qsort
Header file # include
Define the function void * bsearch (const void * key, const void * base, size_t nmemb, size_tsize, int (* compar) (const void *, const void *));
Function Description: bsearch () uses binary search to find data from sorted arrays. The parameter key points to the key data to be searched. The parameter base points to the address starting with the array to be searched. The parameter nmemb indicates the number of elements in the array. The size of each element is determined by the parameter size, the last compar parameter is a function pointer used to determine the size relationship between two elements, if the element data referred to by the first parameter passed to compar is greater than the element data referred to by the second parameter, the value greater than 0 must be returned. If the two elements are equal, the return value is 0.
If key data is found in the array, return the address. If no key data is found in the array, return NULL.
Example # 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; I Printf ("% 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 );
}
}
}
Execute> hello/* enter the hello string */
Hello not found! /* The hello string cannot be found */
Add hello to data array/* add the hello string */
>. List/* list all data */
Freebsd
Linux
Solaris
Sunos
Windows
Hello
> Hello
Found: hello
Lfind (linear search)
Related functions
Header file # include
Define the function void * lfind (const void * key, const void * base, size_t * nmemb, size_t
Size, int (* compar) (const void *, const void *));
Function Description: lfind () uses linear search to search for data from the beginning to the end of an array. The parameter key points to the key data to be searched. The parameter base points to the address starting with the array to be searched. The parameter nmemb indicates the number of elements in the array. The size of each element is determined by the parameter size, the last compar parameter is a function pointer, which is used to determine whether two elements are the same, if the element data referred to by the remote parameter passed to compar is the same as the element data referred to by the second parameter, 0 is returned. If the two elements are different, a non-0 value is returned. The difference between Lfind () and lsearch () is that when key data cannot be found, lfind () Only returns NULL, rather than actively adding the data to the end of the array.
If the key data is not found in the array, the NULL pointer is returned ).
For more information, see lsearch ().
Lsearch)
Related functions
Header file # include
Define the function void * lsearch (const void * key, const void * base, size_t * nmemb, size_t size, int (* compar) (const void *, const void *));
Function Description: lsearch () uses linear search to search for data from the beginning to the end of an array. The parameter key points to the key data to be searched. The parameter base points to the address starting with the array to be searched. The parameter nmemb indicates the number of elements in the array. The size of each element is determined by the parameter size, the last compar parameter is a function pointer, which is used to determine whether two elements are the same, if the element data referred to by the first parameter passed to compar is the same as the element data referred to by the second parameter, 0 is returned. If the two elements are different, a non-0 value is returned. If the key data cannot be found by lsearch (), the data is automatically added to the array.
If the key data is not found in the array, the key data is added to the array and the address after the array is returned.
Example # 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 );
}
}
}
Run linux
Found: linux
OS/2
OS/2 not found!
Add OS/2 to data array
OS/2
Found: OS/2
Qsort (sort arrays by quick sorting)
Related Function bsearch
Header file # include
Define the void qsort (void * base, size_t nmemb, size_t size, int (* compar) (const void *, const void *) function *));
Function Description: The base parameter points to the address starting with the array to be sorted. The nmemb parameter indicates the number of elements in the array. The size of each element is determined by the parameter size. The final parameter compar is a function pointer, this function is used to determine the size relationship between two elements. If the element data passed to the first parameter of compar is greater than the element data referred to by the second parameter, it must return a value greater than zero, if the two elements are equal, 0 is returned.
Return Value
Additional instructions
Example # 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 };
Int I;
For (I = 0; I Printf ("% d", base );
Printf ("\ n ");
Qsort (base, nmemb, sizeof (int), compar );
For (I = 0; I Printf ("% d" base );
Printf ("\ n ");
}
Run 3 102 5-2 98 52 18
-2 3 5 18 52 98 102
Rand (Random Number Generation)
Related functions: srand, random, and srandom
Header file # include
Define the int rand (void) Function)
Function Description rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. For details about random seed, see srand ().
The Return Value Returns a random value between 0 and RAND_MAX. RAND_MAX is defined in stdlib. h and its value is 2147483647.
Example/* generates a random number ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
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 );
}
}
Run 9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6
Srand (set Random Seed)
Related functions: rand, random, srandom
Header file # include
Define the void srand (unsigned int seed) function );
Function Description srand () is used to set the Random Seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.
Return Value
Example/* generates a random number ranging from 1 to 10. This example and execution result can be referenced with 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 );
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.