readwriteconfigReadwriteconfig - Click here to download readwriteconfig is a little utility to manipulate flat files that contain text in the VARIABLE=value form. Its written in plain C and is completely free. If you write embedded systems, or just want to configure parts of an operating system with a very simple tool, then this is for you. It doesnt do anything that sed/grep cant do, but it does provide a consistent interface for embedded code, CGI scripts, BASH etc. License: "Free code, you may use it in whole or part. This code is supplied "As is" without warranty." The code is tested only on gcc on GNU/Linix, its not tested on any other platform but should compile on systems that impliment the truncate system call.
Example: readwriteconfig run with no arguments # readwriteconfig readwriteconfig: read or write a text file of type VARIABLE=value, variable must be uppercase value may be mixed case. Read: readwriteconfig -r filename variable Read silent: readwriteconfig -rs filename variable Write: readwriteconfig -w filename variable=value Examples: Update value readwriteconfig -w /etc/sysconfig/networking/profiles/default/ifcfg-eth0 IPADDR=192.168.1.113 Read value readwriteconfig -r /etc/sysconfig/networking/profiles/default/ifcfg-eth0 IPADDR Read value,fail silently readwriteconfig -rs /etc/sysconfig/networking/profiles/default/ifcfg-eth0 IPADDR Writing will return a single character A=appended C=Changed N=File to large. Reading will return the variables value only or nothing if the string is not found Example a Fedora/Redhat configuration file for networking may look something like this : [root]# cat /etc/sysconfig/networking/devices/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none HWADDR=00:E0:EF:85:A0:22 ONBOOT=yes NETMASK=255.255.255.0 IPADDR=192.168.1.100 USERCTL=no IPV6INIT=no PEERDNS=yes GATEWAY=192.168.1.1 TYPE=Ethernet Readwriteconfig can be used to read a single value from the file, much like grep. -r is read : [root]# readwriteconfig -r /etc/sysconfig/networking/devices/ifcfg-eth0 IPADDR 192.168.1.100 Or you can change the value with the -w option, readwriteconfig replied with 'C' for changed : [root]# readwriteconfig -w /etc/sysconfig/networking/devices/ifcfg-eth0 IPADDR=192.168.1.101 C Now add an extra field to the file, readwriteconfig replied with 'A' for Added : [root]# readwriteconfig -w /etc/sysconfig/networking/devices/ifcfg-eth0 FORWARD=1 A The useful part of readwriteconfig is within scripts or CGI code, lets write a script to reconfigure the IP details for a network interface and restart it Redhat/Fedora : #!/bin/bash # setaddress.sh # Example for setting an IP Address and Subnet mask and restarting the network interface # This just saves some typing :-) FILENAME="/etc/sysconfig/networking/devices/ifcfg-eth0" # Write new setting for eth0 readwriteconfig -w $FILENAME IPADDR=192.168.1.101 readwriteconfig -w $FILENAME NETMASK=255.255.255.0 readwriteconfig -w $FILENAME GATEWAY=192.168.1.1 readwriteconfig -w $FILENAME ONBOOT=yes # Restart the interface with new values ifdown eth0 ifup eth0 Why the replies ? Whats the 'C' and 'A' for ...... here is a bash script fragment to illustrate RESULT=`readwriteconfig -w $FILENAME IPADDR=192.168.1.100` if [ "$RESULT" == "A" ]; then echo "Warning: That interface did not have an IP Address assigned, i've added one" else echo "IP Address has been changed" fi I'm writing a shell script, but I want readwriteconfig to shut up and stop telling me its 'A' and 'C's. readwriteconfig -w $FILENAME IPADDR=192.168.1.101 &>/dev/null You are writing a web front end for an embedded product, it needs to be lite on code. You can write CGI forms in bash, Here is a simple example: For a web server try mini_httpd or apache. ![]() (Question) I need to write C code, I just want the functions - can I lift them?
//These two files are self contained suitable for inclusion in other source
#include "readconfigfile_inc.c"
#include "writeconfigfile_inc.c"
main ()
{
// Example write
char c;
c=write_configfile("/path/to/filename.txt","THISVARIABLE","equals_this_value");
printf("write_configfile returned %c\n"); // 'C' 'A' or 'E'
// Example read with errors reported on stdout
char st[1024];
read_configfile("/path/to/filename.txt","THISVARIABLE",st,1);
printf ("THISVARIABLE=%s\n",st);
// Example read silent, no errors reported
char st[1024];
st[0]='\0';
read_configfile("/path/to/filename.txt","THISVARIABLE",st,0);
if (st[0]=='\0')
printf ("THISVARIABLE was not found in this file\n");
else printf ("THISVARIABLE=%s\n",st);
}
Linux, gcc, GNU, Free, Free Software, Embedded, Code, Flatfile C, Flat file database, CGI
All content on this site is (c) 2010 Jonathan Andrews, This article is marked as free and open. Feel free to copy and or republish it |