1 /* lib.h - header file for lib directory
3 * Copyright 2006 Rob Landley <rob@landley.net>
6 // Unfortunately, sizeof() doesn't work in a preprocessor test. TODO.
8 //#if sizeof(double) <= sizeof(long)
9 //typedef double FLOAT;
14 // libc generally has this, but the headers are screwed up
15 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
19 // All these list types can be handled by the same code because first element
20 // is always next pointer, so next = (mytype *)&struct.
23 struct string_list *next;
28 struct arg_list *next;
33 struct double_list *next, *prev;
37 void llist_traverse(void *list, void (*using)(void *data));
38 void *llist_pop(void *list); // actually void **list, but the compiler's dumb
39 void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
40 struct double_list *dlist_add(struct double_list **list, char *data);
43 void get_optflags(void);
47 // Values returnable from callback function (bitfield, or them together)
48 // Default with no callback is 0
50 // Add this node to the tree
51 #define DIRTREE_SAVE 1
52 // Recurse into children
53 #define DIRTREE_RECURSE 2
54 // Call again after handling all children of this directory
55 // (Ignored for non-directories, sets linklen = -1 before second call.)
56 #define DIRTREE_COMEAGAIN 4
57 // Follow symlinks to directories
58 #define DIRTREE_SYMFOLLOW 8
59 // Don't look at any more files in this directory.
60 #define DIRTREE_ABORT 256
62 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
65 struct dirtree *next, *parent, *child;
66 long extra; // place for user to store their stuff (can be pointer)
69 int data; // dirfd for directory, linklen for symlink, -1 = comeagain
73 struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow);
74 char *dirtree_path(struct dirtree *node, int *plen);
75 int dirtree_notdotdot(struct dirtree *catch);
76 int dirtree_parentfd(struct dirtree *node);
77 struct dirtree *handle_callback(struct dirtree *new,
78 int (*callback)(struct dirtree *node));
79 void dirtree_recurse(struct dirtree *node,
80 int (*callback)(struct dirtree *node), int symfollow);
81 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
84 void xstrcpy(char *dest, char *src, size_t size);
85 void verror_msg(char *msg, int err, va_list va);
86 void error_msg(char *msg, ...);
87 void perror_msg(char *msg, ...);
88 void error_exit(char *msg, ...) noreturn;
89 void perror_exit(char *msg, ...) noreturn;
90 void *xmalloc(size_t size);
91 void *xzalloc(size_t size);
92 void *xrealloc(void *ptr, size_t size);
93 char *xstrndup(char *s, size_t n);
94 char *xstrdup(char *s);
95 char *xmsprintf(char *format, ...);
96 void xprintf(char *format, ...);
100 void xexec(char **argv);
101 void xaccess(char *path, int flags);
102 void xunlink(char *path);
103 int xcreate(char *path, int flags, int mode);
104 int xopen(char *path, int flags);
107 FILE *xfopen(char *path, char *mode);
108 ssize_t readall(int fd, void *buf, size_t len);
109 ssize_t writeall(int fd, void *buf, size_t len);
110 size_t xread(int fd, void *buf, size_t len);
111 void xreadall(int fd, void *buf, size_t len);
112 void xwrite(int fd, void *buf, size_t len);
113 off_t xlseek(int fd, off_t offset, int whence);
114 off_t lskip(int fd, off_t offset);
115 char *readfile(char *name);
116 char *xreadfile(char *name);
118 void xstat(char *path, struct stat *st);
119 char *xabspath(char *path, unsigned missing);
120 char *xrealpath(char *path);
121 void xchdir(char *path);
122 void xmkpath(char *path, int mode);
123 void xsetuid(uid_t uid);
124 struct string_list *find_in_path(char *path, char *filename);
125 void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
126 void itoa_to_buf(int n, char *buf, unsigned buflen);
127 char *utoa(unsigned n);
131 int stridx(char *haystack, char needle);
132 off_t fdlength(int fd);
133 char *xreadlink(char *name);
134 void loopfiles_rw(char **argv, int flags, int permissions, int failok,
135 void (*function)(int fd, char *name));
136 void loopfiles(char **argv, void (*function)(int fd, char *name));
137 char *get_rawline(int fd, long *plen, char end);
138 char *get_line(int fd);
139 void xsendfile(int in, int out);
140 int wfchmodat(int rc, char *name, mode_t mode);
141 int copy_tempfile(int fdin, char *name, char **tempname);
142 void delete_tempfile(int fdin, int fdout, char **tempname);
143 void replace_tempfile(int fdin, int fdout, char **tempname);
144 void crc_init(unsigned int *crc_table, int little_endian);
145 void terminal_size(unsigned *x, unsigned *y);
146 int yesno(char *prompt, int def);
147 void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid));
152 struct mtab_list *next;
154 struct statvfs statvfs;
160 struct mtab_list *getmountlist(int die);
162 void bunzipStream(int src_fd, int dst_fd);
166 void sigatexit(void *handler);
167 int sig_to_num(char *pidstr);
168 char *num_to_sig(int sig);
170 mode_t string_to_mode(char *mode_str, mode_t base);
172 // password helper functions
173 int read_password(char * buff, int buflen, char* mesg);
174 int update_password(char *filename, char* username, char* encrypted);
176 // du helper functions
177 char* make_human_readable(unsigned long long size, unsigned long unit);
179 // cut helper functions
180 unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange);