]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
readprofile: fix coding style
authorSami Kerola <kerolasa@iki.fi>
Mon, 31 Oct 2011 20:52:56 +0000 (21:52 +0100)
committerSami Kerola <kerolasa@iki.fi>
Wed, 2 Nov 2011 17:41:56 +0000 (18:41 +0100)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/readprofile.c

index 201323c4a0b20c2c5096cf57d6053ad5085d2bd0..e72f79d46f6c8af1dfb869a310870e76a836902b 100644 (file)
  * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
  */
 
-#include <getopt.h>
 #include <errno.h>
-#include <stdio.h>
 #include <fcntl.h>
+#include <getopt.h>
+#include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/utsname.h>
+#include <unistd.h>
 
-#include "xalloc.h"
 #include "nls.h"
+#include "xalloc.h"
 
 #define S_LEN 128
 
 static char defaultmap[]="/boot/System.map";
 static char defaultpro[]="/proc/profile";
 
-static FILE *
-myopen(char *name, char *mode, int *flag) {
+static FILE *myopen(char *name, char *mode, int *flag)
+{
        int len = strlen(name);
 
-       if (!strcmp(name+len-3,".gz")) {
+       if (!strcmp(name + len - 3, ".gz")) {
                FILE *res;
-               char *cmdline = xmalloc(len+6);
+               char *cmdline = xmalloc(len + 6);
                sprintf(cmdline, "zcat %s", name);
-               res = popen(cmdline,mode);
+               res = popen(cmdline, mode);
                free(cmdline);
                *flag = 1;
                return res;
        }
        *flag = 0;
-       return fopen(name,mode);
+       return fopen(name, mode);
 }
 
 #ifndef BOOT_SYSTEM_MAP
 #define BOOT_SYSTEM_MAP "/boot/System.map-"
 #endif
-static char *
-boot_uname_r_str(void) {
+
+static char *boot_uname_r_str(void)
+{
        struct utsname uname_info;
        char *s;
        size_t len;
@@ -95,8 +96,9 @@ boot_uname_r_str(void) {
        return s;
 }
 
-static void __attribute__((__noreturn__))
-usage(FILE *out) {
+static void __attribute__ ((__noreturn__))
+    usage(FILE * out)
+{
        fputs(USAGE_HEADER, out);
        fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
        fputs(USAGE_OPTIONS, out);
@@ -122,28 +124,28 @@ usage(FILE *out) {
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
-int
-main(int argc, char **argv) {
+int main(int argc, char **argv)
+{
        FILE *map;
        int proFd;
-       char *mapFile, *proFile, *mult=0;
-       size_t len=0, indx=1;
-       unsigned long long add0=0;
+       char *mapFile, *proFile, *mult = 0;
+       size_t len = 0, indx = 1;
+       unsigned long long add0 = 0;
        unsigned int step;
        unsigned int *buf, total, fn_len;
-       unsigned long long fn_add, next_add;          /* current and next address */
-       char fn_name[S_LEN], next_name[S_LEN];   /* current and next name */
+       unsigned long long fn_add, next_add;    /* current and next address */
+       char fn_name[S_LEN], next_name[S_LEN];  /* current and next name */
        char mode[8];
        int c;
        ssize_t rc;
-       int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
-       int optBins=0, optSub=0;
+       int optAll = 0, optInfo = 0, optReset = 0, optVerbose = 0, optNative = 0;
+       int optBins = 0, optSub = 0;
        char mapline[S_LEN];
-       int maplineno=1;
-       int popenMap;   /* flag to tell if popen() has been used */
+       int maplineno = 1;
+       int popenMap;           /* flag to tell if popen() has been used */
        int header_printed;
 
-       static const char optstring[]="m:p:M:ivabsrnVh";
+       static const char optstring[] = "m:p:M:ivabsrnVh";
        static const struct option longopts[] = {
                {"mapfile", required_argument, NULL, 'm'},
                {"profile", required_argument, NULL, 'p'},
@@ -170,7 +172,7 @@ main(int argc, char **argv) {
        mapFile = defaultmap;
 
        while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
-               switch(c) {
+               switch (c) {
                case 'm':
                        mapFile = optarg;
                        break;
@@ -214,20 +216,20 @@ main(int argc, char **argv) {
        if (optReset || mult) {
                int multiplier, fd, to_write;
 
-               /*
-                * When writing the multiplier, if the length of the write is
-                * not sizeof(int), the multiplier is not changed
-                */
+               /* When writing the multiplier, if the length of the
+                * write is not sizeof(int), the multiplier is not
+                * changed. */
                if (mult) {
                        multiplier = strtoul(mult, 0, 10);
                        to_write = sizeof(int);
                } else {
                        multiplier = 0;
-                       to_write = 1;   /* sth different from sizeof(int) */
+                       /* sth different from sizeof(int) */
+                       to_write = 1;
                }
                /* try to become root, just in case */
                setuid(0);
-               fd = open(defaultpro,O_WRONLY);
+               fd = open(defaultpro, O_WRONLY);
                if (fd < 0)
                        err(EXIT_FAILURE, "%s", defaultpro);
                if (write(fd, &multiplier, to_write) != to_write)
@@ -236,44 +238,41 @@ main(int argc, char **argv) {
                exit(EXIT_SUCCESS);
        }
 
-       /*
-        * Use an fd for the profiling buffer, to skip stdio overhead
-        */
-       if (((proFd=open(proFile,O_RDONLY)) < 0)
-           || ((int)(len=lseek(proFd,0,SEEK_END)) < 0)
-           || (lseek(proFd,0,SEEK_SET) < 0))
+       /* Use an fd for the profiling buffer, to skip stdio overhead */
+       if (((proFd = open(proFile, O_RDONLY)) < 0)
+           || ((int)(len = lseek(proFd, 0, SEEK_END)) < 0)
+           || (lseek(proFd, 0, SEEK_SET) < 0))
                err(EXIT_FAILURE, "%s", proFile);
 
        buf = xmalloc(len);
 
-       rc = read(proFd,buf,len);
+       rc = read(proFd, buf, len);
        if (rc < 0 || (size_t) rc != len)
-               err(EXIT_FAILURE,"%s", proFile);
+               err(EXIT_FAILURE, "%s", proFile);
        close(proFd);
 
        if (!optNative) {
-               int entries = len/sizeof(*buf);
-               int big = 0,small = 0;
+               int entries = len / sizeof(*buf);
+               int big = 0, small = 0;
                unsigned *p;
                size_t i;
 
-               for (p = buf+1; p < buf+entries; p++) {
-                       if (*p & ~0U << (sizeof(*buf)*4))
+               for (p = buf + 1; p < buf + entries; p++) {
+                       if (*p & ~0U << (sizeof(*buf) * 4))
                                big++;
-                       if (*p & ((1 << (sizeof(*buf)*4))-1))
+                       if (*p & ((1 << (sizeof(*buf) * 4)) - 1))
                                small++;
                }
                if (big > small) {
                        warnx(_("Assuming reversed byte order. "
                                "Use -n to force native byte order."));
-                       for (p = buf; p < buf+entries; p++)
-                               for (i = 0; i < sizeof(*buf)/2; i++) {
-                                       unsigned char *b = (unsigned char *) p;
+                       for (p = buf; p < buf + entries; p++)
+                               for (i = 0; i < sizeof(*buf) / 2; i++) {
+                                       unsigned char *b = (unsigned char *)p;
                                        unsigned char tmp;
-
                                        tmp = b[i];
-                                       b[i] = b[sizeof(*buf)-i-1];
-                                       b[sizeof(*buf)-i-1] = tmp;
+                                       b[i] = b[sizeof(*buf) - i - 1];
+                                       b[sizeof(*buf) - i - 1] = tmp;
                                }
                }
        }
@@ -282,7 +281,7 @@ main(int argc, char **argv) {
        if (optInfo) {
                printf(_("Sampling_step: %i\n"), step);
                exit(EXIT_SUCCESS);
-       } 
+       }
 
        total = 0;
 
@@ -294,12 +293,12 @@ main(int argc, char **argv) {
        if (map == NULL)
                err(EXIT_FAILURE, "%s", mapFile);
 
-       while (fgets(mapline,S_LEN,map)) {
+       while (fgets(mapline, S_LEN, map)) {
                if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
                        errx(EXIT_FAILURE, _("%s(%i): wrong map line"), mapFile,
                             maplineno);
                /* only elf works like this */
-               if (!strcmp(fn_name,"_stext") || !strcmp(fn_name,"__stext")) {
+               if (!strcmp(fn_name, "_stext") || !strcmp(fn_name, "__stext")) {
                        add0 = fn_add;
                        break;
                }
@@ -312,8 +311,8 @@ main(int argc, char **argv) {
        /*
         * Main loop.
         */
-       while (fgets(mapline,S_LEN,map)) {
-               unsigned int this=0;
+       while (fgets(mapline, S_LEN, map)) {
+               unsigned int this = 0;
                int done = 0;
 
                if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
@@ -326,9 +325,10 @@ main(int argc, char **argv) {
                    !strcmp(next_name, "__etext"))
                        done = 1;
                else {
-                       /* ignore any LEADING (before a '[tT]' symbol is found)
-                          Absolute symbols and __init_end because some
-                          architectures place it before .text section */
+                       /* ignore any LEADING (before a '[tT]' symbol
+                        * is found) Absolute symbols and __init_end
+                        * because some architectures place it before
+                        * .text section */
                        if ((*mode == 'A' || *mode == '?')
                            && (total == 0 || !strcmp(next_name, "__init_end")))
                                continue;
@@ -341,13 +341,14 @@ main(int argc, char **argv) {
                        errx(EXIT_FAILURE,
                             _("profile address out of range. Wrong map file?"));
 
-               while (indx < (next_add-add0)/step) {
+               while (indx < (next_add - add0) / step) {
                        if (optBins && (buf[indx] || optAll)) {
                                if (!header_printed) {
-                                       printf ("%s:\n", fn_name);
+                                       printf("%s:\n", fn_name);
                                        header_printed = 1;
                                }
-                               printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
+                               printf("\t%llx\t%u\n", (indx - 1) * step + add0,
+                                      buf[indx]);
                        }
                        this += buf[indx++];
                }
@@ -355,23 +356,23 @@ main(int argc, char **argv) {
 
                if (optBins) {
                        if (optVerbose || this > 0)
-                               printf ("  total\t\t\t\t%u\n", this);
+                               printf("  total\t\t\t\t%u\n", this);
                } else if ((this || optAll) &&
-                          (fn_len = next_add-fn_add) != 0) {
+                          (fn_len = next_add - fn_add) != 0) {
                        if (optVerbose)
                                printf("%016llx %-40s %6i %8.4f\n", fn_add,
-                                      fn_name,this,this/(double)fn_len);
+                                      fn_name, this, this / (double)fn_len);
                        else
                                printf("%6i %-40s %8.4f\n",
-                                      this,fn_name,this/(double)fn_len);
+                                      this, fn_name, this / (double)fn_len);
                        if (optSub) {
                                unsigned long long scan;
 
-                               for (scan = (fn_add-add0)/step + 1;
-                                    scan < (next_add-add0)/step; scan++) {
+                               for (scan = (fn_add - add0) / step + 1;
+                                    scan < (next_add - add0) / step;
+                                    scan++) {
                                        unsigned long long addr;
-
-                                       addr = (scan - 1)*step + add0;
+                                       addr = (scan - 1) * step + add0;
                                        printf("\t%#llx\t%s+%#llx\t%u\n",
                                               addr, fn_name, addr - fn_add,
                                               buf[scan]);
@@ -380,7 +381,7 @@ main(int argc, char **argv) {
                }
 
                fn_add = next_add;
-               strcpy(fn_name,next_name);
+               strcpy(fn_name, next_name);
 
                maplineno++;
                if (done)
@@ -388,16 +389,16 @@ main(int argc, char **argv) {
        }
 
        /* clock ticks, out of kernel text - probably modules */
-       printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
+       printf("%6i %s\n", buf[len / sizeof(*buf) - 1], "*unknown*");
 
        /* trailer */
        if (optVerbose)
                printf("%016x %-40s %6i %8.4f\n",
-                      0,"total",total,total/(double)(fn_add-add0));
+                      0, "total", total, total / (double)(fn_add - add0));
        else
                printf("%6i %-40s %8.4f\n",
-                      total,_("total"),total/(double)(fn_add-add0));
-       
+                      total, _("total"), total / (double)(fn_add - add0));
+
        popenMap ? pclose(map) : fclose(map);
        exit(EXIT_SUCCESS);
 }