rpc_server.c (1288B)
1 #include <err.h> 2 #include <stdlib.h> 3 #include <stdio.h> 4 5 #include "rpc.h" 6 7 static void *emalloc(size_t); 8 9 static void * 10 emalloc(size_t nb) 11 { 12 void *p; 13 14 if ((p = malloc(nb)) == NULL) 15 err(1, "malloc"); 16 return (p); 17 } 18 19 float * 20 calc_avg_1_svc(arg_arr *argp, struct svc_req *rqstp) 21 { 22 static float result; 23 int i, sum; 24 25 for (i = 0, sum = 0; i < argp->n; i++) 26 sum += argp->arr.arr_val[i]; 27 result = sum / (float)argp->n; 28 29 return (&result); 30 } 31 32 struct minmax * 33 calc_minmax_1_svc(arg_arr *argp, struct svc_req *rqstp) 34 { 35 static struct minmax result; 36 int i, *min, *max; 37 38 result.arr.arr_len = 2; 39 result.arr.arr_val = emalloc(2 * sizeof(int)); 40 min = &result.arr.arr_val[0]; 41 max = &result.arr.arr_val[1]; 42 *min = *argp->arr.arr_val; 43 *max = *argp->arr.arr_val; 44 for (i = 0; i < argp->n; i++) { 45 if (argp->arr.arr_val[i] < *min) 46 *min = argp->arr.arr_val[i]; 47 if (argp->arr.arr_val[i] > *max) 48 *max = argp->arr.arr_val[i]; 49 } 50 51 return (&result); 52 } 53 54 struct float_arr * 55 calc_prod_1_svc(arg_prod *argp, struct svc_req *rqstp) 56 { 57 static struct float_arr result; 58 int i; 59 60 result.arr.arr_len = argp->arr.n; 61 result.arr.arr_val = emalloc(argp->arr.n * sizeof(float)); 62 for (i = 0; i < argp->arr.n; i++) 63 result.arr.arr_val[i] = argp->a * argp->arr.arr.arr_val[i]; 64 65 return (&result); 66 }