ctlinnbbsd.c
上传用户:minyiyu
上传日期:2018-12-24
资源大小:864k
文件大小:4k
- #include "innbbsconf.h"
- #include "bbslib.h"
- extern char *optarg;
- extern int opterr, optind;
- usage(name)
- char *name;
- {
- fprintf(stderr, "Usage: %s [-p path] commandsn", name);
- fprintf(stderr, " where available commands:n");
- fprintf(stderr, " ctlinnbbsd reload : reload datafiles for innbbsdn");
- fprintf(stderr, " ctlinnbbsd shutdown : shutdown innbbsd gracefullyn");
- fprintf(stderr, " ctlinnbbsd mode : examine mode of innbbsdn");
- fprintf(stderr, " ctlinnbbsd addhist <mid> path: add historyn");
- fprintf(stderr, " ctlinnbbsd grephist <mid>: query historyn");
- fprintf(stderr, " ctlinnbbsd verboselog on|off : verboselog on/offn");
- fprintf(stderr, " ctlinnbbsd hismaint : maintain historyn");
- fprintf(stderr, " ctlinnbbsd listnodelist : list nodelist.bbsn");
- fprintf(stderr, " ctlinnbbsd listnewsfeeds : list newsfeeds.bbsn");
- fprintf(stderr, " ctlinnbbsd pause : pause innbbsd servern");
- fprintf(stderr, " ctlinnbbsd go : resume innbbsd servern");
- #ifdef GETRUSAGE
- fprintf(stderr, " ctlinnbbsd getrusage: get resource usagen");
- #endif
- #ifdef MALLOCMAP
- fprintf(stderr, " ctlinnbbsd mallocmap: get malloc mapn");
- #endif
- }
- char *DefaultPath = LOCALDAEMON;
- char INNBBSbuffer[4096];
- FILE *innbbsin, *innbbsout;
- int innbbsfd;
- ctlinnbbsd(argc, argv)
- int argc;
- char **argv;
- {
- fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
- printf("%s", INNBBSbuffer);
- if (strcasecmp(argv[0], "shutdown") == 0 ||
- strcasecmp(argv[0], "reload") == 0 ||
- strcasecmp(argv[0], "hismaint") == 0 ||
- #ifdef GETRUSAGE
- strcasecmp(argv[0], "getrusage") == 0 ||
- #endif
- #ifdef MALLOCMAP
- strcasecmp(argv[0], "mallocmap") == 0 ||
- #endif
- strcasecmp(argv[0], "mode") == 0 ||
- strcasecmp(argv[0], "listnodelist") == 0 ||
- strcasecmp(argv[0], "listnewsfeeds") == 0 ||
- strcasecmp(argv[0], "pause") == 0 ||
- strcasecmp(argv[0], "go") == 0
- ) {
- fprintf(innbbsout, "%srn", argv[0]);
- fflush(innbbsout);
- fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
- printf("%s", INNBBSbuffer);
- if (strcasecmp(argv[0], "mode") == 0
- #ifdef GETRUSAGE
- ||
- strcasecmp(argv[0], "getrusage") == 0
- ||
- strcasecmp(argv[0], "mallocmap") == 0
- #endif
- ||
- strcasecmp(argv[0], "listnodelist") == 0
- ||
- strcasecmp(argv[0], "listnewsfeeds") == 0
- ) {
- while (fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin) != NULL) {
- if (strcmp(INNBBSbuffer, ".rn") == 0) {
- break;
- }
- printf("%s", INNBBSbuffer);
- }
- }
- } else if (strcasecmp(argv[0], "grephist") == 0 ||
- strcasecmp(argv[0], "verboselog") == 0) {
- if (argc < 2) {
- usage("ctlinnbbsd");
- } else {
- fprintf(innbbsout, "%s %srn", argv[0], argv[1]);
- fflush(innbbsout);
- fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
- printf("%sn", INNBBSbuffer);
- }
- } else if (strcasecmp(argv[0], "addhist") == 0) {
- if (argc < 3) {
- usage("ctlinnbbsd");
- } else {
- fprintf(innbbsout, "%s %s %srn", argv[0], argv[1], argv[2]);
- fflush(innbbsout);
- fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
- printf("%s", INNBBSbuffer);
- }
- } else {
- fprintf(stderr, "invalid command %sn", argv[0]);
- }
- if (strcasecmp(argv[0], "shutdown") != 0) {
- fprintf(innbbsout, "QUITrn");
- fflush(innbbsout);
- fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
- }
- }
- initsocket()
- {
- innbbsfd = unixclient(DefaultPath, "tcp");
- if (innbbsfd < 0) {
- fprintf(stderr, "Connect to %s error. You may not run innbbsdn", DefaultPath);
- exit(2);
- }
- if ((innbbsin = fdopen(innbbsfd, "r")) == NULL ||
- (innbbsout = fdopen(innbbsfd, "w")) == NULL) {
- fprintf(stderr, "fdopen errorn");
- exit(3);
- }
- }
- closesocket()
- {
- if (innbbsin != NULL)
- fclose(innbbsin);
- if (innbbsout != NULL)
- fclose(innbbsout);
- if (innbbsfd >= 0)
- close(innbbsfd);
- }
- main(argc, argv)
- int argc;
- char **argv;
- {
- int c, errflag = 0;
- while ((c = getopt(argc, argv, "p:h?")) != -1)
- switch (c) {
- case 'p':
- DefaultPath = optarg;
- break;
- case 'h':
- case '?':
- default:
- errflag++;
- break;
- }
- if (errflag > 0) {
- usage(argv[0]);
- return (1);
- }
- if (argc - optind < 1) {
- usage(argv[0]);
- exit(1);
- }
- initial_bbs(NULL);
- initsocket();
- ctlinnbbsd(argc - optind, argv + optind);
- closesocket();
- }