|
发表于 2006-1-16 23:19:48
|
显示全部楼层
will_u_startx.c
- #include <stdio.h>
- #include <termios.h>
- #include <term.h>
- #include <curses.h>
- #include <unistd.h>
- /*#include <stdlib.h>*/
- #define DEFAULT_DELAY 3
- #define INFO "Enter to startx, ESC to break "
- #define CMD_STARTX "/usr/bin/X11/startx"
- /*#define CMD_STARTX "startx"*/
- #define X_POS 1
- #define Y_POS 1
- static struct termios init_setting, new_setting;
- void initialization();
- void finalization();
- int errmsg(const char * msg, int iexit);
- void my_exit(int f);
- int moveto(char *cup, int x, int y);
- int kbhit();
- int startx();
- int main(int argc, char **argv)
- {
- int opt;
- int delay = DEFAULT_DELAY;
- int i, c;
- char *cursor;
- char info[] = INFO;
- initialization();
-
- while ((opt = getopt(argc, argv, "t:")) != -1)
- {
- switch(opt)
- {
- case 't':
- delay = atoi(optarg);
- if (delay == 0)
- delay = DEFAULT_DELAY;
- break;
- case ':':
- errmsg("required delay seconds", 1);
- break;
- case '?':
- errmsg("there are unknown params.", 1);
- break;
-
- }
-
- }
- printf("delay %d seconds.\n", delay);
- setupterm(NULL, fileno(stdout), NULL);
- cursor = tigetstr("cup");
- putp(tigetstr("clear")); /*clear the screen*/
- moveto(cursor, X_POS, Y_POS);
- fprintf(stdout, "%s", info);
- fflush(stdout);
- for(i = 0; i < delay; i++)
- {
- moveto(cursor, X_POS + strlen(info), Y_POS);
- fprintf(stdout, "%d", delay - i);
- fflush(stdout);
- sleep(1);
- if ((c = kbhit()) > 0)
- {
- switch(c)
- {
- case 10:
- case 13:
- startx();
- my_exit(0);
- break;
- case 27:
- my_exit(0);
-
- }
- }
- }
-
- startx();
- my_exit(0);
- }
- void initialization()
- {
- tcgetattr(0, &init_setting);
- new_setting = init_setting;
- new_setting.c_lflag &= ~ICANON;
- new_setting.c_lflag &= ~ECHO;
- new_setting.c_lflag &= ~ISIG;
- new_setting.c_cc[VMIN] = 1;
- new_setting.c_cc[VTIME] = 0;
- tcsetattr(0, TCSANOW, &new_setting);
- }
- void finalization()
- {
- tcsetattr(0, TCSANOW, &init_setting);
- fprintf(stdout, "\r\n");
- }
- int errmsg(const char * msg, int iexit)
- {
- int f = fprintf(stderr, "%s\n", msg);
- if(iexit)
- {
- my_exit(1);
- }
- return f;
- }
- void my_exit(int f)
- {
- finalization();
- exit(f);
- }
- int moveto(char *cup, int x, int y)
- {
- return putp(tparm(cup,y,x));
- }
- int kbhit()
- {
- char ch;
- int nread;
- new_setting.c_cc[VMIN] = 0;
- tcsetattr(0, TCSANOW, &new_setting);
- nread = read(0, &ch, 1);
- new_setting.c_cc[VMIN] = 1;
- tcsetattr(0, TCSANOW, &new_setting);
- if (nread == 1)
- return ch;
- else
- return 0;
- }
- int startx()
- {
- printf("\nstartx....");
- /*system(CMD_STARTX);*/
- finalization();
- execv(CMD_STARTX, NULL);
- }
复制代码
gcc -o will_u_startx will_u_startx.c
cp will_u_startx /usr/local/bin/will_u_startx
vim ~/.bash_profile append below line:
/usr/local/bin/will_u_startx |
|