Relay-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site seismo.CSS.GOV Posting-Version: version B 2.10.2 9/3/84; site panda.UUCP Path: seismo!harvard!talcott!panda!sources-request From: sources-request@panda.UUCP Newsgroups: mod.sources Subject: give: change owner/group of files Message-ID: <1112@panda.UUCP> Date: 15 Nov 85 01:59:45 GMT Sender: jpn@panda.UUCP Lines: 395 Approved: jpn@panda.UUCP Mod.sources: Volume 3, Issue 44 Submitted by: philabs!mcnc!ikonas!!roger [ moderators note: This program ONLY works properly on System III/V where chown(3) allows you to give away ownership of a file. It might be useful to superusers on V7/BSD type systems, perhaps? ] Wednesday, 13 November 1985, 5:32 p.m. Enclosed find the manual page and C source for a program to change owner and group id's for one or more files. If a named file is a directory, the entire tree beginning with (and including) that directory is descended and changed. Roger L. Cordes, Jr. William G. Daniel & Associates ...!mcnc!ikonas!dedalus!roger #!/bin/sh # This is a shar archive: extract with sh, not csh. # This archive contains: # give.1 # give.c echo x - give.1 cat > give.1 << '!Funky!Stuff!' .TH GIVE sd .SH NAME give \- change owner and group information for one or more files .SH SYNOPSIS .B give .I file... .B to .I user [\c .I group\c ] .SH DESCRIPTION .B Give will change ownership and group information for the files specified. If any of the files is a directory, .B give will descend and change the entire directory tree, including the named directory. .sp AS might be expected, .B give will only work for the current owner of the named files or root. .SH DIAGNOSTICS .B Give will exit with a descriptive error message if any of the following occur: .br .in +0.5i access to /etc/passwd (and /etc/group, if applicable) is denied .br the named user (group) does not occur in the file .br files specified cannot be accessed or modified .in -0.5i .SH FILES /etc/passwd \- user list .br /etc/group \- group list .br !Funky!Stuff! echo x - give.c cat > give.c << '!Funky!Stuff!' # /**** **** Wednesday, 13 November 1985, 1:29 p.m. **** give: change owner and group for one or more files as indicated: **** usage: **** give file... to user [group] **** - may be used succesfully only by current owner or root **** - if file is a directory, descend and change the entire tree **** beginning with (including) the named directory **** compile: **** cc -O -o give give.c ****/ main(argc,argv) int argc; char *argv[]; { int i, to, uid = -1, gid = -1; /* * find end of filename list in argv[] and verify command-line syntax: * must say at least: "give SOMETHING to SOMEBODY" */ for ( i=0; i /* * getuser: get integer user id: returns -1 if unknown */ #define is_digit(C) ( ((C)>='0') && ((C)<='9') ) char line[256], *c = (char *)0, *strchr(); getuser(name) char *name; { FILE *file; int uid = -1, i, len, strlen(), intname = 1; len = strlen(name); for ( i=0; i #include #define indent() for ( j=0; j char ln[4096], *strcat(); getflist(names) char ***names; { int n = 0; struct direct dir; FILE *file; if ( !(file=fopen(".","r")) ) return(0); ln[0] = 0; while ( fread(&dir,sizeof(struct direct),1,file) ) if ( !strcmp(dir.d_name,".") || !strcmp(dir.d_name,"..") ) continue; else if ( dir.d_ino ) { strcat(ln,dir.d_name); strcat(ln," "); } fclose(file); n = get_words(ln,names); return(n); } /* * get_words: split a character string into individual words */ #define is_white(C) ( ((C)==' ') || ((C)=='\t') ) int get_words(line,wordlist) char *line; char ***wordlist; { int i, j, k, nwords = 0, len = 0, longest = 0; char *c, **words, *malloc(), was_white = 0, got_a_word = 0; /* * count words, find longest word */ for ( c=line; *c; c++ ) if ( is_white(*c) ) { if ( got_a_word && !was_white ) { if ( len > longest ) longest = len; len = 0; nwords++; } was_white = 1; } else { was_white = 0; got_a_word = 1; len++; } if ( got_a_word && !was_white ) { if ( len > longest ) longest = len; len = 0; nwords++; } if ( !nwords ) return(0); /* * allocate the array of individual words */ if ( !(words=(char **)malloc(nwords*sizeof(char *))) ) { printf("cannot create the word list...\n"); return(0); } longest++; /* plus '\0' */ for ( i=0; i=0; i-- ) free(words[i]); free((char *)words); return(0); } /* * load the array of individual words */ got_a_word = was_white = j = k = 0; for ( i=0; line[i]!='\0'; i++ ) if ( !is_white(line[i]) ) { got_a_word = 1; words[j][k] = line[i]; was_white = 0; k++; } else if ( got_a_word && !was_white ) { words[j][k] = '\0'; was_white = 1; k = 0; j++; } if ( got_a_word && !was_white ) words[j][k] = '\0'; *wordlist = words; return(nwords); } !Funky!Stuff!