|
Home > Archive > Unix Programming > April 2005 > change users shell programmatically
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
change users shell programmatically
|
|
| ben.lemasurier@gmail.com 2005-04-22, 8:47 pm |
| Hey Everyone,
Does anyone know how can I change a users group programmatically with c
under the *nix environment? For instance, if a users current shell is
/bin/bash how would I change it to /bin/csh? I am looking for to do
somthing similar to chsh.
I can provide more information if I am not clear, thanks!
-Ben
| |
| William Ahern 2005-04-23, 2:50 am |
| ben.lemasurier@gmail.com wrote:
> Hey Everyone,
>
> Does anyone know how can I change a users group programmatically with c
> under the *nix environment? For instance, if a users current shell is
> /bin/bash how would I change it to /bin/csh? I am looking for to do
> somthing similar to chsh.
>
>
> I can provide more information if I am not clear, thanks!
You're lucky if chsh(1) exists on all of the Unixes you're using. And from C
it differs from platform to platform, version to version. There are no
standards, AFAIK, even suggesting an interface on how to set these things,
either from C or the shell.
Your best bet would probably be something like:
#include <stdio.h> /* snprintf(3) */
#include <stdlib.h> /* system(3) */
#include <limits.h> /* LOGIN_NAME_MAX NAME_MAX */
#include <errno.h> /* ENAMETOOLONG */
#include <sys/wait.h> /* WEXITSTATUS */
#include <err.h> /* err(3) */
char cmd[sizeof "chsh -s " + NAME_MAX + sizeof " " + LOGIN_NAME_MAX];
int cmdlen, exit_code;
/* Should make sure shell and user are safe to pass to /bin/sh, or even
* better don't use system(3) and instead fork(2) + execl(2) manually.
*/
cmdlen = snprintf(cmd,sizeof cmd,"chsh -s %s %s",shell,user);
if (cmdlen < 0) {
err(EXIT_FAILURE,"snprintf");
} else if (cmdlen >= sizeof cmd) {
errno = ENAMETOOLONG;
err(EXIT_FAILURE,"Too lazy to not use fixed size buffers");
}
exit_code = system(cmd);
if (exit_code == -1)
err(EXIT_FAILURE,"system(%s)",cmd);
exit(WEXITSTATUS(exit_code));
| |
| ben.lemasurier@gmail.com 2005-04-23, 2:50 am |
| Thanks for the detailed reply, this will work great.
-Ben
|
|
|
|
|