LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 641|回复: 2

经常在控制台下登录后startx的朋友注意了

[复制链接]
发表于 2005-3-16 23:14:00 | 显示全部楼层 |阅读模式
我的机没有装gdm 什么的, 所以每次都是在控制台下登录,然后 startx 进入图形界面。
最近在看beginning linux programming,写了个小东东,把它加在 .bash_profile 里,这样
登录后就给个信息:
Enter to startx, ESC to exit  后面跟个秒数,自动是三秒,期间回车就startx了,ESC就退出(到控制台),时间没了也startx,这个时间却省是三秒,可以通过参数-t ? 来指定。

代码在下面。保存为 will_u_startx.c
编译 gcc -o will_u_startx will_u_startx.c -lcurses
把生成的will_u_startx copy到 /usr/local/bin
在~/.bash_profile 里加入一行 will_u_startx -t 5  (5秒等待),OK~!

还有个问题,就是startx后这个程序一直还在那里。继续看书……


  1. #include <stdio.h>
  2. #include <termios.h>
  3. #include <term.h>
  4. #include <curses.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>

  7. #define DEFAULT_DELAY         3
  8. #define INFO                "Enter to startx,ESC to exit  "
  9. #define CMD_STARTX        "startx"
  10. #define X_POS                 1
  11. #define Y_POS                1

  12. static struct termios init_setting, new_setting;

  13. void initialization();
  14. void finalization();

  15. int errmsg(const char * msg, int iexit);
  16. void my_exit(int f);
  17. int moveto(char *cup, int x, int y);
  18. int kbhit();
  19. int startx();

  20. int main(int argc, char **argv)
  21. {
  22.         int opt;
  23.         int delay = DEFAULT_DELAY;
  24.         int i, c;

  25.         char *cursor;
  26.         char info[] = INFO;

  27.         initialization();
  28.        
  29.         while ((opt = getopt(argc, argv, "t:")) != -1)
  30.         {
  31.                 switch(opt)
  32.                 {
  33.                         case 't':
  34.                                 delay = atoi(optarg);                       
  35.                                 if (delay == 0)
  36.                                         delay = DEFAULT_DELAY;
  37.                                 break;
  38.                         case ':':
  39.                                 errmsg("required delay seconds", 1);
  40.                                 break;
  41.                         case '?':
  42.                                 errmsg("there are unknown params.", 1);
  43.                                 break;
  44.                                        
  45.                 }
  46.                
  47.         }

  48.         printf("delay %d seconds.\n", delay);

  49.         setupterm(NULL, fileno(stdout), NULL);
  50.         cursor = tigetstr("cup");
  51.         putp(tigetstr("clear"));                /*clear the screen*/
  52.         moveto(cursor, X_POS, Y_POS);
  53.         fprintf(stdout, "%s", info);
  54.         fflush(stdout);

  55.         for(i = 0; i < delay; i++)
  56.         {
  57.                 moveto(cursor, X_POS + strlen(info), Y_POS);
  58.                 fprintf(stdout, "%d", delay - i);
  59.                 fflush(stdout);
  60.                 sleep(1);
  61.                 if ((c = kbhit()) > 0)
  62.                 {
  63.                         switch(c)
  64.                         {
  65.                                 case 10:
  66.                                 case 13:
  67.                                         startx();
  68.                                         my_exit(0);
  69.                                         break;
  70.                                 case 27:
  71.                                         my_exit(0);
  72.                                        
  73.                         }
  74.                 }
  75.         }
  76.        
  77.         startx();
  78.         my_exit(0);
  79. }


  80. void initialization()
  81. {
  82.         tcgetattr(0, &init_setting);
  83.         new_setting = init_setting;
  84.         new_setting.c_lflag &= ~ICANON;
  85.         new_setting.c_lflag &= ~ECHO;
  86.         new_setting.c_lflag &= ~ISIG;
  87.         new_setting.c_cc[VMIN] = 1;
  88.         new_setting.c_cc[VTIME] = 0;
  89.         tcsetattr(0, TCSANOW, &new_setting);
  90. }

  91. void finalization()
  92. {
  93.         tcsetattr(0, TCSANOW, &init_setting);
  94.         fprintf(stdout, "\r\n");
  95. }

  96. int errmsg(const char * msg, int iexit)
  97. {
  98.         int f = fprintf(stderr, "%s\n", msg);
  99.         if(iexit)
  100.         {       
  101.                 my_exit(1);
  102.         }
  103.         return f;
  104. }

  105. void my_exit(int f)
  106. {
  107.         finalization();
  108.         exit(f);
  109. }

  110. int moveto(char *cup, int x, int y)
  111. {
  112.         return putp(tparm(cup,y,x));
  113. }

  114. int kbhit()
  115. {
  116.         char ch;
  117.         int nread;

  118.         new_setting.c_cc[VMIN] = 0;
  119.         tcsetattr(0, TCSANOW, &new_setting);
  120.         nread = read(0, &ch, 1);
  121.         new_setting.c_cc[VMIN] = 1;
  122.         tcsetattr(0, TCSANOW, &new_setting);
  123.         if (nread == 1)
  124.                 return ch;
  125.         else
  126.                 return 0;
  127. }

  128. int startx()
  129. {
  130.         printf("\nstartx....");
  131.         system(CMD_STARTX);
  132. }

复制代码
发表于 2005-3-16 23:35:21 | 显示全部楼层
用execv函数代替system应该就可以解决该程序在startx后驻留的问题了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-17 10:10:47 | 显示全部楼层
Post by snoopyxp
用execv函数代替system应该就可以解决该程序在startx后驻留的问题了。

谢谢啦,回去研究一下.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表