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 #define MAX_NVPARSE 255
20 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
22 extern int doSystem(char *fmt
, ...);
25 * Reads file and returns contents
26 * @param fd file descriptor
27 * @return contents of file or NULL if an error occurred
29 extern char * fd2str(int fd
);
32 * Reads file and returns contents
33 * @param path path to file
34 * @return contents of file or NULL if an error occurred
36 extern char * file2str(const char *path
);
39 * Waits for a file descriptor to become available for reading or unblocked signal
40 * @param fd file descriptor
41 * @param timeout seconds to wait before timing out or 0 for no timeout
42 * @return 1 if descriptor changed status or 0 if timed out or -1 on error
44 extern int waitfor(int fd
, int timeout
);
47 * Concatenates NULL-terminated list of arguments into a single
48 * commmand and executes it
49 * @param argv argument list
50 * @param path NULL, ">output", or ">>output"
51 * @param timeout seconds to wait before timing out or 0 for no timeout
52 * @param ppid NULL to wait for child termination or pointer to pid
53 * @return return value of executed command or errno
55 extern int _eval(char *const argv
[], const char *path
, int timeout
, pid_t
*ppid
);
58 * Evaluate cmds using taskset while SMP.
59 * @param ppid NULL to wait for child termination or pointer to pid
60 * @param cmds command argument list
61 * The normal command elements protype is as [cpu0/cpu1], [cmd_arg0, cmd_arg1, ..., NULL]
62 * If smp defined, it should specify cpu0/cpu1 at the fist element,
63 * if it is not specified, cpu0 will be the default choice.
64 * On UP case, no need to specify cpu0/1, otherwise will be ignored.
69 extern int _cpu_eval(int *ppid
, char *cmds
[]);
72 * Concatenates NULL-terminated list of arguments into a single
73 * commmand and executes it
74 * @param argv argument list
75 * @return stdout of executed command or NULL if an error occurred
77 // extern char * _backtick(char *const argv[]);
80 * Kills process whose PID is stored in plaintext in pidfile
81 * @param pidfile PID file
82 * @return 0 on success and errno on failure
84 extern int kill_pidfile(char *pidfile
);
85 extern int kill_pidfile_s(char *pidfile
, int sig
);
88 * fread() with automatic retry on syscall interrupt
89 * @param ptr location to store to
90 * @param size size of each element of data
91 * @param nmemb number of elements
92 * @param stream file stream
93 * @return number of items successfully read
95 extern int safe_fread(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
);
98 * fwrite() with automatic retry on syscall interrupt
99 * @param ptr location to read from
100 * @param size size of each element of data
101 * @param nmemb number of elements
102 * @param stream file stream
103 * @return number of items successfully written
105 extern int safe_fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE *stream
);
108 * Convert Ethernet address string representation to binary data
109 * @param a string in xx:xx:xx:xx:xx:xx notation
110 * @param e binary data
111 * @return TRUE if conversion was successful and FALSE otherwise
113 extern int ether_atoe(const char *a
, unsigned char *e
);
116 * Convert Ethernet address binary data to string representation
117 * @param e binary data
118 * @param a string in xx:xx:xx:xx:xx:xx notation
121 extern char * ether_etoa(const unsigned char *e
, char *a
);
124 * Concatenate two strings together into a caller supplied buffer
125 * @param s1 first string
126 * @param s2 second string
127 * @param buf buffer large enough to hold both strings
130 static inline char * strcat_r(const char *s1
, const char *s2
, char *buf
)
137 /* Check for a blank character; that is, a space or a tab */
139 #define isblank(c) ((c) == ' ' || (c) == '\t')
142 /* Strip trailing CR/NL from string <s> */
143 #define chomp(s) ({ \
144 char *c = (s) + strlen((s)) - 1; \
145 while ((c > (s)) && (*c == '\n' || *c == '\r' || *c == ' ')) \
151 /* Simple version of _eval() (no timeout and wait for child termination) */
153 #define eval(cmd, args...) ({ \
154 char * const argv[] = { cmd, ## args, NULL }; \
155 _eval(argv, NULL, 0, NULL); \
158 #define eval(cmd, args...) ({ \
159 char * const argv[] = { cmd, ## args, NULL }; \
160 _eval(argv, ">/dev/console", 0, NULL); \
164 /* another _cpu_eval form */
165 #define cpu_eval(ppid, cmd, args...) ({ \
166 char * argv[] = { cmd, ## args, NULL }; \
167 _cpu_eval(ppid, argv); \
170 /* Copy each token in wordlist delimited by space into word */
171 #define foreach(word, wordlist, next) \
172 for (next = &wordlist[strspn(wordlist, " ")], \
173 strncpy(word, next, sizeof(word)), \
174 word[strcspn(word, " ")] = '\0', \
175 word[sizeof(word) - 1] = '\0', \
176 next = strchr(next, ' '); \
178 next = next ? &next[strspn(next, " ")] : "", \
179 strncpy(word, next, sizeof(word)), \
180 word[strcspn(word, " ")] = '\0', \
181 word[sizeof(word) - 1] = '\0', \
182 next = strchr(next, ' '))
184 /* Copy each token in wordlist delimited by ascii_60 into word */
185 #define foreach_60(word, wordlist, next) \
186 for (next = &wordlist[strspn(wordlist, "<")], \
187 strncpy(word, next, sizeof(word)), \
188 word[strcspn(word, "<")] = '\0', \
189 word[sizeof(word) - 1] = '\0', \
190 next = strchr(next, '<'); \
192 next = next ? &next[strspn(next, "<")] : "", \
193 strncpy(word, next, sizeof(word)), \
194 word[strcspn(word, "<")] = '\0', \
195 word[sizeof(word) - 1] = '\0', \
196 next = strchr(next, '<'))
198 /* Copy each token in wordlist delimited by ascii_62 into word */
199 #define foreach_62(word, wordlist, next) \
200 for (next = &wordlist[strspn(wordlist, ">")], \
201 strncpy(word, next, sizeof(word)), \
202 word[strcspn(word, ">")] = '\0', \
203 word[sizeof(word) - 1] = '\0', \
204 next = strchr(next, '>'); \
206 next = next ? &next[strspn(next, ">")] : "", \
207 strncpy(word, next, sizeof(word)), \
208 word[strcspn(word, ">")] = '\0', \
209 word[sizeof(word) - 1] = '\0', \
210 next = strchr(next, '>'))
212 /* Return NUL instead of NULL if undefined */
213 #define safe_getenv(s) (getenv(s) ? : "")
215 //#define dbg(fmt, args...) do { FILE *fp = fopen("/dev/console", "w"); if (fp) { fprintf(fp, fmt, ## args); fclose(fp); } } while (0)
216 #define dbg(fmt, args...) do { FILE *fp = fopen("/dev/console", "w"); if (fp) { fprintf(fp, fmt, ## args); fclose(fp); } else fprintf(stderr, fmt, ## args); } while (0)
217 #define dbG(fmt, args...) dbg("%s(0x%04x): " fmt , __FUNCTION__ , __LINE__, ## args)
218 extern void cprintf(const char *format
, ...);
222 * Parse the unit and subunit from an interface string such as wlXX or wlXX.YY
224 * @param ifname interface string to parse
225 * @param unit pointer to return the unit number, may pass NULL
226 * @param subunit pointer to return the subunit number, may pass NULL
227 * @return Returns 0 if the string ends with digits or digits.digits, -1 otherwise.
228 * If ifname ends in digits.digits, then unit and subuint are set
229 * to the first and second values respectively. If ifname ends
230 * in just digits, unit is set to the value, and subunit is set
231 * to -1. On error both unit and subunit are -1. NULL may be passed
232 * for unit and/or subuint to ignore the value.
234 extern int get_ifname_unit(const char* ifname
, int *unit
, int *subunit
);
237 * Set the ip configuration index given the eth name
238 * Updates both wlXX_ipconfig_index and lanYY_ifname.
240 * @param eth_ifname pointer to eth interface name
241 * @return 0 if successful -1 if not.
243 extern int set_ipconfig_index(char *eth_ifname
, int index
);
246 * Get the ip configuration index if it exists given the
249 * @param wl_ifname pointer to eth interface name
250 * @return index or -1 if not found
252 extern int get_ipconfig_index(char *eth_ifname
);
254 * Get interfaces belonging to a specific bridge.
256 * @param bridge_name pointer to bridge interface name
257 * @return list on interfaces beloging to the bridge
260 get_bridged_interfaces(char *bridge_name
);
264 Remove the specified word from the list.
266 @param name word to be removed from the list
267 @param list List to modify
268 @param listsize Max size the list can occupy
272 extern int remove_from_list(const char *name
, char *list
, int listsize
);
276 Add the specified interface(string) to the list as long as
277 it will fit in the space left in the list.
279 @param name Name of interface to be added to the list
280 @param list List to modify
281 @param listsize Max size the list can occupy
285 extern int add_to_list(const char *name
, char *list
, int listsize
);
287 extern char *find_in_list(const char *haystack
, const char *needle
);
289 extern char *remove_dups(char *inlist
, int inlist_size
);
291 extern int nvifname_to_osifname(const char *nvifname
, char *osifname_buf
,
292 int osifname_buf_len
);
293 extern int osifname_to_nvifname(const char *osifname
, char *nvifname_buf
,
294 int nvifname_buf_len
);
296 int ure_any_enabled(void);
298 int is_hwnat_loaded(void);
300 #define vstrsep(buf, sep, args...) _vstrsep(buf, sep, args, NULL)
301 extern int _vstrsep(char *buf
, const char *sep
, ...);
303 #endif /* _shutils_h_ */