make kernel_menuconfig work without target toolchain
[openwrt.git] / package / nvram / src / include / shutils.h
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$
13 */
14
15 #ifndef _shutils_h_
16 #define _shutils_h_
17 #include <string.h>
18 /*
19 * Reads file and returns contents
20 * @param fd file descriptor
21 * @return contents of file or NULL if an error occurred
22 */
23 extern 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 */
30 extern 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 */
38 extern 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 */
49 extern 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 */
57 extern 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 */
64 extern 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 */
74 extern 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 */
84 extern 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 */
92 extern 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 */
100 extern 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 */
109 static 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' || *c == ' ')) \
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 #ifdef vxworks
173
174 #include <inetLib.h>
175 #define inet_aton(a, n) ((inet_aton((a), (n)) == ERROR) ? 0 : 1)
176 #define inet_ntoa(n) ({ char a[INET_ADDR_LEN]; inet_ntoa_b ((n), a); a; })
177
178 #include <typedefs.h>
179 #include <bcmutils.h>
180 #define ether_atoe(a, e) bcm_ether_atoe((a), (e))
181 #define ether_etoa(e, a) bcm_ether_ntoa((e), (a))
182
183 /* These declarations are not available where you would expect them */
184 extern int vsnprintf (char *, size_t, const char *, va_list);
185 extern int snprintf(char *str, size_t count, const char *fmt, ...);
186 extern char *strdup(const char *);
187 extern char *strsep(char **stringp, char *delim);
188 extern int strcasecmp(const char *s1, const char *s2);
189 extern int strncasecmp(const char *s1, const char *s2, size_t n);
190
191 /* Neither are socket() and connect() */
192 #include <sockLib.h>
193
194 #ifdef DEBUG
195 #undef dprintf
196 #define dprintf printf
197 #endif
198 #endif
199
200 #endif /* _shutils_h_ */
This page took 0.066929 seconds and 5 git commands to generate.