chmode.c

Brian Bebeau brianb at marque.mu.edu
Thu Jun 8 04:54:17 AEST 1989


In article <21508 at iuvax.cs.indiana.edu> regoli at silver.bacs.indiana.edu (michael regoli) writes:
>
>if an industrious soul wants to make improvements to allow more than
>any combination of "rwxrwxrwx" (e.g., "rwsr-xr-x") the patches would
>be most appreciated.
>
>please post your patches here.  or rewrite the whole damn thing. 
>
>--
>michael regoli
>regoli at iubacs.bitnet
>regoli at sivler.bacs.indiana.edu
>...rutgers!iuvax!silver!regoli
 
Ok, I did. I rewrote it basically from translate() on keeping the main()
stuff. 
ADDITIONS:  	accepts setuid, setgid
		accepts sticky bit
		accepts file/record locking
		complains if chmod fails

It also lints clean. This should be fine on a SYSV machine, I don't know
about Berkeley, I can't try it. There are two problems. 
1) Although it will catch errors like "rwz" it will silently accept "rww".
2) If you try to set the sticky bit and you don't have permission, it
   won't complain.

---cut------cut------cut------cut------cut------cut------cut------cut---
/*
 *
 *	CHMOD2.C
 *
 *  	Based on the chmode program by:
 *	michael regoli 		regoli at sivler.bacs.indiana.edu
 *      Modified by: 
 *    	Brian Bebeau		brianb at marque.mu.edu
 *
 *	New version of the chmod program: accepts inputs of the
 *	same format as given by the ls -l command, ie it can
 *	be invoked like 'chmode rwxrw-r-- *' to make the modes
 *	of each file specified match the template!
 *
 *		cc -O chmod2.c -o chmod2
 *
 */

#include <stdio.h>
#include <errno.h>

#define ERROR   -1
#define	SUID	04000		/* set user id */
#define	SGID	02000		/* set group id */
#define	STKY	01000		/* sticky bit */
#define	ROWN	00400		/* owner read permission */
#define	WOWN	00200		/* owner write permission */
#define	XOWN	00100		/* owner execute permissin */
#define	RGRP	00040		/* group read permission */
#define	WGRP	00020		/* group write permission */
#define	XGRP	00010		/* group execute permission */
#define	ROTH	00004		/* other read permission */
#define	WOTH	00002		/* other write permission */
#define	XOTH	00001		/* other execute permission */

void exit(), perror();
char *strcpy(), *strncpy();

main(argc, argv)
int argc;
char *argv[];
{
        register int newmode;
	register int j;
        char     buffer[16];

        if (argc < 3) {
          (void) fprintf(stderr, "Usage: %s <graphic mode> file(s)\n",argv[0]);
          exit(1);
	}

        --argc;
	newmode = 0;
	j = 1;

        (void) strncpy(buffer,argv[j++], 9);

        /** lets figure out the graphic mode translation! **/

        if ((newmode = translate(buffer)) == ERROR) {
          (void) fprintf(stderr, "Bad graphic mode designator!  Please use 'rwxrwxrwx' as a template, \n");
          (void) fprintf(stderr, "indicating those accesses that you desire to prevent with a dash\n");
          (void) fprintf(stderr, "      For example: 'chmode rw-r--r-- test.c'\n");
          exit(2);
        }

        while (--argc > 0)
        if (chmod(argv[j++], newmode) < 0) {
	  perror("chmod failed");
	  exit(3);
	}
	exit(0);
	/*NOTREACHED*/
}

translate(buffer)
char buffer[];
{
        /** translate a graphic representation of file access to
            an equivalent number as defined in CHMOD(2) **/

	register int mode=0, i;

	for (i=0; i < 9; i++) {
	  switch(buffer[i]) {
	    case 'r':
		if (i == 0)
		  mode |= ROWN;
		if (i == 3)
		  mode |= RGRP;
		if (i == 6)
		  mode |= ROTH;
		break;
	    case 'w':
		if (i == 1)
		  mode |= WOWN;
		if (i == 4)
		  mode |= WGRP;
		if (i == 7)
		  mode |= WOTH;
		break;
	    case 'x':
		if (i == 2)
		  mode |= XOWN;
		if (i == 5)
		  mode |= XGRP;
		if (i == 8)
		  mode |= XOTH;
		break;
	    case 's':
		if (i == 2){
		  mode |= SUID;
		  mode |= XOWN;
		}
		if (i == 5) {
		  mode |= SGID;
		  mode |= XGRP;
		}
		break;
	    case 't':
		if (i == 8) {
		  mode |= STKY;
		  mode |= XOTH;
		}
		break;
	    case 'l':
		if (i == 5) {
		  mode |= SGID;
		  mode &= ~XGRP;
		}
		break;
	    case '-':
		break;
	    default:
	      return (ERROR);
	  } /* end switch */
	} /* end for */

	return (mode);

} /* end routine */
 
/* end program chmod2.c */
---cut------cut------cut------cut------cut------cut------cut------cut---
---------------------------------------------------------------------------
Brian Bebeau				 Marquette University		

DOMAIN:  brianb at marque.mu.edu 
  UUCP:  {uwvax,uunet}!marque!brianb
  ARPA:  brianb%marque.uucp at csd1.milw.wisc.edu     BITNET:  6877BEBE at MUCSD
---------------------------------------------------------------------------



More information about the Alt.sources mailing list