/* * Command parser. */ static void cmdscanner(void) { register struct cmd *c; static EditLine *el; static History *hist; HistEvent he; size_t len; int num; const char *bp; num = 0; bp = NULL; el = NULL; hist = NULL; for (;;) { if (fromatty) { if (!el) { el = el_init("lpc", stdin, stdout, stderr); hist = history_init(); history(hist, &he, H_SETSIZE, 100); el_set(el, EL_HIST, history, hist); el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_PROMPT, lpc_prompt); el_set(el, EL_SIGNAL, 1); el_source(el, NULL); /* * EditLine init may call 'cgetset()' to set a * capability-db meant for termcap (eg: to set * terminal type 'xterm'). Reset that now, or * that same db-information will be used for * printcap (giving us an "xterm" printer, with * all kinds of invalid capabilities...). */ cgetset(NULL); } if ((bp = el_gets(el, &num)) == NULL || num == 0) quit(0, NULL); len = (num > MAX_CMDLINE - 1) ? MAX_CMDLINE - 1 : num; memcpy(cmdline, bp, len); cmdline[len] = 0; history(hist, &he, H_ENTER, bp); } else { if (fgets(cmdline, MAX_CMDLINE, stdin) == 0) quit(0, NULL); if (cmdline[0] == 0 || cmdline[0] == '\n') break; } makeargv(); if (margc == 0) continue; if (el != NULL && el_parse(el, margc, margv) != -1) continue; c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); continue; } if (c == 0) { printf("?Invalid command\n"); continue; } if ((c->c_opts & LPC_PRIVCMD) && getuid() && ingroup(LPR_OPER) == 0) { printf("?Privileged command\n"); continue; } /* * Two different commands might have the same generic rtn * (eg: "clean" and "tclean"), and just use different * handler routines for distinct command-setup. The handler * routine might also be set on a generic routine for * initial parameter processing. */ if (c->c_generic != 0) generic(c->c_generic, c->c_opts, c->c_handler, margc, margv); else (*c->c_handler)(margc, margv); } }
/* * Receive file(s). */ void get(int argc, char *argv[]) { int fd; int n; char *cp; char *src; if (argc < 2) { strlcpy(line, "get ", sizeof(line)); printf("(files) "); readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin); if (makeargv()) return; argc = margc; argv = margv; } if (argc < 2) { getusage(argv[0]); return; } if (!connected) { for (n = 1; n < argc; n++) if (strrchr(argv[n], ':') == 0) { getusage(argv[0]); return; } } for (n = 1; n < argc; n++) { src = strrchr(argv[n], ':'); if (src == NULL) src = argv[n]; else { char *cp; *src++ = 0; cp = argv[n]; if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') { cp[strlen(cp) - 1] = '\0'; cp++; } setpeer(cp, NULL); if (!connected) continue; } if (argc < 4) { cp = argc == 3 ? argv[2] : tail(src); fd = creat(cp, 0644); if (fd < 0) { warn("create: %s", cp); return; } if (verbose) printf("getting from %s:%s to %s [%s]\n", hostname, src, cp, mode); recvfile(fd, src, mode); break; } cp = tail(src); /* new .. jdg */ fd = creat(cp, 0644); if (fd < 0) { warn("create: %s", cp); continue; } if (verbose) printf("getting from %s:%s to %s [%s]\n", hostname, src, cp, mode); recvfile(fd, src, mode); } }
/* * Send file(s). */ void put(int argc, char *argv[]) { int fd; int n; char *cp, *targ; if (argc < 2) { strlcpy(line, "send ", sizeof(line)); printf("(file) "); readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin); if (makeargv()) return; argc = margc; argv = margv; } if (argc < 2) { putusage(argv[0]); return; } targ = argv[argc - 1]; if (strchr(argv[argc - 1], ':')) { struct hostent *hp; for (n = 1; n < argc - 1; n++) if (strchr(argv[n], ':')) { putusage(argv[0]); return; } cp = argv[argc - 1]; targ = strchr(cp, ':'); *targ++ = 0; hp = gethostbyname(cp); if (hp == NULL) { warnx("%s: %s", cp, hstrerror(h_errno)); return; } bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, hp->h_length); peeraddr.sin_family = hp->h_addrtype; connected = 1; port = sp->s_port; strlcpy(hostname, hp->h_name, sizeof(hostname)); } if (!connected) { printf("No target machine specified.\n"); return; } if (argc < 4) { cp = argc == 2 ? tail(targ) : argv[1]; fd = open(cp, O_RDONLY); if (fd < 0) { warn("open: %s", cp); return; } if (verbose) printf("putting %s to %s:%s [%s]\n", cp, hostname, targ, mode); peeraddr.sin_port = port; sendfile(fd, targ, mode); return; } /* * this assumes the target is a directory on * on a remote unix system. hmmmm. */ for (n = 1; n < argc - 1; n++) { if (asprintf(&cp, "%s/%s", targ, tail(argv[n])) == -1) err(1, "asprintf"); fd = open(argv[n], O_RDONLY); if (fd < 0) { warn("open: %s", argv[n]); free(cp); continue; } if (verbose) printf("putting %s to %s:%s [%s]\n", argv[n], hostname, cp, mode); peeraddr.sin_port = port; sendfile(fd, cp, mode); free(cp); } }
/* * Receive file(s). */ void get(int argc, char *argv[]) { int fd; int n; char *cp; char *src; if (argc < 2) { strlcpy(line, "get ", sizeof(line)); printf("(files) "); readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin); if (makeargv()) return; argc = margc; argv = margv; } if (argc < 2) { getusage(argv[0]); return; } if (!connected) { for (n = 1; n < argc; n++) if (strchr(argv[n], ':') == 0) { getusage(argv[0]); return; } } for (n = 1; n < argc; n++) { src = strchr(argv[n], ':'); if (src == NULL) src = argv[n]; else { struct hostent *hp; *src++ = 0; hp = gethostbyname(argv[n]); if (hp == NULL) { warnx("%s: %s", argv[n], hstrerror(h_errno)); continue; } bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, hp->h_length); peeraddr.sin_family = hp->h_addrtype; connected = 1; strlcpy(hostname, hp->h_name, sizeof(hostname)); } if (argc < 4) { cp = argc == 3 ? argv[2] : tail(src); fd = creat(cp, 0644); if (fd < 0) { warn("create: %s", cp); return; } if (verbose) printf("getting from %s:%s to %s [%s]\n", hostname, src, cp, mode); peeraddr.sin_port = port; recvfile(fd, src, mode); break; } cp = tail(src); /* new .. jdg */ fd = creat(cp, 0644); if (fd < 0) { warn("create: %s", cp); continue; } if (verbose) printf("getting from %s:%s to %s [%s]\n", hostname, src, cp, mode); peeraddr.sin_port = port; recvfile(fd, src, mode); } }
void setpeer (int argc, char *argv[]) { struct hostent *host; if (argc < 2) { getmoreargs ("connect ", "(to) "); makeargv (); argc = margc; argv = margv; } if ((argc < 2) || (argc > 3)) { printf ("usage: %s host-name [port]\n", argv[0]); return; } host = gethostbyname (argv[1]); if (host == 0) { connected = 0; printf ("%s: unknown host\n", argv[1]); return; } peeraddr.sin_family = host->h_addrtype; bcopy (host->h_addr, &peeraddr.sin_addr, host->h_length); hostname = xstrdup (host->h_name); port = sp->s_port; if (argc == 3) { struct servent *usp; usp = getservbyname (argv[2], "udp"); if (usp) { port = usp->s_port; } else { unsigned long myport; char *ep; myport = strtoul (argv[2], &ep, 10); if (*ep || myport > 65535UL) { printf ("%s: bad port number\n", argv[2]); connected = 0; return; } port = htons ((u_short) myport); } } if (verbose) { printf ("Connected to %s (%s), port %u\n", hostname, inet_ntoa (peeraddr.sin_addr), (unsigned int) ntohs (port)); } connected = 1; }
int main(void) { char delim[] = " \t"; char inbuf[MAX_BUFFER]; char **chargv; ServerID_t serverID; ACSHandle_t acsHandle; int i, numtokens; char *chInput; char *chargv0; //DJ_U8 g_u8UnitID = 255; int row, col; int curRow, curCol; char *chargv1; char *chargv2; initCursel(&row, &col); printstr(NULL, "This is a monitor tool to SS7\n"); printstr(NULL, "press q or ctrl+c exit programmer\n"); printstr(NULL, "Input service IP and port(q exit, h help):\n"); for (; ;) { getyx(stdscr, curRow, curCol); if (curRow == row -1) clear(); getstr(inbuf); if (ldebug_flag) printstr(NULL,"inbuf:%s\n", inbuf); chInput = strlwr(inbuf); if (strcmp(chInput, QUIT_STRING) == 0) { exitwin(); return 0; } else if(strcmp(chInput, HELP_STRING) == 0) { helpProc(); } else { if ((numtokens = makeargv(inbuf, delim, &chargv)) >= 4) { if (ldebug_flag) { for (i = 0; i < numtokens; i++) printstr(NULL,"%d:%s\n", i, chargv[i]); } chargv1 = strlwr(chargv[0]); chargv2 = strlwr(chargv[2]); if ((strcmp(chargv1, "ip") == 0) && (strcmp(chargv[1], "\0") != 0)) { strncpy(serverID.m_s8ServerIp, chargv[1], sizeof(serverID.m_s8ServerIp)); if ((strcmp(chargv2, "port") == 0)&& (strcmp(chargv[3], "\0") != 0)) { serverID.m_u32ServerPort = atoi(chargv[3]); if (initsystem(serverID, &acsHandle) >= 0) { getyx(stdscr, curRow, curCol); mvprintw(curRow, 0,"Service has been monitored\n"); usleep(500); break; } else printstr(NULL,"Please check IP and Port, and please input again(q exit, h help)\n"); } else { printstr(NULL,"Input Port error, please input again(q exit, h help)\n"); } } else { printstr(NULL,"Input IP error,please input again(q exit, h help)\n"); } } else { printstr(NULL,"Input IP and Port has been omittem,please input again(q exit, h help)\n"); } } } for(; ;) { getyx(stdscr, curRow, curCol); if (curRow == row -1) clear(); getstr(inbuf); chInput = strlwr(inbuf); if (ldebug_flag) printstr(NULL,"inbuf:%s\n", chInput); if (strcmp(chInput, QUIT_STRING) == 0) { exitsystem(acsHandle); exitwin(); exit(0); } else if(strcmp(chInput, HELP_STRING) == 0) { helpProc(); } else { if ((numtokens = makeargv(inbuf, delim, &chargv)) > 0) { chargv0 = strlwr(chargv[0]); if (strcmp(chargv0, "monitor") == 0) { if (ldebug_flag) printstr(NULL,"monitor paramiter number is %d\n", numtokens); monitorProc(chargv, acsHandle, numtokens); printstr(NULL,"Input command again if operator(q exit, h help)\n"); } else if(strcmp(chargv0, "maintain") == 0) { maintainProc(chargv, acsHandle, numtokens); printstr(NULL,"Input command again if operator(q exit, h help)\n");; } else { printstr(NULL,"Input command error, please input again(q exit, h help)\n"); } } free(chargv); } usleep(30); } exitwin(); return 0; }
void domacro(int argc, char *argv[]) { int margc; char *marg; char **margv; register int i, j; register char *cp1, *cp2; int count = 2, loopflg = 0; char line2[200]; struct cmd *c; if (argc < 2 && !another(&argc, &argv, "macro name")) { printf("Usage: %s macro_name.\n", argv[0]); code = -1; return; } for (i = 0; i < macnum; ++i) { if (!strncmp(argv[1], macros[i].mac_name, 9)) { break; } } if (i == macnum) { printf("'%s' macro not found.\n", argv[1]); code = -1; return; } (void) strcpy(line2, line); TOP: cp1 = macros[i].mac_start; while (cp1 != macros[i].mac_end) { while (isspace(*cp1)) { cp1++; } cp2 = line; while (*cp1 != '\0') { switch(*cp1) { case '\\': *cp2++ = *++cp1; break; case '$': if (isdigit(*(cp1+1))) { j = 0; while (isdigit(*++cp1)) { j = 10*j + *cp1 - '0'; } cp1--; if (argc - 2 >= j) { (void) strcpy(cp2, argv[j+1]); cp2 += strlen(argv[j+1]); } break; } if (*(cp1+1) == 'i') { loopflg = 1; cp1++; if (count < argc) { (void) strcpy(cp2, argv[count]); cp2 += strlen(argv[count]); } break; } /* intentional drop through */ default: *cp2++ = *cp1; break; } if (*cp1 != '\0') { cp1++; } } *cp2 = '\0'; margv = makeargv(&margc, &marg); c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); code = -1; } else if (c == NULL) { printf("?Invalid command\n"); code = -1; } else if (c->c_conn && !connected) { printf("Not connected.\n"); code = -1; } else { if (ftpverbose) { printf("%s\n",line); } if (c->c_handler_v) c->c_handler_v(margc, margv); else if (c->c_handler_0) c->c_handler_0(); else c->c_handler_1(marg); if (bell && c->c_bell) { (void) putchar('\007'); } (void) strcpy(line, line2); margv = makeargv(&margc, &marg); argc = margc; argv = margv; } if (cp1 != macros[i].mac_end) { cp1++; } } if (loopflg && ++count < argc) { goto TOP; } }
/* * Command parser. */ void cmdscanner(void) { struct cmd *c; char *p; #ifndef NO_EDITCOMPLETE int ch; #endif size_t num; for (;;) { #ifndef NO_EDITCOMPLETE if (!editing) { #endif /* !NO_EDITCOMPLETE */ if (fromatty) { fputs(prompt(), ttyout); p = rprompt(); if (*p) fprintf(ttyout, "%s ", p); } (void)fflush(ttyout); num = getline(stdin, line, sizeof(line), NULL); switch (num) { case -1: /* EOF */ case -2: /* error */ if (fromatty) putc('\n', ttyout); quit(0, NULL); /* NOTREACHED */ case -3: /* too long; try again */ fputs("Sorry, input line is too long.\n", ttyout); continue; case 0: /* empty; try again */ continue; default: /* all ok */ break; } #ifndef NO_EDITCOMPLETE } else { const char *buf; HistEvent ev; cursor_pos = NULL; buf = el_gets(el, &ch); num = ch; if (buf == NULL || num == 0) { if (fromatty) putc('\n', ttyout); quit(0, NULL); } if (num >= sizeof(line)) { fputs("Sorry, input line is too long.\n", ttyout); break; } memcpy(line, buf, num); if (line[--num] == '\n') { line[num] = '\0'; if (num == 0) break; } history(hist, &ev, H_ENTER, buf); } #endif /* !NO_EDITCOMPLETE */ makeargv(); if (margc == 0) continue; c = getcmd(margv[0]); if (c == (struct cmd *)-1) { fputs("?Ambiguous command.\n", ttyout); continue; } if (c == NULL) { #if !defined(NO_EDITCOMPLETE) /* * attempt to el_parse() unknown commands. * any command containing a ':' would be parsed * as "[prog:]cmd ...", and will result in a * false positive if prog != "ftp", so treat * such commands as invalid. */ if (strchr(margv[0], ':') != NULL || el_parse(el, margc, (const char **)margv) != 0) #endif /* !NO_EDITCOMPLETE */ fputs("?Invalid command.\n", ttyout); continue; } if (c->c_conn && !connected) { fputs("Not connected.\n", ttyout); continue; } confirmrest = 0; margv[0] = c->c_name; (*c->c_handler)(margc, margv); if (bell && c->c_bell) (void)putc('\007', ttyout); if (c->c_handler != help) break; } (void)xsignal(SIGINT, intr); (void)xsignal(SIGPIPE, lostpeer); }
/* * Command parser. */ void cmdscanner(int top) { struct cmd *c; int num; #if 0 HistEvent hev; if (!top && !editing) (void)putc('\n', ttyout); #endif for (;;) { #if 0 if (!editing) { #endif if (fromatty) { fputs(prompt(), ttyout); (void)fflush(ttyout); } if (fgets(line, sizeof(line), stdin) == NULL) quit(0, 0); num = strlen(line); if (num == 0) break; if (line[--num] == '\n') { if (num == 0) break; line[num] = '\0'; } else if (num == sizeof(line) - 2) { fputs("sorry, input line too long.\n", ttyout); while ((num = getchar()) != '\n' && num != EOF) /* void */; break; } /* else it was a line without a newline */ #if 0 } else { const char *buf; cursor_pos = NULL; if ((buf = el_gets(el, &num)) == NULL || num == 0) quit(0, 0); if (buf[--num] == '\n') { if (num == 0) break; } if (num >= sizeof(line)) { fputs("sorry, input line too long.\n", ttyout); break; } memcpy(line, buf, (size_t)num); line[num] = '\0'; history(hist, &hev, H_ENTER, buf); } #endif makeargv(); if (margc == 0) continue; c = getcmd(margv[0]); if (c == (struct cmd *)-1) { fputs("?Ambiguous command.\n", ttyout); continue; } if (c == 0) { #if 0 /* * Give editline(3) a shot at unknown commands. * XXX - bogus commands with a colon in * them will not elicit an error. */ if (editing && el_parse(el, margc, (const char **)margv) != 0) #endif fputs("?Invalid command.\n", ttyout); continue; } if (c->c_conn && !connected) { fputs("Not connected.\n", ttyout); continue; } confirmrest = 0; (*c->c_handler)(margc, margv); if (bell && c->c_bell) (void)putc('\007', ttyout); if (c->c_handler != help) break; } (void)signal(SIGINT, (sig_t)intr); (void)signal(SIGPIPE, (sig_t)lostpeer); }
//This function will parse makefile input from user or default makeFile. int parse(char * lpszFileName) { int nLine=0; char szLine[1024]; char * lpszLine; FILE * fp = file_open(lpszFileName); if(fp == NULL) { return -1; } /* This is not a simple initializer but an executable code */ make406.current_id = 0; while(file_getline(szLine, fp) != NULL) { nLine++; // this loop will go through the given file, one line at a time // this is where you need to do the work of interpreting // each line of the file to be able to deal with it later //Remove newline character at end if there is one lpszLine = strtok(szLine, "\n"); //Skip if blank or comment. if(szLine[0] == '#' || szLine[0] == '\n') continue; char **argvadr; int ntokens = makeargv(lpszLine, " ", &argvadr); //Remove leading whitespace. //Skip if whitespace-only. if(ntokens <= 0) continue; int curr = -1; /* Indicates the current line status */ if(argvadr[0][strlen(argvadr[0]) - 1] != ':') { fprintf(stderr, "%d line should be target", nLine); return -1; } curr = TARGET; /* We are right now in a target line */ /* Initialize a node in the MakeTree*/ target_t *node = malloc(sizeof(target_t)); make406.nodelist[make406.current_id++] = node; /* TODO: push this node's target field. */ strncpy(node->szTarget, argvadr[0], strlen(argvadr[0])-1); node->nDependencyCount = ntokens-1; int i; for(i=0; i<ntokens-1; ++i) strcpy(node->szDependencies[i], argvadr[i+1]); if(file_getline(szLine, fp) == NULL) { fprintf(stderr, "%d line is missing", nLine); return -1; } nLine++; lpszLine = strtok(szLine, "\n"); if((lpszLine[0] != '\t')) { fprintf(stderr, "%d line should be command", nLine); return -1; } curr = COMMAND; /* We are currently in a command line */ strcpy(node->szCommand, lpszLine); for(i = 0; i < ntokens; ++i) { printf("\t%d\t%d\t%d\t%s\n", nLine, curr, ntokens, argvadr[i]); } //Only single command is allowed. //If you found any syntax error, stop parsing. } // Close the makefile. fclose(fp); // Printing out the nodes here. int i,j; i = 0; for( ; i<make406.current_id; ++i) { printf("%s\n", make406.nodelist[i]->szTarget); j = 0; printf("no_dependencies: %d\n", make406.nodelist[i]->nDependencyCount); for( ; j < make406.nodelist[i]->nDependencyCount; ++j) printf("\t%s\n", make406.nodelist[i]->szDependencies[j]); } pid_t main_child_pid; int status = 0; generate_parsingtree(make406.nodelist[0], &main_child_pid); main_child_pid = waitpid(main_child_pid, &status, 0); return 0; }
//Handles all forking and exec'ing of commands. Execs the commands (if there are //any) for the processes with zero dependencies that have not been built. //Decriments the number of dependencies for parents. Also handles timestamps. int forkExec(Node **toBeExeced, int numElements){ int i; int k = 0; int comp; if(strcmp(targ, "clean")==0) commands[1] = 1; for(i=0;i<numElements;i++){ char **execargv; pid_t childpid; int execargc; int status; int recompile = 1; //if recompile is 1, then build //-n flag set if(commands[1] == 0){ recompile = 1; for(k = 0;k<toBeExeced[i]->sizeDepends;k++){ comp = compare_modification_time(toBeExeced[i]->dependencies[k],toBeExeced[i]->target); int child_timestamp = get_file_modification_time(toBeExeced[i]->dependencies[k]); int parent_timestamp = get_file_modification_time(toBeExeced[i]->target); //if the timestamp for one doesn't exist, or the timestamp of the child is greater (newer) than the parent if(comp < 2 || (parent_timestamp == -1)){ recompile = 1; } } } //if child is older than the parent, don't rebuild if(recompile == 0 && commands[0] != 1){ continue; } if (strcmp (toBeExeced[i]->command, " ") != 0){ if(commands[0] == 1){ printf("%s\n", toBeExeced[i]->command); if (toBeExeced[i]->toParent != NULL){ toBeExeced[i]->toParent->numTargetDep--; } continue; } execargc = makeargv (toBeExeced[i]->command," ",&execargv); toBeExeced[i]->pid = childpid = fork(); }else { continue; } if (childpid == -1){ perror("Failed to Fork\n"); return -1; } if(childpid ==0){ execvp(execargv[0],&execargv[0]); perror("Child failed to Exec \n"); return -1; } if(childpid > 0){ wait(&status); if(toBeExeced[i]->toParent != NULL){ toBeExeced[i]->toParent->numTargetDep--; } } freemakeargv(execargv); } return 1; }
/* * Receive file(s). */ void get (int argc, char *argv[]) { int fd; int n; char *cp; char *src; if (argc < 2) { getmoreargs ("get ", "(files) "); makeargv (); argc = margc; argv = margv; } if (argc < 2) { getusage (argv[0]); return; } if (!connected) { for (n = 1; n < argc; n++) if (strchr (argv[n], ':') == 0) { getusage (argv[0]); return; } } for (n = 1; n < argc; n++) { src = strchr (argv[n], ':'); if (src == NULL) src = argv[n]; else { struct hostent *hp; *src++ = 0; hp = gethostbyname (argv[n]); if (hp == NULL) { fprintf (stderr, "tftp: %s: ", argv[n]); herror ((char *) NULL); continue; } bcopy (hp->h_addr, (caddr_t) & peeraddr.sin_addr, hp->h_length); peeraddr.sin_family = hp->h_addrtype; connected = 1; hostname = xstrdup (hp->h_name); } if (argc < 4) { cp = argc == 3 ? argv[2] : tail (src); fd = open (cp, O_WRONLY | O_CREAT | O_TRUNC | mode->m_openflags, 0666); if (fd < 0) { fprintf (stderr, "tftp: "); perror (cp); return; } if (verbose) printf ("getting from %s:%s to %s [%s]\n", hostname, src, cp, mode->m_mode); peeraddr.sin_port = port; tftp_recvfile (fd, src, mode->m_mode); break; } cp = tail (src); /* new .. jdg */ fd = open (cp, O_WRONLY | O_CREAT | O_TRUNC | mode->m_openflags, 0666); if (fd < 0) { fprintf (stderr, "tftp: "); perror (cp); continue; } if (verbose) printf ("getting from %s:%s to %s [%s]\n", hostname, src, cp, mode->m_mode); peeraddr.sin_port = port; tftp_recvfile (fd, src, mode->m_mode); } }
/* * Send file(s). */ void put (int argc, char *argv[]) { int fd; int n; char *cp, *targ; if (argc < 2) { getmoreargs ("send ", "(file) "); makeargv (); argc = margc; argv = margv; } if (argc < 2) { putusage (argv[0]); return; } targ = argv[argc - 1]; if (strchr (argv[argc - 1], ':')) { struct hostent *hp; for (n = 1; n < argc - 1; n++) if (strchr (argv[n], ':')) { putusage (argv[0]); return; } cp = argv[argc - 1]; targ = strchr (cp, ':'); *targ++ = 0; hp = gethostbyname (cp); if (hp == NULL) { fprintf (stderr, "tftp: %s: ", cp); herror ((char *) NULL); return; } bcopy (hp->h_addr, (caddr_t) & peeraddr.sin_addr, hp->h_length); peeraddr.sin_family = hp->h_addrtype; connected = 1; hostname = xstrdup (hp->h_name); } if (!connected) { printf ("No target machine specified.\n"); return; } if (argc < 4) { cp = argc == 2 ? tail (targ) : argv[1]; fd = open (cp, O_RDONLY | mode->m_openflags); if (fd < 0) { fprintf (stderr, "tftp: "); perror (cp); return; } if (verbose) printf ("putting %s to %s:%s [%s]\n", cp, hostname, targ, mode->m_mode); peeraddr.sin_port = port; tftp_sendfile (fd, targ, mode->m_mode); return; } /* this assumes the target is a directory */ /* on a remote unix system. hmmmm. */ cp = strchr (targ, '\0'); *cp++ = '/'; for (n = 1; n < argc - 1; n++) { strcpy (cp, tail (argv[n])); fd = open (argv[n], O_RDONLY | mode->m_openflags); if (fd < 0) { fprintf (stderr, "tftp: "); perror (argv[n]); continue; } if (verbose) printf ("putting %s to %s:%s [%s]\n", argv[n], hostname, targ, mode->m_mode); peeraddr.sin_port = port; tftp_sendfile (fd, targ, mode->m_mode); } }
/* * Command parser. */ void cmdscanner (int top) { struct cmd *c; int l; if (!top) putchar ('\n'); for (;;) { if (line) { free (line); line = NULL; } line = readline (prompt); if (!line) quit (0, 0); l = strlen (line); if (l >= MAXLINE) { printf ("Line too long.\n"); break; } #if HAVE_LIBHISTORY if (line && *line) add_history (line); #endif if (l == 0) break; makeargv (); if (margc == 0) continue; c = getcmd (margv[0]); if (c == (struct cmd *) -1) { printf ("?Ambiguous command\n"); continue; } if (c == 0) { printf ("?Invalid command\n"); continue; } if (c->c_conn && !connected) { printf ("Not connected.\n"); continue; } (*c->c_handler) (margc, margv); if (bell && c->c_bell) putchar ('\007'); if (c->c_handler != help) break; } signal (SIGINT, intr); signal (SIGPIPE, lostpeer); }
int dologin(const char *host) { char tmp[80]; char *luser, *pass, *zacct; int n, aflag = 0; luser = pass = zacct = 0; if (xruserpass(host, &luser, &pass, &zacct) < 0) { code = -1; return(0); } while (luser == NULL) { char *myname = getlogin(); if (myname == NULL) { struct passwd *pp = getpwuid(getuid()); if (pp != NULL) myname = pp->pw_name; } if (myname) printf("Name (%s:%s): ", host, myname); else printf("Name (%s): ", host); #ifdef EMBED fflush(stdout); #endif if (fgets(tmp, sizeof(tmp) - 1, stdin)==NULL) { fprintf(stderr, "\nLogin failed.\n"); return 0; } tmp[strlen(tmp) - 1] = '\0'; if (*tmp == '\0') luser = myname; else luser = tmp; } n = command("USER %s", luser); if (n == CONTINUE) { if (pass == NULL) { /* fflush(stdout); */ pass = getpass("Password:"******"PASS %s", pass); } if (n == CONTINUE) { aflag++; /* fflush(stdout); */ zacct = getpass("Account:"); n = command("ACCT %s", zacct); } if (n != COMPLETE) { fprintf(stderr, "Login failed.\n"); return (0); } if (!aflag && zacct != NULL) (void) command("ACCT %s", zacct); if (proxy) return(1); for (n = 0; n < macnum; ++n) { if (!strcmp("init", macros[n].mac_name)) { int margc; char **margv; strcpy(line, "$init"); margv = makeargv(&margc, NULL); domacro(margc, margv); break; } } return (1); }
bool Process::start( const QString &cmd ) { if( _state!=INIT ) return false; #ifdef Q_OS_WIN HANDLE in[2],out[2],err[2]; SECURITY_ATTRIBUTES sa= {sizeof(sa),0,1}; CreatePipe( &in[0],&in[1],&sa,0 ); CreatePipe( &out[0],&out[1],&sa,0 ); CreatePipe( &err[0],&err[1],&sa,0 ); STARTUPINFOW si= {sizeof(si)}; si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; si.hStdInput=in[0]; si.hStdOutput=out[1]; si.hStdError=err[1]; si.wShowWindow=SW_HIDE; PROCESS_INFORMATION pi= {0}; int res=CreateProcessW( 0,(LPWSTR)cmd.toStdWString().c_str(),0,0,-1,CREATE_NEW_PROCESS_GROUP,0,0,&si,&pi ); CloseHandle( in[0] ); CloseHandle( out[1] ); CloseHandle( err[1] ); if( !res ) { CloseHandle( in[1] ); CloseHandle( out[0] ); CloseHandle( err[0] ); return false; } CloseHandle( pi.hThread ); _pid=pi.hProcess; _in=in[1]; _out=out[0]; _err=err[0]; #else int in[2],out[2],err[2]; pipe( in ); pipe( out ); pipe( err ); _pid=vfork(); if( !_pid ) { #if __linux setsid(); #else setpgid(0,0); #endif dup2( in[PIPE_READ],STDIN_FILENO ); dup2( out[PIPE_WRITE],STDOUT_FILENO ); dup2( err[PIPE_WRITE],STDERR_FILENO ); close( in[PIPE_READ] ); close( out[PIPE_WRITE] ); close( err[PIPE_WRITE] ); close( in[PIPE_WRITE] ); close( out[PIPE_READ] ); close( err[PIPE_READ] ); char **argv=makeargv( cmd.toStdString().c_str() ); execvp( argv[0],argv ); _exit( -1 ); return false; } close( in[PIPE_READ] ); close( out[PIPE_WRITE] ); close( err[PIPE_WRITE] ); if( _pid==-1 ) { close( in[PIPE_WRITE] ); close( out[PIPE_READ] ); close( err[PIPE_READ] ); return false; } _in=in[PIPE_WRITE]; _out=out[PIPE_READ]; _err=err[PIPE_READ]; #endif _procwaiter=new ProcWaiter( _pid ); connect( _procwaiter,SIGNAL(finished()),SLOT(onFinished()) ); _linereaders[0]=new LineReader( _out ); connect( _linereaders[0],SIGNAL(finished()),SLOT(onFinished()) ); _linereaders[1]=new LineReader( _err ); connect( _linereaders[1],SIGNAL(finished()),SLOT(onFinished()) ); _procwaiter->start(); _linereaders[0]->start(); _linereaders[1]->start(); _state=RUNNING; return true; }
int main(int argc, char *argv[]) { register struct cmd *c; openlog("timedc", LOG_ODELAY, LOG_AUTH); /* * security dictates! */ if (priv_resources() < 0) errx(1, "could not get privileged resources"); if (setuid(getuid()) != 0) err(1, "setuid()"); if (--argc > 0) { c = getcmd(*++argv); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); exit(1); } if (c == 0) { printf("?Invalid command\n"); exit(1); } if (c->c_priv && getuid()) { printf("?Privileged command\n"); exit(1); } (*c->c_handler)(argc, argv); exit(0); } fromatty = isatty(fileno(stdin)); if (setjmp(toplevel)) putchar('\n'); (void) signal(SIGINT, intr); for (;;) { if (fromatty) { printf("timedc> "); (void) fflush(stdout); } if (fgets(cmdline, sizeof(cmdline), stdin) == NULL) quit(); if (cmdline[0] == 0) break; makeargv(); if (margv[0] == 0) continue; c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); continue; } if (c == 0) { printf("?Invalid command\n"); continue; } if (c->c_priv && getuid()) { printf("?Privileged command\n"); continue; } (*c->c_handler)(margc, margv); } return 0; }
/* * Send file(s). */ void put(int argc, char *argv[]) { int fd; int n; char *cp, *targ; if (argc < 2) { strlcpy(line, "put ", sizeof(line)); printf("(file) "); readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin); if (makeargv()) return; argc = margc; argv = margv; } if (argc < 2) { putusage(argv[0]); return; } targ = argv[argc - 1]; if (strrchr(argv[argc - 1], ':')) { for (n = 1; n < argc - 1; n++) if (strchr(argv[n], ':')) { putusage(argv[0]); return; } cp = argv[argc - 1]; targ = strrchr(cp, ':'); *targ++ = 0; if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') { cp[strlen(cp) - 1] = '\0'; cp++; } setpeer(cp, NULL); } if (!connected) { printf("No target machine specified.\n"); return; } if (argc < 4) { cp = argc == 2 ? tail(targ) : argv[1]; fd = open(cp, O_RDONLY); if (fd < 0) { warn("open: %s", cp); return; } if (verbose) printf("putting %s to %s:%s [%s]\n", cp, hostname, targ, mode); sendfile(fd, targ, mode); return; } /* * this assumes the target is a directory on * on a remote unix system. hmmmm. */ for (n = 1; n < argc - 1; n++) { if (asprintf(&cp, "%s/%s", targ, tail(argv[n])) == -1) err(1, "asprintf"); fd = open(argv[n], O_RDONLY); if (fd < 0) { warn("open: %s", argv[n]); free(cp); continue; } if (verbose) printf("putting %s to %s:%s [%s]\n", argv[n], hostname, cp, mode); sendfile(fd, cp, mode); free(cp); } }
int main(int argc, char *argv[]) { const struct cmd *c; fcntl(3, F_CLOSEM); openlog("timedc", 0, LOG_AUTH); /* * security dictates! */ if (priv_resources() < 0) errx(EXIT_FAILURE, "Could not get privileged resources"); if (drop_privileges() < 0) errx(EXIT_FAILURE, "Could not drop privileges"); if (--argc > 0) { c = getcmd(*++argv); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); exit(EXIT_FAILURE); } if (c == 0) { printf("?Invalid command\n"); exit(EXIT_FAILURE); } (*c->c_handler)(argc, argv); exit(EXIT_SUCCESS); } fromatty = isatty(fileno(stdin)); if (setjmp(toplevel)) putchar('\n'); (void) signal(SIGINT, intr); for (;;) { if (fromatty) { printf("timedc> "); (void) fflush(stdout); } if (fgets(cmdline, sizeof(cmdline), stdin) == NULL) quit(0, NULL); if (cmdline[0] == 0) break; if (makeargv()) { printf("?Too many arguments\n"); continue; } if (margv[0] == 0) continue; c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); continue; } if (c == 0) { printf("?Invalid command\n"); continue; } (*c->c_handler)(margc, margv); } return 0; }