Results 1 to 6 of 6
Linux format have started a new programming academy which is a c primer. Very cool and a good way to get another perspective.
The first code is a basic shell!
...
- 08-27-2011 #1
How should I cast...
Linux format have started a new programming academy which is a c primer. Very cool and a good way to get another perspective.
The first code is a basic shell!
I mostly understand this but... the lines in red generate warnings:Code:#include <stdio.h> #include <unistd.h> #include <stdlib.h> int parse(char **args) { static char line[100]; printf("> "); if (gets(line) == NULL) return -1; *args++ = strtok(line, " \t"); while (*args++ = strtok(NULL, " \t")); return 1; } void 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); } } }
warning: assignment makes pointer from integer without a cast
So reading the strtok documentation I see that it returns a char * so I cast to char *
Code:*args++ = (char *)strtok(line, " \t"); while (*args++ = (char *)strtok(NULL, " \t"));
They generate a different warning:
warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
What should I cast to so that I get rid of the warnings?If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 08-29-2011 #2
With my gcc version It gave a bonus warning message

$ gcc elija.c
elija.c: In function ‘parse’:
elija.c:14: warning: assignment makes pointer from integer without a cast
elija.c:15: warning: assignment makes pointer from integer without a cast
/tmp/cchURIpq.o: In function `parse':
elija.c
.text+0x24): warning: the `gets' function is dangerous and should not be used.
Googling around a bit came across suggestion - Turn on more warning with "-Wall option"$ gcc --version
gcc (GCC) 4.4.4 20100630 (Red Hat 4.4.4-10)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
gave thisI did man strtok seems like it needs string.h which is missing with your code.[laks@space ~]$ gcc elija.c -Wall
elija.c: In function ‘parse’:
elija.c:14: warning: implicit declaration of function ‘strtok’
elija.c:14: warning: assignment makes pointer from integer without a cast
elija.c:15: warning: assignment makes pointer from integer without a cast
elija.c:15: warning: suggest parentheses around assignment used as truth value
elija.c: At top level:
elija.c:19: warning: return type of ‘main’ is not ‘int’
elija.c: In function ‘main’:
elija.c:28: warning: implicit declaration of function ‘wait’
/tmp/ccIHuq0E.o: In function `parse':
elija.c
.text+0x24): warning: the `gets' function is dangerous and should not be used.
Added #include <string.h> then ran without -Wall now the warning disappeared
Thanks to your code - I learned about new method to get rid of warnings , previously I used to ignore them.- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-29-2011 #3
Thanks Laks, I didn't even think about needing another header as the code works as printed. Loving the -Wall option as I was taught in college that warnings are basically errors that need to man up. So I'm having a go at fixing them up now.
Oh and
Code:richard@andromeda shell $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.1-4' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-multiarch --with-multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/x86_64-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib/x86_64-linux-gnu --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.1 (Debian 4.6.1-4)
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 08-29-2011 #4
I find it strange that gcc version 4.6.1 didn't show the "gets dangerous" warning message but gcc 4.4.4 shows it ?
Elija,you may be interested in reading more about it here - c - warning:gets function is dangerous - Stack Overflow- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-29-2011 #5
That's an interesting read. Thanks.
I also read the fgets manpage and it says that gets is deprecated, so I wonder if gcc no longer issues a warning? I can't find anything on Google about it though.
Final code
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]; 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; } // 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; }Last edited by elija; 08-29-2011 at 08:41 AM.
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 08-29-2011 #6
Okay. I checked above code ,Its fine with my gcc version too.
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------


Reply With Quote