ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am writing a program that is going to be involved (among other things) in binding and unbinding IP addresses from the server's interface.
To bind the IPs, it adds IP:netmask, e.g. "220.127.116.11:255.255.255.0", to the end of a plain text file, and then calls a script which makes the necessary additions elsewhere (this is a server running cPanel, if anyone is familiar with that, not that it makes any difference). So the text file might look like this:
To unbind the IP address, it needs to do the opposite: Remove that IP's entry from the text file, and then call the same script. My question is, what approach would you take? All I can think to do is to open the first file for read, read one line at a time, and if the line does not match the IP:netmask to be unbound, it is copied to a temp file. If the line does match, it is not copied. Then the file is closed and deleted, the temp file renamed to the first file's name, and the cPanel script called.
Can anyone think of a better approach? This sounds kind of wasteful, although I am sure that the resources consumed would not be a problem.
Hi, thanks for the reply! That seems like it would be more appropriate in a shell script than in a C program. I'm not arguing with you; would it be more efficient for the program to fork a child and then call exec(sed) than my scheme?
When I first read the original post, I thought this:
All I can think to do is to open the first file for read, read one line at a time, and if the line does not match the IP:netmask to be unbound, it is copied to a temp file. If the line does match, it is not copied. Then the file is closed and deleted
meant you were doing the editing by hand.
A misunderstanding on my part, and yes, leonscape is right, using the method I described would add a bit of overhead. That type of solution is better placed in "quick and dirty" programs where you just want something to work and get it to work quickly. At least, that's the philosophy where I work. Sorry if I led you down an inappropriate path.