static char *sccsid
= "@(#)chfn.c 4.1 (Berkeley) 10/1/80";
* chfn - change full name (or other info in gecos field)
char passwd
[] = "/etc/passwd";
char temp
[] = "/etc/ptmp";
struct passwd
*getpwent();
printf("Usage: chfn user full-name\n");
if (index(argv
[2], ':') || index(argv
[2], '\n')) {
printf("Illegal character in new string\n");
while((pwd
=getpwent()) != NULL
){
if(strcmp(pwd
->pw_name
,argv
[1]) == 0){
if(u
!=0 && u
!= pwd
->pw_uid
){
printf("Permission denied.\n");
if(access(temp
, 0) >= 0) {
printf("Temporary file busy -- try again\n");
if((tf
=fopen(temp
,"w")) == NULL
) {
printf("Cannot create temporary file\n");
* copy passwd to temp, replacing matching lines
while((pwd
=getpwent()) != NULL
) {
if(strcmp(pwd
->pw_name
,argv
[1]) == 0) {
if(u
!= 0 && u
!= pwd
->pw_uid
) {
printf("Permission denied.\n");
fprintf(tf
,"%s:%s:%d:%d:%s:%s:%s\n",
* copy temp back to passwd file
if((fi
=open(temp
,0)) < 0) {
printf("Temp file disappeared!\n");
if((fo
=creat(passwd
, 0644)) < 0) {
printf("Cannot recreat passwd file.\n");
while((u
=read(fi
,buf
,sizeof(buf
))) > 0) write(fo
,buf
,u
);