]> granicus.if.org Git - strace/blobdiff - pathtrace.c
Improve code readability by avoiding assignments inside if()
[strace] / pathtrace.c
index bcab4bb64675c489b7cb5d0ed4ee41348b4c3684..b2efd336eb69a4ba4dc756df018170e7b200f514 100644 (file)
@@ -40,8 +40,6 @@
 
 #include "syscall.h"
 
-#define NumElem(a)  ((int)((sizeof (a))/((sizeof (a)[0]))))
-
 #define MAXSELECTED  256       /* max number of "selected" paths */
 static const char *selected[MAXSELECTED];      /* paths selected for tracing */
 
@@ -51,10 +49,9 @@ static const char *selected[MAXSELECTED];    /* paths selected for tracing */
 static int
 pathmatch(const char *path)
 {
-       int     i;
+       unsigned int i;
 
-       for (i = 0; i < NumElem(selected); ++i)
-       {
+       for (i = 0; i < ARRAY_SIZE(selected); ++i) {
                if (selected[i] == NULL)
                        return 0;
                if (!strcmp(path, selected[i]))
@@ -69,7 +66,7 @@ pathmatch(const char *path)
 static int
 upathmatch(struct tcb *tcp, unsigned long upath)
 {
-       char    path[PATH_MAX + 1];
+       char path[PATH_MAX + 1];
 
        return umovestr(tcp, upath, sizeof path, path) == 0 &&
                pathmatch(path);
@@ -93,28 +90,25 @@ fdmatch(struct tcb *tcp, int fd)
 static int
 storepath(const char *path)
 {
-       int     i;
+       unsigned int i;
 
-       if (path == NULL)
-       {
-               for (i = 0; i < NumElem(selected); ++i)
-                       if (selected[i])
-                       {
+       if (path == NULL) {
+               for (i = 0; i < ARRAY_SIZE(selected); ++i)
+                       if (selected[i]) {
                                free((char *) selected[i]);
                                selected[i] = NULL;
                        }
                return 0;
        }
 
-       for (i = 0; i < NumElem(selected); ++i)
-               if (!selected[i])
-               {
+       for (i = 0; i < ARRAY_SIZE(selected); ++i)
+               if (!selected[i]) {
                        selected[i] = path;
                        return 0;
                }
 
-       fprintf(stderr, "Max trace paths exceeded, only using first %d\n",
-               NumElem(selected));
+       fprintf(stderr, "Max trace paths exceeded, only using first %u\n",
+               (unsigned int) ARRAY_SIZE(selected));
        return -1;
 }
 
@@ -163,8 +157,7 @@ pathtrace_select(const char *path)
                return 0;
 
        /* if realpath and specified path are same, we're done */
-       if (!strcmp(path, rpath))
-       {
+       if (!strcmp(path, rpath)) {
                free(rpath);
                return 0;
        }
@@ -202,7 +195,7 @@ pathtrace_match(struct tcb *tcp)
            s->sys_func == sys_dup3 ||
            s->sys_func == sys_sendfile ||
            s->sys_func == sys_sendfile64 ||
-            !strcmp(s->sys_name, "tee"))
+           !strcmp(s->sys_name, "tee"))
        {
                /* fd, fd */
                return fdmatch(tcp, tcp->u_arg[0]) ||
@@ -221,7 +214,7 @@ pathtrace_match(struct tcb *tcp)
            s->sys_func == sys_readlinkat ||
            s->sys_func == sys_utimensat ||
            s->sys_func == sys_fchownat ||
-            s->sys_func == sys_pipe2)
+           s->sys_func == sys_pipe2)
        {
                /* fd, path */
                return fdmatch(tcp, tcp->u_arg[0]) ||
@@ -232,7 +225,7 @@ pathtrace_match(struct tcb *tcp)
            s->sys_func == sys_pivotroot ||
            s->sys_func == sys_rename ||
            s->sys_func == sys_symlink ||
-            s->sys_func == sys_mount)
+           s->sys_func == sys_mount)
        {
                /* path, path */
                return upathmatch(tcp, tcp->u_arg[0]) ||
@@ -240,7 +233,7 @@ pathtrace_match(struct tcb *tcp)
        }
 
        if (s->sys_func == sys_renameat ||
-            s->sys_func == sys_linkat)
+           s->sys_func == sys_linkat)
        {
                /* fd, path, fd, path */
                return fdmatch(tcp, tcp->u_arg[0]) ||
@@ -249,44 +242,39 @@ pathtrace_match(struct tcb *tcp)
                        upathmatch(tcp, tcp->u_arg[3]);
        }
 
-       if (s->sys_func == sys_old_mmap || s->sys_func == sys_mmap)
-       {
+       if (s->sys_func == sys_old_mmap || s->sys_func == sys_mmap) {
                /* x, x, x, x, fd */
                return fdmatch(tcp, tcp->u_arg[4]);
        }
 
-       if (s->sys_func == sys_symlinkat)
-       {
+       if (s->sys_func == sys_symlinkat) {
                /* path, fd, path */
                return fdmatch(tcp, tcp->u_arg[1]) ||
                        upathmatch(tcp, tcp->u_arg[0]) ||
-                        upathmatch(tcp, tcp->u_arg[2]);
+                       upathmatch(tcp, tcp->u_arg[2]);
        }
 
-       if (!strcmp(s->sys_name, "splice"))
-       {
+       if (!strcmp(s->sys_name, "splice")) {
                /* fd, x, fd, x, x */
                return fdmatch(tcp, tcp->u_arg[0]) ||
                        fdmatch(tcp, tcp->u_arg[2]);
        }
 
-       if (s->sys_func == sys_epoll_ctl)
-       {
+       if (s->sys_func == sys_epoll_ctl) {
                /* x, x, fd, x */
                return fdmatch(tcp, tcp->u_arg[2]);
        }
 
        if (s->sys_func == sys_select ||
            s->sys_func == sys_oldselect ||
-            s->sys_func == sys_pselect6)
+           s->sys_func == sys_pselect6)
        {
                int     i, j, nfds;
                long   *args, oldargs[5];
                unsigned fdsize;
                fd_set *fds;
 
-               if (s->sys_func == sys_oldselect)
-               {
+               if (s->sys_func == sys_oldselect) {
                        if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
                                   (char*) oldargs) < 0)
                        {
@@ -302,26 +290,22 @@ pathtrace_match(struct tcb *tcp)
                          & -sizeof(long));
                fds = malloc(fdsize);
 
-               if (fds == NULL)
-               {
+               if (fds == NULL) {
                        fprintf(stderr, "out of memory\n");
                        return 0;
                }
 
-               for (i = 1; i <= 3; ++i)
-               {
+               for (i = 1; i <= 3; ++i) {
                        if (args[i] == 0)
                                continue;
 
-                       if (umoven(tcp, args[i], fdsize, (char *) fds) < 0)
-                       {
+                       if (umoven(tcp, args[i], fdsize, (char *) fds) < 0) {
                                fprintf(stderr, "umoven() failed\n");
                                continue;
                        }
 
                        for (j = 0; j < nfds; ++j)
-                               if (FD_ISSET(j, fds) && fdmatch(tcp, j))
-                               {
+                               if (FD_ISSET(j, fds) && fdmatch(tcp, j)) {
                                        free(fds);
                                        return 1;
                                }
@@ -331,7 +315,7 @@ pathtrace_match(struct tcb *tcp)
        }
 
        if (s->sys_func == sys_poll ||
-            s->sys_func == sys_ppoll)
+           s->sys_func == sys_ppoll)
        {
                struct pollfd fds;
                unsigned nfds;