 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
elija
Joined: Mon Aug 29, 2011 8:39 am Posts: 5 Location: Kent
|
Posted: Mon Aug 29, 2011 8:50 am Post subject: The C Primer |
|
|
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: |
#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 |
|
| Back to top |
|
 |
elija
Joined: Mon Aug 29, 2011 8:39 am Posts: 5 Location: Kent
|
Posted: Mon Aug 29, 2011 9:24 am Post subject: |
|
|
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: |
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 |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|