setcifsacl: clean up parse_cmdline_aces
authorJeff Layton <jlayton@samba.org>
Wed, 7 Nov 2012 15:19:17 +0000 (10:19 -0500)
committerJeff Layton <jlayton@samba.org>
Wed, 7 Nov 2012 15:19:17 +0000 (10:19 -0500)
One of the reasons to use "goto" in an error condition is to eliminate
unnecessary indentation. Fix that here by revering some error checks
end getting rid of some unneeded "else" cases.

After using strstr() to find "ACL:", there's no need to then use
strchr() to find ':'. We know where it is -- it's 3 bytes past the
current position.

Finally, there's no need to copy these strings into new buffers,
just set the pointers in the array to their original values.

Signed-off-by: Jeff Layton <jlayton@samba.org>
setcifsacl.c

index 522731f55136bc0d0566e819ea0f092f89847882..d72d2d2b6b26db8e83783e4538d2e94f80cc7356 100644 (file)
@@ -655,48 +655,36 @@ build_cmdline_aces_ret:
 }
 
 static char **
-parse_cmdline_aces(char *optarg, int numcaces)
+parse_cmdline_aces(char *acelist, int numcaces)
 {
        int i = 0, len;
        char *acestr, *vacestr, **arrptr = NULL;
 
-       errno = EINVAL;
        arrptr = (char **)malloc(numcaces * sizeof(char *));
        if (!arrptr) {
-               printf("%s: Error %d allocating char array\n", __func__, errno);
+               printf("%s: Unable to allocate char array\n", __func__);
                return NULL;
        }
 
        while (i < numcaces) {
-               acestr = strtok(optarg, ","); /* everything before , */
-               if (acestr) {
-                       vacestr = strstr(acestr, "ACL:"); /* ace as ACL:*" */
-                       if (vacestr) {
-                               vacestr = strchr(vacestr, ':');
-                               if (vacestr)
-                                       ++vacestr; /* go past : */
-                               if (vacestr) {
-                                       len = strlen(vacestr);
-                                       arrptr[i] = malloc(len + 1);
-                                       if (!arrptr[i])
-                                               goto parse_cmdline_aces_ret;
-                                       strcpy(arrptr[i], vacestr);
-                                       ++i;
-                               } else
-                                       goto parse_cmdline_aces_ret;
-                       } else
-                               goto parse_cmdline_aces_ret;
-               } else
-                       goto parse_cmdline_aces_ret;
-               optarg = NULL;
-       }
-       errno = 0;
+               acestr = strtok(acelist, ","); /* everything before , */
+               if (!acestr)
+                       goto parse_cmdline_aces_err;
+
+               vacestr = strstr(acestr, "ACL:"); /* ace as ACL:*" */
+               if (!vacestr)
+                       goto parse_cmdline_aces_err;
+               vacestr += 4; /* skip past "ACL:" */
+               if (*vacestr) {
+                       arrptr[i] = vacestr;
+                       ++i;
+               }
+               acelist = NULL;
+       }
        return arrptr;
 
-parse_cmdline_aces_ret:
-       printf("%s: Error %d parsing ACEs\n", __func__, errno);
-       for (;  i >= 0; --i)
-               free(arrptr[i]);
+parse_cmdline_aces_err:
+       printf("%s: Error parsing ACEs\n", __func__);
        free(arrptr);
        return NULL;
 }
@@ -908,8 +896,6 @@ setcifsacl_cmdlineverify_ret:
        free(cacesptr);
 
 setcifsacl_cmdlineparse_ret:
-       for (i = 0; i < numcaces; ++i)
-               free(arrptr[i]);
        free(arrptr);
 
 setcifsacl_numcaces_ret: