stdio流是由FILE控制块来进行管理的。例如,我们也许已经多次编写了如下所示的代码: FILE *in; in = fopen(pathname,"r"); if ( in == NULL ) { fprintf(stderr,"%s: opening %s for read.\n",strerror(errno),pathname); exit(1); }
缓冲控制函数概要如下所示: #include int setbuf(FILE *stream,char *buf); int setbuffer(FILE *stream, char *buf, size_t size); int setlinebuf(FILE *stream); int setvbuf(FILE *stream, char *buf, int mode, size_t size);
/* * Operation "seed": * * Operands: * 1: the least significant 32 bits * will seed the randm number * generator via srand(3): * * Result: * none. */ static int rpn_seed(void) { int z; mpz_t *opr; long lv;
if((z = rpn_pop(&opr)) <0 ) /* No operand available */ return -1;
/* * Get long value,ignoring errors. * Then seed the random number * generator: */ lv = mpz_get_si(*opr); srand((int)lv);
rpn_free(&opr); return z; }
/* * Operation "random": * * Operands: * 1. A modulo value to apply after * the random number is generated. * Result: * 1. A random value: 0 */ static int rpn_random(void) { mpz_t *opr,*res; mpz_t r; size_t limbs;
if(rpn_pop(&opr) <0) /* No operand available */ return -1;
mpz_init(r); res = rpn_alloc();
/* * Pop the top to use as the modulo * operand.Generate a random number * r.The compute r % opr as the * final result: */ limbs = mpz_size(*opr); mpz_random(r,limbs); mpz_tdiv_r(*res,r,*opr);
mpz_clear(r); rpn_free(&opr);
return rpn_push(res); }
/* * Operation "tprime": * * Test for probability of being * a prime number: * * Operands: * 1. Number to test * 2. Number of tests to try * (typically 25) * Result: * 1. Number tested is probably * prime when value =1. * Number tested is not prime * when result is zero. */ static int rpn_test_prime(void) { mpz_t *opr1,*opr2; long reps; int z;
if(sp < 2) /* Insufficient operands */ return -1;
rpn_pop(&opr1); rpn_pop(&opr2);
if(mpz_size(*opr2) >1) /* Too many limbs in size */ return -1;
reps = mpz_get_si(*opr2); if(reps < 1L || reps > 32768L) /* too large for opr2 */ return -1;
z = mpz_probab_prime_p(*opr1,reps); mpz_set_si(*opr1,(long)z); rpn_free(&opr2);
return rpn_push(opr1); }
/* * Operation "genprime": * * Generate a random prime number. * * Operands: * 1. The modulo value to apply * to the randomizing value * (see "random") * 2. The number of primality * tests to perform(typically * this value is 25). * Result: * 1. The randomly generated prime * number(actually,only a high * probability of being prime). */ static int rpn_genprime(void) { mpz_t *opr1; mpz_t *opr2; mpz_t *res;
extern int mkaddr(void *addr, int *addr_len, char *input_address, char *protocol);
extern void rpn_process(FILE *tx, char *buf);
/* * This function reports the error and * exits back to the shell: */ static void bail(const char *on_what) { if(errno != 0) { fputs(strerror(errno),stderr); fputs(": ",stderr); } fputs(on_what,stderr); fputc('\n',stderr); exit(1); }
int main(int argc,char **argv) { int z; char *srvr_addr = "127.0.0.1:9090"; struct sockaddr_in adr_srvr; /* AF_INET */ struct sockaddr_in adr_clnt; /* AF_INET */ int len_inet; /* length */ int s = -1; /* Socket */ int c = -1; /* Client socket */ FILE *rx = NULL; /* Read Stream */ FILE *tx = NULL: /* Write stream */ char buf[4096]; /* I/O buffer */
/* * use a server address from the command * line,otherwise default to 127.0.0.1: */ if(argc >= 2) srvr_addr = argv[1];
len_inet = sizeof adr_srvr; z = mkaddr(&adr_srvr,&len_inet,srvr_addr,"tcp");
if(z<0 || !adr_srvr.sin_port) { fprintf(stderr,"Invalid server " "address,or no port number " "was specified.\n"); exit(1); }
/* * Create a TCP/IP socket to use: */ s = socket(PF_INET,SOCK_STREAM,0); if(s==-1) bail("socket(2)");
/* * Bind the server address: */ z = bind(s,(struct sockaddr *)&adr_srvr,len_inet); if(z==-1) bail("bind(2)");
/* * Make it a listening socket: */ z = listen(s,10); if(z == -1) bail("listen(2)");
/* * start the server loop: */ for(;;) { /* * Wait for a connect: */ len_inet = sizeof adr_clnt; c = accept(s,(struct sockaddr *)&adr_clnt,&len_inet); if(c==-1) bail("accept(2)");