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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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. "18.104.22.168: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.