Tomato 1.28
[tomato.git] / release / src / router / shared / shutils.h
blob693ce101ceaf48b88e3897045b6b916dcda5f8fa
1 /*
2 * Shell-like utility functions
4 * Copyright 2005, 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.
12 * $Id: shutils.h,v 1.8 2005/03/07 08:35:32 kanki Exp $
15 #ifndef _shutils_h_
16 #define _shutils_h_
17 #include <string.h>
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);
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
38 // extern int waitfor(int fd, int timeout);
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
49 extern int _eval(char *const argv[], const char *path, int timeout, pid_t *ppid);
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
57 // extern char * _backtick(char *const argv[]);
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
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
98 * @return a
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
107 * @return buf
109 static inline char * strcat_r(const char *s1, const char *s2, char *buf)
111 strcpy(buf, s1);
112 strcat(buf, s2);
113 return buf;
116 /* Check for a blank character; that is, a space or a tab */
117 #define isblank(c) ((c) == ' ' || (c) == '\t')
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' || *c == ' ')) \
123 *c-- = '\0'; \
124 s; \
128 /* Simple version of _eval() (no timeout and wait for child termination) */
129 #if 1
130 #define eval(cmd, args...) ({ \
131 char *argv[] = { cmd, ## args, NULL }; \
132 _eval(argv, NULL, 0, NULL); \
134 #else
135 #define eval(cmd, args...) ({ \
136 char *argv[] = { cmd, ## args, NULL }; \
137 _eval(argv, ">/dev/console", 0, NULL); \
139 #endif
141 /* Copy each token in wordlist delimited by space into word */
142 #define foreach(word, wordlist, next) \
143 for (next = &wordlist[strspn(wordlist, " ")], \
144 strncpy(word, next, sizeof(word)), \
145 word[strcspn(word, " ")] = '\0', \
146 word[sizeof(word) - 1] = '\0', \
147 next = strchr(next, ' '); \
148 strlen(word); \
149 next = next ? &next[strspn(next, " ")] : "", \
150 strncpy(word, next, sizeof(word)), \
151 word[strcspn(word, " ")] = '\0', \
152 word[sizeof(word) - 1] = '\0', \
153 next = strchr(next, ' '))
155 /* Return NUL instead of NULL if undefined */
156 #define safe_getenv(s) (getenv(s) ? : "")
159 extern void cprintf(const char *format, ...);
163 * Parse the unit and subunit from an interface string such as wlXX or wlXX.YY
165 * @param ifname interface string to parse
166 * @param unit pointer to return the unit number, may pass NULL
167 * @param subunit pointer to return the subunit number, may pass NULL
168 * @return Returns 0 if the string ends with digits or digits.digits, -1 otherwise.
169 * If ifname ends in digits.digits, then unit and subuint are set
170 * to the first and second values respectively. If ifname ends
171 * in just digits, unit is set to the value, and subunit is set
172 * to -1. On error both unit and subunit are -1. NULL may be passed
173 * for unit and/or subuint to ignore the value.
175 extern int get_ifname_unit(const char* ifname, int *unit, int *subunit);
178 * Set the ip configuration index given the eth name
179 * Updates both wlXX_ipconfig_index and lanYY_ifname.
181 * @param eth_ifname pointer to eth interface name
182 * @return 0 if successful -1 if not.
184 extern int set_ipconfig_index(char *eth_ifname, int index);
187 * Get the ip configuration index if it exists given the
188 * eth name.
190 * @param wl_ifname pointer to eth interface name
191 * @return index or -1 if not found
193 extern int get_ipconfig_index(char *eth_ifname);
196 * Get interfaces belonging to a specific bridge.
198 * @param bridge_name pointer to bridge interface name
199 * @return list on interfaces beloging to the bridge
201 extern char *
202 get_bridged_interfaces(char *bridge_name);
205 remove_from_list
206 Remove the specified word from the list.
208 @param name word to be removed from the list
209 @param list List to modify
210 @param listsize Max size the list can occupy
212 @return error code
214 extern int remove_from_list(const char *name, char *list, int listsize);
217 add_to_list
218 Add the specified interface(string) to the list as long as
219 it will fit in the space left in the list.
221 @param name Name of interface to be added to the list
222 @param list List to modify
223 @param listsize Max size the list can occupy
225 @return error code
227 extern int add_to_list(const char *name, char *list, int listsize);
229 extern char *find_in_list(const char *haystack, const char *needle);
231 extern int nvifname_to_osifname(const char *nvifname, char *osifname_buf,
232 int osifname_buf_len);
233 extern int osifname_to_nvifname(const char *osifname, char *nvifname_buf,
234 int nvifname_buf_len);
236 int ure_any_enabled(void);
238 #endif /* _shutils_h_ */