<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://www.linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Tue May 21, 2013 4:21 am by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Tue May 21, 2013 4:21 am</pubDate>
  <lastBuildDate>Tue May 21, 2013 4:21 am</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://www.linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: The C Primer</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=101365#101365</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=67678'&gt;elija&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Aug 29, 2011 9:24 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Having an interesting discussion on another forum about the danger of gets. It can't check the size of the buffer passed in so is susceptible to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
According to the man page, the lsb deprecates the function anyway.&lt;br /&gt;
&lt;br /&gt;
Here is parse using fgets instead. fgets includes the carriage return in the string whereas gets doesn't.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
int parse&amp;#40;char **args&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;static char line&amp;#91;100&amp;#93;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;int l = 0;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;printf&amp;#40;&amp;quot;&amp;gt; &amp;quot;&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;if &amp;#40;fgets&amp;#40;line, 100, stdin&amp;#41; == NULL&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;return -1;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;//&amp;nbsp; There is probably a simpler way of removing the carriage return&lt;br /&gt;
&amp;nbsp; &amp;nbsp;l = strlen&amp;#40;line&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;if &amp;#40;10 == *&amp;#40;line + &amp;#40;l-1&amp;#41;&amp;#41;&amp;#41; &amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;*&amp;#40;line + &amp;#40;--l&amp;#41;&amp;#41; = '\0';&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;// cast strtok return to char *&lt;br /&gt;
&amp;nbsp; &amp;nbsp;*args++ = &amp;#40;char *&amp;#41;strtok&amp;#40;line, &amp;quot; \t&amp;quot;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;// cast strtok return to char *&lt;br /&gt;
&amp;nbsp; &amp;nbsp;// put paranthesis around assignment&lt;br /&gt;
&amp;nbsp; &amp;nbsp;while &amp;#40;&amp;#40;*args++ = &amp;#40;char *&amp;#41;strtok&amp;#40;NULL, &amp;quot; \t&amp;quot;&amp;#41;&amp;#41;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;return 1;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
Interestingly, gcc 4.4.4 shows a warning when you use gets but gcc 4.6.1 doesn't.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=101365#101365</comments>
                                        <author>elija</author>
                                        <pubDate>Mon Aug 29, 2011 9:24 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=101365#101365</guid>
                                      </item>
                                      <item>
                                        <title>The C Primer</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=101364#101364</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=67678'&gt;elija&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Aug 29, 2011 8:50 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      By co-incidence I started to teach myself c a couple of months ago and was pleasantly surprised at how easy the the code was to follow and understand.&lt;br /&gt;
&lt;br /&gt;
However, I was getting warnings:&lt;br /&gt;
warning: assignment makes pointer from integer without a cast&lt;br /&gt;
and when I tried to cast I got&lt;br /&gt;
warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]&lt;br /&gt;
&lt;br /&gt;
I did a bit of googling and asking around and learned that code will run without the appropriate header files being included and about the -Wall switch to gcc to show all warnings.&lt;br /&gt;
&lt;br /&gt;
To cut a long story short, here is the code with all the warnings fixed.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
//&amp;nbsp; Missing headers&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int parse&amp;#40;char **args&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;static char line&amp;#91;100&amp;#93;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;printf&amp;#40;&amp;quot;&amp;gt; &amp;quot;&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;if &amp;#40;gets&amp;#40;line&amp;#41; == NULL&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;return -1;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;// cast strtok return to char *&lt;br /&gt;
&amp;nbsp; &amp;nbsp;*args++ = &amp;#40;char *&amp;#41;strtok&amp;#40;line, &amp;quot; \t&amp;quot;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;// cast strtok return to char *&lt;br /&gt;
&amp;nbsp; &amp;nbsp;// put paranthesis around assignment&lt;br /&gt;
&amp;nbsp; &amp;nbsp;while &amp;#40;&amp;#40;*args++ = &amp;#40;char *&amp;#41;strtok&amp;#40;NULL, &amp;quot; \t&amp;quot;&amp;#41;&amp;#41;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;return 1;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;br /&gt;
//&amp;nbsp; main should return int&lt;br /&gt;
int main&amp;#40;&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;char *args&amp;#91;20&amp;#93;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;while&amp;#40;parse&amp;#40;args&amp;#41; &amp;gt; 0&amp;#41; &amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if &amp;#40;fork&amp;#40;&amp;#41; == 0&amp;#41; &amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;execvp&amp;#40;args&amp;#91;0&amp;#93;, args&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;printf&amp;#40;&amp;quot;%s not found\n&amp;quot;, args&amp;#91;0&amp;#93;&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;exit&amp;#40;1&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;#125; else &amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;wait&amp;#40;0&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp;//&amp;nbsp; main should return int&lt;br /&gt;
&amp;nbsp; &amp;nbsp;return 0;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
I'm looking forward to the next instalment.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=101364#101364</comments>
                                        <author>elija</author>
                                        <pubDate>Mon Aug 29, 2011 8:50 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=101364#101364</guid>
                                      </item></channel></rss>