2 * Shell-like utility functions
4 * Copyright 2005, Broadcom Corporation
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.
12 * $Id: shutils.h,v 1.8 2005/03/07 08:35:32 kanki Exp $
19 * Reads file and returns contents
20 * @param fd file descriptor
21 * @return contents of file or NULL if an error occurred
23 extern char * fd2str(int fd
);
26 * Reads file and returns contents
27 * @param path path to file
28 * @return contents of file or NULL if an error occurred
30 extern char * file2str(const char *path
);
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
38 // extern int waitfor(int fd, int timeout);
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
49 extern int _eval(char *const argv
[], const char *path
, int timeout
, pid_t
*ppid
);
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
57 // extern char * _backtick(char *const argv[]);
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
64 extern int kill_pidfile(char *pidfile
);
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
74 extern int safe_fread(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
);
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
84 extern int safe_fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE *stream
);
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
92 extern int ether_atoe(const char *a
, unsigned char *e
);
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
100 extern char * ether_etoa(const unsigned char *e
, char *a
);
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
109 static inline char * strcat_r(const char *s1
, const char *s2
, char *buf
)
116 /* Strip trailing CR/NL from string <s> */
117 #define chomp(s) ({ \
118 char *c = (s) + strlen((s)) - 1; \
119 while ((c > (s)) && (*c == '\n' || *c == '\r' || *c == ' ')) \
125 /* Simple version of _eval() (no timeout and wait for child termination) */
127 #define eval(cmd, args...) ({ \
128 char *argv[] = { cmd, ## args, NULL }; \
129 _eval(argv, NULL, 0, NULL); \
132 #define eval(cmd, args...) ({ \
133 char *argv[] = { cmd, ## args, NULL }; \
134 _eval(argv, ">/dev/console", 0, NULL); \
138 /* Copy each token in wordlist delimited by space into word */
139 #define foreach(word, wordlist, next) \
140 for (next = &wordlist[strspn(wordlist, " ")], \
141 strncpy(word, next, sizeof(word)), \
142 word[strcspn(word, " ")] = '\0', \
143 word[sizeof(word) - 1] = '\0', \
144 next = strchr(next, ' '); \
146 next = next ? &next[strspn(next, " ")] : "", \
147 strncpy(word, next, sizeof(word)), \
148 word[strcspn(word, " ")] = '\0', \
149 word[sizeof(word) - 1] = '\0', \
150 next = strchr(next, ' '))
152 /* Return NUL instead of NULL if undefined */
153 #define safe_getenv(s) (getenv(s) ? : "")
156 extern void cprintf(const char *format
, ...);
160 * Parse the unit and subunit from an interface string such as wlXX or wlXX.YY
162 * @param ifname interface string to parse
163 * @param unit pointer to return the unit number, may pass NULL
164 * @param subunit pointer to return the subunit number, may pass NULL
165 * @return Returns 0 if the string ends with digits or digits.digits, -1 otherwise.
166 * If ifname ends in digits.digits, then unit and subuint are set
167 * to the first and second values respectively. If ifname ends
168 * in just digits, unit is set to the value, and subunit is set
169 * to -1. On error both unit and subunit are -1. NULL may be passed
170 * for unit and/or subuint to ignore the value.
172 extern int get_ifname_unit(const char* ifname
, int *unit
, int *subunit
);
175 * Set the ip configuration index given the eth name
176 * Updates both wlXX_ipconfig_index and lanYY_ifname.
178 * @param eth_ifname pointer to eth interface name
179 * @return 0 if successful -1 if not.
181 extern int set_ipconfig_index(char *eth_ifname
, int index
);
184 * Get the ip configuration index if it exists given the
187 * @param wl_ifname pointer to eth interface name
188 * @return index or -1 if not found
190 extern int get_ipconfig_index(char *eth_ifname
);
193 * Get interfaces belonging to a specific bridge.
195 * @param bridge_name pointer to bridge interface name
196 * @return list on interfaces beloging to the bridge
199 get_bridged_interfaces(char *bridge_name
);
203 Remove the specified word from the list.
205 @param name word to be removed from the list
206 @param list List to modify
207 @param listsize Max size the list can occupy
211 extern int remove_from_list(const char *name
, char *list
, int listsize
);
215 Add the specified interface(string) to the list as long as
216 it will fit in the space left in the list.
218 @param name Name of interface to be added to the list
219 @param list List to modify
220 @param listsize Max size the list can occupy
224 extern int add_to_list(const char *name
, char *list
, int listsize
);
226 extern char *find_in_list(const char *haystack
, const char *needle
);
228 extern char *remove_dups(char *inlist
, int inlist_size
);
230 extern char *find_smallest_in_list(char *haystack
);
232 extern char *sort_list(char *inlist
, int inlist_size
);
234 extern int nvifname_to_osifname(const char *nvifname
, char *osifname_buf
,
235 int osifname_buf_len
);
236 extern int osifname_to_nvifname(const char *osifname
, char *nvifname_buf
,
237 int nvifname_buf_len
);
239 int ure_any_enabled(void);
241 #endif /* _shutils_h_ */