/* nice -- run a program with modified niceness                                 This is the nice utility
   Copyright (C) 1990-2018 Free Software Foundation, Inc.                       
                                                                                
   This program is free software: you can redistribute it and/or modify         
   it under the terms of the GNU General Public License as published by         
   the Free Software Foundation, either version 3 of the License, or            
   (at your option) any later version.                                          
                                                                                
   This program is distributed in the hope that it will be useful,              
   but WITHOUT ANY WARRANTY; without even the implied warranty of               
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                
   GNU General Public License for more details.                                 
                                                                                
   You should have received a copy of the GNU General Public License            
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */   The GNUv3 license
                                                                                
/* David MacKenzie <djm@gnu.ai.mit.edu> */                                      
                                                                                
#include <config.h>                                                             Provides system specific information
#include <stdio.h>                                                              Provides standard I/O capability
#include <getopt.h>                                                             ...!includes auto-comment...
#include <sys/types.h>                                                          Provides system data types
                                                                                
#include "system.h"                                                             ...!includes auto-comment...
                                                                                
#if ! HAVE_NICE                                                                 Line 26
/* Include this after "system.h" so we're sure to have definitions              
   (from time.h or sys/time.h) required for e.g. the ru_utime member.  */       
# include <sys/resource.h>                                                      ...!includes auto-comment...
#endif                                                                          Line 30
                                                                                
#include "die.h"                                                                ...!includes auto-comment...
#include "error.h"                                                              ...!includes auto-comment...
#include "quote.h"                                                              ...!includes auto-comment...
#include "xstrtol.h"                                                            ...!includes auto-comment...
                                                                                
/* The official name of this program (e.g., no 'g' prefix).  */                 
#define PROGRAM_NAME "nice"                                                     Line 38
                                                                                
#define AUTHORS proper_name ("David MacKenzie")                                 Line 40
                                                                                
#if HAVE_NICE                                                                   Line 42
# define GET_NICENESS() nice (0)                                                Line 43
#else                                                                           Line 44
# define GET_NICENESS() getpriority (PRIO_PROCESS, 0)                           Line 45
#endif                                                                          Line 46
                                                                                
#ifndef NZERO                                                                   Line 48
# define NZERO 20                                                               Line 49
#endif                                                                          Line 50
                                                                                
/* This is required for Darwin Kernel Version 7.7.0.  */                        
#if NZERO == 0                                                                  Line 53
# undef  NZERO                                                                  Line 54
# define NZERO 20                                                               Line 55
#endif                                                                          Line 56
                                                                                
static struct option const longopts[] =                                         Line 58
{                                                                               
  {"adjustment", required_argument, NULL, 'n'},                                 Line 60
  {GETOPT_HELP_OPTION_DECL},                                                    Line 61
  {GETOPT_VERSION_OPTION_DECL},                                                 Line 62
  {NULL, 0, NULL, 0}                                                            Line 63
};                                                                              Block 1
                                                                                
void                                                                            Line 66
usage (int status)                                                              Line 67
{                                                                               
  if (status != EXIT_SUCCESS)                                                   Line 69
    emit_try_help ();                                                           ...!common auto-comment...
  else                                                                          Line 71
    {                                                                           
      printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);      Line 73
      printf (_("\                                                              Line 74
Run COMMAND with an adjusted niceness, which affects process scheduling.\n\     Line 75
With no COMMAND, print the current niceness.  Niceness values range from\n\     Line 76
%d (most favorable to the process) to %d (least favorable to the process).\n\   Line 77
"),                                                                             Line 78
              - NZERO, NZERO - 1);                                              Line 79
                                                                                
      emit_mandatory_arg_note ();                                               ...!common auto-comment...
                                                                                
      fputs (_("\                                                               Line 83
  -n, --adjustment=N   add integer N to the niceness (default 10)\n\            Line 84
"), stdout);                                                                    Line 85
      fputs (HELP_OPTION_DESCRIPTION, stdout);                                  Line 86
      fputs (VERSION_OPTION_DESCRIPTION, stdout);                               Line 87
      printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);                             Line 88
      emit_ancillary_info (PROGRAM_NAME);                                       Line 89
    }                                                                           
  exit (status);                                                                Line 91
}                                                                               Block 2
                                                                                
static bool                                                                     Line 94
perm_related_errno (int err)                                                    Line 95
{                                                                               
  return err == EACCES || err == EPERM;                                         Line 97
}                                                                               Block 3
                                                                                
int                                                                             
main (int argc, char **argv)                                                    Line 101
{                                                                               
  int current_niceness;                                                         Line 103
  int adjustment = 10;                                                          Line 104
  char const *adjustment_given = NULL;                                          Line 105
  bool ok;                                                                      Line 106
  int i;                                                                        Line 107
                                                                                
  initialize_main (&argc, &argv);                                               VMS-specific entry point handling wildcard expansion
  set_program_name (argv[0]);                                                   Retains program name and discards path
  setlocale (LC_ALL, "");                                                       Sets up internationalization (i18n)
  bindtextdomain (PACKAGE, LOCALEDIR);                                          Assigns i18n directorySets text domain for _() [gettext()] function
  textdomain (PACKAGE);                                                         Sets text domain for _() [gettext()] function
                                                                                
  initialize_exit_failure (EXIT_CANCELED);                                      Line 115
  atexit (close_stdout);                                                        Close stdout on exit (see gnulib)
                                                                                
  for (i = 1; i < argc; /* empty */)                                            Line 118
    {                                                                           
      char const *s = argv[i];                                                  Line 120
                                                                                
      if (s[0] == '-' && ISDIGIT (s[1 + (s[1] == '-' || s[1] == '+')]))         Line 122
        {                                                                       
          adjustment_given = s + 1;                                             Line 124
          ++i;                                                                  Line 125
        }                                                                       
      else                                                                      Line 127
        {                                                                       
          int c;                                                                Line 129
          int fake_argc = argc - (i - 1);                                       Line 130
          char **fake_argv = argv + (i - 1);                                    Line 131
                                                                                
          /* Ensure that any getopt diagnostics use the right name.  */         
          fake_argv[0] = argv[0];                                               Line 134
                                                                                
          /* Initialize getopt_long's internal state.  */                       
          optind = 0;                                                           Line 137
                                                                                
          c = getopt_long (fake_argc, fake_argv, "+n:", longopts, NULL);        Line 139
          i += optind - 1;                                                      Line 140
                                                                                
          switch (c)                                                            Line 142
            {                                                                   
            case 'n':                                                           Line 144
              adjustment_given = optarg;                                        Line 145
              break;                                                            Line 146
                                                                                
            case -1:                                                            Line 148
              break;                                                            Line 149
                                                                                
            case_GETOPT_HELP_CHAR;                                              Line 151
                                                                                
            case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);                   Line 153
                                                                                
            default:                                                            Line 155
              usage (EXIT_CANCELED);                                            Line 156
              break;                                                            Line 157
            }                                                                   
                                                                                
          if (c == -1)                                                          Line 160
            break;                                                              Line 161
        }                                                                       
    }                                                                           
                                                                                
  if (adjustment_given)                                                         Line 165
    {                                                                           
      /* If the requested adjustment is outside the valid range,                
         silently bring it to just within range; this mimics what               
         "setpriority" and "nice" do.  */                                       
      enum { MIN_ADJUSTMENT = 1 - 2 * NZERO, MAX_ADJUSTMENT = 2 * NZERO - 1 };  Line 170
      long int tmp;                                                             Line 171
      if (LONGINT_OVERFLOW < xstrtol (adjustment_given, NULL, 10, &tmp, ""))    Line 172
        die (EXIT_CANCELED, 0, _("invalid adjustment %s"),                      Line 173
             quote (adjustment_given));                                         Line 174
      adjustment = MAX (MIN_ADJUSTMENT, MIN (tmp, MAX_ADJUSTMENT));             Line 175
    }                                                                           
                                                                                
  if (i == argc)                                                                Line 178
    {                                                                           
      if (adjustment_given)                                                     Line 180
        {                                                                       
          error (0, 0, _("a command must be given with an adjustment"));        Line 182
          usage (EXIT_CANCELED);                                                Line 183
        }                                                                       
      /* No command given; print the niceness.  */                              
      errno = 0;                                                                Line 186
      current_niceness = GET_NICENESS ();                                       Line 187
      if (current_niceness == -1 && errno != 0)                                 Line 188
        die (EXIT_CANCELED, errno, _("cannot get niceness"));                   Line 189
      printf ("%d\n", current_niceness);                                        Line 190
      return EXIT_SUCCESS;                                                      Line 191
    }                                                                           
                                                                                
  errno = 0;                                                                    Line 194
#if HAVE_NICE                                                                   Line 195
  ok = (nice (adjustment) != -1 || errno == 0);                                 Line 196
#else                                                                           Line 197
  current_niceness = GET_NICENESS ();                                           Line 198
  if (current_niceness == -1 && errno != 0)                                     Line 199
    die (EXIT_CANCELED, errno, _("cannot get niceness"));                       Line 200
  ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0);     Line 201
#endif                                                                          Line 202
  if (!ok)                                                                      Line 203
    {                                                                           
      error (perm_related_errno (errno) ? 0                                     Line 205
             : EXIT_CANCELED, errno, _("cannot set niceness"));                 Line 206
      /* error() flushes stderr, but does not check for write failure.          
         Normally, we would catch this via our atexit() hook of                 
         close_stdout, but execvp() gets in the way.  If stderr                 
         encountered a write failure, there is no need to try calling           
         error() again.  */                                                     
      if (ferror (stderr))                                                      Line 212
        return EXIT_CANCELED;                                                   Line 213
    }                                                                           
                                                                                
  execvp (argv[i], &argv[i]);                                                   Line 216
                                                                                
  int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;         Line 218
  error (0, errno, "%s", quote (argv[i]));                                      Line 219
  return exit_status;                                                           Line 220
}                                                                               Block 4