PersonalCorpus 版 (精华区)
发信人: cliff (夺命小辣椒), 信区: Unix
标 题: adduser.c
发信站: 紫 丁 香 (Mon Dec 6 11:17:45 1999) WWW-POST
/* adduser.c, by digger */
#include
#include
#include
#include
#define TRUE 0
#define FALSE -1
extern char err_info[128]; /* defined in passwdio.c */
extern user_add(char *username,
char *passwd,
char *fullname,
gid_t gid,
char *dir,
char *shell,
int if_make_dir); /* defined in passwdio.c */
char default_shell[] = "/bin/sh";
char default_dir[64];
char default_fullname[] = "";
void usage(char * command)
{
printf("\tusage:\t%s [-p passwd] [-g group_id] [-s shell]\n", command);
printf("\t\t[-f fullname] [-d directory] [-m] username\n");
printf("\tdefault password is as same as username,\n");
printf("\tdefault directory of usernameis /export/home/username,\n");
printf("\tdefault group id is 1(other),\n");
printf("\tdefault shell is /bin/sh\n");
printf("\t-m option means making home directory for user\n");
}
main(int argc, char **argv )
{
char *user = NULL;
char *passwd = NULL;
char *fullname = NULL;
gid_t gid = 1;
char *shell = NULL;
char *dir = NULL;
int if_make_dir = FALSE;
int i;
extern int optind;
extern char *optarg;
if (argc < 2) {
usage(argv[0]);
exit(-1);
}
while ((i = getopt(argc, argv, "p:g:s:f:d:m")) != EOF) {
switch (i) {
case 'p':
passwd = optarg;
break;
case 'g':
gid = atoi(optarg);
if (!getgrgid(gid)) {
printf("error, invalid group id\n");
exit(-1);
}
break;
case 's':
shell = optarg;
break;
case 'f':
fullname = optarg;
break;
case 'd':
dir = optarg;
break;
case 'm':
if_make_dir = TRUE;
break;
default :
usage(argv[0]);
exit(-1);
}
}
if (optind > argc-1 ) {
usage(argv[0]);
exit(-1);
}
user = argv[optind];
if (!passwd) passwd = user;
if (!fullname) fullname = default_fullname;
if (!shell) shell = default_shell;
if (!dir) {
sprintf(default_dir, "/export/home/%s", user);
dir = default_dir;
}
if (user_add(user, passwd, fullname, gid, dir, shell, if_make_dir)
== TRUE) {
printf("user %s added successfully\n", user);
printf("clear passwd: %s\n", passwd);
printf("fullname: %s\n", fullname);
printf("home directory: %s ", dir);
if (if_make_dir == FALSE)
printf("yet not made\n");
else
printf("made OK\n");
printf("shell: %s\n", shell);
printf("gid: %d\n", gid);
} else
printf("failed in adding user %s: %s\n", user, err_info);
}
--
________________
/________________/|
| | |
| 网 | |
| 络 | |
※ 来源:·紫 丁 香 bbs.hit.edu.cn·[FROM: 202.118.239.10]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.021毫秒