int main(int argc, char* argv[]) { char* buf; if (argc > 1) { buf = argv[1]; } else { buf = malloc(27 * sizeof(char)); if (get_stdin(buf, 27) != 0) { fprintf(stderr, HINTS); return 1; } } size_t length = strlen(buf); char* input = trits_from_trytes(buf, length); int i = 0; for(i = 0; i < length * 3; i++ ) { switch(input[i]) { case 1: { fprintf(stdout, "+"); }break; case -1: { fprintf(stdout, "-"); } break; default: { fprintf(stdout, "0"); } } } return 0; }
void reset_fd(void) { get_stdin(0); get_stdout(1); get_stderr(2); if (get_pipes(0)) while (get_pipes(-1)); }
int main( int argc, char *argv[]) { char *name = NULL; unsigned int age = 0; char *username = NULL; char *logfile = "logfile"; FILE *log = NULL; printf("What is your name? "); get_stdin(&name); printf("What is your age? "); scanf("%i", &age); printf("What is your username? "); get_stdin(&username); printf("your name is %s, you are %i years old, and your username is %s\n", name, age, username); log = fopen(logfile, "a"); fprintf(log, "your name is %s, you are %i years old, and your username is %s\n", name, age, username); return 0; }
void create_processes(char* buffer, char commands[][CMD_SIZE], char* envp[]) { int size = parse_cmds(buffer, commands); int pip[2], total = 0, pipe_in = 0; switch(fork()) { case -1: shell_error("fork()"); case 0: while(total != size) { if(pipe(pip) == -1) shell_error("pipe()"); //printf("pip : %d, %d\n", pip[0], pip[1]); char* temp[CMD_SIZE]; char* args[CMD_SIZE]; int used = separate_commands(total, commands, size, temp); get_arguments(temp, used, args); //NULL TERMINATED //print_args(temp, used); //print_args2(args); int in = get_stdin(temp, used, pipe_in); //get instream and outstream. int out = get_stdout(temp, used, pip); //printf("%d : %d\n", in, out); prepare_to_execute(args, envp, in, out); pipe_in = has_pipe(temp, used, pip); //printf("[%d]\n", pipe_in); total += used; } exit(1); default: if(!has_bg(commands, size)) if (wait(0) == -1) shell_error("wait()"); } }
bool WhmShellProcess::run(WhmShellContext *sc) { WhmShellCommand *c = command; bool result = true; Token_t token = NULL; #ifndef HTTP_PROXY if (runAsUser) { //创建运行身份令牌 if (sc->vh==NULL) { sc->last_error = 128; return false; } token = sc->vh->createToken(result); if(!result){ sc->last_error = 129; return false; } } #endif //the commands stdout pipe PIPE_T big_stdout_pipe[2]; if (!KPipeStream::create(big_stdout_pipe)) { if (token) { KVirtualHost::closeToken(token); } return false; } //the commands stdin pipe PIPE_T big_stdin_pipe[2]; bool big_stdin_pipe_created = false; //init the std file PIPE_T hstdin = get_stdin(sc); if (hstdin==INVALIDE_PIPE && sc->in_buffer.getLen()>0) { //如果没有输入文件并且输入有数据。就要创建输入管道 if (!KPipeStream::create(big_stdin_pipe)) { //关闭打开的big_stdout_pipe ClosePipe(big_stdout_pipe[0]); ClosePipe(big_stdout_pipe[1]); if (token) { KVirtualHost::closeToken(token); } return false; } big_stdin_pipe_created = true; hstdin = big_stdin_pipe[READ_PIPE]; } PIPE_T hstdout = get_stdout(sc); PIPE_T hstderr = get_stderr(sc); PIPE_T in,out,err; err = hstderr; if (err==INVALIDE_PIPE) { err = big_stdout_pipe[WRITE_PIPE]; } PIPE_T pp[2]; for(int i=0;i<2;i++){ pp[i] = INVALIDE_PIPE; } while (c) { char **arg = c->makeArgs(sc); KCmdEnv *env = c->makeEnv(sc); pid_t pid; kfinit(pid); //set in if (c==command) { in = hstdin; } else { assert(pp[READ_PIPE]!=INVALIDE_PIPE); in = pp[READ_PIPE]; pp[READ_PIPE] = INVALIDE_PIPE; assert(pp[WRITE_PIPE]!=INVALIDE_PIPE); //close the out pipe ClosePipe(pp[WRITE_PIPE]); pp[WRITE_PIPE] = INVALIDE_PIPE; } //set out if (c->next) { //create a new pipe if (!KPipeStream::create(pp)) { if (c!=command) { ClosePipe(in); } break; } out = pp[WRITE_PIPE]; } else { //if the command is last. //then set the out to big_pipe out = hstdout; if (out==INVALIDE_PIPE) { out = big_stdout_pipe[WRITE_PIPE]; } } char *curdir2 = NULL; if (curdir) { curdir2 = sc->parseString(curdir); } result = createProcess(token,arg,env,curdir2,in,out,err,pid); if (curdir2) { free(curdir2); } if (c!=command) { //close the in pipe ClosePipe(in); } //free args for (int i=0;;i++) { if(arg[i]==NULL){ break; } free(arg[i]); } delete[] arg; if (env) { delete env; } sc->setLastPid(pid); if (!result) { sc->last_error = 127; break; } c = c->next; } //关闭输入,输出父进程无用的管道端 if (kflike(hstdin)) { ClosePipe(hstdin); } ClosePipe(big_stdout_pipe[WRITE_PIPE]); //处理输入 if (big_stdin_pipe_created) { if (result) { //创建成功才写入数据 buff *buf = sc->in_buffer.getHead(); while (buf && buf->data) { if (write_pipe(big_stdin_pipe[WRITE_PIPE],buf->data,buf->used)!=buf->used) { break; } buf = buf->next; } } //清理输入数据和管道资源 sc->in_buffer.destroy(); ClosePipe(big_stdin_pipe[WRITE_PIPE]); } //处理输出 if (result && (hstdout==INVALIDE_PIPE || hstderr==INVALIDE_PIPE)) { for (;;) { char buf[512]; int len = read_pipe(big_stdout_pipe[READ_PIPE],buf,512); if (len<=0) { break; } sc->lock.Lock(); if (sc->out_buffer.getLen() > 1048576) { //the out msg is too large.drop it. sc->lock.Unlock(); fwrite(buf,1,len,stdout); } else { sc->out_buffer.write_all(buf,len); sc->lock.Unlock(); } //fwrite(buf,1,len,stdout); } } if (kflike(hstdout)) { ClosePipe(hstdout); } if (kflike(hstderr)) { ClosePipe(hstderr); } ClosePipe(big_stdout_pipe[READ_PIPE]); if (token) { KVirtualHost::closeToken(token); } #ifdef _WIN32 if (kflike(sc->last_pid)) { WaitForSingleObject(sc->last_pid,INFINITE); } #endif return result; }
/* ChapelIO.chpl:1 */ void __init_ChapelIO(int32_t _ln, _string _fn) { chpl_bool T1; _cfile T2; file T5 = NULL; file T3 = NULL; _syncvar_uint64_t T4 = NULL; _cfile T6; file T9 = NULL; file T7 = NULL; _syncvar_uint64_t T8 = NULL; _cfile T10; file T13 = NULL; file T11 = NULL; _syncvar_uint64_t T12 = NULL; T1 = (!__run_ChapelIO_firsttime3); if (T1) { goto _end___init_ChapelIO; } __run_ChapelIO_firsttime3 = false; T2 = get_stdin(); T3 = (file)chpl_alloc(sizeof(_file), "instance of class _unknown", _ln, _fn); ((object)T3)->_cid = _e_file; T3->filename = ""; T3->mode = FileAccessMode_read; T3->path = ""; T3->_fp = 0; T3->filename = "stdin"; T3->mode = FileAccessMode_read; T3->path = "/dev"; T3->_fp = T2; T4 = _construct__syncvar(UINT64(0), _ln, _fn); T3->_lock = T4; T5 = _construct_file("stdin", FileAccessMode_read, "/dev", T2, T4, T3, _ln, _fn); _stdin_16853 = T5; T6 = get_stdout(); T7 = (file)chpl_alloc(sizeof(_file), "instance of class _unknown", _ln, _fn); ((object)T7)->_cid = _e_file; T7->filename = ""; T7->mode = FileAccessMode_read; T7->path = ""; T7->_fp = 0; T7->filename = "stdout"; T7->mode = FileAccessMode_write; T7->path = "/dev"; T7->_fp = T6; T8 = _construct__syncvar(UINT64(0), _ln, _fn); T7->_lock = T8; T9 = _construct_file("stdout", FileAccessMode_write, "/dev", T6, T8, T7, _ln, _fn); _stdout_16871 = T9; T10 = get_stderr(); T11 = (file)chpl_alloc(sizeof(_file), "instance of class _unknown", _ln, _fn); ((object)T11)->_cid = _e_file; T11->filename = ""; T11->mode = FileAccessMode_read; T11->path = ""; T11->_fp = 0; T11->filename = "stderr"; T11->mode = FileAccessMode_write; T11->path = "/dev"; T11->_fp = T10; T12 = _construct__syncvar(UINT64(0), _ln, _fn); T11->_lock = T12; T13 = _construct_file("stderr", FileAccessMode_write, "/dev", T10, T12, T11, _ln, _fn); _stderr_16889 = T13; _end___init_ChapelIO: ; return; }
void loop_step(const char * input) { char string[65536]; char buffer[256]; // read a line if (input != NULL) { get_local(string,65536,input); } else { get_stdin(string,65536); } // parse if (false) { } else if (string_start_with(string,"debug ")) { if (!Searching) { parse_debug(string); } else { ASSERT(false); } } else if (string_start_with(string,"go ")) { if (!Searching && !Delay) { init(); parse_go(string); } else { ASSERT(false); } } else if (string_equal(string,"isready")) { if (!Searching && !Delay) { init(); } send("readyok"); // no need to wait when searching (fixit SMK) } else if (string_equal(string,"ponderhit")) { if (Searching) { ASSERT(Infinite); SearchInput->infinite = false; Infinite = false; } else if (Delay) { send_best_move(); Delay = false; } else { ASSERT(false); } } else if (string_start_with(string,"position ")) { if (!Searching && !Delay) { init(); parse_position(string); } else { ASSERT(false); } } else if (string_equal(string,"quit")) { ASSERT(!Searching); // violated by Arena UI ASSERT(!Delay); if (Searching && option_get_int("Threads") > 1) { // Arena issue SearchInfo->stop = true; Infinite = false; smp_sleep(); } trans_free(); tb_close(); egbb_close(); smp_close(); exit(EXIT_SUCCESS); } else if (string_start_with(string,"setoption ")) { if (!Searching && !Delay) { parse_setoption(string); } else { ASSERT(false); } } else if (string_equal(string,"stop")) { if (Searching) { SearchInfo->stop = true; Infinite = false; } else if (Delay) { send_best_move(); Delay = false; } } else if (string_equal(string,"uci")) { ASSERT(!Searching); ASSERT(!Delay); send("id name Fruit reloaded %s", my_version(buffer)); send("id author Fabien Letouzey, Ryan Benitez and Daniel Mehrmann"); option_list(); send("uciok"); } else if (string_equal(string,"ucinewgame")) { if (!Searching && !Delay && Init) { trans_clear(); pawn_clear(); material_clear(); } else { ASSERT(false); } } }
int main(int argc, char *argv[]) { int x, y, z, m, v4args, v6args, argcount; struct if_info *ifarg_start, *ifarg_cur; int ch, parse_stdin; struct misc_args m_argv4, m_argv6; int split_errv4, split_errv6, range_err; char oldcmdstr[ARGLEN] = ""; struct argbox *abox_start, *abox_cur, *abox_tmp; char *stdinarg[3]; ident = progname(argv[0]); if (argc < 2) return usage(1); parse_stdin = 0; /* * Our default v4 and v6 options, hopefully what's mostly used. */ v4args = V4_INFO; v6args = V6_INFO; m_argv4.splitmask = 0; m_argv4.numnets = 0; m_argv4.range_min = 0; m_argv4.range_max = 0; m_argv6.splitmask = 0; m_argv6.numnets = 0; split_errv4 = 0; split_errv6 = 0; range_err = 0; ifarg_start = NULL; /* * abox == argument box == a box that holds (commandline) arguments :) * This is the structure we use to store all user input parsed into * (hopefully) manageable chunks. * This excludes most of the -[a-z] flags, they're generally handled by * v[4,6]args. */ abox_start = abox_cur = (struct argbox *)calloc(1, sizeof(struct argbox)); /* * v[4,6]args holds flags based on commandline arguments for what we * want to output. */ while ((ch = getopt(argc, argv, "cehnrR:s:S:v")) != -1) { switch (ch) { case 'c': v4args |= V4CHECK; v6args |= V6CHECK; break; case 'e': v6args |= V4INV6; break; case 'n': colorize = 0; break; case 'r': v6args |= V6REV; break; case 'R': y = getrange_min_max(optarg, &m_argv4.range_min, &m_argv4.range_max); if (!y) { v4args |= V4RANGE; } else { warnx("Invalid Range MIN:MAX values, required MIN > 0 and MIN < MAX."); range_err = 1; } break; case 's': y = getsplitnumv4(optarg, &m_argv4.splitmask); if (!y) { v4args |= V4SPLIT; } else { warnx("Invalid IPv4 splitmask, unable to split."); split_errv4 = 1; } break; case 'S': y = getsplitnumv6(optarg, &m_argv6.v6splitmask, &m_argv6.v6splitnum); if (!y) { v6args |= V6SPLIT; } else { warnx("Invalid IPv6 splitmask, unable to split."); split_errv6 = 1; } break; case 'v': printf("%s\n", VERSION); return 0; case '?': case 'h': return usage(0); default: return usage(1); } } if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) colorize = 0; if (split_errv4 || split_errv6 || range_err) { nothing: warnx("No (valid) commands received, nothing to do."); free_boxargs(abox_start); return 1; } argcount = optind; if (m_argv4.numnets < 1) m_argv4.numnets = -1; if (argv[argcount]) { if (argv[argcount][0] == '-' && argv[argcount][1] == '\0') parse_stdin = 1; } else { if (abox_start->str[0] == '\0') { free_boxargs(abox_start); return usage(0); } } /* * Populate our argumentbox. * (Ie., see what's on the commandline). */ if (!parse_stdin && argv[argcount]) { abox_cur = get_boxargs(argc, argv, argcount, abox_cur); abox_tmp = abox_start; while (abox_tmp->next != abox_cur) { abox_tmp = abox_tmp->next; } abox_tmp->next = NULL; free(abox_cur); abox_cur = NULL; } /* * This will try to gather information about the network interfaces * present on the local machine. */ if (!parse_stdin) ifarg_cur = ifarg_start = parse_abox(abox_start); if (!ifarg_start && !parse_stdin) goto nothing; ifarg_cur = ifarg_start; while (ifarg_cur && !parse_stdin) { out_cmdline(ifarg_cur, v4args, m_argv4, v6args, m_argv6); strlcpy(oldcmdstr, ifarg_cur->cmdstr, sizeof(oldcmdstr)); ifarg_cur = ifarg_cur->next; } z = 0; y = 1; while (parse_stdin && y > -1) { stdinarg[0] = (char *)calloc(1, ARGLEN); stdinarg[1] = (char *)calloc(1, ARGLEN); stdinarg[2] = NULL; y = get_stdin(stdinarg); if (y > 0) { m = 2; if (stdinarg[1][0] == '\0') { free(stdinarg[1]); stdinarg[1] = NULL; m = 1; } abox_cur = get_boxargs(m, stdinarg, 0, abox_cur); abox_tmp = abox_start; while (abox_tmp->next != abox_cur && abox_tmp != abox_cur) { abox_tmp = abox_tmp->next; } abox_tmp->next = NULL; free(abox_cur); abox_cur = NULL; ifarg_cur = ifarg_start = parse_abox(abox_start); if (ifarg_start) { ifarg_cur = ifarg_start; while (ifarg_cur) { out_cmdline(ifarg_cur, v4args, m_argv4, v6args, m_argv6); strlcpy(oldcmdstr, ifarg_cur->cmdstr, sizeof(oldcmdstr)); ifarg_cur = ifarg_cur->next; } } z = 1; free_if(ifarg_start); free_boxargs(abox_start); abox_start = abox_cur = (struct argbox *)calloc(1, sizeof(struct argbox)); } for (x = 0; x < 2; x++) { if (stdinarg[x]) { free(stdinarg[x]); stdinarg[x] = NULL; } } if (y == -1) warnx("Problem parsing stdin"); } if (parse_stdin) { free(stdinarg[0]); free(stdinarg[1]); } if (!z && parse_stdin) warnx("No arguments found on stdin"); if (!parse_stdin) free_if(ifarg_start); return 0; }
int main( int argc, char **argv ) { int sk; struct sockaddr_in server; struct hostent *hp; char buf[MAXBUF]; int buf_len; int n_sent; int n_read; /* Make sure we have the right number of command line args */ if (argc!=3) { printf("Usage: %s <server name> <port number>\n",argv[0]); exit(0); } /* create a socket IP protocol family (PF_INET) UDP (SOCK_DGRAM) */ if ((sk = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) { printf("Problem creating socket\n"); exit(1); } /* Using UDP we don't need to call bind unless we care what our port number is - most clients don't care */ /* now create a sockaddr that will be used to contact the server fill in an address structure that will be used to specify the address of the server we want to connect to address family is IP (AF_INET) server IP address is found by calling gethostbyname with the name of the server (entered on the command line) server port number is argv[2] (entered on the command line) */ server.sin_family = AF_INET; if ((hp = gethostbyname(argv[1]))==0) { printf("Invalid or unknown host\n"); exit(1); } /* copy the IP address into the sockaddr It is already in network byte order */ memcpy( &server.sin_addr.s_addr, hp->h_addr, hp->h_length); /* establish the server port number - we must use network byte order! */ server.sin_port = htons(atoi(argv[2])); /* read everything possible */ buf_len = get_stdin(buf,MAXBUF); printf("Got %d bytes from stdin - sending...\n",buf_len); /* send it to the echo server */ n_sent = sendto(sk,buf,buf_len,0, (struct sockaddr*) &server,sizeof(server)); if (n_sent<0) { perror("Problem sending data"); exit(1); } if (n_sent!=buf_len) { printf("Sendto sent %d bytes\n",n_sent); } /* Wait for a reply (from anyone) */ n_read = recvfrom(sk,buf,MAXBUF,0,NULL,NULL); if (n_read<0) { perror("Problem in recvfrom"); exit(1); } printf("Got back %d bytes\n",n_read); /* send what we got back to stdout */ if (write(STDOUT_FILENO,buf,n_read) < 0) { perror("Problem writing to stdout"); exit(1); } return(0); }