2 * Shell-like utility functions
4 * Copyright 2004, 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.
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
[], 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 /* 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 == ' ')) \
127 /* Simple version of _backtick() */
128 #define backtick(cmd, args...) ({ \
129 char *argv[] = { cmd, ## args, NULL }; \
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); \
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, ' '); \
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, ' '))
153 /* Return NUL instead of NULL if undefined */
154 #define safe_getenv(s) (getenv(s) ? : "")
156 /* Print directly to the console */
157 #define cprintf(fmt, args...) do { \
158 FILE *fp = fopen("/dev/console", "w"); \
160 fprintf(fp, fmt, ## args); \
167 #define dprintf(fmt, args...) cprintf("%s: " fmt, __FUNCTION__, ## args)
169 #define dprintf(fmt, args...)
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; })
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))
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
);
191 /* Neither are socket() and connect() */
196 #define dprintf printf
200 #endif /* _shutils_h_ */
This page took 0.049837 seconds and 5 git commands to generate.