home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
cowen_tools.lzh
/
owner1.c
< prev
Wrap
C/C++ Source or Header
|
1992-03-06
|
6KB
|
296 lines
/*
** OWNER
** changes the ownership of files using the Cumana Password System
** owner <newgroup>.<newowner> <file> <file> ...
** options are:
** -r = with recursion into directories
** -? = report usage
**
** The password file is looked for on the following
** /DD/SYS/PASSWORD
** /H0/SYS/PASSWORD
** /D0/SYS/PASSWORD
**
**
** copyright 1991 by Cowen Software Ltd
** 23 Bristol Ave, Levenshulme, Manchester,
** GB-M19 3NU, England, UK
** released to the public domain for non-commercial use
**
*/
#include <stdio.h>
#include <modes.h>
#include <ctype.h>
#define true 1
#define false 0
#define trace false
char qflag = false;
int pcount = 0;
extern int errno;
char rflag = FALSE; /* recursive */
char pbuff[140],
*ppntr;
char **arg;
char *username;
union {short userno;
struct { char userhi, userlo;} users;
} user;
int userint;
/* ------------Main Procedure--------------------------------------*/
main(argc,argv)
int argc;
char *argv[];
{
#if trace
fprintf(stderr,"main(%d parameters)\n",argc);
fflush(stderr);
#endif
getflags(argc,argv);
if (pcount < 3) help ();
arg = argv;
getowner();
printf("User %03u.%03u : %s\n",(int) user.users.userhi,
(int) user.users.userlo,
username);
userint = user.users.userhi * 65536 + user.users.userlo;
/* This is because CHOWN demands an int as para */
while ( pcount-- > 0 ) newowner();
#if trace
fprintf(stderr,"exit\n");
fflush(stderr);
#endif
exit(0);
}
/* -----------------Get User from Password File----------------------*/
/*
** Scans down the PBUFF buffer to get the next parameter
** Terminates on "," (replacing it by \0)
** Errors on \n
** Uses the global pointer PPNTR, returns pointer to parameter
*/
char * getpass()
{
int pass, cnt;
char *x1, *x2;
#if trace
fprintf(stderr,"getpass(); ppntr = %-20s\n",ppntr);
fflush(stderr);
#endif
if (*ppntr == '\0') return ppntr;
for (x1 = ppntr; ( (*x1 != ',') & (*x1 != '\0') ) ;x1++);
*x1++ = '\0';
x2 = ppntr;
ppntr = x1;
return x2;
}
/* get a User Number from a string pointer.
** called from getowner
*/
getno(ptr)
char *ptr;
{
char *p1, *p2;
#if trace
fprintf(stderr,"getno(%-8s)\n",ptr);
fflush(stderr);
#endif
while (*ptr == ' ') ptr++;
p1 = ptr;
if (!isdigit(*ptr)) error ("No Group Number");
while (isdigit(*ptr)) ptr++;
if (*ptr != '.') error("No dot in User Number");
*ptr++ = '\0';
if (!isdigit(*ptr)) error ("No User Number");
p2 = ptr;
while (isdigit(*ptr)) ptr++;
while (*ptr == ' ') ptr++;
if (*ptr != '\0') error("Invalid User Number");
return (atoi(p1) * 256 + atoi(p2) );
}
/*
** Reads the new owner from the first parameter
** Called from MAIN procedure
*/
getowner()
{
int para_no, pass, cnt;
char x, *x1, *x2;
#if trace
fprintf(stderr,"getowner()\n");
fflush(stderr);
#endif
pcount--;
while (**++arg == '-') ;
pass = open("/DD/SYS/PASSWORD",_READ);
if ( pass == -1 ) pass = open("/H0/SYS/PASSWORD",_READ);
if ( pass == -1 ) pass = open("/D0/SYS/PASSWORD",_READ);
if ( pass == -1 ) error("Can't open password file");
x = **arg;
if (!isascii(x)) error("Corrupt Password File");
para_no = (isdigit(x)) ? getno(*arg) : 0;
while (cnt = readln(pass,pbuff,138) )
{
if (cnt == -1) error("Error reading Password File");
pbuff[cnt-1] = '\0';
#if trace
fprintf(stderr,"read: cnt = %d; pbuff = %-20s\n",cnt,pbuff);
fflush(stderr);
#endif
ppntr = pbuff;
while (*ppntr == ' ') ppntr++;
if (*ppntr == '\0') continue;
username = getpass();
getpass(); /* skip password */
x1 = getpass();
user.userno = getno(x1);
#if trace
fprintf(stderr,"getno.loop para_no = %d; userno = %d\n",para_no,user.userno);
fprintf(stderr," *arg = %-9s; username = %-9s\n",*arg,username);
fflush(stderr);
#endif
if (isdigit(x)) { if (para_no == user.userno) return 0; }
else { if (!strcmp(*arg,username) ) return 0; }
}
error("No such user");
exit(5);
}
/* -----------------Change Ownership on named files-----------------*/
/*
** Changes the File Ownership on named files
** Called from MAIN procedure
*/
newowner()
{
#if trace
fprintf(stderr,"newowner()");
fflush(stderr);
#endif
while (**++arg == '-') ;
if (chown(*arg, userint) != 0)
exit(errno);
printf("Owner changed for file : %s \n",*arg);
;
}
/* -------------------Help, Error, Etc------------------------------*/
getflags(carg,varg)
int carg;
char **varg;
{
char *p, c;
while ( (--carg) )
{
if (*(p = *++varg) == '-')
{
while (c = *++p)
{
switch (c)
{
case '?' :
help();
break;
case 'r' :
rflag = TRUE; /* recursive*/
break;
default :
help();
break;
}
}
}
else pcount++;
}
}
/*
** provide usage info for this command
*/
help()
{
fputs("owner: change ownership of files\n", stderr);
fputs(" (super user only)\n", stderr);
fputs("Usage: owner user file file ...\n", stderr);
fputs("Where:\n", stderr);
fputs("User = group.userno or username\n", stderr);
fputs("Options = -r - process directories recursively \n", stderr);
fputs(" -? - help\n", stderr);
exit (0);
}
/*
** write an error message, by
** writing the string passed and then a <cr>
*/
error(s1)
char *s1;
{
fputs(s1,stderr);
fputs("\n",stderr);
exit(5);
}