📄 prompt.c
字号:
/* prompt.c -- Magic C++ command prompt process program
(Mostly) portable public-domain implementation
-- Copyright(C) 2003 Magicunix Infomation Technology Limited
This file is part of magicd.
magicd is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
For details, see the Magic C++ World-Wide-Web page,
`http://www.magicunix.com',
or send a mail to the Magic C++ developers <support@magicunix.com>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "errno.h"
#include "prompt.h"
#include "tools.h"
extern char szInstallDir[200];
extern int debug ;/*from magicd.c*/
/*find the command prompt for a user in log file*/
int util_get_prompt(char * user ,char * initprompt )
{
char theuser[100];
char theprompt[100];
char *home;
char lname[100];
int find = 0;
char line[1024];
FILE * lfd;
memset( theuser , 0 , sizeof( theuser ));
memset( theprompt , 0 , sizeof( theprompt ));
memset( lname , 0 , sizeof( lname ));
memset( line , 0 , sizeof( line ));
if( szInstallDir[ 0 ] != '/' )
{
fprintf(stderr,"Can not get install directory!\n");
return -1;
}
memset( lname , 0 , sizeof( lname ));
sprintf(lname,"%s/etc/prompt.ini",szInstallDir);
lfd = fopen( lname , "r+" );
if( lfd == NULL )
{
util_err_log("Can not open prompt file!\n",__FILE__,__LINE__,errno );
return -1;
}
while( util_getline( lfd , 1023 , line) != -1)
{
memset( theuser , 0 , sizeof( theuser ));
memset( theprompt , 0 , sizeof( theprompt ));
/*parse user command prompt*/
if( util_getkeyvalue_notrim( line , theuser , theprompt ) != 0 )
{
util_err_log("read prompt ini file error",__FILE__,__LINE__, errno );
}
if( !strcmp( user , theuser ) )
{
if(debug)
util_log("find the user %s in prompt.ini\n",user );
strcpy( initprompt , theprompt );
find = 1;
break;
}
}
if( find == 1 )
{
return 0;
}
else
return -1;
}
int util_set_prompt(char * user ,char * initprompt )
{
int i = 0;
int j = 0;
int find = 0;
char lname[100];
char theuser[100];
char theprompt[100];
char line[1024];
FILE * lfd;
char *home;
struct
{
char username[100];
char prompt[100];
}prompt[100];
if( szInstallDir[ 0 ] != '/' )
{
fprintf(stderr,"Can not get install directory!\n");
return -1;
}
memset( lname , 0 , sizeof( lname ));
sprintf(lname,"%s/etc/prompt.ini",szInstallDir);
lfd = fopen( lname , "r+" );
if( lfd == NULL )
{
util_err_log("Can not open sem log file!\n",__FILE__,__LINE__,errno );
exit(-3);
}
memset( line , 0 , sizeof(line ));
while( util_getline( lfd , 1023 , line) != -1)
{
memset( theuser , 0 , sizeof( theuser ));
memset( theprompt , 0 , sizeof( theprompt ));
/*get user name and command prompt*/
if( util_getkeyvalue_notrim( line , theuser , theprompt ) != 0 )
{
util_err_log("read prompt ini file error",__FILE__,__LINE__, errno );
}
if( !isalpha( theuser[0] ) )
continue;
strcpy( prompt[ i ].username , theuser );
if(!strcmp( theuser , user ) )
{
strcpy( prompt[ i ].prompt , initprompt );
find = 1;
}
else
strcpy( prompt[ i ].prompt , theprompt );
i++;
memset( line , 0 , sizeof( line ));
}
fclose(lfd );
/*can not find command prompt in log file ,so append*/
if( find == 0 )
{
strcpy( prompt[ i ].prompt , initprompt );
strcpy( prompt[ i ].username , user );
i++;
}
lfd = fopen( lname , "w+" );
for( j = 0 ; j < i ; j ++ )
{
fprintf( lfd , "%s=%s\n",prompt[j].username ,
prompt[j].prompt );
}
fclose( lfd );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -