The C Primer

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

The C Primer

Postby elija » Mon Aug 29, 2011 7:50 am

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.

However, I was getting warnings:
warning: assignment makes pointer from integer without a cast
and when I tried to cast I got
warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

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.

To cut a long story short, here is the code with all the warnings fixed.

Code: Select all
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

//  Missing headers
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int parse(char **args)
{

   static char line[100];
   printf("> ");
   if (gets(line) == NULL)
      return -1;

   // cast strtok return to char *
   *args++ = (char *)strtok(line, " \t");

   // cast strtok return to char *
   // put paranthesis around assignment
   while ((*args++ = (char *)strtok(NULL, " \t")));

   return 1;
}

//  main should return int
int main()
{
   char *args[20];
   while(parse(args) > 0) {
      if (fork() == 0) {
         execvp(args[0], args);
         printf("%s not found\n", args[0]);
         exit(1);
      } else {
         wait(0);
      }
   }

   //  main should return int
   return 0;
}


I'm looking forward to the next instalment.
"Sometimes free software isn't free and sometimes free software isn't free. Sometimes it's both free and free and sometimes it's neither free nor free! Why is this so hard to explain" - Elija
elija
 
Posts: 5
Joined: Mon Aug 29, 2011 7:39 am
Location: Kent

Postby elija » Mon Aug 29, 2011 8:24 am

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.

According to the man page, the lsb deprecates the function anyway.

Here is parse using fgets instead. fgets includes the carriage return in the string whereas gets doesn't.

Code: Select all
int parse(char **args)
{

   static char line[100];
   int l = 0;
   
   printf("> ");
   if (fgets(line, 100, stdin) == NULL)
      return -1;

   //  There is probably a simpler way of removing the carriage return
   l = strlen(line);

   if (10 == *(line + (l-1))) {
      *(line + (--l)) = '\0';
   }

   // cast strtok return to char *
   *args++ = (char *)strtok(line, " \t");

   // cast strtok return to char *
   // put paranthesis around assignment
   while ((*args++ = (char *)strtok(NULL, " \t")));

   return 1;
}


Interestingly, gcc 4.4.4 shows a warning when you use gets but gcc 4.6.1 doesn't.
"Sometimes free software isn't free and sometimes free software isn't free. Sometimes it's both free and free and sometimes it's neither free nor free! Why is this so hard to explain" - Elija
elija
 
Posts: 5
Joined: Mon Aug 29, 2011 7:39 am
Location: Kent


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests