/* * Time-stamp: */ /* Copyright (C) 2003 Free Software Foundation, Inc. * * Chsh is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Chsh is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public * License for more details. * * You should have received a copy of the GNU General Public License * along with Chsh; see the file COPYING. If not, write to the Free * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ #include #include /* for strcmp() and strcpy() */ #include /* for getuid() */ #include /* for pwent utils and structure */ char me[128]; /* * Look up the current user in the password database. */ char * lookMeUp() { struct passwd *currentPW; currentPW=getpwuid(getuid()); strcpy(me, currentPW->pw_name); return(me); } /* * Dump the current entry in the same order as the structure. */ void dumpPwEnt(struct passwd *currentPW, char *newShell) { if(currentPW->pw_comment) printf("%s:%s:%d:%d:%s:%s:%s:%s\n", currentPW->pw_name, currentPW->pw_passwd, currentPW->pw_uid, currentPW->pw_gid, currentPW->pw_comment, currentPW->pw_gecos, currentPW->pw_dir, newShell ? newShell : currentPW->pw_shell); else printf("%s:%s:%d:%d:%s:%s:%s\n", currentPW->pw_name, currentPW->pw_passwd, currentPW->pw_uid, currentPW->pw_gid, currentPW->pw_gecos, currentPW->pw_dir, newShell ? newShell : currentPW->pw_shell); } int main(int argc, char *argv[]) { char *name; char *newShell; struct passwd *currentPW; switch(argc) { case 2: name=lookMeUp(); newShell=argv[1]; break; case 3: name=argv[1]; newShell=argv[2]; break; default: fprintf(stderr,"usage: %s [name] /path/to/new/shell\n",argv[0]); return(1); break; } setpwent(); currentPW=getpwent(); do { if(!strcmp(currentPW->pw_name, name)) dumpPwEnt(currentPW,newShell); else dumpPwEnt(currentPW,NULL); currentPW=getpwent(); } while(currentPW && (currentPW->pw_uid >= 0) && (currentPW->pw_gid >= 0)); endpwent(); return(0); }