/* * Shell-like utility functions * * Copyright 2004, Broadcom Corporation * All Rights Reserved. * * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE. * * $Id: shutils.h.brcm,v 1.1.1.1 2008/10/15 03:31:22 james26_jang Exp $ */ #ifndef _shutils_h_ #define _shutils_h_ /* * Reads file and returns contents * @param fd file descriptor * @return contents of file or NULL if an error occurred */ extern char * fd2str(int fd); /* * Reads file and returns contents * @param path path to file * @return contents of file or NULL if an error occurred */ extern char * file2str(const char *path); /* * Waits for a file descriptor to become available for reading or unblocked signal * @param fd file descriptor * @param timeout seconds to wait before timing out or 0 for no timeout * @return 1 if descriptor changed status or 0 if timed out or -1 on error */ extern int waitfor(int fd, int timeout); /* * Concatenates NULL-terminated list of arguments into a single * commmand and executes it * @param argv argument list * @param path NULL, ">output", or ">>output" * @param timeout seconds to wait before timing out or 0 for no timeout * @param ppid NULL to wait for child termination or pointer to pid * @return return value of executed command or errno */ extern int _eval(char *const argv[], char *path, int timeout, pid_t *ppid); /* * Concatenates NULL-terminated list of arguments into a single * commmand and executes it * @param argv argument list * @return stdout of executed command or NULL if an error occurred */ extern char * _backtick(char *const argv[]); /* * Kills process whose PID is stored in plaintext in pidfile * @param pidfile PID file * @return 0 on success and errno on failure */ extern int kill_pidfile(char *pidfile); /* * fread() with automatic retry on syscall interrupt * @param ptr location to store to * @param size size of each element of data * @param nmemb number of elements * @param stream file stream * @return number of items successfully read */ extern int safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream); /* * fwrite() with automatic retry on syscall interrupt * @param ptr location to read from * @param size size of each element of data * @param nmemb number of elements * @param stream file stream * @return number of items successfully written */ extern int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); /* * Convert Ethernet address string representation to binary data * @param a string in xx:xx:xx:xx:xx:xx notation * @param e binary data * @return TRUE if conversion was successful and FALSE otherwise */ extern int ether_atoe(const char *a, unsigned char *e); /* * Convert Ethernet address binary data to string representation * @param e binary data * @param a string in xx:xx:xx:xx:xx:xx notation * @return a */ extern char * ether_etoa(const unsigned char *e, char *a); /* * Concatenate two strings together into a caller supplied buffer * @param s1 first string * @param s2 second string * @param buf buffer large enough to hold both strings * @return buf */ static inline char * strcat_r(const char *s1, const char *s2, char *buf) { strcpy(buf, s1); strcat(buf, s2); return buf; } /* Check for a blank character; that is, a space or a tab */ #define isblank(c) ((c) == ' ' || (c) == '\t') /* Strip trailing CR/NL from string */ #define chomp(s) ({ \ char *c = (s) + strlen((s)) - 1; \ while ((c > (s)) && (*c == '\n' || *c == '\r')) \ *c-- = '\0'; \ s; \ }) /* Simple version of _backtick() */ #define backtick(cmd, args...) ({ \ char *argv[] = { cmd, ## args, NULL }; \ _backtick(argv); \ }) /* Simple version of _eval() (no timeout and wait for child termination) */ #define eval(cmd, args...) ({ \ char *argv[] = { cmd, ## args, NULL }; \ _eval(argv, ">/dev/console", 0, NULL); \ }) /* Copy each token in wordlist delimited by space into word */ #define foreach(word, wordlist, next) \ for (next = &wordlist[strspn(wordlist, " ")], \ strncpy(word, next, sizeof(word)), \ word[strcspn(word, " ")] = '\0', \ word[sizeof(word) - 1] = '\0', \ next = strchr(next, ' '); \ strlen(word); \ next = next ? &next[strspn(next, " ")] : "", \ strncpy(word, next, sizeof(word)), \ word[strcspn(word, " ")] = '\0', \ word[sizeof(word) - 1] = '\0', \ next = strchr(next, ' ')) /* Return NUL instead of NULL if undefined */ #define safe_getenv(s) (getenv(s) ? : "") /* Print directly to the console */ #define cprintf(fmt, args...) do { \ FILE *fp = fopen("/dev/console", "w"); \ if (fp) { \ fprintf(fp, fmt, ## args); \ fclose(fp); \ } \ } while (0) /* Debug print */ #ifdef DEBUG #define dprintf(fmt, args...) cprintf("%s: " fmt, __FUNCTION__, ## args) #else #define dprintf(fmt, args...) #endif #endif /* _shutils_h_ */