Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2001-04-05
07:13 • Closed ticket [217946ffff]: permissions patch plus 3 other changes artifact: 514626d984 user: dgp
07:12 • Open ticket [217946ffff]. artifact: 7ba4e85306 user: dgp
2000-10-26
08:10 • Closed ticket [217946ffff]. artifact: ccbfc41679 user: welch
00:46 • New ticket [217946ffff]. artifact: 907f95148f user: nobody

permissions patch

Closed / Fixed217946ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:46:28
OriginalBugID: 3907 RFE
Version: 8.3b1
SubmitDate: '1999-12-20'
LastModified: '2000-01-04'
Severity: LOW
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 3970
OS: Linux
OSVersion: 2.0.38
Machine: P II, 64MB, 300 MHz
FixedDate: '2000-10-25'
ClosedDate: '2000-01-04'


Name:
Ulrich Schöbel

CVS:
tclCmdAH.c,  checked out Dec 13 01:40

Extensions:
BLT2.4m, oratcl2.5, Img??

CustomShell:
Patch introduces "file permissions" subcommand.

DesiredBehavior:
Some time ago I posted a patch to clt to introduce a
"file permissions" subcommand.

This subcommand understood the usual octal permissions
as well as a permission string in "rwxrwxrwx" format.

I now extended it to understand the chmod syntax
ugoa+-=rwxst?,ugoa+...?.

Maybe it's useful enough (and not too late) to be included
in 8.3.

I created this patch against tclCmdAH.c, tcl8.3, checked out
from cvs Dec 13 01:40.

Patch:
*** tclCmdAH.c.orig     Wed Dec 15 00:49:24 1999
--- tclCmdAH.c  Wed Dec 15 01:08:07 1999
***************
*** 792,815 ****

      static char *fileOptions[] = {
        "atime",        "attributes",   "channels",     "copy",
!       "delete",
!       "dirname",      "executable",   "exists",       "extension",
!       "isdirectory",  "isfile",       "join",         "lstat",
!       "mtime",        "mkdir",        "nativename",   "owned",
!       "pathtype",     "readable",     "readlink",     "rename",
!       "rootname",     "size",         "split",        "stat",
!       "tail",         "type",         "volumes",      "writable",
        (char *) NULL
      };
      enum options {
        FILE_ATIME,     FILE_ATTRIBUTES, FILE_CHANNELS, FILE_COPY,
!       FILE_DELETE,
!       FILE_DIRNAME,   FILE_EXECUTABLE, FILE_EXISTS,   FILE_EXTENSION,
!       FILE_ISDIRECTORY, FILE_ISFILE,  FILE_JOIN,      FILE_LSTAT,
!       FILE_MTIME,     FILE_MKDIR,     FILE_NATIVENAME, FILE_OWNED,
!       FILE_PATHTYPE,  FILE_READABLE,  FILE_READLINK,  FILE_RENAME,
!       FILE_ROOTNAME,  FILE_SIZE,      FILE_SPLIT,     FILE_STAT,
!       FILE_TAIL,      FILE_TYPE,      FILE_VOLUMES,   FILE_WRITABLE
      };

      if (objc < 2) {
--- 792,815 ----

      static char *fileOptions[] = {
        "atime",        "attributes",   "channels",     "copy",
!       "delete",       "dirname",      "executable",   "exists",
!       "extension",    "isdirectory",  "isfile",       "join",
!       "lstat",        "mtime",        "mkdir",        "nativename",
!       "owned",        "pathtype",     "permissions",  "readable",
!       "readlink",     "rename",       "rootname",     "size",
!       "split",        "stat",         "tail",         "type",
!       "volumes",      "writable",
        (char *) NULL
      };
      enum options {
        FILE_ATIME,     FILE_ATTRIBUTES, FILE_CHANNELS, FILE_COPY,
!       FILE_DELETE,    FILE_DIRNAME,   FILE_EXECUTABLE, FILE_EXISTS,
!       FILE_EXTENSION, FILE_ISDIRECTORY, FILE_ISFILE,  FILE_JOIN,
!       FILE_LSTAT,     FILE_MTIME,     FILE_MKDIR,     FILE_NATIVENAME,
!       FILE_OWNED,     FILE_PATHTYPE,  FILE_PERMISSIONS, FILE_READABLE,
!       FILE_READLINK,  FILE_RENAME,    FILE_ROOTNAME,  FILE_SIZE,
!       FILE_SPLIT,     FILE_STAT,      FILE_TAIL,      FILE_TYPE,
!       FILE_VOLUMES,   FILE_WRITABLE
      };

      if (objc < 2) {
***************
*** 1122,1127 ****
--- 1122,1186 ----
            }
            return TCL_OK;
        }
+         case FILE_PERMISSIONS: {
+           char *fileName;
+           struct stat buf;
+             char returnString[6];
+             long modeInt;
+             mode_t newMode;
+
+             if ((objc < 3)||(objc > 4)) {
+             Tcl_WrongNumArgs(interp, 2, objv, "name ?permissions?");
+             return TCL_ERROR;
+             }
+           fileName = Tcl_GetString(objv[2]);
+           if (GetStatBuf(interp, objv[2], TclStat, &buf) != TCL_OK) {
+               return TCL_ERROR;
+           }
+             newMode = (mode_t) (buf.st_mode & 0x00000FFF);
+             if (objc == 4) {
+                 if (Tcl_GetLongFromObj(interp, objv[3], &modeInt) == TCL_OK) {
+                     /* First try if last arg is a number */
+                     newMode = (mode_t) (modeInt & 0x00000FFF);
+                 } else if (Tcl_GetModeFromPermString(interp,
+                              Tcl_GetStringFromObj(objv[3], NULL), &newMode)
+                              == TCL_OK) {
+                     /* Second try if last arg is of form "rwxrwxrwx" */
+                 } else if (Tcl_GetModeFromChmodString(interp,
+                              Tcl_GetStringFromObj(objv[3], NULL),
+                              &newMode)
+                              == TCL_OK) {
+                     /* Last try if last arg is of form "ugo=rwx" */
+                 } else {
+                       Tcl_ResetResult(interp);
+                       Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
+                                       "format error in modestring \"",
+                                       Tcl_GetStringFromObj(objv[3], NULL),
+                                       "\"", (char *) NULL);
+                       return TCL_ERROR;
+                 }
+                 if (chmod(fileName, newMode) != 0) {
+                     Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
+                             "could not set permissions for file \"",
+                             fileName, "\": ",
+                             Tcl_PosixError(interp), (char *) NULL);
+                     return TCL_ERROR;
+                 }
+             }
+             /* Always return the *original* permissions */
+             /* This point should be discussed:
+                I think, it´s more convenient and more logical,
+                but also more confusing to novices.
+                If you prefer "file permissions" to return
+                the new settings, uncomment the following statement
+                and comment the next. */
+             /*
+             sprintf(returnString, "%0#5lo", newMode);
+             */
+             sprintf(returnString, "%0#5lo", (buf.st_mode & 0x00000FFF));
+             Tcl_SetStringObj(resultPtr, returnString, -1);
+             return TCL_OK;
+             }
        case FILE_READABLE: {
            if (objc != 3) {
                goto only3Args;
***************
*** 2344,2349 ****
--- 2403,2620 ----
      }
      Tcl_DecrRefCount(resultPtr);
      return TCL_ERROR;
+ }
+
+ ^L
+ /*
+  *----------------------------------------------------------------------
+  *
+  * Tcl_GetModeFromPermString --
+  *
+  *    This procedure is invoked to process the "file permissions"
+  *      Tcl command if invoked with a "rwxrwxrwx"-like string.
+  *    See the user documentation for details on what it does.
+  *
+  * Results:
+  *    A standard Tcl result.
+  *
+  * Side effects:
+  *    See the user documentation.
+  *
+  *----------------------------------------------------------------------
+  */
+
+ int
+ Tcl_GetModeFromPermString(interp, modeStringPtr, modePtr)
+     Tcl_Interp *interp;             /* The interp we are using for errors. */
+     char *modeStringPtr;            /* Ptr to the "rwxrwxrwx" String */
+     mode_t *modePtr;                /* Ptr to the mode value */
+ {
+     int i;
+
+     if (strlen(modeStringPtr) != 9) {
+         return TCL_ERROR;
+     }
+
+     *modePtr = (mode_t) 0;
+     for (i=0; i<9; i++) {
+          switch (*(modeStringPtr+i)) {
+              case 'r':
+                  if ((i%3) != 0) {
+                      return TCL_ERROR;
+                  }
+                  *modePtr |= (1<<(8-i));
+                  break;
+              case 'w':
+                  if ((i%3) != 1) {
+                      return TCL_ERROR;
+                  }
+                  *modePtr |= (1<<(8-i));
+                  break;
+              case 'x':
+                  if ((i%3) != 2) {
+                      return TCL_ERROR;
+                  }
+                  *modePtr |= (1<<(8-i));
+                  break;
+              case 's':
+                  if (((i%3) != 2) || (i > 5)) {
+                      return TCL_ERROR;
+                  }
+                  *modePtr |= (1<<(8-i));
+                  *modePtr |= (1<<(11-(i/3)));
+                  break;
+              case 'S':
+                  if (((i%3) != 2) || (i > 5)) {
+                      return TCL_ERROR;
+                  }
+                  *modePtr |= (1<<(11-(i/3)));
+                  break;
+              case 't':
+                  if (i != 8) {
+                      return TCL_ERROR;
+                  }
+                  *modePtr |= (1<<(8-i));
+                  *modePtr |= (1<<9);
+                  break;
+              case 'T':
+                  if (i != 8) {
+                      return TCL_ERROR;
+                  }
+                  *modePtr |= (1<<9);
+                  break;
+              case '-':
+                  break;
+              default:
+                  return TCL_ERROR;
+          }
+     }
+     return TCL_OK;
+ }
+ ^L
+ /*
+  *----------------------------------------------------------------------
+  *
+  * Tcl_GetModeFromChmodString --
+  *
+  *    This procedure is invoked to process the "file permissions"
+  *      Tcl command if invoked with a "ugoa+-=rwxst"-like string.
+  *    See the user documentation for details on what it does.
+  *
+  * Results:
+  *    A standard Tcl result.
+  *
+  * Side effects:
+  *    See the user documentation.
+  *
+  *----------------------------------------------------------------------
+  */
+
+ int
+ Tcl_GetModeFromChmodString(interp, modeStringPtr, newModePtr)
+     Tcl_Interp *interp;             /* The interp we are using for errors. */
+     char *modeStringPtr;            /* Ptr to the "ugoa+-=rwxst" String */
+     mode_t *newModePtr;             /* Ptr to the mode value after change*/
+ {
+     mode_t oldMode;                 /* Mode of file before change */
+     int who, op, what, op_found, who_found;
+     int i = 0;
+     int n;
+
+     for (n = 0 ; *(modeStringPtr+n) != '\0' ; n = n + i)
+        {
+        oldMode = *newModePtr;
+        who = op = what = op_found = who_found = 0;
+        for (i = 0 ; *(modeStringPtr+n+i) != '\0' ; i++ )
+           {
+           if (!who_found)
+             {
+             /* who */
+             switch (*(modeStringPtr+n+i))
+                {
+                case 'u' :
+                    who |= 0x9c0;
+                    continue;
+                case 'g' :
+                    who |= 0x438;
+                    continue;
+                case 'o' :
+                    who |= 0x207;
+                    continue;
+                case 'a' :
+                    who |= 0xfff;
+                    continue;
+                }
+              }
+           who_found = 1;
+           if (who == 0)
+             who = 0xfff;
+           if (!op_found)
+             {
+             /* op */
+             switch (*(modeStringPtr+n+i))
+                {
+                case '+' :
+                    op = 1;
+                    op_found = 1;
+                    continue;
+                case '-' :
+                    op = 2;
+                    op_found = 1;
+                    continue;
+                case '=' :
+                    op = 3;
+                    op_found = 1;
+                    continue;
+                default  :
+                    return TCL_ERROR;
+                    break;
+                }
+              }
+           /* what */
+           switch (*(modeStringPtr+n+i))
+              {
+              case 'r' :
+                  what |= 0x124;
+                  continue;
+              case 'w' :
+                  what |= 0x92;
+                  continue;
+              case 'x' :
+                  what |= 0x49;
+                  continue;
+              case 's' :
+                  what |= 0xc00;
+                  continue;
+              case 't' :
+                  what |= 0x200;
+                  continue;
+              case ',' :
+                  break;
+              default  :
+                  return TCL_ERROR;
+                  break;
+              }
+           if (*(modeStringPtr+n+i) == ',')
+              {
+              i++;
+              break;
+              }
+           }
+           switch (op)
+              {
+              case 1 :
+                  *newModePtr = oldMode | (who & what);
+                  continue;
+              case 2 :
+                  *newModePtr = oldMode & ~(who & what);
+                  continue;
+              case 3 :
+                  *newModePtr = (oldMode & ~who) | (who & what);
+                  continue;
+              }
+        }
+     return TCL_OK;
  }
  ^L
  /*

PatchFiles:
tclCmdAH.c
welch Closed ticket [217946ffff]. [History diff]2000-10-26 08:10:55
dgp Open ticket [217946ffff]. [History diff]2001-04-05 07:12:25
dgp Closed ticket [217946ffff]. [History diff]2001-04-05 07:13:40
Log in (anonymous is fine) to add a comment.