Re: [patch] vcs.4: broken example code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



mån 2018-06-25 klockan 21:32 +0000 skrev Michael Witten:
> Anyway, putting aside the fact  that this code example makes certain
> assumptions about the  sizes of data types (including  a `short', as
> not even POSIX constrains it to exactly 16 bits), there is still the
> issue of endianness. Namely, the following works only on machines of
> little-endian nature:

Good catch, let's fix that as well while we're at it. Updated patch
below.

One thing I didn't fix, but that you may want to do something about in
a wider overhaul of the page, is the fact that the example claims to
change the background colour at the cursor position but that is only
true on a VGA console; framebuffer consoles seem to have their
attribute bits arranged differently, and the example affects the
foreground colour instead. Is there a way for user code to find out how
the attribute bits are laid out for a specific device?

----------------
Fix broken example code in the vcs.4 man page

- use of wrong variable (attrib, which is uninitialised, instead of s)
- variable ch too narrow
- printing a font char index with %c, as if it were ASCII (it's not)
- removing the high font bit while changing the background colour
- unwarranted assumption of little-endian byte order

Also be friendly and use SEEK_* instead of numbers.

diff --git a/man4/vcs.4 b/man4/vcs.4
index aebe8cfda..f695b956c 100644
--- a/man4/vcs.4
+++ b/man4/vcs.4
@@ -140,7 +140,8 @@ main(void)
     struct {unsigned char lines, cols, x, y;} scrn;
     unsigned short s;
     unsigned short mask;
-    unsigned char ch, attrib;
+    unsigned char attrib;
+    int ch;
 
     fd = open(console, O_RDWR);
     if (fd < 0) {
@@ -158,16 +159,16 @@ main(void)
         exit(EXIT_FAILURE);
     }
     (void) read(fd, &scrn, 4);
-    (void) lseek(fd, 4 + 2*(scrn.y*scrn.cols + scrn.x), 0);
+    (void) lseek(fd, 4 + 2*(scrn.y*scrn.cols + scrn.x), SEEK_SET);
     (void) read(fd, &s, 2);
     ch = s & 0xff;
-    if (attrib & mask)
+    if (s & mask)
         ch |= 0x100;
     attrib = ((s & ~mask) >> 8);
-    printf("ch=\(aq%c\(aq attrib=0x%02x\\n", ch, attrib);
-    attrib ^= 0x10;
-    (void) lseek(fd, \-1, 1);
-    (void) write(fd, &attrib, 1);
+    printf("ch=0x%03x attrib=0x%02x\\n", ch, attrib);
+    s ^= 0x1000;
+    (void) lseek(fd, \-2, SEEK_CUR);
+    (void) write(fd, &s, 2);
     exit(EXIT_SUCCESS);
 }
 .EE

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux