OpenVMS Source Code Demos

CHMOD_DEMO.C

//==============================================================================
// title  : HACK_C_CHMOD_100.C
// author : Neil Rieck
// created: 2011-08-31
// notes  : chmod.c (changes the protection bits of a file or directory)
//        : changing 3-bits with chmod affect 4-bits on OpenVMS
//==============================================================================
#define __NEW_STARLET	1				// enable new (strict) starlet for OpenVMS Alpha 7.0 and later
							// http://h41379.www4.hpe.com/doc/82final/5841/5841pro_059.html
#include <stdio.h>					// c   - standard i/o
#include <string.h>					// c   - string stuff
#include <starlet.h>					// vms - system library
#include <ssdef.h>					// vms - system services definitions
#include <stat.h>					// vms - stat (for chmod)

int main ()						//
{	int		rc;				//
	char		fileTemp[255];			// longer for path + file on ODS5 volume
	unsigned short	prot_bits;			// see UNIX docs
	//
	//	define an expendible (junk) file for us to play with
	//
	strcpy(fileTemp, "neil.txt");			// <<< change file as required
	//
	//	Example Values (and what they will do to VMS files)
	//
	//	value		    vms file protection result
	//	=====		    ==========================
	//			    sys  own  grup wrld
	prot_bits =  1;		// (    ,    ,    ,  E )	  1
	prot_bits =  2;		// (    ,    ,    , W D)	  2 <<< setting 'd' also sets 'w'
	prot_bits =  3;		// (    ,    ,    , WED)	  3
	prot_bits =  4;		// (    ,    ,    ,R   )	  4
	prot_bits =  5;		// (    ,    ,    ,R E )	  5
	prot_bits =  6;		// (    ,    ,    ,RW D)	  6
	prot_bits =  7;		// (    ,    ,    ,RWED)	  7
				//  -------------------
	prot_bits =  8;		// (    ,    ,  E ,    )  1x8  =  8
	prot_bits = 16;		// (    ,    , W D,    )  2x8  = 16
	prot_bits = 24;		// (    ,    , WED,    )  3x8  = 24
	prot_bits = 32;		// (    ,    ,R   ,    )  4x8  = 32
				//			  5x8  = 40
				//			  6x8  = 48
	prot_bits = 56;		// (    ,    ,RWED,    )  7x8  = 56
	prot_bits = 63;		// (    ,    ,RWED,RWED)  8^2-1= 63
				//  -------------------
	prot_bits = 64;		// (  E ,  E ,    ,    )  8x8 =  64 <<< owner + system groups both set
	prot_bits =128;		// ( W D, W D,    ,    ) 16x8 = 128
	prot_bits =192;		// ( WED, WED,    ,    ) 24x8 = 192
	prot_bits =256;		// (R   ,R   ,    ,    ) 32x8 = 256
				//			 40x8 = 320
				//			 48x8 = 384
				//			 56x8 = 448
	prot_bits =511;		// (RWED,RWED,RWED,RWED) 8^3-1= 511
				//  -------------------
	prot_bits =512;		// (    ,    ,    ,    ) 8^3  = 512
	//----------------------------------------------------------------------
	// prototype:
	//	int chmod (const char *file_spec, mode_t mode);
	// see:
	//	http://h41379.www4.hpe.com/doc/732final/5763/5763pro_022.html#index_x_625
	//
	//	returns:	0 = sucess
	//			-1 = fail
	//----------------------------------------------------------------------
	prot_bits =511;					// (RWED,RWED,RWED,RWED)
	rc = chmod( fileTemp, prot_bits );		//
	if (rc == -1)					//
	{    printf ("-e- wcsm_c_chmod: FAILED\n");	//
	}else{						//
	     printf ("-i- wcsm_c_chmod:SUCCEEDED\n");	//
	}						//
	return(1);					// VMS-i- (goes back to DCL)
}