ptg10805159
658 Advanced IPC Chapter 17
char *ptr, *argv[MAXARGC];
int argc;
if (strtok(buf, WHITE) == NULL) /* an argv[0] is required */
return(-1);
argv[argc = 0] = buf;
while ((ptr = strtok(NULL, WHITE)) != NULL) {
if (++argc >= MAXARGC-1) /* -1 for room for NULL at end */
return(-1);
argv[argc] = ptr;
}
argv[++argc] = NULL;
/*
*Since argv[] pointers point into the user’s buf[],
*user’s function can just copy the pointers, even
*though argv[] array will disappear on return.
*/
return((*optfunc)(argc, argv));
}
Figure 17.23 Thebuf_argsfunction
The server’s function that is called bybuf_argsiscli_args(Figure17.24). It
verifies that the client sent the right number of arguments and stores the pathname and
open mode in global variables.
#include "opend.h"
/*
*This function is called by buf_args(), which is called by
*handle_request(). buf_args() has broken up the client’s
*buffer into an argv[]-style array, which we now process.
*/
int
cli_args(int argc, char **argv)
{
if (argc != 3 || strcmp(argv[0], CL_OPEN) != 0) {
strcpy(errmsg, "usage: <pathname> <oflag>\n");
return(-1);
}
pathname = argv[1]; /* save ptr to pathname to open */
oflag = atoi(argv[2]);
return(0);
}
Figure 17.24 Thecli_argsfunction
This completes the open server that is invoked by aforkandexecfrom the client.
Asingle fd-pipe is created beforetheforkand is used to communicate between the
client and the server.With this arrangement, we have one server per client.