The Linux Programming Interface

(nextflipdebug5) #1
System V Message Queues 939

static void / Print usage info, then exit /
usageError(const char progName, const char msg)
{
if (msg != NULL)
fprintf(stderr, "%s", msg);
fprintf(stderr, "Usage: %s [-cx] {-f pathname | -k key | -p} "
"[octal-perms]\n", progName);
fprintf(stderr, " -c Use IPC_CREAT flag\n");
fprintf(stderr, " -x Use IPC_EXCL flag\n");
fprintf(stderr, " -f pathname Generate key using ftok()\n");
fprintf(stderr, " -k key Use 'key' as key\n");
fprintf(stderr, " -p Use IPC_PRIVATE key\n");
exit(EXIT_FAILURE);
}


int
main(int argc, char argv[])
{
int numKeyFlags; /
Counts -f, -k, and -p options */
int flags, msqid, opt;
unsigned int perms;
long lkey;
key_t key;


/ Parse command-line options and arguments /


numKeyFlags = 0;
flags = 0;


while ((opt = getopt(argc, argv, "cf:k:px")) != -1) {
switch (opt) {
case 'c':
flags |= IPC_CREAT;
break;


case 'f': / -f pathname /
key = ftok(optarg, 1);
if (key == -1)
errExit("ftok");
numKeyFlags++;
break;


case 'k': / -k key (octal, decimal or hexadecimal) /
if (sscanf(optarg, "%li", &lkey) != 1)
cmdLineErr("-k option requires a numeric argument\n");
key = lkey;
numKeyFlags++;
break;


case 'p':
key = IPC_PRIVATE;
numKeyFlags++;
break;

Free download pdf