免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: lhb1k
打印 上一主题 下一主题

[SCO UNIX] 如何在终端上不出现login,直接执行应用程序。 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2002-12-06 16:02 |只看该作者

如何在终端上不出现login,直接执行应用程序。

俺也想知道!!!!
151515 该用户已被删除
12 [报告]
发表于 2002-12-06 16:35 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
13 [报告]
发表于 2002-12-06 16:41 |只看该作者

如何在终端上不出现login,直接执行应用程序。

[这个贴子最后由fdlj53u在 2002/12/06 04:44pm 编辑]

我给出一个示例文件,希望大家有所帮助.
文件一:tty_mng.c(用来设置终端是直接登录还是出现login,root权限使用)
---------------------------------------
#include "stdio.h"
#include "string.h"
main(argc,argv)
int argc&#59;
char *argv[]&#59;
{
int i&#59;

printf("Make tty to  'face' or 'login'...\n&quot&#59;
printf("USAGE:`tty_mng  face  env tty`\n&quot&#59;
printf("   or `tty_mng  login     tty`\n\n&quot&#59;
if(argc<3) {
   printf(&quot;Too few parameters\n&quot&#59;
   exit(1)&#59;
}
if(strcmp(argv[1],&quot;login&quot==0) {
       for(i=2&#59;i<argc&#59;i++) login(argv)&#59;
}
else if(strcmp(argv[1],&quot;face&quot==0) {
       for(i=3&#59;i<argc&#59;i++) face(argv[2],argv)&#59;
}
system(&quot;/bin/telinit q&quot&#59;
exit(i)&#59;
}

login(tty)
char *tty&#59;
{
int  ignore&#59;
char tmp[100]&#59;
char tmp1[100]&#59;
char s[512+1]&#59;
int  i&#59;
int  j&#59;
FILE *f1,*f2&#59;

sprintf(tmp,&quot;/dev/%s&quot;,tty)&#59;
sprintf(tmp1,&quot;respawn:/etc/getty %s&quot;,tty)&#59;
if(access(tmp,0)==(-1)) {
     printf(&quot;Error tty [%s],ignored\n&quot;,tmp)&#59;
     return(-1)&#59;
}
f1=fopen(&quot;/etc/inittab&quot;,&quot;r&quot&#59;
if(f1==NULL){
     perror(&quot;/etc/inittab&quot&#59;
     exit(1)&#59;
}
f2=fopen(&quot;/tmp/inittab&quot;,&quot;w&quot&#59;
if(f2==NULL){
     perror(&quot;/tmp/inittab&quot;)&#59;
     fclose(f1)&#59;
     exit(1)&#59;
}
ignore=0&#59;
while(!feof(f1)){
     s[0]='\0'&#59;
     fgets(s,512,f1)&#59;
     if(s[0]=='\0') break&#59;
     if((strstr(s,tmp))==NULL||strstr(s,&quot;/usr/bin/face&quot;)==NULL){
             fprintf(f2,&quot;%s&quot;,s)&#59;
             if(s[0]!='#'&amp;&amp;strstr(s,tmp1)!=NULL) ignore=1&#59;
     }
     else printf(&quot;#%s&quot;,s)&#59;
}
j=strlen(tty)&#59;
if(!ignore)fprintf(f2,&quot;%s:23:respawn:/etc/getty %s %s\n&quot;,&amp;tty[3],tty,j==5?&quot;sc_m&quot;:&quot;m&quot;)&#59;
fclose(f1)&#59;
fclose(f2)&#59;
if(ignore) printf(&quot;login %s ignored\n&quot;,tty)&#59;
else printf(&quot;login %s OK\n&quot;,tty)&#59;
system(&quot;cp /tmp/inittab /etc/inittab&quot;)&#59;
}

face(env,tty)
char *env,*tty&#59;
{
int  ignore=0&#59;
char tmp[100]&#59;
char tmp1[100]&#59;
char tmp2[100]&#59;
char s[512+1]&#59;
int  i&#59;
FILE *f1,*f2&#59;

sprintf(tmp,&quot;/dev/%s&quot;,tty)&#59;
if(access(tmp,0)==(-1)) {
     printf(&quot;Error tty [%s],ignored\n&quot;,tmp)&#59;
     return(-1)&#59;
}
sprintf(tmp1,&quot;respawn:/usr/bin/face&quot;)&#59;
sprintf(tmp2,&quot;/dev/%s&quot;,tty)&#59;
sprintf(tmp,&quot;/etc/getty %s&quot;,tty)&#59;
f1=fopen(&quot;/etc/inittab&quot;,&quot;r&quot;)&#59;
if(f1==NULL){
     perror(&quot;/etc/inittab&quot;)&#59;
     exit(1)&#59;
}
f2=fopen(&quot;/tmp/inittab&quot;,&quot;w&quot;)&#59;
if(f2==NULL){
     perror(&quot;/tmp/inittab&quot;)&#59;
     fclose(f1)&#59;
     exit(1)&#59;
}
ignore=0&#59;
while(!feof(f1)){
     s[0]='\0'&#59;
     fgets(s,512,f1)&#59;
     if(s[0]=='\0') break&#59;
     if((strstr(s,tmp))==NULL) {
        fprintf(f2,&quot;%s&quot;,s)&#59;
        if(s[0]!='#'&amp;&amp;strstr(s,tmp2)!=NULL&amp;&amp;strstr(s,tmp1)!=NULL) ignore=1&#59;
     }
     else printf(&quot;#%s&quot;,s)&#59;
}
if(!ignore) fprintf(f2,&quot;%s:23:respawn:/usr/bin/face %s >;/dev/%s 2>;/dev/%s </dev/%s\n&quot;,&amp;tty[3],env,tty,tty,tty)&#59;
fclose(f1)&#59;
fclose(f2)&#59;
if(ignore) printf(&quot;face %s ignored\n&quot;,tty)&#59;
else printf(&quot;face %s OK\n&quot;,tty)&#59;
system(&quot;cp /tmp/inittab /etc/inittab&quot;)&#59;
}
---------------------------------------
文件2:face.c (一个屏幕显示的操作界面,修改一下可以用的)
---------------------#include &quot;curses.h&quot;
#include &quot;string.h&quot;
#include &quot;time.h&quot;
#include &quot;ctype.h&quot;
#include &quot;stdio.h&quot;
#include &quot;signal.h&quot;
#define  UP     11201
#define  LEFT   11202
#define  DOWN   11203
#define  RIGHT  11204
#define  F1   11205
#define  F2   11206
#define  F3   11207
#define  F4   11208
#define  LINE 4
#define  COL  5
static char env[100][100]&#59;
char pwd[100]&#59;
char tty[100]&#59;
int  root=0&#59;
int  limit=10&#59;
int  ACTIVE=1&#59;
int  mod&#59;
char TERM[20]&#59;
char TERMTYPE[20]&#59;
int  CLOCK()&#59;
int  tick=0&#59;
int  lang=0&#59;     /* 0-chinese  1- english  2- other  **/
char *cmd[LINE*COL]={NULL,
                     &quot;exec ../bin/aaa.exe&quot;,
                     &quot;exec ../bin/bbb.exe&quot;,
                     &quot;exec ../bin/ccc.exe&quot;,
                     &quot;exec ../bin/ddd.exe&quot;,
                     &quot;exec ../bin/eee.exe&quot;,
                     &quot;exec ../bin/fff.exe&quot;,
                     &quot;exec ../bin/ggg.exe&quot;,
                     &quot;exec ../bin/hhh.exe&quot;,
                     &quot;exec ../bin/iii.exe&quot;,
                     &quot;exec ../bin/jjj.exe&quot;,
                     &quot;exec ../bin/kkk.exe&quot;,
                     &quot;exec ../bin/lll.exe&quot;,
                     &quot;exec ../bin/mmm.exe&quot;,
                     &quot;exec su - history&quot;,
                     &quot;exec su - test&quot;,
                     &quot;exec ../bin/ffda.exe&quot;,
                     &quot;exec ../bin/tt.exe&quot;,
                     &quot;exec ../bin/tttt.exe&quot;,
                     &quot;tput clear&#59;echo Type EXIT to return...&#59;/bin/sh&quot;
}&#59;
char *func[LINE*COL+1]={&quot;系统登录&quot;,
                      &quot;⒈AAAAAAAA&quot;,
                      &quot;⒉BBBBBBBB&quot;,
                      &quot;⒊CCCCCCCC&quot;,
                      &quot;⒋DDDDDDDD&quot;,
                      &quot;⒌EEEEEEEE&quot;,
                      &quot;⒍FFFFFFFF&quot;,
                      &quot;⒎GGGGGGGG&quot;,
                      &quot;⒏HHHHHHHH&quot;,
                      &quot;⒐IIIIIIII&quot;,
                      &quot;⒑JJJJJJJJ&quot;,
                      &quot;⒒KKKKKKKK&quot;,
                      &quot;⒓LLLLLLLL&quot;,
                      &quot;⒔MMMMMMMM&quot;,
                      &quot;⒕NNNNNNNN&quot;,
                      &quot;⒖系统练习&quot;,
                      &quot;⒗PPPPPPPP&quot;,
                      &quot;⒘系统维护&quot;,
                      &quot;⒙杂项功能&quot;,
                      &quot;UNIX\nS H E L L&quot;,
                      NULL
}&#59;
int a[LINE*COL]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}&#59;
int x[COL]= {6,20,34,48,62}&#59;
int y[LINE]={3 , 7,11,15}&#59;

main(argc,argv)
int argc&#59;
char *argv[]&#59;
{
int max,i,k,org&#59;
char comd[120]&#59;
int key&#59;
int j&#59;
FILE *fpt&#59;
char pass[100]&#59;
char buff[10],b_p=0&#59;

putenv(&quot;TERM=vt100&quot;)&#59;
putenv(&quotRN=FILE&quot;)&#59;
set_env(argv[1])&#59;
chdir(getenv(&quotWD&quot;))&#59;
getcwd(pwd)&#59;
sprintf(TERM,&quot;%.18s&quot;,getenv(&quot;TERM&quot;))&#59;
sprintf(TERMTYPE,&quot;%.18s&quot;,getenv(&quot;TERMTYPE&quot;))&#59;
sprintf(comd,&quot;grep '^%s' /etc/passwd | cut -d':' -f3&quot;,getenv(&quot;LOGNAME&quot;))&#59;
fpt=popen(comd,&quot;r&quot;)&#59;
fscanf(fpt,&quot;%d&quot;,&amp;j)&#59;
pclose(fpt)&#59;
setuid(j)&#59;
umask(0)&#59;
for(max=0&#59;func[max]!=(char *)NULL&#59;max++) &#59;
form_scr()&#59;
refresh()&#59;
k=0&#59;
a[k]=2&#59;
org=k&#59;
refresh()&#59;
for(&#59;&#59;org=k){
    draw(k)&#59;
    refresh()&#59;
    if(!ACTIVE&amp;&amp;tick>;120) alarm(0)&#59;
    else alarm(2)&#59;
    key=wc_getch(stdscr)&#59;
    alarm(0)&#59;
    if(key!=(-1)) tick=0&#59;
    else {
       continue&#59;
    }
    switch(key){
        case LEFT:do k-=LINE&#59;while(a[k]==0&amp;&amp;k>;=0)&#59;
                  if(k<0) k+=LINE&#59;
                  if(a[k]==0) k=org&#59;
                  a[org]=1&#59;
                  a[k]=2&#59;
                  break&#59;
       case RIGHT:
        case    9:do k+=LINE&#59; while(a[k]==0&amp;&amp;k<max)&#59;
                  if(k>;=max) k-=LINE&#59;
                  if(a[k]==0) k=org&#59;
                  a[org]=1&#59;
                  a[k]=2&#59;
                  break&#59;
        case   UP:do k--&#59;while(a[k]==0&amp;&amp;k>;=0)&#59;
                  if(k<0) k++&#59;
                  if(a[k]==0) k=org&#59;
                  a[org]=1&#59;
                  a[k]=2&#59;
                  break&#59;
        case DOWN:do k++&#59;while(a[k]==0&amp;&amp;k<max)&#59;
                  if(k>;=max) k--&#59;
                  if(a[k]==0) k=org&#59;
                  a[org]=1&#59;
                  a[k]=2&#59;
                  break&#59;
       case '0'  :
       case '1'  :
       case '2'  :
       case '3'  :
       case '4'  :
       case '5'  :
       case '6'  :
       case '7'  :
       case '8'  :
       case '9'  :buff[b_p++]=key&#59;
                  buff[b_p]='\0'&#59;
                  org=k&#59;
                  k=atoi(buff)&#59;
                  if(k<=0||k>;1 {
                      buff[0]=key&#59;
                      b_p=1&#59;
                      buff[1]='\0'&#59;
                  }
                  k=atoi(buff)&#59;
                  if(!a[k]) {
                      if(key=='1') &#59;else b_p=0&#59;
                      k=org&#59;
                  }
                  else {
                      a[org]=1&#59;
                      a[k]=2&#59;
                  }
                  break&#59;
       case 10   :
       case 13   :
                  if(cmd[k]!=NULL){
                       endwin()&#59;
                       sprintf(comd,&quot;%s&quot;,cmd[k])&#59;
                       system(comd)&#59;
                  }
                  break&#59;
       case 'x'  :
       case 24   :
       case 27   :
       case F4   :endwin()&#59;exit(0)&#59;
       case 11   :endwin()&#59;form_scr()&#59;break&#59;
       default   :break&#59;
    }
    draw(org)&#59;
}
endwin()&#59;
}

draw(i)
int i&#59;
{
int attr&#59;

switch(a){
     case 0:attr=A_DIM&#59;break&#59;
     case 1:attr=A_BOLD&#59;break&#59;
     case 2:attr=A_BOLD|A_REVERSE&#59; break&#59;
}
attron(attr)&#59;
BOX(y[i%LINE],x[i/LINE],func)&#59;
attroff(attr)&#59;
refresh()&#59;
return(a)&#59;
}

BOX(by,bx,str)
int   by,bx&#59;
char  *str&#59;
{
char *tmp,tmp1[100],buff[1024]&#59;
int  k,lines=0&#59;

tmp=str&#59;
do{
  tmp=strchr(tmp+1,'\n')&#59;
  if(tmp!=NULL) lines++&#59;
}while(tmp!=NULL)&#59;
lines=3-lines&#59;
sprintf(tmp1,&quot;%s&quot;,str)&#59;
do{
  tmp=strchr(tmp1,'\n')&#59;
  if(tmp==NULL) sprintf(buff,&quot;%s&quot;,tmp1)&#59;
  else {
     (*tmp)='\0'&#59;
     sprintf(buff,&quot;%s&quot;,tmp1)&#59;
     sprintf(tmp1,&quot;%s&quot;,tmp+1)&#59;
  }
  if(strlen(buff)>;12) k=0&#59;
  else k=(12-strlen(buff))/2&#59;
  if(k<=0) mvprintw(by+lines,bx,&quot;%-12.12s&quot;,buff)&#59;
  else mvprintw(by+lines,bx,&quot;%*.*s%-*.*s&quot;,k,k,&quot;&quot;,12-k,12-k,buff)&#59;
  lines++&#59;
}while(tmp!=NULL)&#59;
return(lines)&#59;
}

form_scr()
{
int i&#59;

if(strcmp(TERM,&quot;ansi&quot;)==0) printf(&quot;[0m[=0E[=7F[=1G[0m[J[7m[m&quot;)&#59;
initscr()&#59;cbreak()&#59;
mvaddstr(0,0,&quot;┌─────────────────────────────────────┐&quot;)&#59;
mvaddstr(1,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(2,0,&quot;├─────────────────────────────────────┤&quot;)&#59;
mvaddstr(3,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(4,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(5,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(6,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(7,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(8,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(9,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(10,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(11,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(12,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(13,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(14,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(15,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(16,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(17,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(18,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(19,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(20,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(21,0,&quot;├─────────────────────────────────────┤&quot;)&#59;
mvaddstr(22,0,&quot;│                                                                          │&quot;)&#59;
mvaddstr(23,0,&quot;└─────────────────────────────────────┘&quot;)&#59;
mvprintw(22,2,&quot;%s&quot;,tty)&#59;
attron(A_BOLD)&#59;
mvprintw(22,20,&quot;%s&quot;,getenv(&quot;VERSION&quot;))&#59;
mvprintw(1,26,&quot;%s&quot;,getenv(&quot;HOTEL&quot;))&#59;
attroff(A_BOLD)&#59;
for(i=0&#59;func!=NULL&#59;i++) draw(i)&#59;
CLOCK()&#59;
signal(SIGPWR,SIG_IGN)&#59;
signal(3,SIG_IGN)&#59;
signal(2,SIG_IGN)&#59;
refresh()&#59;
}

int wc_getch(win)
WINDOW *win&#59;
{
int c,next,next1&#59;

noecho()&#59;
do{
  if(!ACTIVE&amp;&amp;tick>;120) alarm(0)&#59;
  else alarm(2)&#59;
  c=wgetch(win)&#59;
  alarm(0)&#59;
  if(c!=27) return(c)&#59;                 /* Choice Number        */
  else break&#59;                          /* Arrow Key            */
} while(1)&#59;
next=wgetch(win)&#59;
if(next!='['&amp;&amp;next!='O') return(next)&#59;
next1=wgetch(win)&#59;
switch(next1){
   case 'P': return(F1)&#59;
   case 'Q': return(F2)&#59;
   case 'R': return(F3)&#59;
   case 'S': return(F4)&#59;
   case 'A': return(UP)&#59;
   case 'B': return(DOWN)&#59;
   case 'C': return(RIGHT)&#59;
   case 'D': return(LEFT)&#59;
   default : return(11200+next1)&#59;
}&#59;
}

CLOCK()
{
struct tm *ttm&#59;
long   tt&#59;
int    yy,mm,dd,hh,mi,sec&#59;

tt=time(NULL)&#59;
ttm=localtime(&amp;tt)&#59;
yy=ttm->;tm_year+1900&#59;
mm=ttm->;tm_mon+1&#59;
dd=ttm->;tm_mday&#59;
hh=ttm->;tm_hour&#59;
mi=ttm->;tm_min&#59;
sec=ttm->;tm_sec&#59;

mvprintw(1,2,&quot;%s&quot;,pwd)&#59;
attron(A_BOLD)&#59;
mvprintw(1,56,&quot;%02d/%02d/%02d %02d:%02d`%02d&quot;,mm,dd,yy,hh,mi,sec)&#59;
if(ACTIVE) {
     mvprintw(22,62,&quot;%-10.10s&quot;,&quot;LOGIN&quot;)&#59;
}
else {
   mvprintw(22,62,&quot;%-10.10s&quot;,&quot;&quot;)&#59;
   mvprintw(22,74,&quot;  &quot;)&#59;
}
attroff(A_BOLD)&#59;
tick++&#59;
if(ACTIVE&amp;&amp;tick>;600) {endwin()&#59;exit(1)&#59;}
refresh()&#59;
signal(SIGALRM,CLOCK)&#59;
}

set_env(str)
char *str&#59;
{
FILE *fpt&#59;
int  i=0&#59;

fpt=popen(&quot;tty&quot;,&quot;r&quot;)&#59;
fscanf(fpt,&quot;%s&quot;,tty)&#59;
pclose(fpt)&#59;
sprintf(env,&quot;TTY=%s&quot;,tty)&#59;
putenv(env)&#59;
fpt=fopen(str,&quot;r&quot;)&#59;
if(fpt==NULL) {
    perror(str)&#59;
    return(-1)&#59;
}
i=1&#59;
while(!feof(fpt)){
    env[0]='\0'&#59;
    fgets(env,80,fpt)&#59;
    if(env[0]=='\0') break&#59;
    env[strlen(env)-1]='\0'&#59;
    if(env[0]!='#'&amp;&amp;env[0]!='!') putenv(env)&#59;
    else if(env[0]=='!') system(&amp;env[1])&#59;
    i++&#59;
}
fclose(fpt)&#59;
if(strcmp(getenv(&quotRN&quot;),&quot;FILE&quot;)==0) {
     sprintf(env,&quotRN=/tmp/%s&quot;,&amp;tty[5])&#59;
     putenv(env)&#59;
}
return(1)&#59;
}

------------------------
文件3.env(一个环境设置文件,必须要有,否则执行出错)
----------------------
TERM=vt100
LOGNAME=test
HOME=/usr/test
PRN=FILE
HOTEL=AAAAAAAAAAAA
VERSION=2002.8   fdlj53u  版权所有
!stty icanon icrnl onlcr 38400 -istrip  ixon ixoff ixany echo echoe opost
------------------------

论坛徽章:
0
14 [报告]
发表于 2002-12-06 17:06 |只看该作者

如何在终端上不出现login,直接执行应用程序。

辛苦劳动
无私奉献!

论坛徽章:
0
15 [报告]
发表于 2002-12-06 17:13 |只看该作者

如何在终端上不出现login,直接执行应用程序。

精华的贴子。

论坛徽章:
0
16 [报告]
发表于 2002-12-06 17:15 |只看该作者

如何在终端上不出现login,直接执行应用程序。

高人!

论坛徽章:
0
17 [报告]
发表于 2002-12-06 21:38 |只看该作者

如何在终端上不出现login,直接执行应用程序。

face.c,这个程序我编译错,是不是这样的
cc face.c -o face -lcurses

论坛徽章:
0
18 [报告]
发表于 2002-12-06 22:17 |只看该作者

如何在终端上不出现login,直接执行应用程序。

这个问题应该问工商银行。他们那都是这样的,用不着在LOGIN下注册用户。
这样的程序保密性很强。

论坛徽章:
0
19 [报告]
发表于 2002-12-06 22:53 |只看该作者

如何在终端上不出现login,直接执行应用程序。

楼上老大,
应该是安全性好。

论坛徽章:
0
20 [报告]
发表于 2002-12-06 23:17 |只看该作者

如何在终端上不出现login,直接执行应用程序。

[这个贴子最后由f7zz在 2002/12/07 01:26am 编辑]


其实道理很简单,下面是一个试验。
如果想在终端/dev/tty6a上搞一个这个,
先disable /dev/tty6a(你得是“根”哦)。
然后试试这个:(也得注意你的权限)
scoadmin < /dev/tty6a >; /dev/tty6a 2>;&amp;1
然后在/dev/tty6a上你就可以看到scoadmin了,并且使用它,和在主机上没有区别。


主要的问题在于如何管理程序和终端之间的关系,比如启动,关闭程序。
小的以前写过一个咚咚,同前面那位兄弟的从道理上讲一样,free给大家——不精彩,供大家参考斧正。有什么问题,mail我,一起讨论,共同改进。
================================================================================
/* sterm.c
*/

#include <stdio.h>;
#include <errno.h>;

void main (int argc, char *argv[])
/* argv[1] - 终端名称
* argv[2] - 终端模式
* argv[3] - 应用程序
*/
{
FILE   *fp&#59;  
char   ttyname[16],sEnv[64]&#59;

if(argc<3)  exit(0)&#59;

/* 测试终端是否有效/可读写 */
sprintf(ttyname,&quot;/dev/%s&quot;,argv[1])&#59;
if((fp=fopen(ttyname,&quot;rw&quot)==NULL) exit(0)&#59;
fclose(fp)&#59;

/* 设置终端模式 */
sprintf(sEnv,&quot;TERM=%s&quot;,argv[2])&#59;
   putenv(sEnv)&#59;

/* 将当前进程替换给 argv[3]  */
   execl(argv[3],argv[3],(char *)0)&#59;

exit(1)&#59;
}
================================================================================
// tmgr.c


/* ==================================================================
*终端控制程序
*2001.10.20f7zz Write
* ==================================================================*/

#include <stdio.h>;
#include <errno.h>;
#include <unistd.h>;
#include <sys/types.h>;
#include <dirent.h>;
#include <curses.h>;
#include <string.h>;

#define true 1
#define false 0
// ------------------------------------------------------------------
//  启动前台程序
#define PROGRAM&quot;main&quot;
int uptty(char *ttyname,char *ttytype,char *sErr)
{
charsCmd[128],sTmp[128]&#59;

// 检查终端程序是否处于运行状态
if(access(ttyname,F_OK) == 0)
{
sprintf(sErr,&quot;%s终端处于激活状态,请关闭后再启动!\n&quot;,ttyname)&#59;
return false&#59;
}
// 检查对该终端是否有读写权限
sprintf(sTmp,&quot;/dev/%s&quot;,ttyname)&#59;
if(access(sTmp,R_OK|W_OK) == -1)
{
sprintf(sErr,&quot;对%s终端没有合法的读写权限,请检查后重试!\n&quot;,ttyname)&#59;
return false&#59;
}
// 启动终端
sprintf(sCmd,&quot;sterm %s %s %s </dev/%s>;/dev/%s 2>;&amp;1&amp; &quot;,
ttyname,ttytype,PROGRAM,ttyname,ttyname)&#59;
system(sCmd)&#59;

sleep(1)&#59;
// 检查终端程序是否运行
if(access(ttyname,F_OK) != 0)
{
sprintf(sErr,&quot;%s终端程序启动失败,请重试!\n&quot;,ttyname)&#59;
return false&#59;
}
sprintf(sErr,&quot;%s终端启动成功!\n&quot;,ttyname)&#59;
return true&#59;
}

// ------------------------------------------------------------------
// 关闭前台终端
void killtty(char *ttyname, char *sErr)
{
FILE *fp&#59;
char sCmd[128],sTmp[128]&#59;
// 检查终端程序是否处于运行状态
if(access(ttyname,F_OK) == -1 )
{
sprintf(sErr,&quot;%s终端没有程序运行,请检查终端号!\n&quot;,ttyname)&#59;
return&#59;
}
if((fp=fopen(ttyname,&quot;r&quot)==NULL)
{
sprintf(sErr,&quot;%s终端进程文件读入失败!\n&quot;,ttyname)&#59;
return&#59;
}
// 取得进程号
fgets(sTmp,sizeof(sTmp),fp)&#59;
if(atoi(sTmp) == 0)
{
sprintf(sErr,&quot;&quot&#59;
return&#59;
}
fclose(fp)&#59;

// 杀死进程
sprintf(sCmd,&quot;kill %s&quot;,sTmp)&#59;
system(sCmd)&#59;
// 删除文件
remove(ttyname)&#59;
sprintf(sErr,&quot;%s终端程序关闭成功!\n&quot;,ttyname)&#59;
return&#59;
}

// ------------------------------------------------------------------
// 启动全部终端
#define TTYTABFILE&quot;ttytab&quot;
void upalltty()
{
FILE *fp&#59;
char sTmp[128],ttyname[32],ttytype[32],sErr[128]&#59;

if((fp=fopen(TTYTABFILE,&quot;r&quot)==NULL)
{
fprintf(stdout,&quot;打开ttytab文件失败,请检查后重试!\n&quot&#59;
return&#59;
}

while(!feof(fp))
{
memset(ttyname,0,sizeof(ttyname))&#59;
memset(ttytype,0,sizeof(ttytype))&#59;

fgets(sTmp,sizeof(sTmp),fp)&#59;
if(!memcmp(sTmp,&quot;!end&quot;,4))
break&#59;

getsubstr(sTmp,1,ttyname)&#59;
getsubstr(sTmp,2,ttytype)&#59;

// 启动终端程序
uptty(ttyname,ttytype,sErr)&#59;
// 打印结果
fprintf(stdout,sErr)&#59;
}
fclose(fp)&#59;
return&#59;
}

// ------------------------------------------------------------------
// 关闭全部程序
int killalltty()
{
DIR*dp&#59;
struct dirent*dirp&#59;
charsErr[128]&#59;

if((dp = opendir(&quot;./&quot) == NULL)
{
fprintf(stdout,&quot;当前目录打开错误,请检查权限!&quot&#59;
return&#59;
}

while((dirp = readdir(dp)) != NULL)
{
if(memcmp(&quot;tty&quot;,dirp->;d_name,3) == 0)
{
killtty(dirp->;d_name,sErr)&#59;
fprintf(stdout,sErr)&#59;
}
}
closedir(dp)&#59;
return&#59;
}

void chkstatus()
{
DIR*dp&#59;
struct dirent *dirp&#59;
charsTmp[64]&#59;
FILE*fp&#59;

if((dp =opendir(&quot;./&quot) ==NULL)
{
fprintf(stdout,&quot;当前目录打开错误,请检查权限!&quot&#59;
return&#59;
}
while((dirp =readdir(dp))!=NULL)
{
if(memcmp(&quot;tty&quot;,dirp->;d_name,3) == 0)
{
fp=fopen(dirp->;d_name,&quot;r&quot&#59;
fgets(sTmp,sizeof(sTmp),fp)&#59;
fclose(fp)&#59;
if(atoi(sTmp) == 0)continue&#59;
else fprintf(stdout,&quot;活动终端: %s  进程号: %s \n&quot;,dirp->;d_name,sTmp)&#59;
}
}
closedir(dp)&#59;
return&#59;
}

// 字符串函数
int getsubstr(char *str,int n,char *substr)
{
int i,j,h&#59;
char *p1,*p2&#59;

h=0&#59;
for(i=0&#59;i<n&#59;i++)
{
j=0&#59;
memset(substr,0,sizeof(substr))&#59;
while(!isgraph(str[h]))
h++&#59;
p1=p2=str+h&#59;
while(isgraph(*p2))
{
p2++&#59;
j++&#59;
h++&#59;
}
}
memcpy(substr,p1,j)&#59;
substr[j+1]=0&#59;
return j&#59;
}


// ------------------------------------------------------------------
// 主程序
char *sPrompt[]=
{&quot;\n&quot;,
&quot;\t  +========================================================+\n&quot;
&quot;\t  |     中国工商银行*****************终端管理程序 V1.2SP   |\n&quot;,
&quot;\t  +--------------------------------------------------------+\n&quot;,
&quot;\t  |                 1.激活ttytab中的全部终端               |\n&quot;,
&quot;\t  |                 2.关闭ttytab中的全部活动终端           |\n&quot;,
&quot;\t  |                 3.激活选定终端                         |\n&quot;,
&quot;\t  |                 4.关闭选定终端                         |\n&quot;,
&quot;\t  |                 5.查看全部活动终端的状态               |\n&quot;,
&quot;\t  |                                                        |\n&quot;,
&quot;\t  |                 9.退出程序                             |\n&quot;,
&quot;\t  +========================================================+\n&quot;,
&quot;\t                    请选择(1-5):&quot;,
NULL}&#59;

void main(void)
{
// 系统提示
int  i&#59;
char sInput[32],tInput[32],sErr[64]&#59;
// 接受输入
while(true)
{
system(&quot;clear&quot;)&#59;
i=0&#59;
while(sPrompt!=NULL)fprintf(stdout,&quot;%s&quot;,sPrompt[i++])&#59;
gets(sInput)&#59;
fprintf(stdout,&quot;\n&quot;)&#59;
switch(atoi(sInput))
{
case 1:
upalltty()&#59;
break&#59;
case 2:
killalltty()&#59;
break&#59;
case 3:
fprintf(stdout,&quot;终端设备名(如tty6a):&quot;)&#59;
gets(sInput)&#59;
fprintf(stdout,&quot;仿真类型(默认为vt100):&quot;)&#59;
gets(tInput)&#59;
if(tInput[0]== 0) strcpy(tInput,&quot;vt100&quot;)&#59;
uptty(sInput,tInput,sErr)&#59;
fprintf(stdout,&quot;\n&quot;)&#59;
fprintf(stdout,sErr)&#59;
break&#59;
case 4:
fprintf(stdout,&quot;终端设备名(如tty6a):&quot;)&#59;
gets(sInput)&#59;
killtty(sInput,sErr)&#59;
fprintf(stdout,&quot;\n&quot;)&#59;
fprintf(stdout,sErr)&#59;
break&#59;
case 5:
chkstatus()&#59;
break&#59;
case 9:
goto end&#59;
default:
fprintf(stdout,&quot;错误的选项,请选择1-5\n&quot;)&#59;
}
  fprintf(stdout,&quot;按Enter键继续......&quot;)&#59;
getchar()&#59;
}
end:
return&#59;
}



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

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP