Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

% sign used in wierd way 2

Status
Not open for further replies.

simanek

Programmer
Jan 19, 2001
137
US
Can some translate the following line for me?
syslog (LOG_INFO,"%s, while reading line user=%.80s host=%.80s", e,user ? user : "???",tcp_clienthost ());

It comes from the source of an imap file. I know what the syslog command does, but what on earth does everything after "user=" mean? I would really appreciate the help. Thanks. Mike
~~~~
simanek@uiuc.edu
"It's a Swingline!"
~~~~
 
Okay, let's break it down. Here's the original call:

syslog (LOG_INFO,"%s, while reading line user=%.80s host=%.80s", e,user ? user : "???",tcp_clienthost ());

[/b]%s[/b] is the string variable called 'e'

The first %.80s is a maximum 80 characters, and is the string variable called user if the user string is not null. Otherwise, it's the string literal "???".

The second %.80s is a maximum 80 characters, and is the string returned by the function tcp_clienthost().

Hope that helps.
 
I'm not sure how much you know about C/C++, so I'll err on the side of explaining too much.

In C and C++, strings are often built "on the fly" using % as an placeholder for a variable value. The character(s) following the % identifies the type of variable. Other modifiers which further define the variable value may also be included. Here are some examples:

%s is the placeholder for a string variable
%i is the placeholder for an integer

and the additional modifier you saw in your previous example:

%.80s describes a placeholder for a string variable which
is limited to a maximum of 80 characters long.

So, in your syslog example, the string is built by substituting values (in sequential order) from the variables which follow it, and *then* it is passed as a completed string parameter to the syslog() function.

Sometimes programmers can make the parameter substitution more clear by how they align the elements. Notice the second parameter (the first is "LOG_INFO"), which is the string to be formatted or built (and which is enclosed in quotation marks. The elements that follow the "format string" are the variables to be used or substituted in the string:

syslog (LOG_INFO,
"%s, while reading line user=%.80s host=%.80s",
e,
user ? user : "???",
tcp_clienthost ()
);


You may also find the line:

user ? user : "???"

to be rather mysterious. It is shorthand notation for a comparison. The statement to the left of the question mark is treated as a test (a different test could be: "age == 30"). If the test is true, then what is to the left of the colon :)) is used. If the test is false, then what is to the right of the colon is used. The test above simply says, "if the user value exists (is not NULL)". To make this more simple to understand, the test could be written this way in longhand:

if (user != NULL)
{
// the user variable is not empty; so the user is known.
// use the value of the variable user for the user name.
}
else
{
// value is empty, so the user is unknown.
// use the string, "???" for the user name.
}


Hope this helps. ;-)
 
programsecrets, I've not done much string formatting in this respect. I've seen it done a little when working with C and using functions like print(), but I never truly understood what was going on. What you said helped, but is it function specific? It seems as though syslog()<or any other function that formats strings in this manner> is accepting these parameters and will format the string accordingly, but it is also conceivable that it is native to C that if there are comma separated values after a string that has %xx in it these values will be substituted in the string. Am I close on either of these guesses? Thanks! MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
:^) Thanks myenigmaself.

Yes, it is function specific. The printf() family, as well as syslog() and other similar functions are specifically &quot;built&quot; to handle this. They must:

1) accept a variable number of arguments,
2) use string parsing to find these special format characters,
3) have functionality to interpret the format specifiers, such as a switch/case statements,
4) use string functions to create the entire string.

So, your first guess is the correct one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top