Question:
How can "foo.c" be passed into filename? char* filename = "foo.c"; system("find -name filename");
bigrastay
2006-11-22 14:53:52 UTC
How can I get "foo.c" passed in for filename in the system call?

char* filename = "foo.c";
system("find -name filename");

This is in a C file in linux...
Three answers:
Steve A
2006-11-22 21:54:51 UTC
Mn is close, but there are 2 errors:



1) There is no len() function. Use strlen() instead of len().



2) He didn't allocate enough memory for the sprintf to fit the entire result string in the buffer. The line needs room to store the \n (or in windows, 2 chars - cr and lf ) in addition to the null terminator. So, his code needs to add 2 instead of 1 to the length calculation.



Taking this a little further, the number of chars should be multiplied by the sizeof(t_char), which MIGHT NOT be 1 byte if the code is destined to use unicode.



Chances are, this is ascii, so the multiplication is unnecessary, but it is better to be safe than sorry. Besides, if this was production code, you'd want to be able to use the same code in both places without any need to change it. Nothing worse than having to go back and recalculate buffer manipulation code during a port.



The above subtleties demonstrate precisely how and why it is so very easy to make a mistake in c/c++, leading to a buffer underallocation or overwrite, and hence a security hole.
Mn
2006-11-22 15:05:34 UTC
char* filename = "foo.c";

char * command = "find -name ";

char * fullcommand = malloc(len(filename) + len(command) + 1)

sprintf(fullcommand, "%s%s\n", command, filename);



system(fullcommand);



Not certain about how to concatenate but you should get the idea.
?
2016-11-26 06:57:53 UTC
I had 2 miscarriages in 2007 my husband and that i on no account advised everyone i substitute into pregnant so while it did happen we only had to handle it ourselves. regrettably i substitute into admitted to scientific institution with the 2d miscarriage as they thought it may be ectopic and my husband had to tell his ***** mom as we mandatory somebody to look after our son. She substitute into crying and saying nicely she could have accepted i substitute into pregnant (i substitute into only 5 weeks and only found out myself) my own mom didnt additionally be attentive to. some days later we went round her abode and he or she introduced that my husbands cousin substitute into pregnant and he or she had advised her mom in the present day away, couldnt care much less approximately my thoughts and the reality identity only lost a toddler. Then a pair of month later we went out for my FIL birthday and because shed had a drink she further all of it up approximately how she could have accepted and how it made her experience no longer as quickly as did she ask how i substitute into or how I felt. My husband and that i attempted to tell her that none of it substitute into approximately her even if it doesnt remember what it extremely is she continually turns it around so its approximately her she should be centre of interest. She is a chain smoker and smokes at the same time as my son is eating or at the same time as hes sat on her knee. She supplies him crisps and goodies while she is usual with hes going to have his tea at abode. She is in certainty a terrible individual who I ought to submit with for my husband.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...