<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WiredRevolution.com &#187; C</title>
	<atom:link href="http://www.wiredrevolution.com/category/c/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wiredrevolution.com</link>
	<description>A Bit of Linux Wisdom</description>
	<lastBuildDate>Wed, 18 Jan 2012 22:45:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Find controlling terminal with ttyname</title>
		<link>http://www.wiredrevolution.com/c/find-controlling-terminal-with-ttyname?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=find-controlling-terminal-with-ttyname</link>
		<comments>http://www.wiredrevolution.com/c/find-controlling-terminal-with-ttyname#comments</comments>
		<pubDate>Thu, 06 Nov 2008 13:08:37 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[/dev/pts]]></category>
		<category><![CDATA[/dev/tty]]></category>
		<category><![CDATA[controlling]]></category>
		<category><![CDATA[file descriptors]]></category>
		<category><![CDATA[pseudo-terminal]]></category>
		<category><![CDATA[stderr]]></category>
		<category><![CDATA[stdin]]></category>
		<category><![CDATA[stdout]]></category>
		<category><![CDATA[system call]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[ttyname]]></category>
		<category><![CDATA[virtual console]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=615</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/>You can identify the path the the controlling terminal device path for your current process in C with the ttyname() system call. This controlling terminal can be a virtual console (/dev/ttyn) or a pseudo-terminal (/dev/pts/n). The ttyname system call takes the following format. char *ttyname(int fd); The stdin, stdout, stderr file descriptors unless redirected are [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/io-redirection-in-bash' rel='bookmark' title='Permanent Link: I/O redirection in BASH'>I/O redirection in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
<li><a href='http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c' rel='bookmark' title='Permanent Link: Display hostname and IP address in C'>Display hostname and IP address in C</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/><p>You can identify the path the the controlling terminal device path for your current process in C with the ttyname() system call.  This controlling terminal can be a <strong>virtual console</strong> (<strong>/dev/tty</strong>n) or a <strong>pseudo-terminal</strong> (<strong>/dev/pts/</strong>n).</p>
<p>The ttyname system call takes the following format.</p>
<p><strong>char *ttyname(int fd);</strong></p>
<p>The <strong>stdin</strong>, <strong>stdout</strong>, <strong>stderr</strong> file descriptors unless redirected are by default mapped to the controlling terminal. So for example you can pass it the stdin file descriptor.</p>
<pre>
char * pathname;
pathname = ttyname(0);

printf("controlling terminal : %s\n", pathname);
</pre>
<pre>
controlling terminal : /dev/pts/0
</pre>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/io-redirection-in-bash' rel='bookmark' title='Permanent Link: I/O redirection in BASH'>I/O redirection in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
<li><a href='http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c' rel='bookmark' title='Permanent Link: Display hostname and IP address in C'>Display hostname and IP address in C</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/c/find-controlling-terminal-with-ttyname/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find IP address from remote end of a TCP socket</title>
		<link>http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=find-ip-address-from-remote-end-of-a-tcp-socket</link>
		<comments>http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket#comments</comments>
		<pubDate>Fri, 31 Oct 2008 12:49:34 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[getpeername]]></category>
		<category><![CDATA[inet_ntoa]]></category>
		<category><![CDATA[IP address]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sockaddr]]></category>
		<category><![CDATA[sockaddr_in]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[TCP]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=560</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/>In C or C++ it is fairly simple to find the IP address of the remote end of a TCP socket. The following example shows how to do this using the getpeername() and inet_ntoa() system calls. int sockfd; int len; char * hostip; struct sockaddr_in sin; len = sizeof(sin); if (0 != getpeername(sockfd, (struct sockaddr [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c' rel='bookmark' title='Permanent Link: Display hostname and IP address in C'>Display hostname and IP address in C</a></li>
<li><a href='http://www.wiredrevolution.com/windows/find-the-mac-address-on-a-windows-machine' rel='bookmark' title='Permanent Link: Find the MAC address on a Windows machine'>Find the MAC address on a Windows machine</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/><p>In C or C++ it is fairly simple to find the IP address of the remote end of a TCP socket.</p>
<p>The following example shows how to do this using the <strong>getpeername</strong>() and <strong>inet_ntoa</strong>() system calls.</p>
<pre>
int sockfd;
int len;
char * hostip;
struct sockaddr_in sin;

len = sizeof(sin);

if (0 != getpeername(sockfd, (struct sockaddr *) &#038;sin, &#038;len))
        perror("getpeername");
}

hostip = inet_ntoa(sin.sin_addr);

printf("client IP: %s\n", hostip);
</pre>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c' rel='bookmark' title='Permanent Link: Display hostname and IP address in C'>Display hostname and IP address in C</a></li>
<li><a href='http://www.wiredrevolution.com/windows/find-the-mac-address-on-a-windows-machine' rel='bookmark' title='Permanent Link: Find the MAC address on a Windows machine'>Find the MAC address on a Windows machine</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display hostname and IP address in C</title>
		<link>http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=display-hostname-and-ip-address-in-c</link>
		<comments>http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c#comments</comments>
		<pubDate>Sat, 18 Oct 2008 12:54:53 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[gethostbyname]]></category>
		<category><![CDATA[gethostname]]></category>
		<category><![CDATA[hostent]]></category>
		<category><![CDATA[hostname]]></category>
		<category><![CDATA[inet_ntoa]]></category>
		<category><![CDATA[IP address]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=394</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/>Here is a good way to determine the hostname and IP address of the local machine in C. You first have to grab the hostname with gethostname(). char hostbuf[256]; gethostname(hostbuf,sizeof(hostbuf)); Take the hostname and use it to grab the hostent struct with gethostbyname(). struct hostent *hostentry; hostentry = gethostbyname(hostbuf); Finally you have to take the [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket' rel='bookmark' title='Permanent Link: Find IP address from remote end of a TCP socket'>Find IP address from remote end of a TCP socket</a></li>
<li><a href='http://www.wiredrevolution.com/red-hat/change-hostname-on-redhat' rel='bookmark' title='Permanent Link: Change hostname on RedHat'>Change hostname on RedHat</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/><p>Here is a good way to determine the hostname and IP address of the local machine in C.</p>
<p>You first have to grab the hostname with <strong>gethostname</strong>().</p>
<pre>
char hostbuf[256];
gethostname(hostbuf,sizeof(hostbuf));
</pre>
<p>Take the hostname and use it to grab the hostent struct with <strong>gethostbyname</strong>().</p>
<pre>
struct hostent *hostentry;
hostentry = gethostbyname(hostbuf);
</pre>
<p>Finally you have to take the hostent that is returned and pull out the IP address.  It is in network byte order, so you have to convert it to a string with <strong>inet_ntoa</strong>().</p>
<pre>
char * ipbuf;
ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
</pre>
<p>Put all this together into a working programs.</p>
<pre>
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;
#include &lt;netdb.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;

int main()
{
        char hostbuf[256];
        char * ipbuf;
        struct hostent *hostentry;
        int ret;

        ret = gethostname(hostbuf,sizeof(hostbuf));

        if(-1 == ret){
                perror("gethostname");
                exit(1);
        }

        hostentry = gethostbyname(hostbuf);

        if(NULL == hostentry){
                perror("gethostbyname");
                exit(1);
        }

        ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));

        if(NULL == ipbuf){
                perror("inet_ntoa");
                exit(1);
        }

        printf("Hostname: %s Host IP: %s\n", hostbuf, ipbuf);

        return 0;
}
</pre>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket' rel='bookmark' title='Permanent Link: Find IP address from remote end of a TCP socket'>Find IP address from remote end of a TCP socket</a></li>
<li><a href='http://www.wiredrevolution.com/red-hat/change-hostname-on-redhat' rel='bookmark' title='Permanent Link: Change hostname on RedHat'>Change hostname on RedHat</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Format output using printf</title>
		<link>http://www.wiredrevolution.com/c/format-output-using-printf?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=format-output-using-printf</link>
		<comments>http://www.wiredrevolution.com/c/format-output-using-printf#comments</comments>
		<pubDate>Sat, 04 Oct 2008 13:18:54 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=194</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/>printf and its family of functions allow you to easily format string data. There are so many formatting options that it is easy to miss some of its more powerful functionality. Here is a rundown of the more useful features and some examples. The basic printf format. printf(format string, format parameter list); The format string [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c' rel='bookmark' title='Permanent Link: Display hostname and IP address in C'>Display hostname and IP address in C</a></li>
<li><a href='http://www.wiredrevolution.com/c/find-controlling-terminal-with-ttyname' rel='bookmark' title='Permanent Link: Find controlling terminal with ttyname'>Find controlling terminal with ttyname</a></li>
<li><a href='http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket' rel='bookmark' title='Permanent Link: Find IP address from remote end of a TCP socket'>Find IP address from remote end of a TCP socket</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/><p>printf and its family of functions allow you to easily format string data.  There are so many formatting options that it is easy to miss some of its more powerful functionality. Here is a rundown of the more useful features and some examples.</p>
<p>The basic printf format.</p>
<pre>
printf(format string, format parameter list);
</pre>
<p>The format string is composed of a literal string that may include one or more conversion specifiers.  These all begin with &#8216;%&#8217; and have the format below.</p>
<pre>
%[flags][minimum field width][precision/max field width][length]conversion specifier
</pre>
<p></p>
<h3>flags</h3>
<p><strong>#</strong> The value should be converted to an &#8221;alternate form&#8221;. For o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively. For e, E and f, it forces the written output to contain a decimal point. For g or G the result is the same as with e or E but trailing zeros are not removed.<br />
<strong>0</strong> The value should be zero padded.<br />
<strong>-</strong> The converted value is to be left justified.<br />
&#8216; &#8216; (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion.<br />
<strong>+</strong> A sign (+ or -) should always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers.<br />
<strong>&#8216;</strong> Thousands grouping.</p>
<p></p>
<h3>minimum field width</h3>
<p>A minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). If the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.</p>
<p></p>
<h3>precision/max field width</h3>
<p>An optional precision, in the form of a period &#8216;.&#8217; followed by a number which represents the maximum number of significant digits, or the maximum number of characters to be printed from a string.</p>
<p></p>
<h3>length</h3>
<p><strong>hh</strong> char<br />
<strong>h</strong> short<br />
<strong>l</strong> long<br />
<strong>ll</strong> long long<br />
<strong>L</strong> long double<br />
<strong>j</strong> intmax_t, uintmax_t<br />
<strong>z</strong> size_t, ssize_t<br />
<strong>t</strong> ptrdiff_t</p>
<p></p>
<h3>conversion specifier</h3>
<p><strong>d</strong>,<strong>i</strong> The int argument is converted to signed decimal notation.<br />
<strong>u</strong> An unsigned int.<br />
<strong>o</strong>,<strong>x</strong>,<strong>X</strong> An unsigned octal (o), or unsigned hexadecimal (x and X) notation.<br />
<strong>e</strong>,<strong>E</strong> The double [-]d.ddde±dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision. if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent.<br />
<strong>f</strong>,<strong>F</strong> The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd.<br />
<strong>c</strong> An unsigned char.<br />
<strong>s</strong> A pointer to an array of character type (pointer to a string). The array must contain a terminating null.<br />
<strong>p</strong> The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx).<br />
<strong>%</strong> A &#8216;%&#8217; is written. No argument is converted. The complete conversion specification is &#8216;%%&#8217;.</p>
<p></p>
<h3>Examples</h3>
<pre>
printf("%c \n", 'a');
<strong>a</strong>
</pre>
<pre>
printf("%d \n", 100);
<strong>100</strong>
</pre>
<pre>
printf("%ld \n", 5000000000);
<strong>5000000000</strong>
</pre>
<pre>
printf("%06d \n", 100);
<strong>000100</strong>
</pre>
<pre>
printf("%6d \n", 100);
<strong>   100</strong>
</pre>
<pre>
printf("%s \n", "14 char string");
<strong>14 char string</strong>
</pre>
<pre>
printf("%.10s \n", "14 char string");
<strong>14 char st</strong>
</pre>
<pre>
char * ptr = "string";
printf("%p \n", ptr);
<strong>0x400788</strong>
</pre>
<pre>
printf("%X \n", 100);
<strong>64</strong>
</pre>
<pre>
printf("%#X \n", 100);
<strong>0X64</strong>
</pre>
<pre>
printf("%o \n", 100);
<strong>144</strong>
</pre>
<pre>
printf("%#o \n", 100);
<strong>0144</strong>
</pre>
<pre>
printf("%#x \n", 100);
<strong>0x64</strong>
</pre>
<pre>
printf("%f \n", 3.14159);
<strong>3.141590</strong>
</pre>
<pre>
printf("%e \n", 3.14159);
<strong>3.141590e+00</strong>
</pre>
<pre>
printf("%E \n", 3.14159);
<strong>3.141590E+00</strong>
</pre>
<pre>
printf("%6d %d \n", 100, 100);
<strong>   100 100</strong>
</pre>
<pre>
printf("%-6d %d \n", 100, 100);
<strong>100    100</strong>
</pre>
<p>The entire family of printf functions including fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, and vsnprintf take advantage of these formatting rules as well.  In addition, many other languages have printf implementations which operate in much the same way.</p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c' rel='bookmark' title='Permanent Link: Display hostname and IP address in C'>Display hostname and IP address in C</a></li>
<li><a href='http://www.wiredrevolution.com/c/find-controlling-terminal-with-ttyname' rel='bookmark' title='Permanent Link: Find controlling terminal with ttyname'>Find controlling terminal with ttyname</a></li>
<li><a href='http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket' rel='bookmark' title='Permanent Link: Find IP address from remote end of a TCP socket'>Find IP address from remote end of a TCP socket</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/c/format-output-using-printf/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

