diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2012-03-31 12:17:32 -0700 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2012-03-31 12:17:32 -0700 |
commit | cca4818eb7769d6e776bdc30516a5f871f1d6393 (patch) | |
tree | e67dd65d5c016028ae976b09b2d69f6b7525aa5f /upb/stdc/error.c | |
parent | 26ed1e996171c8ffa2ced42ac69b1b82c1956e1f (diff) |
Sync from internal Google development.
Diffstat (limited to 'upb/stdc/error.c')
-rw-r--r-- | upb/stdc/error.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/upb/stdc/error.c b/upb/stdc/error.c new file mode 100644 index 0000000..313866c --- /dev/null +++ b/upb/stdc/error.c @@ -0,0 +1,44 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2009 Google Inc. See LICENSE for details. + * Author: Josh Haberman <jhaberman@gmail.com> + * + * Handling of errno. + */ + +#include "upb/stdc/error.h" + +#include <errno.h> +#include <string.h> + +void upb_status_fromerrno(upb_status *status, int code) { + if (code != 0 && !upb_errno_is_wouldblock(code)) { + status->error = true; + upb_status_setcode(status, &upb_stdc_errorspace, code); + } +} + +bool upb_errno_is_wouldblock(int code) { + return +#ifdef EAGAIN + code == EAGAIN || +#endif +#ifdef EWOULDBLOCK + code == EWOULDBLOCK || +#endif + false; +} + +bool upb_stdc_codetostr(int code, char *buf, size_t len) { + // strerror() may use static buffers and is not guaranteed to be thread-safe, + // but it appears that it is not subject to buffer overflows in practice, and + // it used by other portable and high-quality software like Lua. For more + // discussion see: http://thread.gmane.org/gmane.comp.lang.lua.general/89506 + char *err = strerror(code); + if (strlen(err) >= len) return false; + strcpy(buf, err); + return true; +} + +upb_errorspace upb_stdc_errorspace = {"stdc", &upb_stdc_codetostr}; |