summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarel Klic <kklic@redhat.com>2010-04-29 15:09:34 +0200
committerKarel Klic <kklic@redhat.com>2010-04-29 15:09:34 +0200
commit1d4051836f620499d7c3f1e8e2caf4c022e74b81 (patch)
treeb08cc99b5b3ab88391781e7590102f34e5bca937 /lib
parent6b0f5859b291a2b4a09764000a3e33d770d8070d (diff)
downloadabrt-1d4051836f620499d7c3f1e8e2caf4c022e74b81.tar.gz
abrt-1d4051836f620499d7c3f1e8e2caf4c022e74b81.tar.xz
abrt-1d4051836f620499d7c3f1e8e2caf4c022e74b81.zip
Source code formatting only.
Diffstat (limited to 'lib')
-rw-r--r--lib/Utils/xfuncs.cpp325
1 files changed, 164 insertions, 161 deletions
diff --git a/lib/Utils/xfuncs.cpp b/lib/Utils/xfuncs.cpp
index fd29b704..53c10f9a 100644
--- a/lib/Utils/xfuncs.cpp
+++ b/lib/Utils/xfuncs.cpp
@@ -27,26 +27,26 @@
/* Turn on nonblocking I/O on a fd */
int ndelay_on(int fd)
{
- return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+ return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
}
int ndelay_off(int fd)
{
- return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
+ return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
}
int close_on_exec_on(int fd)
{
- return fcntl(fd, F_SETFD, FD_CLOEXEC);
+ return fcntl(fd, F_SETFD, FD_CLOEXEC);
}
// Die if we can't allocate size bytes of memory.
void* xmalloc(size_t size)
{
- void *ptr = malloc(size);
- if (ptr == NULL && size != 0)
- die_out_of_memory();
- return ptr;
+ void *ptr = malloc(size);
+ if (ptr == NULL && size != 0)
+ die_out_of_memory();
+ return ptr;
}
// Die if we can't resize previously allocated memory. (This returns a pointer
@@ -54,271 +54,273 @@ void* xmalloc(size_t size)
// It'll copy the contents to a new chunk and free the old one if necessary.)
void* xrealloc(void *ptr, size_t size)
{
- ptr = realloc(ptr, size);
- if (ptr == NULL && size != 0)
- die_out_of_memory();
- return ptr;
+ ptr = realloc(ptr, size);
+ if (ptr == NULL && size != 0)
+ die_out_of_memory();
+ return ptr;
}
// Die if we can't allocate and zero size bytes of memory.
void* xzalloc(size_t size)
{
- void *ptr = xmalloc(size);
- memset(ptr, 0, size);
- return ptr;
+ void *ptr = xmalloc(size);
+ memset(ptr, 0, size);
+ return ptr;
}
// Die if we can't copy a string to freshly allocated memory.
char* xstrdup(const char *s)
{
- char *t;
+ char *t;
+ if (s == NULL)
+ return NULL;
- if (s == NULL)
- return NULL;
+ t = strdup(s);
- t = strdup(s);
+ if (t == NULL)
+ die_out_of_memory();
- if (t == NULL)
- die_out_of_memory();
-
- return t;
+ return t;
}
// Die if we can't allocate n+1 bytes (space for the null terminator) and copy
// the (possibly truncated to length n) string into it.
char* xstrndup(const char *s, int n)
{
- int m;
- char *t;
+ int m;
+ char *t;
- /* We can just xmalloc(n+1) and strncpy into it, */
- /* but think about xstrndup("abc", 10000) wastage! */
- m = n;
- t = (char*) s;
- while (m) {
- if (!*t) break;
- m--;
- t++;
- }
- n -= m;
- t = (char*) xmalloc(n + 1);
- t[n] = '\0';
+ /* We can just xmalloc(n+1) and strncpy into it, */
+ /* but think about xstrndup("abc", 10000) wastage! */
+ m = n;
+ t = (char*) s;
+ while (m)
+ {
+ if (!*t) break;
+ m--;
+ t++;
+ }
+ n -= m;
+ t = (char*) xmalloc(n + 1);
+ t[n] = '\0';
- return (char*) memcpy(t, s, n);
+ return (char*) memcpy(t, s, n);
}
void xpipe(int filedes[2])
{
- if (pipe(filedes))
- perror_msg_and_die("can't create pipe");
+ if (pipe(filedes))
+ perror_msg_and_die("can't create pipe");
}
void xdup(int from)
{
- if (dup(from) < 0)
- perror_msg_and_die("can't duplicate file descriptor");
+ if (dup(from) < 0)
+ perror_msg_and_die("can't duplicate file descriptor");
}
void xdup2(int from, int to)
{
- if (dup2(from, to) != to)
- perror_msg_and_die("can't duplicate file descriptor");
+ if (dup2(from, to) != to)
+ perror_msg_and_die("can't duplicate file descriptor");
}
// "Renumber" opened fd
void xmove_fd(int from, int to)
{
- if (from == to)
- return;
- xdup2(from, to);
- close(from);
+ if (from == to)
+ return;
+ xdup2(from, to);
+ close(from);
}
// Die with an error message if we can't write the entire buffer.
void xwrite(int fd, const void *buf, size_t count)
{
- if (count) {
- ssize_t size = full_write(fd, buf, count);
- if ((size_t)size != count)
- error_msg_and_die("short write");
- }
+ if (count == 0)
+ return;
+ ssize_t size = full_write(fd, buf, count);
+ if ((size_t)size != count)
+ error_msg_and_die("short write");
}
+
void xwrite_str(int fd, const char *str)
{
- xwrite(fd, str, strlen(str));
+ xwrite(fd, str, strlen(str));
}
// Die with an error message if we can't lseek to the right spot.
off_t xlseek(int fd, off_t offset, int whence)
{
- off_t off = lseek(fd, offset, whence);
- if (off == (off_t)-1) {
- if (whence == SEEK_SET)
- perror_msg_and_die("lseek(%llu)", (long long)offset);
- perror_msg_and_die("lseek");
- }
- return off;
+ off_t off = lseek(fd, offset, whence);
+ if (off == (off_t)-1) {
+ if (whence == SEEK_SET)
+ perror_msg_and_die("lseek(%llu)", (long long)offset);
+ perror_msg_and_die("lseek");
+ }
+ return off;
}
void xchdir(const char *path)
{
- if (chdir(path))
- perror_msg_and_die("chdir(%s)", path);
+ if (chdir(path))
+ perror_msg_and_die("chdir(%s)", path);
}
char* xvasprintf(const char *format, va_list p)
{
- int r;
- char *string_ptr;
+ int r;
+ char *string_ptr;
#if 1
- // GNU extension
- r = vasprintf(&string_ptr, format, p);
+ // GNU extension
+ r = vasprintf(&string_ptr, format, p);
#else
- // Bloat for systems that haven't got the GNU extension.
- va_list p2;
- va_copy(p2, p);
- r = vsnprintf(NULL, 0, format, p);
- string_ptr = xmalloc(r+1);
- r = vsnprintf(string_ptr, r+1, format, p2);
- va_end(p2);
+ // Bloat for systems that haven't got the GNU extension.
+ va_list p2;
+ va_copy(p2, p);
+ r = vsnprintf(NULL, 0, format, p);
+ string_ptr = xmalloc(r+1);
+ r = vsnprintf(string_ptr, r+1, format, p2);
+ va_end(p2);
#endif
- if (r < 0)
- die_out_of_memory();
- return string_ptr;
+ if (r < 0)
+ die_out_of_memory();
+ return string_ptr;
}
// Die with an error message if we can't malloc() enough space and do an
// sprintf() into that space.
char* xasprintf(const char *format, ...)
{
- va_list p;
- char *string_ptr;
+ va_list p;
+ char *string_ptr;
- va_start(p, format);
- string_ptr = xvasprintf(format, p);
- va_end(p);
+ va_start(p, format);
+ string_ptr = xvasprintf(format, p);
+ va_end(p);
- return string_ptr;
+ return string_ptr;
}
std::string ssprintf(const char *format, ...)
{
- va_list p;
- char *string_ptr;
+ va_list p;
+ char *string_ptr;
- va_start(p, format);
- string_ptr = xvasprintf(format, p);
- va_end(p);
+ va_start(p, format);
+ string_ptr = xvasprintf(format, p);
+ va_end(p);
- std::string res = string_ptr;
- free(string_ptr);
- return res;
+ std::string res = string_ptr;
+ free(string_ptr);
+ return res;
}
void xsetenv(const char *key, const char *value)
{
- if (setenv(key, value, 1))
- die_out_of_memory();
+ if (setenv(key, value, 1))
+ die_out_of_memory();
}
// Die with an error message if we can't open a new socket.
int xsocket(int domain, int type, int protocol)
{
- int r = socket(domain, type, protocol);
-
- if (r < 0) {
- const char *s = "INET";
- if (domain == AF_PACKET) s = "PACKET";
- if (domain == AF_NETLINK) s = "NETLINK";
- if (domain == AF_INET6) s = "INET6";
- perror_msg_and_die("socket(AF_%s)", s);
- }
+ int r = socket(domain, type, protocol);
+ if (r < 0)
+ {
+ const char *s = "INET";
+ if (domain == AF_PACKET) s = "PACKET";
+ if (domain == AF_NETLINK) s = "NETLINK";
+ if (domain == AF_INET6) s = "INET6";
+ perror_msg_and_die("socket(AF_%s)", s);
+ }
- return r;
+ return r;
}
// Die with an error message if we can't bind a socket to an address.
void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
{
- if (bind(sockfd, my_addr, addrlen)) perror_msg_and_die("bind");
+ if (bind(sockfd, my_addr, addrlen))
+ perror_msg_and_die("bind");
}
// Die with an error message if we can't listen for connections on a socket.
void xlisten(int s, int backlog)
{
- if (listen(s, backlog)) perror_msg_and_die("listen");
+ if (listen(s, backlog))
+ perror_msg_and_die("listen");
}
// Die with an error message if sendto failed.
// Return bytes sent otherwise
-ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
- socklen_t tolen)
+ssize_t xsendto(int s, const void *buf, size_t len,
+ const struct sockaddr *to,
+ socklen_t tolen)
{
- ssize_t ret = sendto(s, buf, len, 0, to, tolen);
- if (ret < 0) {
- close(s);
- perror_msg_and_die("sendto");
- }
- return ret;
+ ssize_t ret = sendto(s, buf, len, 0, to, tolen);
+ if (ret < 0)
+ {
+ close(s);
+ perror_msg_and_die("sendto");
+ }
+ return ret;
}
// xstat() - a stat() which dies on failure with meaningful error message
void xstat(const char *name, struct stat *stat_buf)
{
- if (stat(name, stat_buf))
- perror_msg_and_die("can't stat '%s'", name);
+ if (stat(name, stat_buf))
+ perror_msg_and_die("can't stat '%s'", name);
}
std::string get_home_dir(uid_t uid)
{
struct passwd* pw = getpwuid(uid);
+ // TODO: handle errno
return pw ? pw->pw_dir : "";
}
// Die if we can't open a file and return a fd
int xopen3(const char *pathname, int flags, int mode)
{
- int ret;
-
- ret = open(pathname, flags, mode);
- if (ret < 0) {
- perror_msg_and_die("can't open '%s'", pathname);
- }
- return ret;
+ int ret;
+ ret = open(pathname, flags, mode);
+ if (ret < 0)
+ perror_msg_and_die("can't open '%s'", pathname);
+ return ret;
}
// Die if we can't open an existing file and return a fd
int xopen(const char *pathname, int flags)
{
- return xopen3(pathname, flags, 0666);
+ return xopen3(pathname, flags, 0666);
}
#if 0 //UNUSED
// Warn if we can't open a file and return a fd.
int open3_or_warn(const char *pathname, int flags, int mode)
{
- int ret;
-
- ret = open(pathname, flags, mode);
- if (ret < 0) {
- perror_msg("can't open '%s'", pathname);
- }
- return ret;
+ int ret;
+ ret = open(pathname, flags, mode);
+ if (ret < 0)
+ perror_msg("can't open '%s'", pathname);
+ return ret;
}
// Warn if we can't open a file and return a fd.
int open_or_warn(const char *pathname, int flags)
{
- return open3_or_warn(pathname, flags, 0666);
+ return open3_or_warn(pathname, flags, 0666);
}
#endif
void xunlink(const char *pathname)
{
- if (unlink(pathname))
- perror_msg_and_die("can't remove file '%s'", pathname);
+ if (unlink(pathname))
+ perror_msg_and_die("can't remove file '%s'", pathname);
}
/* Just testing dent->d_type == DT_REG is wrong: some filesystems
@@ -327,17 +329,17 @@ void xunlink(const char *pathname)
*/
int is_regular_file(struct dirent *dent, const char *dirname)
{
- if (dent->d_type == DT_REG)
- return 1;
- if (dent->d_type != DT_UNKNOWN)
- return 0;
+ if (dent->d_type == DT_REG)
+ return 1;
+ if (dent->d_type != DT_UNKNOWN)
+ return 0;
- char *fullname = xasprintf("%s/%s", dirname, dent->d_name);
- struct stat statbuf;
- int r = lstat(fullname, &statbuf);
- free(fullname);
+ char *fullname = xasprintf("%s/%s", dirname, dent->d_name);
+ struct stat statbuf;
+ int r = lstat(fullname, &statbuf);
+ free(fullname);
- return r == 0 && S_ISREG(statbuf.st_mode);
+ return r == 0 && S_ISREG(statbuf.st_mode);
}
/* Is it "." or ".."? */
@@ -356,35 +358,36 @@ bool dot_or_dotdot(const char *filename)
*/
char *last_char_is(const char *s, int c)
{
- if (s && *s) {
- s += strlen(s) - 1;
- if ((unsigned char)*s == c)
- return (char*)s;
- }
- return NULL;
+ if (s && *s)
+ {
+ s += strlen(s) - 1;
+ if ((unsigned char)*s == c)
+ return (char*)s;
+ }
+ return NULL;
}
std::string concat_path_file(const char *path, const char *filename)
{
- char *lc;
+ char *lc;
- while (*filename == '/')
- filename++;
- lc = last_char_is(path, '/');
- return ssprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
+ while (*filename == '/')
+ filename++;
+ lc = last_char_is(path, '/');
+ return ssprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
}
bool string_to_bool(const char *s)
{
- if (s[0] == '1' && s[1] == '\0')
- return true;
- if (strcasecmp(s, "on") == 0)
- return true;
- if (strcasecmp(s, "yes") == 0)
- return true;
- if (strcasecmp(s, "true") == 0)
- return true;
- return false;
+ if (s[0] == '1' && s[1] == '\0')
+ return true;
+ if (strcasecmp(s, "on") == 0)
+ return true;
+ if (strcasecmp(s, "yes") == 0)
+ return true;
+ if (strcasecmp(s, "true") == 0)
+ return true;
+ return false;
}
void xsetreuid(uid_t ruid, uid_t euid)