I forgot to malloc space for the NULL.
[oweals/busybox.git] / fbset.c
diff --git a/fbset.c b/fbset.c
index 47130e355bb24284f8bfd1a7c0beae0c2eeb0c84..80711ec9f3f0cc3bae0283072b627ff14487ff45 100644 (file)
--- a/fbset.c
+++ b/fbset.c
  *     Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
  */
 
-#include "busybox.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <ctype.h>
+#include <string.h>
 #include <sys/ioctl.h>
-
-#define PERROR(ctx)   do { perror(ctx); exit(1); } while(0)
+#include "busybox.h"
 
 #define DEFAULTFBDEV  "/dev/fb0"
 #define DEFAULTFBMODE "/etc/fb.modes"
 
-#define OPT_CHANGE    1
-#define OPT_INFO      (1 << 1)
-#define OPT_READMODE  (1 << 2)
-
-#define CMD_HELP        0
-#define CMD_FB         1
-#define CMD_DB         2
-#define CMD_GEOMETRY   3
-#define CMD_TIMING     4
-#define CMD_ACCEL      5
-#define CMD_HSYNC      6
-#define CMD_VSYNC      7
-#define CMD_LACED      8
-#define CMD_DOUBLE     9
-/* #define CMD_XCOMPAT     10 */
-#define CMD_ALL         11
-#define CMD_INFO        12
-#define CMD_CHANGE      13
+static const int OPT_CHANGE   = (1 << 0);
+static const int OPT_INFO     = (1 << 1);
+static const int OPT_READMODE = (1 << 2);
+
+enum {
+       CMD_HELP = 0,
+       CMD_FB = 1,
+       CMD_DB = 2,
+       CMD_GEOMETRY = 3,
+       CMD_TIMING = 4,
+       CMD_ACCEL = 5,
+       CMD_HSYNC = 6,
+       CMD_VSYNC = 7,
+       CMD_LACED = 8,
+       CMD_DOUBLE = 9,
+/*     CMD_XCOMPAT =     10, */
+       CMD_ALL = 11,
+       CMD_INFO = 12,
+       CMD_CHANGE = 13,
 
 #ifdef BB_FEATURE_FBSET_FANCY
-#define CMD_XRES       100
-#define CMD_YRES       101
-#define CMD_VXRES      102
-#define CMD_VYRES      103
-#define CMD_DEPTH      104
-#define CMD_MATCH      105
-#define CMD_PIXCLOCK   106
-#define CMD_LEFT       107
-#define CMD_RIGHT      108
-#define CMD_UPPER      109
-#define CMD_LOWER      110
-#define CMD_HSLEN      111
-#define CMD_VSLEN      112
-#define CMD_CSYNC      113
-#define CMD_GSYNC      114
-#define CMD_EXTSYNC    115
-#define CMD_BCAST      116
-#define CMD_RGBA       117
-#define CMD_STEP       118
-#define CMD_MOVE       119
+       CMD_XRES = 100,
+       CMD_YRES = 101,
+       CMD_VXRES = 102,
+       CMD_VYRES = 103,
+       CMD_DEPTH = 104,
+       CMD_MATCH = 105,
+       CMD_PIXCLOCK = 106,
+       CMD_LEFT = 107,
+       CMD_RIGHT = 108,
+       CMD_UPPER = 109,
+       CMD_LOWER = 110,
+       CMD_HSLEN = 111,
+       CMD_VSLEN = 112,
+       CMD_CSYNC = 113,
+       CMD_GSYNC = 114,
+       CMD_EXTSYNC = 115,
+       CMD_BCAST = 116,
+       CMD_RGBA = 117,
+       CMD_STEP = 118,
+       CMD_MOVE = 119,
 #endif
+};
 
 static unsigned int g_options = 0;
 
 /* Stuff stolen from the kernel's fb.h */
-#define FBIOGET_VSCREENINFO     0x4600
-#define FBIOPUT_VSCREENINFO     0x4601
+static const int FBIOGET_VSCREENINFO = 0x4600;
+static const int FBIOPUT_VSCREENINFO = 0x4601;
 #define __u32                  unsigned int
 struct fb_bitfield {
        __u32 offset;                   /* beginning of bitfield        */
@@ -131,7 +132,7 @@ struct fb_var_screeninfo {
 };
 
 
-struct cmdoptions_t {
+static struct cmdoptions_t {
        char *name;
        unsigned char param_count;
        unsigned char code;
@@ -182,12 +183,12 @@ struct cmdoptions_t {
 
 #ifdef BB_FEATURE_FBSET_READMODE
 /* taken from linux/fb.h */
-#define FB_VMODE_INTERLACED    1       /* interlaced   */
-#define FB_VMODE_DOUBLE                2       /* double scan */
-#define FB_SYNC_HOR_HIGH_ACT   1       /* horizontal sync high active  */
-#define FB_SYNC_VERT_HIGH_ACT  2       /* vertical sync high active    */
-#define FB_SYNC_EXT            4       /* external sync                */
-#define FB_SYNC_COMP_HIGH_ACT  8       /* composite sync high active   */
+static const int FB_VMODE_INTERLACED = 1;      /* interlaced   */
+static const int FB_VMODE_DOUBLE = 2;  /* double scan */
+static const int FB_SYNC_HOR_HIGH_ACT = 1;     /* horizontal sync high active  */
+static const int FB_SYNC_VERT_HIGH_ACT = 2;    /* vertical sync high active    */
+static const int FB_SYNC_EXT = 4;      /* external sync                */
+static const int FB_SYNC_COMP_HIGH_ACT = 8;    /* composite sync high active   */
 #endif
 static int readmode(struct fb_var_screeninfo *base, const char *fn,
                                        const char *mode)
@@ -197,8 +198,7 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
        char buf[256];
        char *p = buf;
 
-       if ((f = fopen(fn, "r")) == NULL)
-               PERROR("readmode(fopen)");
+       f = xfopen(fn, "r");
        while (!feof(f)) {
                fgets(buf, sizeof(buf), f);
                if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
@@ -283,7 +283,7 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
                }
        }
 #else
-       errorMsg( "mode reading not compiled in\n");
+       error_msg( "mode reading not compiled in");
 #endif
        return 0;
 }
@@ -333,26 +333,6 @@ static void showmode(struct fb_var_screeninfo *v)
        printf("endmode\n\n");
 }
 
-static void fbset_usage(void)
-{
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       int i;
-#endif
-
-#ifndef STANDALONE
-       fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
-                       BB_VER, BB_BT);
-#endif
-       fprintf(stderr, "Usage: fbset [options] [mode]\n");
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       fprintf(stderr, "\nShows and modifies frame buffer device settings\n\n");
-       fprintf(stderr, "The following options are recognized:\n");
-       for (i = 0; g_cmdoptions[i].name; i++)
-               fprintf(stderr, "\t%s\n", g_cmdoptions[i].name);
-#endif
-       exit(-1);
-}
-
 #ifdef STANDALONE
 int main(int argc, char **argv)
 #else
@@ -374,10 +354,10 @@ extern int fbset_main(int argc, char **argv)
                for (i = 0; g_cmdoptions[i].name; i++) {
                        if (!strcmp(thisarg, g_cmdoptions[i].name)) {
                                if (argc - 1 < g_cmdoptions[i].param_count)
-                                       fbset_usage();
+                                       show_usage();
                                switch (g_cmdoptions[i].code) {
                                case CMD_HELP:
-                                       fbset_usage();
+                                       show_usage();
                                case CMD_FB:
                                        fbdev = argv[1];
                                        break;
@@ -422,18 +402,18 @@ extern int fbset_main(int argc, char **argv)
                                mode = *argv;
                                g_options |= OPT_READMODE;
                        } else {
-                               fbset_usage();
+                               show_usage();
                        }
                }
        }
 
        if ((fh = open(fbdev, O_RDONLY)) < 0)
-               PERROR("fbset(open)");
+               perror_msg_and_die("fbset(open)");
        if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
-               PERROR("fbset(ioctl)");
+               perror_msg_and_die("fbset(ioctl)");
        if (g_options & OPT_READMODE) {
                if (!readmode(&var, modefile, mode)) {
-                       errorMsg("Unknown video mode `%s'\n", mode);
+                       error_msg("Unknown video mode `%s'", mode);
                        return EXIT_FAILURE;
                }
        }
@@ -441,7 +421,7 @@ extern int fbset_main(int argc, char **argv)
        setmode(&var, &varset);
        if (g_options & OPT_CHANGE)
                if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
-                       PERROR("fbset(ioctl)");
+                       perror_msg_and_die("fbset(ioctl)");
        showmode(&var);
        /* Don't close the file, as exiting will take care of that */
        /* close(fh); */