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.
18 * Reads file and returns contents
19 * @param fd file descriptor
20 * @return contents of file or NULL if an error occurred
22 extern char * fd2str(int fd
);
25 * Reads file and returns contents
26 * @param path path to file
27 * @return contents of file or NULL if an error occurred
29 extern char * file2str(const char *path
);
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
37 extern int waitfor(int fd
, int timeout
);
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
48 extern int _eval(char *const argv
[], char *path
, int timeout
, pid_t
*ppid
);
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
56 extern char * _backtick(char *const argv
[]);
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
63 extern int kill_pidfile(char *pidfile
);
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
73 extern int safe_fread(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
);
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
83 extern int safe_fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE *stream
);
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
91 extern int ether_atoe(const char *a
, unsigned char *e
);
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
99 extern char * ether_etoa(const unsigned char *e
, char *a
);
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
108 static inline char * strcat_r(const char *s1
, const char *s2
, char *buf
)
115 /* Check for a blank character; that is, a space or a tab */
116 #define isblank(c) ((c) == ' ' || (c) == '\t')
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 == ' ')) \
126 /* Simple version of _backtick() */
127 #define backtick(cmd, args...) ({ \
128 char *argv[] = { cmd, ## args, NULL }; \
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); \
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, ' '); \
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, ' '))
152 /* Return NUL instead of NULL if undefined */
153 #define safe_getenv(s) (getenv(s) ? : "")
155 /* Print directly to the console */
156 #define cprintf(fmt, args...) do { \
157 FILE *fp = fopen("/dev/console", "w"); \
159 fprintf(fp, fmt, ## args); \
166 #define dprintf(fmt, args...) cprintf("%s: " fmt, __FUNCTION__, ## args)
168 #define dprintf(fmt, args...)
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; })
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))
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
);
190 /* Neither are socket() and connect() */
195 #define dprintf printf
199 #endif /* _shutils_h_ */
This page took 0.064922 seconds and 5 git commands to generate.