1/* 2 * Shell-like utility functions 3 * 4 * Copyright 2004, Broadcom Corporation 5 * All Rights Reserved. 6 * 7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY 8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM 9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE. 11 * 12 * $Id: shutils.h.brcm,v 1.1.1.1 2008/10/15 03:31:22 james26_jang Exp $ 13 */ 14 15#ifndef _shutils_h_ 16#define _shutils_h_ 17 18/* 19 * Reads file and returns contents 20 * @param fd file descriptor 21 * @return contents of file or NULL if an error occurred 22 */ 23extern char * fd2str(int fd); 24 25/* 26 * Reads file and returns contents 27 * @param path path to file 28 * @return contents of file or NULL if an error occurred 29 */ 30extern char * file2str(const char *path); 31 32/* 33 * Waits for a file descriptor to become available for reading or unblocked signal 34 * @param fd file descriptor 35 * @param timeout seconds to wait before timing out or 0 for no timeout 36 * @return 1 if descriptor changed status or 0 if timed out or -1 on error 37 */ 38extern int waitfor(int fd, int timeout); 39 40/* 41 * Concatenates NULL-terminated list of arguments into a single 42 * commmand and executes it 43 * @param argv argument list 44 * @param path NULL, ">output", or ">>output" 45 * @param timeout seconds to wait before timing out or 0 for no timeout 46 * @param ppid NULL to wait for child termination or pointer to pid 47 * @return return value of executed command or errno 48 */ 49extern int _eval(char *const argv[], char *path, int timeout, pid_t *ppid); 50 51/* 52 * Concatenates NULL-terminated list of arguments into a single 53 * commmand and executes it 54 * @param argv argument list 55 * @return stdout of executed command or NULL if an error occurred 56 */ 57extern char * _backtick(char *const argv[]); 58 59/* 60 * Kills process whose PID is stored in plaintext in pidfile 61 * @param pidfile PID file 62 * @return 0 on success and errno on failure 63 */ 64extern int kill_pidfile(char *pidfile); 65 66/* 67 * fread() with automatic retry on syscall interrupt 68 * @param ptr location to store to 69 * @param size size of each element of data 70 * @param nmemb number of elements 71 * @param stream file stream 72 * @return number of items successfully read 73 */ 74extern int safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream); 75 76/* 77 * fwrite() with automatic retry on syscall interrupt 78 * @param ptr location to read from 79 * @param size size of each element of data 80 * @param nmemb number of elements 81 * @param stream file stream 82 * @return number of items successfully written 83 */ 84extern int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); 85 86/* 87 * Convert Ethernet address string representation to binary data 88 * @param a string in xx:xx:xx:xx:xx:xx notation 89 * @param e binary data 90 * @return TRUE if conversion was successful and FALSE otherwise 91 */ 92extern int ether_atoe(const char *a, unsigned char *e); 93 94/* 95 * Convert Ethernet address binary data to string representation 96 * @param e binary data 97 * @param a string in xx:xx:xx:xx:xx:xx notation 98 * @return a 99 */ 100extern char * ether_etoa(const unsigned char *e, char *a); 101 102/* 103 * Concatenate two strings together into a caller supplied buffer 104 * @param s1 first string 105 * @param s2 second string 106 * @param buf buffer large enough to hold both strings 107 * @return buf 108 */ 109static inline char * strcat_r(const char *s1, const char *s2, char *buf) 110{ 111 strcpy(buf, s1); 112 strcat(buf, s2); 113 return buf; 114} 115 116/* Check for a blank character; that is, a space or a tab */ 117#define isblank(c) ((c) == ' ' || (c) == '\t') 118 119/* Strip trailing CR/NL from string <s> */ 120#define chomp(s) ({ \ 121 char *c = (s) + strlen((s)) - 1; \ 122 while ((c > (s)) && (*c == '\n' || *c == '\r')) \ 123 *c-- = '\0'; \ 124 s; \ 125}) 126 127/* Simple version of _backtick() */ 128#define backtick(cmd, args...) ({ \ 129 char *argv[] = { cmd, ## args, NULL }; \ 130 _backtick(argv); \ 131}) 132 133/* Simple version of _eval() (no timeout and wait for child termination) */ 134#define eval(cmd, args...) ({ \ 135 char *argv[] = { cmd, ## args, NULL }; \ 136 _eval(argv, ">/dev/console", 0, NULL); \ 137}) 138 139/* Copy each token in wordlist delimited by space into word */ 140#define foreach(word, wordlist, next) \ 141 for (next = &wordlist[strspn(wordlist, " ")], \ 142 strncpy(word, next, sizeof(word)), \ 143 word[strcspn(word, " ")] = '\0', \ 144 word[sizeof(word) - 1] = '\0', \ 145 next = strchr(next, ' '); \ 146 strlen(word); \ 147 next = next ? &next[strspn(next, " ")] : "", \ 148 strncpy(word, next, sizeof(word)), \ 149 word[strcspn(word, " ")] = '\0', \ 150 word[sizeof(word) - 1] = '\0', \ 151 next = strchr(next, ' ')) 152 153/* Return NUL instead of NULL if undefined */ 154#define safe_getenv(s) (getenv(s) ? : "") 155 156/* Print directly to the console */ 157#define cprintf(fmt, args...) do { \ 158 FILE *fp = fopen("/dev/console", "w"); \ 159 if (fp) { \ 160 fprintf(fp, fmt, ## args); \ 161 fclose(fp); \ 162 } \ 163} while (0) 164 165/* Debug print */ 166#ifdef DEBUG 167#define dprintf(fmt, args...) cprintf("%s: " fmt, __FUNCTION__, ## args) 168#else 169#define dprintf(fmt, args...) 170#endif 171 172 173#endif /* _shutils_h_ */ 174