]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/readprofile.c
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / readprofile.c
index ca0037529c220df8d65553a83ffb94e4c3459392..3abf6c7279ab935e0ab0d9e413cc4f3095acf896 100644 (file)
@@ -1,21 +1,14 @@
 /*
- *  readprofile.c - used to read /proc/profile
+ * SPDX-License-Identifier: GPL-2.0-or-later
  *
- *  Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
+ * 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 2 of the License, or
+ * (at your option) any later version.
  *
- *   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 2 of the License, or
- *   (at your option) any later version.
+ * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
  *
- *   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, write to the Free Software Foundation, Inc.,
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * readprofile.c - used to read /proc/profile
  */
 
 /*
@@ -51,6 +44,8 @@
 #include <sys/utsname.h>
 #include <unistd.h>
 
+#include "c.h"
+#include "strutils.h"
 #include "nls.h"
 #include "xalloc.h"
 #include "closestream.h"
@@ -68,7 +63,7 @@ static FILE *myopen(char *name, char *mode, int *flag)
        if (!strcmp(name + len - 3, ".gz")) {
                FILE *res;
                char *cmdline = xmalloc(len + 6);
-               sprintf(cmdline, "zcat %s", name);
+               snprintf(cmdline, len + 6, "zcat %s", name);
                res = popen(cmdline, mode);
                free(cmdline);
                *flag = 1;
@@ -122,16 +117,16 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -r, --reset               reset all the counters (root only)\n"), out);
        fputs(_(" -n, --no-auto             disable byte order auto-detection\n"), out);
        fputs(USAGE_SEPARATOR, out);
-       printf(USAGE_HELP_OPTIONS(27));
-       printf(USAGE_MAN_TAIL("readprofile(8)"));
+       fprintf(out, USAGE_HELP_OPTIONS(27));
+       fprintf(out, USAGE_MAN_TAIL("readprofile(8)"));
        exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char **argv)
 {
        FILE *map;
-       int proFd;
-       char *mapFile, *proFile, *mult = NULL;
+       int proFd, has_mult = 0, multiplier = 0;
+       char *mapFile, *proFile;
        size_t len = 0, indx = 1;
        unsigned long long add0 = 0;
        unsigned int step;
@@ -147,6 +142,7 @@ int main(int argc, char **argv)
        int maplineno = 1;
        int popenMap;           /* flag to tell if popen() has been used */
        int header_printed;
+       double rep = 0;
 
        static const struct option longopts[] = {
                {"mapfile", required_argument, NULL, 'm'},
@@ -169,7 +165,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        proFile = defaultpro;
        mapFile = defaultmap;
@@ -198,7 +194,8 @@ int main(int argc, char **argv)
                        optInfo++;
                        break;
                case 'M':
-                       mult = optarg;
+                       multiplier = strtol_or_err(optarg, _("failed to parse multiplier"));
+                       has_mult = 1;
                        break;
                case 'r':
                        optReset++;
@@ -206,9 +203,9 @@ int main(int argc, char **argv)
                case 'v':
                        optVerbose++;
                        break;
+
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
                        usage();
                default:
@@ -216,14 +213,13 @@ int main(int argc, char **argv)
                }
        }
 
-       if (optReset || mult) {
-               int multiplier, fd, to_write;
+       if (optReset || has_mult) {
+               int fd, to_write;
 
                /* When writing the multiplier, if the length of the
                 * write is not sizeof(int), the multiplier is not
                 * changed. */
-               if (mult) {
-                       multiplier = strtoul(mult, NULL, 10);
+               if (has_mult) {
                        to_write = sizeof(int);
                } else {
                        multiplier = 0;
@@ -263,9 +259,9 @@ int main(int argc, char **argv)
                size_t i;
 
                for (p = buf + 1; p < buf + entries; p++) {
-                       if (*p & ~0U << (sizeof(*buf) * 4))
+                       if (*p & ~0U << ((unsigned) sizeof(*buf) * 4U))
                                big++;
-                       if (*p & ((1 << (sizeof(*buf) * 4)) - 1))
+                       if (*p & ((1U << ((unsigned) sizeof(*buf) * 4U)) - 1U))
                                small++;
                }
                if (big > small) {
@@ -346,7 +342,7 @@ int main(int argc, char **argv)
                        errx(EXIT_FAILURE,
                             _("profile address out of range. Wrong map file?"));
 
-               while (indx < (next_add - add0) / step) {
+               while (step > 0 && indx < (next_add - add0) / step) {
                        if (optBins && (buf[indx] || optAll)) {
                                if (!header_printed) {
                                        printf("%s:\n", fn_name);
@@ -370,7 +366,7 @@ int main(int argc, char **argv)
                        else
                                printf("%6u %-40s %8.4f\n",
                                       this, fn_name, this / (double)fn_len);
-                       if (optSub) {
+                       if (optSub && step > 0) {
                                unsigned long long scan;
 
                                for (scan = (fn_add - add0) / step + 1;
@@ -396,13 +392,16 @@ int main(int argc, char **argv)
        /* clock ticks, out of kernel text - probably modules */
        printf("%6u %s\n", buf[len / sizeof(*buf) - 1], "*unknown*");
 
+       if (fn_add > add0)
+               rep = total / (double)(fn_add - add0);
+
        /* trailer */
        if (optVerbose)
                printf("%016x %-40s %6u %8.4f\n",
-                      0, "total", total, total / (double)(fn_add - add0));
+                      0, "total", total, rep);
        else
                printf("%6u %-40s %8.4f\n",
-                      total, _("total"), total / (double)(fn_add - add0));
+                      total, _("total"), rep);
 
        popenMap ? pclose(map) : fclose(map);
        exit(EXIT_SUCCESS);