![]() |
| | #16 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| You'll need additional open flags if you expect the file to be created for you. http://www.opengroup.org/onlinepubs/...ions/open.html >> but it doesnt write anything to stdout.txt either You haven't even saved the file handle returned by open(). Why would you expect anything to be written to it? >> I'm guessing close(1) closes stdout? Yes, 1 corresponds to standard out. http://www.opengroup.org/onlinepubs/...ons/stdin.html >> Why _exit(1) vs exit(1)? http://www.opengroup.org/onlinepubs/...ons/_Exit.html It seems to me that you've jumped into the deep end without first learning to swim... http://www.amazon.com/Pointers-C-Ken.../dp/0673999866 gg |
| Codeplug is online now | |
| | #17 |
| Registered User Join Date: Feb 2009
Posts: 9
| Code: #include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char small[32], big[128];
int i = 16;
if(strstr(argv[1], "t8")) { i = 8; }
if(strstr(argv[1], "t16")) { i = 16; }
if(strstr(argv[1], "t32")) { i = 32; }
if(strstr(argv[1], "t64")) { i = 64; }
if(strstr(argv[1], "t128")) { i = 128; }
memset(big, 'Z', sizeof(big));
snprintf(small, i, "%s", big);
printf("%s\n", small);
return 0;
}
Code: bash$ ./bvss t8 ZZZZZZZ bash$ ./bvss t16 ZZZZZZZZZZZZZZZ bash$ ./bvss t32 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ bash$ ./bvss t64 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ Segmentation fault bash$ ./bvss t128 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ Segmentation fault bash$ bash$ ./test ZZZZZZZ ZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ bash$ Code: void run(char *bin, char *data)
{
if(fork() == 0)
{
if(execl(bin, bin, data, 0) == -1)
{
perror("execl");
_exit(1);
}
}
else
{
int pid, status;
pid = wait(&status);
}
}
.....
for(i = 0; i < 5; i++)
{
run(PATH, data);
}
|
| tadams is offline | |
| | #18 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > snprintf(small, i, "%s", big); Well yeah, when you lie about the size of the buffer, then it's gonna hurt you at some point. Anyway, it will be the shell which prints segmentation fault, not the program which dies. Having done a wait(&status), you need to read this http://www.manpagez.com/man/2/waitpid/ and do things like Code: if ( WIFSIGNALED(status) ) {
int sig = WTERMSIG(status);
printf( "Program died due to signal %d\n", sig );
}
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #19 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| You're lucky that bvss doesn't segfault every time you run it since small is never null-terminated. >> Segmentation fault That's coming from bash, not bvss. So you can either run bvss via bash or detect the segfault and printf("Segmentation fault") yourself. gg |
| Codeplug is online now | |
| | #20 |
| Registered User Join Date: Feb 2009
Posts: 9
| Thanks both of you, I finally got it to work using the signal catching solution. Couple short questions: 1) snprintf() null terminates for us, so I don't need to do a snprintf(blah, sizeof(blah)-1 just sizeof(blah), correct? 2) With memset(blah, 'Z', sizeof(blah)), could I and would I need to null terminate blah like blah[SIZE] = '\0' or blah[SIZE-1] = '\0' or ? Thanks again for you help |
| tadams is offline | |
![]() |
| Tags |
| execl, fault, fork, linux, output |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| code output... | roaan | C Programming | 6 | 07-03-2009 02:22 AM |
| Help for my output array | qwertysingh | C Programming | 1 | 02-17-2009 03:08 PM |
| More digits in output, particular digits output, output in *.txt | Ene Dene | C++ Programming | 4 | 11-30-2005 04:44 PM |
| Formatting output into even columns? | Uncle Rico | C Programming | 2 | 08-16-2005 05:10 PM |
| Output problems with structures | Gkitty | C Programming | 1 | 12-16-2002 05:27 AM |