日期:2014-05-16  浏览次数:20678 次

ldd3中的“重定向控制台消息”,老是错误,为啥????
重定向到控制台的程序《Linux设备驱动程序(第3版)》提供,如下setconsole.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
  char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */

  if (argc==2) 
  {
  bytes[1] = atoi(argv[1]); /* the chosen console */
  }else 
  {
  fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
  }
  if ( ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0 ) 
  { /* use stdin */
  fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n", argv[0], strerror(errno));
  exit(1);
  }
  exit(0);

#gcc -o setconsole setconsole.c //编译setconsole
#./setconsole tty0 //参数总是错误的,不知道如何设置???


------解决方案--------------------
bytes[1] = atoi(argv[1]); 明显参数需要的是一个数字,你输入tty0,bytes就是{11,0};
我猜想应该是ioctl缺少参数了,因为ioctl是一个可变参数的函数,编译是不会出错的,具体还是得看TIOCLINUX的内核实现