/*
 * chog.c
 * chown and chgrp of files
 */
#include <stdio.h>
#include  <stdfun.h> 
#include <errno.h>
#include <pwd.h>
#include <grp.h>


static char _sccsid[] = { " chog.c 1.1 9/22/97 " };

int main( argc, argv )
int argc;
char *argv[];
{
	extern int chown();
	extern struct passwd *getpwnam();
	extern struct group *getgrnam();

	struct passwd *pws;
	struct group *gps;
	int ownerid, groupid;

	int i;

	if( argc < 4 )    {
		fprintf( stderr, "Usage: %s owner group file [file ...]\n", argv[0] );
		exit( 1 );
	}
	if( (pws = getpwnam( argv[1] )) == (struct passwd *)0 )    {
		fprintf( stderr, "%s: no such user '%s'\n", argv[0], argv[1] );
		exit( 1 );
	}
	if( (gps = getgrnam( argv[2] )) == (struct group *)0 )    {
		fprintf( stderr, "%s: no such group '%s'\n", argv[0], argv[2] );
		exit( 1 );
	}
	ownerid = pws->pw_uid;
	groupid = gps->gr_gid;

	/* Loop on file arguments */
	i = 3;
	while( i < argc )    {

		if( chown( argv[i], ownerid, groupid ) < 0 )    {
			perror( argv[0] );
			errno = 0;
			/* But continue processing */
		}
		++i;

	} /* End while processing file arguments */

	exit( 0 );
	return( 0 );

} /* End main() */


Return to Home Page
Return to Metayoga Page
Return to C Language Page


The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/CPROGS/chog.html
Created: 1997
Last Updated: May 28, 2000 Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL