view lib/lib.h @ 551:2548e6e590b2

Add string to mode_t parser added new function string_to_mode(char *m_string, mode_t base) which parses a given string and converts it to a mode_t. If either + or - are part of m_string the permissions are either added or removed from base. Currently support for permision copy is missing (e.g. g=u), but all other flags should work. Format for m_string: either symbolic modes or octal representation. symbolic modes: [auog][[+-=][rwxst]*] examples: string_to_mode("u=rwx,g=rw,o=r", 0); string_to_mode("a-x", 0777); string_to_mode("0744", 0);
author Daniel Walter <>
date Mon, 19 Mar 2012 19:57:56 -0500
parents c2f39708a4c4
children 44abf4d901f3
line wrap: on
line source

/* vi: set ts=4 :*/
/* lib.h - header file for lib directory
 * Copyright 2006 Rob Landley <>

// Unfortunately, sizeof() doesn't work in a preprocessor test.  TODO.

//#if sizeof(double) <= sizeof(long)
//typedef double FLOAT;
typedef float FLOAT;

// libc generally has this, but the headers are screwed up
ssize_t getline(char **lineptr, size_t *n, FILE *stream);

// llist.c

struct string_list {
	struct string_list *next;
	char str[0];

struct arg_list {
	struct arg_list *next;
	char *arg;

struct double_list {
	struct double_list *next;
	struct double_list *prev;
	char *data;

void llist_free(void *list, void (*freeit)(void *data));
void *llist_pop(void *list);  // actually void **list, but the compiler's dumb
void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
struct double_list *dlist_add(struct double_list **list, char *data);

// args.c
void get_optflags(void);

// dirtree.c
struct dirtree {
	struct dirtree *next, *child, *parent;
	struct stat st;
	int depth;
	char name[];

struct dirtree *dirtree_add_node(char *path);
struct dirtree *dirtree_read(char *path, struct dirtree *parent,
                    int (*callback)(char *path, struct dirtree *node));

// lib.c
void xstrcpy(char *dest, char *src, size_t size);
void verror_msg(char *msg, int err, va_list va);
void error_msg(char *msg, ...);
void perror_msg(char *msg, ...);
void error_exit(char *msg, ...) noreturn;
void perror_exit(char *msg, ...) noreturn;
void *xmalloc(size_t size);
void *xzalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrndup(char *s, size_t n);
char *xstrdup(char *s);
char *xmsprintf(char *format, ...);
void xprintf(char *format, ...);
void xputs(char *s);
void xputc(char c);
void xflush(void);
void xexec(char **argv);
void xaccess(char *path, int flags);
void xunlink(char *path);
int xcreate(char *path, int flags, int mode);
int xopen(char *path, int flags);
void xclose(int fd);
FILE *xfopen(char *path, char *mode);
ssize_t readall(int fd, void *buf, size_t len);
ssize_t writeall(int fd, void *buf, size_t len);
size_t xread(int fd, void *buf, size_t len);
void xreadall(int fd, void *buf, size_t len);
void xwrite(int fd, void *buf, size_t len);
off_t xlseek(int fd, off_t offset, int whence);
char *readfile(char *name);
char *xreadfile(char *name);
char *xgetcwd(void);
void xstat(char *path, struct stat *st);
char *xabspath(char *path);
void xchdir(char *path);
void xmkpath(char *path, int mode);
void xsetuid(uid_t uid);
struct string_list *find_in_path(char *path, char *filename);
void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
void itoa_to_buf(int n, char *buf, unsigned buflen);
char *utoa(unsigned n);
char *itoa(int n);
long atolx(char *c);
off_t fdlength(int fd);
char *xreadlink(char *name);
void loopfiles_rw(char **argv, int flags, int permissions, int failok,
	void (*function)(int fd, char *name));
void loopfiles(char **argv, void (*function)(int fd, char *name));
char *get_rawline(int fd, long *plen, char end);
char *get_line(int fd);
void xsendfile(int in, int out);
int copy_tempfile(int fdin, char *name, char **tempname);
void delete_tempfile(int fdin, int fdout, char **tempname);
void replace_tempfile(int fdin, int fdout, char **tempname);
void crc_init(unsigned int *crc_table, int little_endian);
void terminal_size(unsigned *x, unsigned *y);
int yesno(char *prompt, int def);
void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid));

// getmountlist.c
struct mtab_list {
	struct mtab_list *next;
	struct stat stat;
	struct statvfs statvfs;
	char *dir;
	char *device;
	char type[0];

struct mtab_list *getmountlist(int die);

void bunzipStream(int src_fd, int dst_fd);

// signal

int sig_to_num(char *pidstr);
char *num_to_sig(int sig);

mode_t string_to_mode(char *mode_str, mode_t base);