"Fossies" - the Fresh Open Source Software Archive

Member "bed-3.1.1/plugins/examples/title.cc" (20 May 2021, 4866 Bytes) of package /linux/misc/bed-3.1.1.src.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "title.cc": 3.1.0_vs_3.1.1.

    1 #include <sys/unistd.h>
    2 #include "global.h" 
    3 #include <stdio.h>
    4 #include <string.h>
    5 #include <stdlib.h>
    6 #include <unistd.h>
    7 #include "config.h"
    8 #include CURSES_HEADER
    9 extern char *  ttyname (int __fildes);
   10 #ifdef _WIN32
   11 #ifdef PDCURSES
   12 #undef MOUSE_MOVED
   13 #endif
   14 #include <windows.h>
   15 #endif
   16 BEDPLUGIN
   17 extern int withinxterm;
   18 #if !defined(PDCURSES)
   19 static const char titlestart[]="\033]0;";
   20 static const char titleend[]="\007";
   21 #define setesctitle(titlebuf) if(withinxterm)  {write(STDOUT_FILENO,titlestart,sizeof(titlestart)-1);write(STDOUT_FILENO ,titlebuf,strlen(titlebuf));write(STDOUT_FILENO ,titleend,sizeof(titleend)-1);}
   22 #endif
   23 #ifdef _WIN32
   24 #ifdef PDCURSES
   25 //#define settitle(titlebuf) SetConsoleTitle(TEXT(titlebuf))
   26 #define settitle(titlebuf) SetConsoleTitle(titlebuf)
   27 #else
   28 void settitle(const char *titlebuf) {
   29 //  SetConsoleTitle(TEXT(titlebuf));
   30     SetConsoleTitle(titlebuf);
   31 #pragma GCC diagnostic push
   32 #pragma GCC diagnostic ignored "-Wunused-result"
   33     setesctitle(titlebuf);
   34 #pragma GCC diagnostic pop
   35 }
   36 #endif
   37 #else
   38 void settitle(const char *titlebuf) {
   39 #pragma GCC diagnostic push
   40 #pragma GCC diagnostic ignored "-Wunused-result"
   41     setesctitle(titlebuf)
   42 #pragma GCC diagnostic pop
   43     }
   44 #endif
   45 void endtitle(void) {
   46     settitle("Unknown");
   47     }
   48 #ifndef PROGRAM
   49 #define PROGRAM "bed"
   50 #endif
   51 #ifndef PDCURSES
   52 #define TTYNAME
   53 #endif
   54 #ifndef  _WIN32
   55 #define HOSTNAME
   56 #endif
   57 /*
   58 #ifdef HAS_TTYNAME_R
   59 __BEGIN_DECLS
   60 extern int ttyname_r (int __fd, char *__buf, size_t __buflen);
   61 __END_DECLS
   62 #endif
   63 */
   64 
   65 #ifdef  _WIN32
   66 //Adaption of the example in http://msdn.microsoft.com/en-us/library/aa376389%28VS.85%29.aspx
   67 BOOL getadmin(void) {
   68 BOOL b;
   69 SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
   70 PSID AdministratorsGroup; 
   71 b = AllocateAndInitializeSid(
   72     &NtAuthority,
   73     2,
   74     SECURITY_BUILTIN_DOMAIN_RID,
   75     DOMAIN_ALIAS_RID_ADMINS,
   76     0, 0, 0, 0, 0, 0,
   77     &AdministratorsGroup); 
   78 if(b) 
   79 {
   80 typedef BOOL WINAPI (*CheckTokenMembershipType)( HANDLE TokenHandle, PSID SidToCheck,PBOOL IsMember);
   81 if(HMODULE advapi32=GetModuleHandle( "Advapi32.dll")) 
   82   if(CheckTokenMembershipType CheckTokenMembership =(CheckTokenMembershipType) GetProcAddress(advapi32,"CheckTokenMembership")) 
   83     if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
   84          b = FALSE;
   85 FreeSid(AdministratorsGroup); 
   86 }
   87 return b;
   88 }
   89 BOOL isadmin=getadmin();
   90     
   91 #endif //  _WIN32
   92 #if defined(DEBUGFLAG) || defined(__ANDROID__)
   93 extern pid_t pid;
   94 #endif
   95 class Editor {
   96 public:
   97 int filetype(void);
   98 int addfile(void);
   99 char *getfilename(char *buf);
  100 int titleset(void) {
  101     const int MAXTITLE=200;
  102     static const char keep_unused_variable __attribute__ ((unused)) =(atexit(endtitle),1);
  103 /*
  104 #ifdef HOSTNAME
  105     static char hostname[50];
  106     static char *ptr=(((hostname[0]='\0',gethostname(hostname, 50),(ptr=strchr(hostname,'.'))?*ptr='\0':1)),ptr);
  107     static int hostnamelen=strlen(hostname);
  108 #endif
  109 */
  110     const int proglen=sizeof(PROGRAM);
  111     char titlebuf[MAXTITLE+1];
  112     int nr=0;
  113 #ifdef  _WIN32
  114 if(isadmin) {
  115     strcpy(titlebuf,"Adm: ");
  116     nr+=5;
  117     }
  118 #endif
  119 #ifdef DEBUGFLAG
  120     nr+=sprintf(titlebuf+nr,"%d ",pid);
  121 #endif
  122 
  123     memcpy(titlebuf+nr,PROGRAM,proglen-1);
  124 
  125     nr+=proglen;
  126     titlebuf[nr-1]=' ';
  127     
  128 #ifdef HOSTNAME
  129     if(MAXTITLE>nr) {
  130 class Host {
  131 char hostname[10];
  132 int len;
  133 public:
  134 inline Host() {
  135     gethostname(hostname, 10);
  136     char *ptr
  137 #ifdef HAVE_STRCHRNUL
  138     =strchrnul(hostname,'.');
  139     {
  140 #else
  141     =strchr(hostname,'.');
  142     if(!ptr) {
  143         len=strlen(hostname);
  144         }
  145     else {
  146 #endif
  147     len=ptr-hostname;
  148     *ptr='\0';
  149     }
  150     };
  151 inline char *name()  {return hostname;};
  152 inline int size()  {return len;};
  153 };
  154         static Host host;
  155 #ifdef HAVE_MEMCCPY
  156         memccpy(titlebuf+nr,host.name(),'\0',MAXTITLE-nr);
  157 #else
  158         strncpy(titlebuf+nr,host.name(),MAXTITLE-nr);
  159 #endif
  160         nr+=host.size();
  161         titlebuf[nr++]='-';
  162         }
  163 #endif
  164 #ifdef TTYNAME
  165 #ifdef  _WIN32
  166 extern int dosconsole(void) ;
  167 if(!dosconsole()) 
  168 #endif
  169 {
  170 #if defined(HAS_TTYNAME_R)|| defined( __ANDROID__)
  171     char nametty[15];
  172 #else
  173     char *nametty;
  174 #endif  
  175 //  ptsname_r(titlebuf,ttys[i],MAXTITLE-nr);
  176     if(isatty(STDOUT_FILENO)) {
  177 #ifdef __ANDROID__
  178 withinxterm=1;
  179         const int pmax=23;
  180         char procname[pmax];
  181         snprintf(procname,pmax,"/proc/%d/fd/0",pid);
  182         int len; 
  183         if((len=readlink(procname, nametty, 15))>0&&len<15) {
  184             nametty[len]='\0';
  185 #else
  186 #ifdef HAS_TTYNAME_R
  187         if(!ttyname_r(STDOUT_FILENO,nametty,15)) {
  188 #else
  189         if((nametty=ttyname(STDOUT_FILENO))) {
  190 #endif
  191 #endif
  192     if(MAXTITLE>nr) {
  193 #ifdef HAVE_MEMCCPY
  194             memccpy(titlebuf+nr,nametty+5,'\0',MAXTITLE-nr);
  195 #else
  196         strncpy(titlebuf+nr,nametty+5,MAXTITLE-nr);
  197 #endif
  198 
  199             nr+=strlen(titlebuf+nr);
  200             }
  201             }
  202         }
  203     if(nr<MAXTITLE)
  204         titlebuf[nr++]=' ';
  205 
  206 }
  207 #endif
  208     if(MAXTITLE>nr) {
  209 
  210 #ifdef HAVE_MEMCCPY
  211         memccpy(titlebuf+nr,getfilename(NULL),'\0',MAXTITLE-nr);
  212 #else
  213         strncpy(titlebuf+nr,getfilename(NULL),MAXTITLE-nr);
  214 #endif
  215         }
  216     titlebuf[MAXTITLE]='\0';
  217     settitle(titlebuf);
  218     return 0;
  219     };
  220 };
  221 
  222 
  223 
  224 
  225 call(titleset)