added opt parse and some misc stuff;

master
Alexander Vdolainen 4 years ago
parent 109d071fed
commit d737e5f836

@ -41,7 +41,96 @@
extern int ejda_username_split(char *buf, char **user, char **domain);
/*
* Test is simple - create a pipe, write a message with eport interface
* read it, reply with erport interface.
* for supported commands replies ok, fail otherwise
*/
static void print_help(const char *cname)
{
fprintf(stdout, "\n%s - %s\n\n", cname, PRG_NAME);
fprintf(stdout, "USAGE:\n");
fprintf(stdout, "\t%s -u | --user <username>"
" -P | --password <password> [ -v | --verbose ]",
cname);
fprintf(stdout, "\n\n\t%s -h | --help\n", cname);
return;
}
int main(int argc, char **argv)
{
char *domain, *user, *password;
char *utbuf = NULL, *msgbuf = NULL;
int verbose = 0, opt, msgbuf_len = 0;
struct ejabber_msg m;
domain = user = password = NULL;
/* parse options */
while(1) {
int option_index = 0;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"user", required_argument, NULL, 'u'},
{"password", required_argument, NULL, 'P'},
{"verbose", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0},
};
if((opt = getopt_long(argc, argv, "hu:P:v", long_options,
&option_index)) == -1) break;
switch(opt) {
case 'h':
/* print help information and exit */
print_help(argv[0]);
return 0;
break;
case 'u': user = optarg; break;
case 'P': password = optarg; break;
case 'v': verbose = 1; break;
default: abort(); break; /* something that's shouldn't ever happen */
}
}
if(!user) {
fprintf(stderr, "Error: no username is given.\n");
main_einval:
print_help(argv[0]);
return EINVAL;
}
if(!password) {
fprintf(stderr, "Error: no password provided.\n");
goto main_einval;
}
/* split user name */
opt = strlen(user) + sizeof(char);
if((utbuf = malloc(opt)) == NULL) {
fprintf(stderr, "Error: not enough memory\n");
return ENOMEM;
} else {
memset(utbuf, '\0', opt);
memcpy(utbuf, user, opt - sizeof(char));
if(ejda_username_split(utbuf, &user, &domain)) {
fprintf(stderr, "Error: username is invalid.\n");
free(utbuf);
return EINVAL;
}
}
/* allocate a message buffer */
msgbuf_len = strlen("removeuser3") + strlen(user) + strlen(domain) +
strlen(password) + sizeof(uint16_t) + sizeof(char)*4;
if((msgbuf = malloc(msgbuf_len)) == NULL) {
fprintf(stderr, "Error: not enough memory\n");
free(utbuf);
return ENOMEM;
} else memset(msgbuf, 0, msgbuf_len);
free(utbuf);
free(msgbuf);
return 0;
}

Loading…
Cancel
Save