[kernel module ps3vram] update dependencies
[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 */
13
14 #ifndef _shutils_h_
15 #define _shutils_h_
16 #include <string.h>
17 /*
18 * Reads file and returns contents
19 * @param fd file descriptor
20 * @return contents of file or NULL if an error occurred
21 */
22 extern char * fd2str(int fd);
23
24 /*
25 * Reads file and returns contents
26 * @param path path to file
27 * @return contents of file or NULL if an error occurred
28 */
29 extern char * file2str(const char *path);
30
31 /*
32 * Waits for a file descriptor to become available for reading or unblocked signal
33 * @param fd file descriptor
34 * @param timeout seconds to wait before timing out or 0 for no timeout
35 * @return 1 if descriptor changed status or 0 if timed out or -1 on error
36 */
37 extern int waitfor(int fd, int timeout);
38
39 /*
40 * Concatenates NULL-terminated list of arguments into a single
41 * commmand and executes it
42 * @param argv argument list
43 * @param path NULL, ">output", or ">>output"
44 * @param timeout seconds to wait before timing out or 0 for no timeout
45 * @param ppid NULL to wait for child termination or pointer to pid
46 * @return return value of executed command or errno
47 */
48 extern int _eval(char *const argv[], char *path, int timeout, pid_t *ppid);
49
50 /*
51 * Concatenates NULL-terminated list of arguments into a single
52 * commmand and executes it
53 * @param argv argument list
54 * @return stdout of executed command or NULL if an error occurred
55 */
56 extern char * _backtick(char *const argv[]);
57
58 /*
59 * Kills process whose PID is stored in plaintext in pidfile
60 * @param pidfile PID file
61 * @return 0 on success and errno on failure
62 */
63 extern int kill_pidfile(char *pidfile);
64
65 /*
66 * fread() with automatic retry on syscall interrupt
67 * @param ptr location to store to
68 * @param size size of each element of data
69 * @param nmemb number of elements
70 * @param stream file stream
71 * @return number of items successfully read
72 */
73 extern int safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
74
75 /*
76 * fwrite() with automatic retry on syscall interrupt
77 * @param ptr location to read from
78 * @param size size of each element of data
79 * @param nmemb number of elements
80 * @param stream file stream
81 * @return number of items successfully written
82 */
83 extern int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
84
85 /*
86 * Convert Ethernet address string representation to binary data
87 * @param a string in xx:xx:xx:xx:xx:xx notation
88 * @param e binary data
89 * @return TRUE if conversion was successful and FALSE otherwise
90 */
91 extern int ether_atoe(const char *a, unsigned char *e);
92
93 /*
94 * Convert Ethernet address binary data to string representation
95 * @param e binary data
96 * @param a string in xx:xx:xx:xx:xx:xx notation
97 * @return a
98 */
99 extern char * ether_etoa(const unsigned char *e, char *a);
100
101 /*
102 * Concatenate two strings together into a caller supplied buffer
103 * @param s1 first string
104 * @param s2 second string
105 * @param buf buffer large enough to hold both strings
106 * @return buf
107 */
108 static inline char * strcat_r(const char *s1, const char *s2, char *buf)
109 {
110 strcpy(buf, s1);
111 strcat(buf, s2);
112 return buf;
113 }
114
115 /* Check for a blank character; that is, a space or a tab */
116 #define isblank(c) ((c) == ' ' || (c) == '\t')
117
118 /* Strip trailing CR/NL from string <s> */
119 #define chomp(s) ({ \
120 char *c = (s) + strlen((s)) - 1; \
121 while ((c > (s)) && (*c == '\n' || *c == '\r' || *c == ' ')) \
122 *c-- = '\0'; \
123 s; \
124 })
125
126 /* Simple version of _backtick() */
127 #define backtick(cmd, args...) ({ \
128 char *argv[] = { cmd, ## args, NULL }; \
129 _backtick(argv); \
130 })
131
132 /* Simple version of _eval() (no timeout and wait for child termination) */
133 #define eval(cmd, args...) ({ \
134 char *argv[] = { cmd, ## args, NULL }; \
135 _eval(argv, ">/dev/console", 0, NULL); \
136 })
137
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, ' '); \
145 strlen(word); \
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, ' '))
151
152 /* Return NUL instead of NULL if undefined */
153 #define safe_getenv(s) (getenv(s) ? : "")
154
155 /* Print directly to the console */
156 #define cprintf(fmt, args...) do { \
157 FILE *fp = fopen("/dev/console", "w"); \
158 if (fp) { \
159 fprintf(fp, fmt, ## args); \
160 fclose(fp); \
161 } \
162 } while (0)
163
164 /* Debug print */
165 #ifdef DEBUG
166 #define dprintf(fmt, args...) cprintf("%s: " fmt, __FUNCTION__, ## args)
167 #else
168 #define dprintf(fmt, args...)
169 #endif
170
171 #ifdef vxworks
172
173 #include <inetLib.h>
174 #define inet_aton(a, n) ((inet_aton((a), (n)) == ERROR) ? 0 : 1)
175 #define inet_ntoa(n) ({ char a[INET_ADDR_LEN]; inet_ntoa_b ((n), a); a; })
176
177 #include <typedefs.h>
178 #include <bcmutils.h>
179 #define ether_atoe(a, e) bcm_ether_atoe((a), (e))
180 #define ether_etoa(e, a) bcm_ether_ntoa((e), (a))
181
182 /* These declarations are not available where you would expect them */
183 extern int vsnprintf (char *, size_t, const char *, va_list);
184 extern int snprintf(char *str, size_t count, const char *fmt, ...);
185 extern char *strdup(const char *);
186 extern char *strsep(char **stringp, char *delim);
187 extern int strcasecmp(const char *s1, const char *s2);
188 extern int strncasecmp(const char *s1, const char *s2, size_t n);
189
190 /* Neither are socket() and connect() */
191 #include <sockLib.h>
192
193 #ifdef DEBUG
194 #undef dprintf
195 #define dprintf printf
196 #endif
197 #endif
198
199 #endif /* _shutils_h_ */
This page took 0.064922 seconds and 5 git commands to generate.