]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tools: Fixed parsing of SUID/SGID/sticky bits in -t and -a options.
authorJan Safranek <jsafrane@redhat.com>
Wed, 30 Nov 2011 14:42:50 +0000 (15:42 +0100)
committerJan Safranek <jsafrane@redhat.com>
Tue, 6 Dec 2011 09:42:15 +0000 (10:42 +0100)
Add support of 4-character octal numbers in parse_mode().

Signed-off-by: Jan Safranek <jsafrane@redhat.com>
src/tools/tools-common.c

index 8a739ceef44e5c6b375692f72575b8783207d333..c763ad65bd15bb34b6dacf57e15cd3c85ffcd502 100644 (file)
@@ -239,33 +239,25 @@ int cgroup_string_list_add_directory(struct cgroup_string_list *list,
 /* allowed mode strings are octal version: "755" */
 int parse_mode(char *string, mode_t *pmode, const char *program_name)
 {
-       mode_t mode = 0;
-       int pos = 0; /* position of the number iin string */
-       int i;
-       int j = 64;
-
-       while (pos < 3) {
-               if ('0' <= string[pos] && string[pos] < '8') {
-                       i = (int)string[pos] - (int)'0';
-                       /* parse the permission triple*/
-                       mode = mode + i*j;
-                       j = j / 8;
-               } else {
-                       fprintf(stdout, "%s wrong mode format %s",
-                               program_name, string);
-                       return -1;
-               }
-               pos++;
-       }
-
-       /* the string have contains three characters */
-       if (string[pos] != '\0') {
-               fprintf(stdout, "%s wrong mode format %s",
-                       program_name, string);
-               return -1;
-       }
+       char *end;
+       long mode;
+       size_t len;
+
+       len = strlen(string);
+       if (len < 3 || len > 4)
+               goto err;
+
+       errno = 0;
+       mode = strtol(string, &end, 8);
+       if (errno != 0 || *end != '\0')
+               goto err;
        *pmode = mode;
        return 0;
+
+err:
+       *pmode = 0;
+       fprintf(stdout, "%s wrong mode format %s", program_name, string);
+       return -1;
 }
 
 int parse_uid_gid(char *string, uid_t *uid, gid_t *gid,