need to write a shell script for appending text into each line of a file
hello,
I have a huge source code of a software in front of me and need to find out the flow of execution. It has many C files that comprise it and its normal execution involves passing values from one code to the other. what i want is a shell script that will enter into the C prog file and append a string such as '; printf("%s %d",$file_name,$line_number); ' at each line.. this will help me understand the flow of the program. can anyone suggest a shell script to do this?? i use ubuntu.. thanks,, |
The syntax "printf("%s %d",$file_name,$line_number); " indicates that you are used to scripting languages like PHP. You won't have $file_name in C. Depending on your compiler you use macros like __FILE__ or __LINE__ for the same purpose. If you'r doing programming anyway, you need to create a program using fgets to read each line of the source code, stripping the trailing linefeed with something like s[strlen(s -1)] = 0, making sure that your buffer is large enough for very long lines, and the outputting the line plus the printf statement followed by a linefeed to a new copy of the source code. A good programming exercise, shouldn't take long to write.
Linux |
I know VERY little about C, but it doesn't seem right to me to add '; blahblahblah' to EVERY signle line of a C source file. What about a line which declares a function, eg:
Quote:
As I said I don't know much about C and am not learning it so I might be completely wrong, it just seems not right to me. Anyway, as far as SED is concerned, it would look like: sed 's/$/staff_to_put_at_the_end_of_the_line/g' name_of_the_file For example, if you wanted to append '; printf();' to the end of each line in the file: program.c, it would look as follows; Code:
sed 's/$/; printf();/g' program.c |
Quote:
anyway, i think i will look up on those macros you suggested, and will experiment with fgets as mentioned. thanks!! |
Quote:
and i didn't get your 'int main()' utility here,, thanks for the sed usage fromat!! i ll have to figure out how to loop and insert line numbers in them, probably the macro __LINE__ should help... |
Code:
int main(void) http://en.wikipedia.org/wiki/Main_fu...#C_and_C.2B.2B |
How about leaving the program as-is, compiling it with symbols not stripped (if it's not already) and using a tracing utility? Just a suggestion; not my area of knowledge. :twocents:
|
Quote:
i am aware of this. I do not know why it was suggested to me here to solve my problem.. |
Quote:
Know nothing about them.. And also what you mean 'compiling with symbols'?? |
Quote:
I was simply pointing out that appending something like ';printf.....' to EACH line of C code would mess things up. As, FOR EXAMPLE: Code:
int main () ; printf("%s %d",$file_name,$line_number); At least that's what my limited knowledge of C syntax tells me:) Am I correct? |
All times are GMT -5. The time now is 12:15 AM. |