]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/ipcmk.c
Make the ways of using output stream consistent in usage()
[thirdparty/util-linux.git] / sys-utils / ipcmk.c
index a862ba1a2807862465158d3af5be86b4cce68087..67a7637f629ef4a7543b1465fb4acf2147f408d4 100644 (file)
 
 #include "c.h"
 #include "nls.h"
+#include "randutils.h"
 #include "strutils.h"
 #include "closestream.h"
 
-static key_t create_key(void)
-{
-       struct timeval now;
-       gettimeofday(&now, NULL);
-       srandom(now.tv_usec);
-       return random();
-}
-
 static int create_shm(size_t size, int permission)
 {
-       key_t key = create_key();
+       key_t key;
+
+       ul_random_get_bytes(&key, sizeof(key));
        return shmget(key, size, permission | IPC_CREAT);
 }
 
 static int create_msg(int permission)
 {
-       key_t key = create_key();
+       key_t key;
+
+       ul_random_get_bytes(&key, sizeof(key));
        return msgget(key, permission | IPC_CREAT);
 }
 
 static int create_sem(int nsems, int permission)
 {
-       key_t key = create_key();
+       key_t key;
+
+       ul_random_get_bytes(&key, sizeof(key));
        return semget(key, nsems, permission | IPC_CREAT);
 }
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(out, USAGE_HEADER);
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
        fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
-       fprintf(out, USAGE_OPTIONS);
 
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Create various IPC resources.\n"), out);
+
+       fputs(USAGE_OPTIONS, out);
        fputs(_(" -M, --shmem <size>       create shared memory segment of size <size>\n"), out);
-       fputs(_(" -S, --semaphore <nsems>  create semaphore array with <nsems> elements\n"), out);
+       fputs(_(" -S, --semaphore <number> create semaphore array with <number> elements\n"), out);
        fputs(_(" -Q, --queue              create message queue\n"), out);
        fputs(_(" -p, --mode <mode>        permission for the resource (default is 0644)\n"), out);
 
-       fprintf(out, USAGE_SEPARATOR);
-       fprintf(out, USAGE_HELP);
-       fprintf(out, USAGE_VERSION);
+       fputs(USAGE_SEPARATOR, out);
+       fprintf(out, USAGE_HELP_OPTIONS(26));
+
+       fputs(USAGE_ARGUMENTS, out);
+       fprintf(out, USAGE_ARG_SIZE(_("<size>")));
+
        fprintf(out, USAGE_MAN_TAIL("ipcmk(1)"));
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char **argv)
@@ -99,12 +106,12 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while((opt = getopt_long(argc, argv, "hM:QS:p:Vh", longopts, NULL)) != -1) {
                switch(opt) {
                case 'M':
-                       size = strtou64_or_err(optarg, _("failed to parse size"));
+                       size = strtosize_or_err(optarg, _("failed to parse size"));
                        ask_shm = 1;
                        break;
                case 'Q':
@@ -115,23 +122,27 @@ int main(int argc, char **argv)
                        ask_sem = 1;
                        break;
                case 'p':
-                       permission = strtoul(optarg, NULL, 8);
+               {
+                       char *end = NULL;
+                       errno = 0;
+                       permission = strtoul(optarg, &end, 8);
+                       if (errno || optarg == end || (end && *end))
+                               err(EXIT_FAILURE, _("failed to parse mode"));
                        break;
+               }
                case 'h':
-                       usage(stdout);
-                       break;
+                       usage();
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                default:
-                       ask_shm = ask_msg = ask_sem = 0;
-                       break;
+                       errtryhelp(EXIT_FAILURE);
                }
        }
 
-       if(!ask_shm && !ask_msg && !ask_sem)
-               usage(stderr);
-
+       if(!ask_shm && !ask_msg && !ask_sem) {
+               warnx(_("bad usage"));
+               errtryhelp(EXIT_FAILURE);
+       }
        if (ask_shm) {
                int shmid;
                if (-1 == (shmid = create_shm(size, permission)))