]> git.e2factory.org Git - e2factory.git/commitdiff
Add octal number parsing to parse_mode, sprinkle some comments
authorTobias Ulmer <tu@emlix.com>
Tue, 8 Oct 2013 12:25:07 +0000 (14:25 +0200)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:01:23 +0000 (15:01 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/le2lib.c

index 46c0fec5a8c1b19b1f55eec5afbd739b12fc488c..5a76abdcbd07477b260e42ae2cef1a7d3170ae42 100644 (file)
@@ -533,18 +533,34 @@ static int
 do_parse_mode(lua_State *lua)
 {
        const char *modestr = luaL_checkstring(lua, 1);
-       size_t len = strlen(modestr);
-       size_t pos;
-       enum { PARSE_OWNERS, PARSE_OP, PARSE_PERMS, PARSE_COMMA };
+       size_t pos, len = strlen(modestr);
 
+       /* Available states and start state */
+       enum { PARSE_OWNERS, PARSE_OP, PARSE_PERMS, PARSE_COMMA };
        int state = PARSE_OWNERS;
+
+       /* Owners, operation and protection bits for a single state pass */
        int owners = 0;
        char op = 0;
-
        mode_t protection = 0;
+
+       /* Result */
        mode_t mode = 0;
 
+       /* Deal with octal numbers and exit early on success */
+       if (len && isdigit(modestr[0])) {
+               if (sscanf(modestr, "%o", &mode) != 1) {
+                       lua_pushboolean(lua, 0);
+                       lua_pushstring(lua, "parsing octal number failed");
+
+                       return 2;
+               }
+
+               lua_pushinteger(lua, mode);
+               return 1;
+       }
 
+       /* Parse the form [ugoa][+|-|=][rwxX][,]...[\0] */
        for (pos = 0; pos <= len; pos++) {
                switch (state) {
                case PARSE_OWNERS:
@@ -616,6 +632,7 @@ do_parse_mode(lua_State *lua)
 
                        break;
                case PARSE_COMMA:
+                       /* Update mode and go back to start state */
                        mode = calc_mode(mode, owners, op, protection);
                        state = PARSE_OWNERS;
                        owners = op = protection = 0;