#include #include #include #include #include #include #include int renice(int which, int who, int adj) { adj += getpriority(which, who); if (setpriority(which, who, adj)) { fprintf(stderr, "renice: %s\n", strerror(errno)); return 1; } return 0; } int main(int argc, char **argv) { int adj = 10; int which = PRIO_PROCESS; int opt; while ((opt = getopt(argc, argv, "n:gup")) != -1) { switch (opt) { case 'n': adj = atoi(optarg); break; case 'g': which = PRIO_PGRP; break; case 'u': which = PRIO_USER; break; case 'p': which = PRIO_PROCESS; break; default: printf("renice [ngup] [id1 id2...]\n\t-n N Add N to the niceness\n\t-g Process group ids\n\t-u Process user names\n\t-p Process ids\n"); return 0; } } argv += optind; argc -= optind; int who = 0; int ret = 0; for (int i = 0; i < argc; i++) { if (which == PRIO_USER) { struct passwd *pw = getpwnam(argv[i]); if (pw == 0) { fprintf(stderr, "renice: %s\n", strerror(errno)); return 1; } who = pw->pw_uid; } else who = atoi(argv[i]); if (renice(which, who, adj)) ret = 1; } return ret; }