Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int result = gethostname(p_buffy, size_buffy);
- int errsv = errno;
- if (result < 0)
- {
- char buf[256];
- char const * str = strerror_r(errsv, buf, 256);
- syslog(LOG_ERR,
- "gethostname failed; errno=%d(%s), buf='%s'",
- errsv,
- str,
- p_buffy);
- return errsv;
- }
- char *
- strerror (errnum)
- int errnum;
- {
- ...
- buf = malloc (1024);
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* Call strerror_r and get the full error message. Allocate memory for the
- * entire string with malloc. Return string. Caller must free string.
- * If malloc fails, return NULL.
- */
- char *all_strerror(int n)
- {
- char *s;
- size_t size;
- size = 1024;
- s = malloc(size);
- if (s == NULL)
- return NULL;
- while (strerror_r(n, s, size) == -1 && errno == ERANGE) {
- size += 2;
- s = realloc(s, size);
- if (s == NULL)
- return NULL;
- }
- return s;
- }
- int main(int argc, char **argv)
- {
- for (int i = 1; i < argc; ++i) {
- int n = atoi(argv[i]);
- char *s = all_strerror(n);
- printf("[%d]: %sn", n, s);
- free(s);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment