---
name: "cfg"
section: "3pub"
platform: "linux"
package: "publib-dev"
version: "0.32-1"
release: "Debian 3.0 (Woody)"
platforms: ["linux"]
url: "https://new.carta.tech/cfg"
---

# cfg(3pub)

routines for reading and writing configuration files

## NAME

cfg\_read\_file, cfg\_write\_file, cfg\_error - routines for reading and writing configuration files

## SYNOPSIS

```
#include <publib.h>
const char *cfg_error(void);
int cfg_read_file(struct cfg_variable *list, const char *filename);
int cfg_write_file(struct cfg_variable *list, const char *filename);
```

## DESCRIPTION

These routines read and write configuration files. *cfg\_read\_file* reads a configuration file into memory, *cfg\_write\_file* writes the in-memory representation to a file. *cfg\_error* returns an error string that describes an error.

The in-memory representation of a configuration file is stored in an array of configuration file descriptors, stored in the following structure type:

```
struct cfg_variable {
	const char *cfg_name;
	enum cfg_type cfg_type;
	void *cfg_value;
};
```

The last element in the array should have *cfg\_name* be NULL.

*cfg\_name* gives the name of a configuration variable, as it is in the configuration file. *cfg\_type* gives its type; it must one of the constants CFG\_LONG, CFG\_ULONG, CFG\_DOUBLE, CFG\_LONG\_EXPR, CFG\_ULONG\_EXPR, CFG\_DOUBLE\_EXPR, CFG\_BOOLEAN, and CFG\_STRING. *cfg\_value* is a pointer to the value; for CFG\_LONG, CFG\_ULONG, CFG\_DOUBLE, and their \_EXPR versions, it should be a pointer to a variable of type long, unsigned long, or double, respectively. That variable is set to the value read from the configuration file. Similarly, for CFG\_BOOLEAN *cfg\_value* should point at a variable of type int. For CFG\_STRING it should be a pointer to a \`char \*', which is set to the string in the configuration file. Space for the new string is allocated automatically; the initial value must be NULL.

## RETURN VALUE

*cfg\_read\_file* and *cfg\_write\_file* return -1 for error, or 0 for OK. If they return an error, *cfg\_error* returns a pointer to the error message string. The string may change the next time *cfg\_read\_file* or *cfg\_write\_file* is called.

## SEE ALSO

publib(3)

## AUTHOR

Lars Wirzenius (lars.wirzenius@helsinki.fi)
