Source: rhutil.h


Annotated List
Files
Globals
Hierarchy
Index
/* Copyright 1999-2000 Red Hat, Inc.
 *
 * Bernhard Rosenkraenzer <bero@redhat.com>
 * Harald Hoyer <harald@redhat.com>
 * Florian La Roche <Florian.LaRoche@redhat.com>
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef _RHUTIL_H
#define _RHUTIL_H

#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <grp.h>

#ifdef RHC_INTERNAL
#include <libintl.h>
#define _(s) dgettext("rhclib",(s))
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if 0
/* FOOL kdoc */
class rhclib {
public:
#endif

void nomem (size_t size)
#ifdef __GNUC__
		__attribute__ ((noreturn))
#endif
	;
void *rh_xcalloc (size_t s);
void *rh_xmalloc (size_t s);
void *rh_xrealloc (void *p, size_t s);
void *rh_xmemcpy (const void *p, size_t s);
char *rh_xstrdup (const char *s);
char *rh_xstrndup (const char *s, size_t n);
#define RH_FREE(x) if (x) free(x)


/* double linked list */
typedef struct rh_list {
	char *data;
	size_t n;
	struct rh_list *next;
	struct rh_list *prev;
} rh_list;
/* add a new node with "data" previous to "pos" or at the end if "pos"
 * is NULL.
 */
rh_list *rh_list_add (rh_list *first, rh_list *pos, char *data, size_t n);
/* delete a complete list */
void rh_list_delete (rh_list *l, int free_data);
/* delete only one list element */
rh_list *rh_list_delete_node (rh_list *first, rh_list *pos);
/* go through the list and call a function for each element */
rh_list *rh_list_traverse (int (*func) (rh_list *), rh_list *first,
		rh_list *pos);
rh_list *rh_list_dup (rh_list *first);


/* s does not have to be '\0'-terminated */
char *string_trim (const char *s, size_t n, size_t *m);
rh_list *string_tok (char *s, size_t *n, const char *delim);


/* return a good title string for dialog boxes etc. */
char *rhcl_title (void);

/** 
 * something similar to "mkdir -p": make a complete directory path with
 * permissions 'mode_t'.
 * it might be good to make a similar function that does not allocate
 * memory, but can change the string to make the subdirectories.
 * this function will not return an error if the complete path is a normal
 * file instead of a directory. to check this, you have to call (l)stat()
 * and check for a directory after calling this function. 
 * @return 0 on success, -2 if we don't get enough 
 * memory and -1 if we cannot create the directories.
 * @param path directory path to make
 * @param n length of the path
 * @param mode permissions of the directory
 */
int mkdir_p (const char *path, size_t n, mode_t mode);

/** Copy one file to another.
 * @param src path to the src file
 * @param dest path to the dest file
 * @return 0 on success, errno otherwise
 */
int copy(const char *src, const char *dest);

int WaitFd (int, int, unsigned long);
int SetBlockFlag (int fd, int block);

/** Get the group id by name.
 * @param name the group name
 * @return the group id, 0 if the group name does not exist
 */
gid_t Gid (const char *name);

/**
 * read 'len' bytes into 'buf' from filedescriptor 'fd'.
 * restart read() in case of partial/interrupted reads.
 * @return the number of read bytes or -1 in case of error.
 */
ssize_t Read (int fd, /*@out@*/ void *buf, size_t len);

/**
 * write 'len' bytes starting at 'buf' to the filedescriptor 'fd'
 * restart the write() command in case of partial/interrupted writes
 * @return 0 on success or -1 on error
 */
int Write (int, const void *, size_t);

typedef struct rhconfig_t {
	char *name;
	struct stat st;

	char *buf;
	size_t n;
} rhconfig_t;
  
/** read a complete file into a new buffer. 
 * *size contains the complete size.
 * @param return NULL on error or a newly alloced buffer. 
 */
rhconfig_t *ReadFile (const char *name, uid_t uid, gid_t gid, mode_t mode);
void FreeFile (rhconfig_t *c);

int BackupFile (const char *name);

/** 
 * WriteFile writes a file
 */
int NewContent (rhconfig_t *c, char *buf, size_t n);
int NewContent2 (rhconfig_t *c, rh_list *list);
int WriteFile (rhconfig_t *c);
int WriteFile2 (rhconfig_t *c);

/** parse a file that is supposed to be in shell-syntax for
 * "key=value" entries. we ignore a lot of things that are possible
 * within shell-scripts, but this routine should cope with all
 * real-world config files. please try to keep this routine
 * reasonable simple.
 * @param s the string buffer
 * @param n the length of the string buffer
 * @param key the search key
 * @return ptr to the line containing key, NULL otherwise
 */
char *GetKeyShell (rh_list *l, rh_list **pos, const char *key);
char *GetKey (rh_list *l, rh_list **pos, const char *key);

/** set a new value; in place editing 
 * @param s the string buffer
 * @param n the length of the string buffer
 * @param key the search key
 * @param data the string to set the key to
 * @return ptr to the line containing key, NULL otherwise
 */
rh_list *SetKeyShell (rh_list *first, rh_list **pos,
		const char *key, const char *data);
void SetKey (rh_list **first, rh_list **pos, const char *key,
		const char *value);
void SetKeyPos (rh_list **first, rh_list *pos, const char *key,
		const char *value);
rh_list *DeleteAllKey (rh_list **first2, const char *key2);


/** change buffer into newly malloced lines
 */
rh_list *Buf2List (const char *buffer);
char *List2Buf (rh_list *l, size_t *);
void remove_line_continuations (rh_list *l);


/** Checks if the user is root (euid 0)
 * This exits otherwise, with a dialog informing the user.
 */
void check_root (void);

/** Checks if the user has write access to a file or path.
 * This exits otherwise, with a dialog informing the user.
 * @param path the path to the file or directory
 * @return 0 for success
 */
int check_for_write_access (const char *);

void vLog (const char *fmt, va_list ap);
void Log (int i, const char *fmt, ...) __attribute__((format(printf, 2, 3)));

void vError (const char *fmt, va_list ap);
void Error (const char *fmt, ...) __attribute__((format(printf, 1, 2)));
int SetError (const char *fmt, ...) __attribute__((format(printf, 1, 2)));

/** Display a dialog before aborting the program
 * @param fmt a printf style format string
 * @return never
 */
void ErrorAbort (const char *fmt, ...)
#ifdef __GNUC__
		__attribute__ ((noreturn,format(printf, 1, 2)));
#endif
;

/** Initialize a display
 * @param argc parameter count
 * @param argv parameters (on some frontends, parameters are used to
 *             handle crap like -geometry)
 * @return 0 on success
 */
int InitDisplay (int *argc, char ***argv, const char *package,
		const char *version);

/** Close a display
 */
void CloseDisplay (void);

int ParseYesNo (const char *);

/** Replace all occurences of
 * @param what
 * with
 * @param with
 * in
 * @param str the string
 *
 * @return String with all occurrences replaced.
 */
char *rhcl_replace (const char *str, const char *what, const char *with);

/** Case-insensitive version of strstr
 * Find the first occurrence of @param haystack in @param needle
 * case insensitive.
 *
 * @return first occurrence
 */
char *rhcl_strcasestr (char *haystack, const char *needle);

/** Remove blanks from beginning and end of line
 * @param line Line you want trimmed
 * @return @param line without blanks at beginning and end
 */
char *rhcl_trim (const char *line);

#ifdef _RH_NETWORK
/* an ugly hack to not include this for all files */
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

/* connect to a single address */
int DoTcpConnect (struct addrinfo *addr, int blocking, unsigned long timeout);
char *GetNetError (struct addrinfo *addr);
/* connect inkl. looking up a hostname */
int TcpConnect (const char *hostname, const char *service, int *fd,
	int blocking, unsigned long timeout);
#endif

#if 0
};
#endif

#ifdef __cplusplus
}
#endif

#endif

Generated by: laroche@dudweiler.redhat.de on Mon Jun 26 19:25:45 2000, using kdoc 2.0a35.