B.2 Creating Strings in the Kernel

   


This section introduces several useful functions to create debugging messages. As was mentioned earlier, they are somewhat similar to the functions provided by standard C libraries.

sprint()

lib/vsprintf.c


sprintf(buffer, str,arg1,...) is a function very useful for converting certain variable types into strings. Its functionality is almost identical to that of the sprintf() function in the C library <stdio.h>. sprintf() also supports the formatting options known from printf().

The character string str consists of regular characters and control characters, if present. Instead of control characters that begin with a % sign and end with a type descriptor (c, s, p, n, o, x, X, d, i, u), variable values are inserted according to the formatting specification. The nth formatting specification refers to the nth argument in the str string. The created string is written to the buffer, and the set of written characters (including the closing null byte) is returned as the result.

The formatting options of sprintf() are as follows: A format specification begins with the % sign and ends with one of the type descriptors mentioned. Between these two characters, there may be the following format specifications (in this order):

  • Blank No leading plus sign is used for a positive number, but instead a blank. A minus sign is inserted for negative numbers. This enables positive and negative numbers to appear aligned (if they have the same length).

  • This argument is inserted left-justified in the string.

  • + A plus sign is inserted if the argument is positive.

  • # If the octal system (o) was selected as the output form, then a leading null is added to the argument; 0x or 0X are inserted for the hexadecimal system (x or X).

  • min, max The numbers min and max specify the minimum or maximum length of an output. min or max can be omitted if the respective option is not desired. If min begins with a null, then the output is padded with zeros to the minimum format length.

  • h, l, or L denote that a variable is of short or long type.

  • type A formatting specification ends with the type of variable to be output. The following types are available:

    • c (character) The character arg is output.

    • s (string) The string arg is output to the first null byte (unless limited by the maximum format length).

    • p (pointer) The pointer address is output in hexadecimal system.

    • o (integer) The number arg is output in octal system.

    • x, X (integer) The number arg is output in hexadecimal system.

    • d, i (integer) The leading sign for the number arg should be considered in the output.

    • u (integer) The number arg is considered to be an unsigned number.

The formatting options described correspond to the options of printk(), because printk() itself uses sprintf() to format an output string. However, sprintf() is useful not only in connection with printk(), but also to generate outputs in the proc directory.

As was described in Section 2.8, the output is recreated upon each read operation on the proc directory. This is not different from a string that includes output data. We can use sprintf() to output not only fixed text blocks, but also formatted contents of variables and memory addresses.

String Operations

lib/string.c


The Linux kernel has several functions for simple (and familiar) work with strings. These functions are similar to the string operations of the C library. We will briefly introduce them here:

  • Copying strings

    • strcpy(dest, src) copies the string src, including the closing null byte, to the address dest. strcpy() returns the dest pointer.

    • strncpy(dest, src, count) copies a maximum of count bytes from the src string to the address dest. If the original string was longer than count bytes, then no closing null byte is appended. dest is also returned here.

    • strcat(s1, src) extends the string s1 by the string s2, which is appended to the end of s1.

    • strncat(s1, s2, count) works like strcat, but copies a maximum of count bytes.

  • Comparing strings

    • strcmp(s1, s2) compares two strings and returns 0 if the strings are equal. Otherwise, it returns a positive value if s1 is lexicographically larger than s2, but a negative value if s1 is lexicographically smaller than s2.

    • strncmp(s1, s2, count) compares a maximum of count bytes of two strings. The return values correspond to those of the strcmp() function.

    • strnicmp(s1, s2, count) also compares the first count characters of two strings, s1 and s2, but ignores lowercase and uppercase.

  • Searching in strings

    • strchr(str, ch) searches for the first occurrence of the character ch in the string str. If it was successful, it returns the memory location of the first occurrence; otherwise, null.

    • strrchr(str, ch) searches for the last occurrence of the character ch in the string str. If it was successful, it returns the memory location of the last occurrence; otherwise, null.

    • strpbrk(str, ch_str) finds the first occurrence of a character from the string ch_str in the string str.

    • strtok(str, tok_str) returns the first string from str that does not contain any character from the string tok_str.

  • Length of strings

    • strlen(str) returns the length of the string str (excluding null bytes).

    • strnlen(str, count) returns the length of the string str or count, if the string is longer than count.

    • strspn(str, ch_str) computes the length of that initial part of the string str in which only characters from the string ch_str occur, beginning from the first character in str.

  • Functions for memory locations

    • memset(ptr, ch, count) fills count bytes of memory (starting from the ptr location) with the value of the ch parameter.

    • memcpy(dest, src, count) copies count bytes from the memory location src to the memory location dest.

    • memcmp(ptr1, ptr2, count) compares count bytes of two memory locations, prt1 and prt2. The return values correspond to those of the strcmp function.


       


    Linux Network Architecture
    Linux Network Architecture
    ISBN: 131777203
    EAN: N/A
    Year: 2004
    Pages: 187

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net