home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / os2util.c < prev    next >
C/C++ Source or Header  |  1992-10-04  |  8KB  |  404 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5.  
  6. #include <termios.h>
  7. #include <pwd.h>
  8. #include <os2elm.h>
  9.  
  10. #undef min
  11. #include "..\hdrs\defs.h"
  12.  
  13. #ifdef __EMX__
  14. #define CCHMAXPATH 256
  15. #else
  16. #undef SHORT
  17. #define INCL_NOPM
  18. #define INCL_SUB
  19. #define INCL_DOS
  20. #include <os2.h>
  21. #endif
  22.  
  23.  
  24. char default_editor[SLEN]   = "builtin";
  25. char default_pager[SLEN]    = "builtin";
  26.  
  27. char default_shell[SLEN]    = "cmd.exe";
  28. char default_printout[SLEN] = "print %s";
  29.  
  30. char uupchome[CCHMAXPATH]   = "c:/uupc";
  31. char elmhome[CCHMAXPATH]    = "c:/uupc/elm";
  32. char mailer[CCHMAXPATH]     = "rmail";
  33. char tempdir[CCHMAXPATH]    = "/";
  34.  
  35. char mailhome[CCHMAXPATH]   = "c:/uupc/mail/";
  36. char mailext[CCHMAXPATH]    = "";
  37. int  maildir                = 0;
  38.  
  39. char _logname[NLEN]         = "unknown";
  40. char _fullname[NLEN]        = "Unknown User";
  41. char _homedir[SLEN]         = "c:/uupc/user";
  42.  
  43. char _hostname[NLEN]        = "local";
  44. char _domainname[SLEN]      = "UUCP";
  45.  
  46.  
  47. #ifndef __EMX__
  48. void sleep(int seconds)
  49. {
  50.   DosSleep(1000L * seconds);
  51. }
  52. #endif
  53.  
  54. int setuid(int id)
  55. {
  56.   return 0;
  57. }
  58.  
  59. int setgid(int id)
  60. {
  61.   return 0;
  62. }
  63.  
  64. int getuid(void)
  65. {
  66.   return 0;
  67. }
  68.  
  69. int getgid(void)
  70. {
  71.   return 0;
  72. }
  73.  
  74. int chown(char *file, int uid, int gid)
  75. {
  76.   return 0;
  77. }
  78.  
  79. int link(char *old, char *new)
  80. {
  81.   errno = EXDEV;
  82.   return -1;
  83. }
  84.  
  85.  
  86. struct passwd *getpwnam(char *name)
  87. {
  88.   static struct passwd pw;
  89.   static char buffer[256];
  90.   char *ptr, *logname, *fullname, *homedir;
  91.   FILE *passwd;
  92.   int i, found = 0;
  93.  
  94.   strcpy(buffer, uupchome);
  95.   strcat(buffer, "/passwd");
  96.  
  97.   pw.pw_name  = _logname;
  98.   pw.pw_dir   = _homedir;
  99.   pw.pw_gecos = _fullname;
  100.  
  101.   if ( (passwd = fopen(buffer, "r")) == NULL )
  102.     if ( strcmp(name, _logname) == 0 )
  103.       return &pw;
  104.     else
  105.       return NULL;
  106.  
  107.   while ( fgets(buffer, sizeof(buffer), passwd) != NULL )
  108.   {
  109.     buffer[strlen(buffer) - 1] = 0;
  110.  
  111.     if ( buffer[0] == '#' )
  112.       continue;
  113.  
  114.     if ( (ptr = strchr(buffer, ':')) != NULL )
  115.       *ptr++ = 0;
  116.     else
  117.       continue;
  118.  
  119.     if ( strcmp(name, buffer) == 0 )
  120.     {
  121.       logname = buffer;
  122.  
  123.       for ( i = 0; i < 3; i++ )
  124.         if ( (ptr = strchr(ptr, ':')) != NULL )
  125.           *ptr++ = 0;
  126.         else
  127.           continue;
  128.  
  129.       fullname = ptr;
  130.  
  131.       if ( (ptr = strchr(ptr, ':')) != NULL )
  132.         *ptr++ = 0;
  133.       else
  134.         continue;
  135.  
  136.       homedir = ptr;
  137.  
  138.       if ( ptr[0] && ptr[1] && (ptr = strchr(ptr + 2, ':')) != NULL )
  139.         *ptr++ = 0;   /* skip drive: */
  140.  
  141.       pw.pw_name  = logname;
  142.       pw.pw_gecos = fullname;
  143.       pw.pw_dir   = homedir;
  144.       found = 1;
  145.  
  146.       break;
  147.     }
  148.   }
  149.  
  150.   fclose(passwd);
  151.  
  152.   if ( !found && strcmp(name, _logname) != 0 )
  153.     return NULL;
  154.  
  155.   return &pw;
  156. }
  157.  
  158. struct passwd *getpwuid(int uid)
  159. {
  160.   char *logname = getenv("LOGNAME");
  161.   return getpwnam(logname ? logname : _logname);
  162. }
  163.  
  164.  
  165. int gethostname(char *buf, int size)
  166. {
  167.   strcpy(buf, _hostname);
  168.   return 0;
  169. }
  170.  
  171. int gethostdomain(char *hostdom, int size)
  172. {
  173.   strcpy(hostdom, _domainname);
  174.   return 0;
  175. }
  176.  
  177.  
  178. int tcgetattr(int file, struct termios *buf)
  179. {
  180.   if ( buf )
  181.   {
  182.     buf -> c_cc[0] = 8;
  183.     buf -> c_cc[1] = 127;
  184.   }
  185.   return 0;
  186. }
  187.  
  188. int tcsetattr(int file, int id, struct termios *buf)
  189. {
  190.   return 0;
  191. }
  192.  
  193.  
  194. int readkey(void)
  195. {
  196.   int chr;
  197. #ifdef __EMX__
  198.   #define keycode    _read_kbd(0, 1, 0)
  199.   #define scancode   _read_kbd(0, 1, 0)
  200. #else
  201.   KBDKEYINFO ki;
  202.   #define keycode    (KbdCharIn(&ki, IO_WAIT, 0) ? -1 : ki.chChar)
  203.   #define scancode   ki.chScan
  204. #endif
  205.  
  206. again:
  207.   chr = keycode;
  208.  
  209.   if ( chr == -1 )
  210.   {
  211.     errno = EINTR;
  212.     return -1;
  213.   }
  214.  
  215.   if ( chr == 0 || chr == 0xE0 )
  216.     switch ( scancode )
  217.     {
  218.     case 0x48: /* up */
  219.       return 'K';
  220.     case 0x50: /* down */
  221.       return 'J';
  222.     case 0x49: /*page up */
  223.     case 0x4B: /* left */
  224.       return '-';
  225.     case 0x51: /* page down */
  226.     case 0x4D: /* right */
  227.       return '+';
  228.     case 0x47: /* home */
  229.       return '=';
  230.     case 0x4F: /* end */
  231.       return '*';
  232.     case 0x52: /* insert */
  233.       return 'u';
  234.     case 0x92: /* ctrl insert */
  235.       return 'U' - 64;
  236.     case 0x53: /* delete */
  237.       return 'd';
  238.     case 0x93: /* ctrl delete */
  239.       return 'D' - 64;
  240.     case 0x94: /* ctrl tab */
  241.       return 'T' - 64;
  242.     default:
  243.       goto again;
  244.     }
  245.  
  246.   switch ( chr )
  247.   {
  248.     case 0x09: /* tab */
  249.       return 't';
  250.     case 0x1B: /* escape */
  251.       return 'q';
  252.   }
  253.  
  254.   return chr;
  255. }
  256.  
  257.  
  258. void fixline(char *buffer)
  259. {
  260.   char *end;
  261.  
  262.   end = buffer + strlen(buffer);
  263.  
  264.   if ( end - buffer > 1 )
  265.     if ( end[-1] == '\n' && end[-2] == '\r' )
  266.     {
  267.       end[-1] = 0;
  268.       end[-2] = '\n';
  269.     }
  270. }
  271.  
  272.  
  273. void _ScreenSize(lines, columns)
  274. int *lines, *columns;
  275. {
  276. #ifdef __EMX__
  277.   int dst[2];
  278.   _scrsize(dst);
  279.   *lines = dst[1];
  280.   *columns = dst[0];
  281. #else
  282.   VIOMODEINFO vmi;
  283.   vmi.cb = sizeof(vmi);
  284.   VioGetMode(&vmi, 0);
  285.   *lines = vmi.row;
  286.   *columns = vmi.col;
  287. #endif
  288. }
  289.  
  290.  
  291. void unixpath(char *path)
  292. {
  293.   for ( ; *path; path++ )
  294.     if ( *path == '\\' )
  295.       *path = '/';
  296. }
  297.  
  298.  
  299. void os2path(char *path)
  300. {
  301.   for ( ; *path && *path != ' '; path++ )
  302.     if ( *path == '/' )
  303.       *path = '\\';
  304. }
  305.  
  306.  
  307. void parserc(char *rc)
  308. {
  309.   FILE *fp;
  310.   char buffer[SLEN];
  311.   char *ptr;
  312.  
  313.   if ( rc == NULL )
  314.   {
  315.     printf("Cannot find UUPC system!\n");
  316.     exit(1);
  317.   }
  318.  
  319.   if ( (fp = fopen(rc, "r")) == NULL )
  320.   {
  321.     printf("Cannot find UUPC configuration file '%s'!\n", rc);
  322.     exit(1);
  323.   }
  324.  
  325.   while ( fgets(buffer, sizeof(buffer) - 1, fp) )
  326.   {
  327.     buffer[strlen(buffer) - 1] = 0;
  328.  
  329.     if ( strnicmp(buffer, "NodeName=", 9) == 0 )
  330.       strcpy(_hostname, buffer + 9);
  331.     else if ( strnicmp(buffer, "Domain=", 7) == 0 )
  332.       strcpy(_domainname, buffer + 7);
  333.     else if ( strnicmp(buffer, "ConfDir=", 8) == 0 )
  334.       strcpy(uupchome, buffer + 8);
  335.     else if ( strnicmp(buffer, "TempDir=", 8) == 0 )
  336.       strcpy(tempdir, buffer + 8);
  337.     else if ( strnicmp(buffer, "MailDir=", 8) == 0 )
  338.       strcpy(mailhome, buffer + 8);
  339.     else if ( strnicmp(buffer, "MailExt=", 8) == 0 )
  340.     {
  341.       strcpy(mailext, ".");
  342.       strcat(mailext, buffer + 8);
  343.     }
  344.     else if ( strnicmp(buffer, "Rmail=", 6) == 0 )
  345.       strcpy(mailer, buffer + 6);
  346.     else if ( strnicmp(buffer, "Mailbox=", 8) == 0 )
  347.       strcpy(_logname, buffer + 8);
  348.     else if ( strnicmp(buffer, "Name=", 5) == 0 )
  349.       strcpy(_fullname, buffer + 5);
  350.     else if ( strnicmp(buffer, "Home=", 5) == 0 )
  351.       strcpy(_homedir, buffer + 5);
  352.     else if ( strnicmp(buffer, "Editor=", 7) == 0 )
  353.     {
  354.       strcpy(default_editor, buffer + 7);
  355.       if ( (ptr = strchr(default_editor, ' ')) != NULL )
  356.         *ptr = 0;  /* first word only */
  357.     }
  358.     else if ( strnicmp(buffer, "Pager=", 6) == 0 )
  359.     {
  360.       strcpy(default_pager, buffer + 6);
  361.       if ( (ptr = strchr(default_pager, ' ')) != NULL )
  362.         *ptr = 0;
  363.     }
  364.     else if ( strnicmp(buffer, "Options=", 8) == 0 )
  365.     {
  366.       for ( ptr = strtok(buffer + 8, " \t"); ptr; ptr = strtok(NULL, " \t") )
  367.         if ( stricmp(ptr, "directory") == 0 )
  368.           maildir = 1;
  369.     }
  370.   }
  371.  
  372.   fclose(fp);
  373. }
  374.  
  375.  
  376. void initpaths(void)
  377. {
  378.   char *sysrc = getenv("UUPCSYSRC");
  379.   char *usrrc = getenv("UUPCUSRRC");
  380.  
  381.   parserc(sysrc);
  382.   parserc(usrrc);
  383.  
  384.   strcpy(elmhome, uupchome);
  385.   strcat(elmhome, "/");
  386.   strcat(elmhome, dotelm);
  387.   
  388.   unixpath(elmhome);
  389.   unixpath(tempdir);
  390.   unixpath(mailhome);
  391.  
  392.   os2path(mailer);
  393.   os2path(default_editor);
  394.   os2path(default_pager);
  395.  
  396.   if ( tempdir[strlen(tempdir) - 1] != '/' )
  397.     strcat(tempdir, "/");
  398.   if ( mailhome[strlen(mailhome) - 1] != '/' )
  399.     strcat(mailhome, "/");
  400.  
  401.   if ( strnicmp(_hostname, _domainname, strlen(_hostname)) == 0 )
  402.     strcpy(_domainname, _domainname + strlen(_hostname));
  403. }
  404.