From 694171759afb90bfdc552676ff6d04697c93af00 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 1 Jun 2008 14:02:09 +0200 Subject: [PATCH] Kill a bunch of unused cruft. --- adu.h | 101 +------------ fd.c | 214 ++------------------------- fd.h | 12 -- string.c | 442 +------------------------------------------------------ string.h | 38 ----- 5 files changed, 15 insertions(+), 792 deletions(-) diff --git a/adu.h b/adu.h index 1118b2c..1e6519b 100644 --- a/adu.h +++ b/adu.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2008 Andre Noll + * Copyright (C) 2008 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -26,9 +26,6 @@ #include #include "gcc-compat.h" -/** used in various contexts */ -#define MAXLINE 255 - /** compute the minimum of \a a and \a b */ #define MIN(a,b) ((a) < (b) ? (a) : (b)) /** compute the maximum of \a a and \a b */ @@ -97,69 +94,6 @@ #define EMERG_LOG(...) #endif /** \endcond */ - -/** - * define a standard log function that always writes to stderr - * - * \param loglevel_barrier If the loglevel of the current message - * is less than that, the message is going to be ignored. - * - */ -#define INIT_STDERR_LOGGING(loglevel_barrier) \ - __printf_2_3 void __log(int ll, const char* fmt,...) \ - { \ - va_list argp; \ - if (ll < loglevel_barrier) \ - return; \ - va_start(argp, fmt); \ - vfprintf(stderr, fmt, argp); \ - va_end(argp); \ - } - -/** version text used by various commands if -V switch was given */ -#define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \ - "Copyright (C) 2008 Andre Noll\n" \ - "This is free software with ABSOLUTELY NO WARRANTY." \ - " See COPYING for details.\n" \ - "Written by Andre Noll.\n" \ - "Report bugs to .\n" - -/** print out \p VERSION_TEXT and exit if version flag was given */ -#define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \ - if (_args_info_struct.version_given) { \ - printf("%s", VERSION_TEXT(_prefix)); \ - exit(EXIT_SUCCESS); \ - } -/** sent by para_server for commands that expect a data file */ -#define AWAITING_DATA_MSG "\nAwaiting Data." -/** sent by para_server if authentication was successful */ -#define PROCEED_MSG "\nProceed.\n" -/** length of the \p PROCEED_MSG string */ -#define PROCEED_MSG_LEN strlen(PROCEED_MSG) -/** sent by para_client to indicate the end of the command line */ -#define EOC_MSG "\nEnd of Command." -/** sent by para_client, followed by the decrypted challenge number */ -#define CHALLENGE_RESPONSE_MSG "challenge_response:" - -/* exec */ -int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds); - -/* time */ -int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff); -long unsigned tv2ms(const struct timeval*); -void d2tv(double, struct timeval*); -void tv_add(const struct timeval*, const struct timeval *, struct timeval *); -void tv_scale(const unsigned long, const struct timeval *, struct timeval *); -void tv_divide(const unsigned long divisor, const struct timeval *tv, - struct timeval *result); -int tv_convex_combination(const long a, const struct timeval *tv1, - const long b, const struct timeval *tv2, - struct timeval *result); -void ms2tv(const long unsigned n, struct timeval *tv); -void compute_chunk_time(long unsigned chunk_num, - struct timeval *chunk_tv, struct timeval *stream_start, - struct timeval *result); - __printf_2_3 void __log(int, const char*, ...); /** @@ -191,36 +125,3 @@ __printf_2_3 void __log(int, const char*, ...); p = para_realloc(p, size); \ } \ } - -/** - * Return a random non-negative integer in an interval. - * - * \param max Determines maximal possible return value. - * - * \return An integer between zero and \p max - 1, inclusively. - */ -static inline long int para_random(unsigned max) -{ - return ((max + 0.0) * (random() / (RAND_MAX + 1.0))); -} - -/** Round up x to a multiple of y */ -#define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y)) - -/** Get the size of an array */ -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - -/** - * Wrapper for isspace. - * NetBSD needs this. - */ -/* - * The values should be cast to an unsigned char first, then to int. - * Why? Because the isdigit (as do all other is/to functions/macros) - * expect a number from 0 upto and including 255 as their (int) argument. - * Because char is signed on most systems, casting it to int immediately - * gives the functions an argument between -128 and 127 (inclusive), - * which they will use as an array index, and which will thus fail - * horribly for characters which have their most significant bit set. - */ -#define para_isspace(c) isspace((int)(unsigned char)(c)) diff --git a/fd.c b/fd.c index 52569a1..40eb2fa 100644 --- a/fd.c +++ b/fd.c @@ -9,8 +9,6 @@ #include #include #include -#include -#include #include "adu.h" #include "error.h" @@ -25,12 +23,11 @@ * This function writes out the given buffer and retries if an interrupt * occurred during the write. * - * \return On success, the number of bytes written is returned, otherwise, the - * function returns \p -E_WRITE. + * \return Standard. * * \sa write(2). */ -ssize_t para_write(int fd, const void *buf, size_t size) +ssize_t __write(int fd, const void *buf, size_t size) { ssize_t ret; @@ -42,7 +39,6 @@ ssize_t para_write(int fd, const void *buf, size_t size) } } - /** * Write the whole buffer to a file descriptor. * @@ -53,17 +49,14 @@ ssize_t para_write(int fd, const void *buf, size_t size) * This function writes the given buffer and continues on short writes and * when interrupted by a signal. * - * \return Positive on success, negative on errors. Possible errors: any - * errors returned by para_write(). - * - * \sa para_write(). + * \return Standard. */ -ssize_t write_all(int fd, const void *buf, size_t size) +static ssize_t write_all(int fd, const void *buf, size_t size) { // DEBUG_LOG("writing %zu bytes\n", size); const char *b = buf; while (size) { - ssize_t ret = para_write(fd, b, size); + ssize_t ret = __write(fd, b, size); // DEBUG_LOG("ret: %zd\n", ret); if (ret < 0) return ret; @@ -87,7 +80,7 @@ ssize_t write_all(int fd, const void *buf, size_t size) * * \sa open(2). */ -int para_open(const char *path, int flags, mode_t mode) +static int __open(const char *path, int flags, mode_t mode) { int ret = open(path, flags, mode); @@ -103,16 +96,13 @@ int para_open(const char *path, int flags, mode_t mode) * \param buf The buffer to write to the file. * \param size The size of \a buf. * - * \return Positive on success, negative on errors. Possible errors include: - * any errors from para_open() or para_write(). - * - * \sa para_open(), para_write(). + * \return Standard. */ int para_write_file(const char *filename, const void *buf, size_t size) { int ret, fd; - ret = para_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644); + ret = __open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644); if (ret < 0) return ret; fd = ret; @@ -125,144 +115,6 @@ out: return ret; } - -/** - * Check whether a file exists. - * - * \param fn The file name. - * - * \return Non-zero iff file exists. - */ -int file_exists(const char *fn) -{ - struct stat statbuf; - - return !stat(fn, &statbuf); -} - -/** - * Paraslash's wrapper for select(2). - * - * It calls select(2) (with no exceptfds) and starts over if select() was - * interrupted by a signal. - * - * \param n The highest-numbered descriptor in any of the two sets, plus 1. - * \param readfds fds that should be checked for readability. - * \param writefds fds that should be checked for writablility. - * \param timeout_tv upper bound on the amount of time elapsed before select() - * returns. - * - * \return The return value of the underlying select() call on success, the - * negative system error code on errors. - * - * All arguments are passed verbatim to select(2). - * \sa select(2) select_tut(2). - */ -int para_select(int n, fd_set *readfds, fd_set *writefds, - struct timeval *timeout_tv) -{ - int ret, err; - do { - ret = select(n, readfds, writefds, NULL, timeout_tv); - err = errno; - } while (ret < 0 && err == EINTR); - if (ret < 0) - return -ERRNO_TO_ERROR(errno); - return ret; -} - -/** - * Set a file descriptor to blocking mode. - * - * \param fd The file descriptor. - * - * \return Standard. - */ -__must_check int mark_fd_blocking(int fd) -{ - int flags = fcntl(fd, F_GETFL); - if (flags < 0) - return -ERRNO_TO_ERROR(errno); - flags = fcntl(fd, F_SETFL, ((long)flags) & ~O_NONBLOCK); - if (flags < 0) - return -ERRNO_TO_ERROR(errno); - return 1; -} - -/** - * Set a file descriptor to non-blocking mode. - * - * \param fd The file descriptor. - * - * \return Standard. - */ -__must_check int mark_fd_nonblocking(int fd) -{ - int flags = fcntl(fd, F_GETFL); - if (flags < 0) - return -ERRNO_TO_ERROR(errno); - flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK); - if (flags < 0) - return -ERRNO_TO_ERROR(errno); - return 1; -} - -/** - * Set a file descriptor in a fd_set. - * - * \param fd The file descriptor to be set. - * \param fds The file descriptor set. - * \param max_fileno Highest-numbered file descriptor. - * - * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon - * return, \a max_fileno contains the maximum of the old_value and \a fd. - * - * \sa para_select. -*/ -void para_fd_set(int fd, fd_set *fds, int *max_fileno) -{ - - if (fd < 0 || fd >= FD_SETSIZE) { - EMERG_LOG("fatal: tried to add invalid fd %d\n", fd); - exit(EXIT_FAILURE); - } -#if 0 - { - int flags = fcntl(fd, F_GETFL); - if (!(flags & O_NONBLOCK)) { - EMERG_LOG("fd %d is a blocking file descriptor\n", fd); - exit(EXIT_FAILURE); - } - } -#endif - FD_SET(fd, fds); - *max_fileno = MAX(*max_fileno, fd); -} - -/** - * Paraslash's wrapper for mmap. - * - * \param length Number of bytes to mmap. - * \param prot Either PROT_NONE or the bitwise OR of one or more of - * PROT_EXEC PROT_READ PROT_WRITE. - * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE. - * \param fd The file to mmap from. - * \param offset Mmap start. - * - * \return This function either returns a valid pointer to the mapped area - * or calls exit() on errors. - */ -void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset) -{ - void *ret = mmap(NULL, length, prot, flags, fd, offset); - if (ret != MAP_FAILED) - return ret; - EMERG_LOG("mmap failed: %s\n", strerror(errno)); - EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n", - length, flags, fd, (size_t)offset); - exit(EXIT_FAILURE); -} - /** * Wrapper for chdir(2). * @@ -270,7 +122,7 @@ void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset) * * \return Standard. */ -int para_chdir(const char *path) +int __chdir(const char *path) { int ret = chdir(path); @@ -308,12 +160,12 @@ int para_opendir(const char *dirname, DIR **dir, int *cwd) int ret; if (cwd) { - ret = para_open(".", O_RDONLY, 0); + ret = __open(".", O_RDONLY, 0); if (ret < 0) return ret; *cwd = ret; } - ret = para_chdir(dirname); + ret = __chdir(dirname); if (ret < 0) goto close_cwd; *dir = opendir("."); @@ -343,21 +195,6 @@ int para_fchdir(int fd) return 1; } -/** - * A wrapper for mkdir(2). - * - * \param path Name of the directory to create. - * \param mode The permissions to use. - * - * \return Standard. - */ -int para_mkdir(const char *path, mode_t mode) -{ - if (!mkdir(path, mode)) - return 1; - return -ERRNO_TO_ERROR(errno); -} - /** * Open a file and map it into memory. * @@ -373,7 +210,7 @@ int para_mkdir(const char *path, mode_t mode) * * \return Standard. * - * \sa para_open(), mmap(2). + * \sa mmap(2). */ int mmap_full_file(const char *path, int open_mode, void **map, size_t *size, int *fd_ptr) @@ -388,7 +225,7 @@ int mmap_full_file(const char *path, int open_mode, void **map, mmap_prot = PROT_READ | PROT_WRITE; mmap_flags = MAP_SHARED; } - ret = para_open(path, open_mode, 0); + ret = __open(path, open_mode, 0); if (ret < 0) return ret; fd = ret; @@ -436,28 +273,3 @@ int para_munmap(void *start, size_t length) strerror(err)); return -ERRNO_TO_ERROR(err); } - -/** - * Check a file descriptor for writability. - * - * \param fd The file descriptor. - * - * \return positive if fd is ready for writing, zero if it isn't, negative if - * an error occurred. - */ - -int write_ok(int fd) -{ - struct timeval tv = {0, 0}; - fd_set wfds; - int ret; -again: - FD_ZERO(&wfds); - FD_SET(fd, &wfds); - tv.tv_sec = 0; - tv.tv_usec = 0; - ret = select(fd + 1, NULL, &wfds, NULL, &tv); - if (ret < 0 && errno == EINTR) - goto again; - return ret; -} diff --git a/fd.h b/fd.h index 33ae29d..ad81942 100644 --- a/fd.h +++ b/fd.h @@ -6,21 +6,9 @@ /** \file fd.h exported symbols from fd.c */ -int write_all(int fd, const char *buf, size_t *len); -int file_exists(const char *); -int para_select(int n, fd_set *readfds, fd_set *writefds, - struct timeval *timeout_tv); -__must_check int mark_fd_nonblocking(int fd); -__must_check int mark_fd_blocking(int fd); -void para_fd_set(int fd, fd_set *fds, int *max_fileno); -void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset); -int para_open(const char *path, int flags, mode_t mode); int para_opendir(const char *dirname, DIR **dir, int *cwd); -int para_mkdir(const char *path, mode_t mode); int para_fchdir(int fd); -int para_chdir(const char *path); int mmap_full_file(const char *filename, int open_mode, void **map, size_t *size, int *fd_ptr); int para_munmap(void *start, size_t length); -int write_ok(int fd); int para_write_file(const char *filename, const void *buf, size_t size); diff --git a/string.c b/string.c index 0da83b0..8bd3446 100644 --- a/string.c +++ b/string.c @@ -8,12 +8,7 @@ #include "adu.h" #include "string.h" - -#include /* gettimeofday */ -#include -#include /* uname() */ #include - #include "error.h" /** @@ -134,416 +129,6 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...) return msg; } -/** - * Paraslash's version of strcat(). - * - * \param a String to be appended to. - * \param b String to append. - * - * Append \p b to \p a. - * - * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e. - * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is \p NULL, - * return \a a without making a copy of \a a. Otherwise, construct the - * concatenation \a c, free \a a (but not \a b) and return \a c. - * - * \sa strcat(3) - */ -__must_check __malloc char *para_strcat(char *a, const char *b) -{ - char *tmp; - - if (!a) - return para_strdup(b); - if (!b) - return a; - tmp = make_message("%s%s", a, b); - free(a); - return tmp; -} - -/** - * Paraslash's version of dirname(). - * - * \param name Pointer to the full path. - * - * Compute the directory component of \p name. - * - * \return If \a name is \p NULL or the empty string, return \p NULL. - * Otherwise, Make a copy of \a name and return its directory component. Caller - * is responsible to free the result. - */ -__must_check __malloc char *para_dirname(const char *name) -{ - char *p, *ret; - - if (!name || !*name) - return NULL; - ret = para_strdup(name); - p = strrchr(ret, '/'); - if (!p) - *ret = '\0'; - else - *p = '\0'; - return ret; -} - -/** - * Paraslash's version of basename(). - * - * \param name Pointer to the full path. - * - * Compute the filename component of \a name. - * - * \return \p NULL if (a) \a name is the empty string or \p NULL, or (b) name - * ends with a slash. Otherwise, a pointer within \a name is returned. Caller - * must not free the result. - */ -__must_check const char *para_basename(const char *name) -{ - const char *ret; - - if (!name || !*name) - return NULL; - ret = strrchr(name, '/'); - if (!ret) - return name; - ret++; - return ret; -} - -/** - * Cut trailing newline. - * - * \param buf The string to be chopped. - * - * Replace the last character in \p buf by zero if it is euqal to - * the newline character. - */ -void chop(char *buf) -{ - int n = strlen(buf); - if (!n) - return; - if (buf[n - 1] == '\n') - buf[n - 1] = '\0'; -} - -/** - * Get a random filename. - * - * This is by no means a secure way to create temporary files in a hostile - * direcory like \p /tmp. However, it is OK to use for temp files, fifos, - * sockets that are created in ~/.paraslash. Result must be freed by the - * caller. - * - * \return A pointer to a random filename. - */ -__must_check __malloc char *para_tmpname(void) -{ - struct timeval now; - unsigned int seed; - - gettimeofday(&now, NULL); - seed = now.tv_usec; - srand(seed); - return make_message("%08i", rand()); -} - -/** - * Get the logname of the current user. - * - * \return A dynammically allocated string that must be freed by the caller. On - * errors, the string "unknown user" is returned, i.e. this function never - * returns \p NULL. - * - * \sa getpwuid(3). - */ -__must_check __malloc char *para_logname(void) -{ - struct passwd *pw = getpwuid(getuid()); - return para_strdup(pw? pw->pw_name : "unknown_user"); -} - -/** - * Get the home directory of the current user. - * - * \return A dynammically allocated string that must be freed by the caller. If - * the home directory could not be found, this function returns "/tmp". - */ -__must_check __malloc char *para_homedir(void) -{ - struct passwd *pw = getpwuid(getuid()); - return para_strdup(pw? pw->pw_dir : "/tmp"); -} - -/** - * Split string and return pointers to its parts. - * - * \param args The string to be split. - * \param argv_ptr Pointer to the list of substrings. - * \param delim Delimiter. - * - * This function modifies \a args by replacing each occurance of \a delim by - * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically - * and these pointers are initialized to point to the broken-up substrings - * within \a args. A pointer to this array is returned via \a argv_ptr. - * - * \return The number of substrings found in \a args. - */ -__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim) -{ - char *p = args; - char **argv; - size_t n = 0, i, j; - - p = args + strspn(args, delim); - for (;;) { - i = strcspn(p, delim); - if (!i) - break; - p += i; - n++; - p += strspn(p, delim); - } - *argv_ptr = para_malloc((n + 1) * sizeof(char *)); - argv = *argv_ptr; - i = 0; - p = args + strspn(args, delim); - while (p) { - argv[i] = p; - j = strcspn(p, delim); - if (!j) - break; - p += strcspn(p, delim); - if (*p) { - *p = '\0'; - p++; - p += strspn(p, delim); - } - i++; - } - argv[n] = NULL; - return n; -} - -/** - * Ensure that file descriptors 0, 1, and 2 are valid. - * - * Common approach that opens /dev/null until it gets a file descriptor greater - * than two. - * - * \sa okir's Black Hats Manual. - */ -void valid_fd_012(void) -{ - while (1) { - int fd = open("/dev/null", O_RDWR); - if (fd < 0) - exit(EXIT_FAILURE); - if (fd > 2) { - close(fd); - break; - } - } -} - -/** - * Get the own hostname. - * - * \return A dynammically allocated string containing the hostname. - * - * \sa uname(2). - */ -__malloc char *para_hostname(void) -{ - struct utsname u; - - uname(&u); - return para_strdup(u.nodename); -} - -/** - * Used to distinguish between read-only and read-write mode. - * - * \sa for_each_line(), for_each_line_ro(). - */ -enum for_each_line_modes{ - /** Activate read-only mode. */ - LINE_MODE_RO, - /** Activate read-write mode. */ - LINE_MODE_RW -}; - -static int for_each_complete_line(enum for_each_line_modes mode, char *buf, - size_t size, line_handler_t *line_handler, void *private_data) -{ - char *start = buf, *end; - int ret, i, num_lines = 0; - -// PARA_NOTICE_LOG("buf: %s\n", buf); - while (start < buf + size) { - char *next_null; - char *next_cr; - - next_cr = memchr(start, '\n', buf + size - start); - next_null = memchr(start, '\0', buf + size - start); - if (!next_cr && !next_null) - break; - if (next_cr && next_null) { - end = next_cr < next_null? next_cr : next_null; - } else if (next_null) { - end = next_null; - } else - end = next_cr; - num_lines++; - if (!line_handler) { - start = ++end; - continue; - } - if (mode == LINE_MODE_RO) { - size_t s = end - start; - char *b = para_malloc(s + 1); - memcpy(b, start, s); - b[s] = '\0'; -// PARA_NOTICE_LOG("b: %s, start: %s\n", b, start); - ret = line_handler(b, private_data); - free(b); - } else { - *end = '\0'; - ret = line_handler(start, private_data); - } - if (ret < 0) - return ret; - start = ++end; - } - if (!line_handler || mode == LINE_MODE_RO) - return num_lines; - i = buf + size - start; - if (i && i != size) - memmove(buf, start, i); - return i; -} - -/** - * Call a custom function for each complete line. - * - * \param buf The buffer containing data seperated by newlines. - * \param size The number of bytes in \a buf. - * \param line_handler The custom function. - * \param private_data Pointer passed to \a line_handler. - * - * If \p line_handler is \p NULL, the function returns the number of complete - * lines in \p buf. Otherwise, \p line_handler is called for each complete - * line in \p buf. The first argument to \p line_handler is the current line, - * and \p private_data is passed as the second argument. The function returns - * if \p line_handler returns a negative value or no more lines are in the - * buffer. The rest of the buffer (last chunk containing an incomplete line) - * is moved to the beginning of the buffer. - * - * \return If \p line_handler is not \p NULL, this function returns the number - * of bytes not handled to \p line_handler on success, or the negative return - * value of the \p line_handler on errors. - * - * \sa for_each_line_ro(). - */ -int for_each_line(char *buf, size_t size, line_handler_t *line_handler, - void *private_data) -{ - return for_each_complete_line(LINE_MODE_RW, buf, size, line_handler, - private_data); -} - -/** - * Call a custom function for each complete line. - * - * \param buf Same meaning as in \p for_each_line(). - * \param size Same meaning as in \p for_each_line(). - * \param line_handler Same meaning as in \p for_each_line(). - * \param private_data Same meaning as in \p for_each_line(). - * - * This function behaves like \p for_each_line(), but \a buf is left unchanged. - * - * \return On success, the function returns the number of complete lines in \p - * buf, otherwise the (negative) return value of \p line_handler is returned. - * - * \sa for_each_line(). - */ -int for_each_line_ro(char *buf, size_t size, line_handler_t *line_handler, - void *private_data) -{ - return for_each_complete_line(LINE_MODE_RO, buf, size, line_handler, - private_data); -} - -/** - * Safely print into a buffer at a given offset - * - * \param b Determines the buffer, its size, and the offset. - * \param fmt The format string. - * - * This function prints into the buffer given by \a b at the offset which is - * also given by \a b. If there is not enough space to hold the result, the - * buffer size is doubled until the underlying call to vsnprintf() succeeds - * or the size of the buffer exceeds the maximal size specified in \a pb. - * - * In the latter case the unmodified \a buf and \a offset values as well as the - * private_data pointer of \a b are passed to the \a max_size_handler of \a b. - * If this function succeeds, i.e. returns a non-negative value, the offset of - * \a b is reset to zero and the given data is written to the beginning of the - * buffer. - * - * Upon return, the offset of \a b is adjusted accordingly so that subsequent - * calls to this function append data to what is already contained in the - * buffer. - * - * It's OK to call this function with \p b->buf being \p NULL. In this case, an - * initial buffer is allocated. - * - * \return The number of bytes printed into the buffer (not including the - * therminating \p NULL byte). - * - * \sa make_message(), vsnprintf(3). - */ -__printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...) -{ - int ret; - - if (!b->buf) { - b->buf = para_malloc(128); - b->size = 128; - b->offset = 0; - } - while (1) { - char *p = b->buf + b->offset; - size_t size = b->size - b->offset; - va_list ap; - if (size) { - va_start(ap, fmt); - ret = vsnprintf(p, size, fmt, ap); - va_end(ap); - if (ret > -1 && ret < size) { /* success */ - b->offset += ret; - return ret; - } - } - /* check if we may grow the buffer */ - if (!b->max_size || 2 * b->size < b->max_size) { /* yes */ - /* try again with more space */ - b->size *= 2; - b->buf = para_realloc(b->buf, b->size); - continue; - } - /* can't grow buffer */ - if (!b->offset || !b->max_size_handler) /* message too large */ - return -ERRNO_TO_ERROR(ENOSPC); - ret = b->max_size_handler(b->buf, b->offset, b->private_data); - if (ret < 0) - return ret; - b->offset = 0; - } -} - /** \cond LLONG_MAX and LLONG_LIN might not be defined. */ #ifndef LLONG_MAX #define LLONG_MAX (1 << (sizeof(long) - 1)) @@ -561,7 +146,7 @@ __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...) * * \return Standard. * - * \sa para_atoi32(), strtol(3), atoi(3). + * \sa strtol(3), atoi(3). */ int para_atoi64(const char *str, int64_t *value) { @@ -581,28 +166,3 @@ int para_atoi64(const char *str, int64_t *value) *value = tmp; return 1; } - -/** - * Convert a string to a 32-bit signed integer value. - * - * \param str The string to be converted. - * \param value Result pointer. - * - * \return Standard. - * - * \sa para_atoi64(). -*/ -int para_atoi32(const char *str, int32_t *value) -{ - int64_t tmp; - int ret; - const int32_t max = 2147483647; - - ret = para_atoi64(str, &tmp); - if (ret < 0) - return ret; - if (tmp > max || tmp < -max - 1) - return -E_ATOI_OVERFLOW; - *value = tmp; - return 1; -} diff --git a/string.h b/string.h index 561ff4e..c98ed89 100644 --- a/string.h +++ b/string.h @@ -6,47 +6,9 @@ /** \file string.h exported sybmols from string.c */ -/** A string buffer used for para_printf(). */ -struct para_buffer { - /** The buffer. May be \p NULL. */ - char *buf; - /** The size of \a buf. */ - size_t size; - /** The maximal size this buffer may grow. Zero means unlimited. */ - size_t max_size; - /** The next para_printf() will write at this offset. */ - size_t offset; - /** - * If an attempt to print into this buffer is made that would need to - * grow \a buf to a size larger than \a max_size, then this function is - * called. - */ - int (*max_size_handler)(char *buf, size_t size, void *private_data); - /** Passed to max_size_handler(). */ - void *private_data; -}; - __must_check __malloc void *para_realloc(void *p, size_t size); __must_check __malloc void *para_malloc(size_t size); __must_check __malloc void *para_calloc(size_t size); __must_check __malloc char *para_strdup(const char *s); __must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...); -__must_check __malloc char *para_strcat(char *a, const char *b); -__must_check __malloc char *para_dirname(const char *name); -__must_check const char *para_basename(const char *name); -void chop(char *buf); -__must_check __malloc char *para_tmpname(void); -__must_check __malloc char *para_logname(void); -__must_check __malloc char *para_homedir(void); -__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim); -__malloc char *para_hostname(void); -void valid_fd_012(void); -__printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...); -/** Used for for_each_line() and for_each_line_ro(). */ -typedef int line_handler_t(char *, void *); -int for_each_line(char *buf, size_t size, line_handler_t *line_handler, - void *private_data); -int for_each_line_ro(char *buf, size_t size, line_handler_t *line_handler, - void *private_data); int para_atoi64(const char *str, int64_t *result); -int para_atoi32(const char *str, int32_t *value); -- 2.30.2