File: accton.c

package info (click to toggle)
acct 6.5.4-2.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,132 kB
  • ctags: 1,428
  • sloc: sh: 10,390; ansic: 9,695; cpp: 289; makefile: 185
file content (212 lines) | stat: -rw-r--r-- 5,037 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Copyright (C) 1993, 1996, 1997, 2003, 2008, 2009, 2010 Free Software Foundation, Inc.

This file contains the code for GNU accton.

The GNU Accounting Utilities are free software; you can redistribute
them and/or modify them under the terms of the GNU General Public
License as published by the Free Software Foundation; either version
3, or (at your option) any later version.

The GNU Accounting Utilities are distributed in the hope that they 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 the GNU Accounting Utilities; see the file COPYING.  If
not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
MA 02139, USA.
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#else
extern int errno;
#endif
#include <string.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "common.h"
#include "files.h"
#ifdef HAVE_GETOPT_LONG_ONLY
#include <getopt.h>
#else
#include "getopt.h"
#endif
#include "version.h"

/* globals */

char *program_name;		/* name of this executable */

void print_option_error (void);
void print_version (void);
void print_help (void);

/* code */

/*
 * to print when there are errors related to options
 */

void print_option_error(void)
{
  (void)printf ("Try '%s --help' for more information.\n", program_name);
}

/*
 * to print version
 */

void print_version(void)
{
  (void)printf("%s: GNU Accounting Utilities (release %s)\n",program_name, VERSION_STRING);
}

/*
 * to print usage (kind of very brief help)
 */

void give_usage(void)
{
  (void)printf ("Usage: %s [OPTION] on|off|ACCOUNTING_FILE\n", program_name);
}

/*
 * to print help
 */

void print_help(void)
{
  give_usage ();

  printf ("\n\
          Turns process accounting on or off, or changes the file where this\n\
          info is saved.\n\n\
          OPTIONS:\n\
          -h, --help       Show help and exit\n\
          -V, --version    Show version and exit\n\n\
          ARGUMENTS:\n\
          on               Activate process accounting and use default file\n\
          off              Deactivate process accounting\n\
          ACCOUNTING_FILE  Activate (if not active) and save information in\n\
          this file\n\n\
          The system's default process accounting file is '%s'.\n\n\
          Report bugs to <bug-acct@gnu.org>\n",
          ACCT_FILE_LOC);
}


/*
 * does all the real work
 */

int main(int argc, char *argv[])
{
  int c, status_acct, number_args;
  char *acct_file = NULL;

  program_name = argv[0];


  /* deal with options */
  while (1)
    {
      int option_index = 0;

      static struct option long_options[] =
      {
        { "version", no_argument, NULL, 1
        },
        { "help", no_argument, NULL, 2 },
        { 0, 0, 0, 0 }
      };

      c = getopt_long (argc, argv, "hV", long_options, &option_index);

      if (c == EOF)
        break;

      switch (c)
        {
        case 'V':
        case 1:
          print_version ();
          exit (EXIT_SUCCESS);
        case 'h':
        case 2:
          print_help ();
          exit (EXIT_SUCCESS);
        default:
          give_usage ();
          print_option_error ();
          exit (EXIT_FAILURE);
        }
    }


  /* get the number of real arguments -- not options nor prog. name */
  number_args = argc - optind;

  /* more than one arg -> error, too many args */
  if (1 < number_args)
    {
      printf ("%s: too many arguments\n", program_name);
      give_usage ();
      print_option_error ();
      exit (EXIT_FAILURE);
    }
  /* no arguments -> error, no arguments */
  else if (0 == number_args)
    {
      printf ("%s: no arguments\n", program_name);
      give_usage ();
      print_option_error ();
      exit (EXIT_FAILURE);
    }
  /* exactly one arg -> on|off|ACCOUNTING_FILE */
  else
    {
      acct_file = argv[optind];

      /* if the argument was "on" then pass the default file to activate.. */
      if (0 == strcasecmp (acct_file, "on"))
        {
          status_acct  = acct (ACCT_FILE_LOC);
          printf ("Turning on process accounting, file set to the default '%s'.\n",
                  ACCT_FILE_LOC);
        }
      /* ..else if was "off", pass NULL to deactivate.. */
      else if (0 == strcasecmp (acct_file, "off"))
        {
          status_acct  = acct (NULL);
          printf ("Turning off process accounting.\n");
        }
      /* ..else, the argument will be used as filename */
      else
        {
          status_acct  = acct (acct_file);
          printf ("Turning on process accounting, file set to '%s'.\n", acct_file);
        }


      /* did the system call return an error? */
      if (-1 == status_acct)
        {
          int err = errno;
          perror ("accton");
          exit (err);
        }
    }
  return EXIT_SUCCESS;
}