written by Sam Leffler; add Berkeley copyright
[unix-history] / usr / src / usr.bin / renice / renice.c
index 994f798..c9e5ac5 100644 (file)
@@ -1,9 +1,32 @@
+/*
+ * Copyright (c) 1983 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)renice.c    4.1 (Berkeley) 83/03/19";
-#endif
+static char sccsid[] = "@(#)renice.c   5.2 (Berkeley) %G%";
+#endif /* not lint */
 
 
-#include <time.h>
-#include <resource.h>
+#include <sys/time.h>
+#include <sys/resource.h>
 #include <stdio.h>
 #include <pwd.h>
 
 #include <stdio.h>
 #include <pwd.h>
 
@@ -16,18 +39,13 @@ main(argc, argv)
        char **argv;
 {
        int which = PRIO_PROCESS;
        char **argv;
 {
        int which = PRIO_PROCESS;
-       int who, id, prio, oldprio, errs = 0;
-       extern int errno;
+       int who = 0, prio, errs = 0;
 
        argc--, argv++;
 
        argc--, argv++;
-       if (argc < 2)
-               usage();
-       if (strcmp(*argv, "-g") == 0) {
-               which = PRIO_PGRP;
-               argv++, argc--;
-       } else if (strcmp(*argv, "-u") == 0) {
-               which = PRIO_USER;
-               argv++, argc--;
+       if (argc < 2) {
+               fprintf(stderr, "usage: renice priority [ [ -p ] pids ] ");
+               fprintf(stderr, "[ [ -g ] pgrps ] [ [ -u ] users ]\n");
+               exit(1);
        }
        prio = atoi(*argv);
        argc--, argv++;
        }
        prio = atoi(*argv);
        argc--, argv++;
@@ -35,9 +53,19 @@ main(argc, argv)
                prio = PRIO_MAX;
        if (prio < PRIO_MIN)
                prio = PRIO_MIN;
                prio = PRIO_MAX;
        if (prio < PRIO_MIN)
                prio = PRIO_MIN;
-       if (argc == 0)
-               usage();
        for (; argc > 0; argc--, argv++) {
        for (; argc > 0; argc--, argv++) {
+               if (strcmp(*argv, "-g") == 0) {
+                       which = PRIO_PGRP;
+                       continue;
+               }
+               if (strcmp(*argv, "-u") == 0) {
+                       which = PRIO_USER;
+                       continue;
+               }
+               if (strcmp(*argv, "-p") == 0) {
+                       which = PRIO_PROCESS;
+                       continue;
+               }
                if (which == PRIO_USER) {
                        register struct passwd *pwd = getpwnam(*argv);
                        
                if (which == PRIO_USER) {
                        register struct passwd *pwd = getpwnam(*argv);
                        
@@ -46,38 +74,37 @@ main(argc, argv)
                                        *argv);
                                continue;
                        }
                                        *argv);
                                continue;
                        }
-                       id = pwd->pw_uid;
+                       who = pwd->pw_uid;
                } else {
                } else {
-                       id = atoi(*argv);
-                       if (id < 0) {
+                       who = atoi(*argv);
+                       if (who < 0) {
                                fprintf(stderr, "renice: %s: bad value\n",
                                        *argv);
                                continue;
                        }
                }
                                fprintf(stderr, "renice: %s: bad value\n",
                                        *argv);
                                continue;
                        }
                }
-               oldprio = getpriority(which, who);
-               if (oldprio == -1 && errno) {
-                       fprintf(stderr, "renice: ");
-                       perror(*argv);
-                       errs++;
-                       continue;
-               }
-               if (setpriority(which, who, prio) < 0) {
-                       fprintf(stderr, "renice: ");
-                       perror(*argv);
-                       errs++;
-                       continue;
-               }
-               printf("%s: old priority %d, new priority %d\n", *argv,
-                       oldprio, prio);
+               errs += donice(which, who, prio);
        }
        exit(errs != 0);
 }
 
        }
        exit(errs != 0);
 }
 
-usage()
+donice(which, who, prio)
+       int which, who, prio;
 {
 {
-       fprintf(stderr, "usage: renice priority pid ....\n");
-       fprintf(stderr, "or, renice -g priority pgrp ....\n");
-       fprintf(stderr, "or, renice -u priority user ....\n");
-       exit(1);
+       int oldprio;
+       extern int errno;
+
+       errno = 0, oldprio = getpriority(which, who);
+       if (oldprio == -1 && errno) {
+               fprintf(stderr, "renice: %d: ", who);
+               perror("getpriority");
+               return (1);
+       }
+       if (setpriority(which, who, prio) < 0) {
+               fprintf(stderr, "renice: %d: ", who);
+               perror("setpriority");
+               return (1);
+       }
+       printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
+       return (0);
 }
 }