home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / os2 / os2util.c < prev    next >
C/C++ Source or Header  |  1996-09-05  |  9KB  |  458 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 tempdir[CCHMAXPATH]    = "/";
  33. char logdir[CCHMAXPATH]     = "c:/uupc";
  34.  
  35. char mailer[CCHMAXPATH]     = "rmail";
  36. int background              = 0;
  37.  
  38. char mailhome[CCHMAXPATH]   = "c:/uupc/mail/";
  39. char mailext[CCHMAXPATH]    = "";
  40. int  maildir                = 0;
  41. char postmaster[SLEN]       = "postmaster";
  42.  
  43. char _logname[NLEN]         = "unknown";
  44. char _fullname[NLEN]        = "Unknown User";
  45. char _reply_to[SLEN]         = "";
  46. char _homedir[SLEN]         = "c:/uupc/user";
  47.  
  48. char _hostname[NLEN]        = "local";
  49. char _domainname[SLEN]      = "local.UUCP";
  50. char _fromdomainname[SLEN]  = "";
  51.  
  52.  
  53. #ifndef __EMX__
  54. void sleep(int seconds)
  55. {
  56.   DosSleep(1000L * seconds);
  57. }
  58. #endif
  59.  
  60. int setuid(int id)
  61. {
  62.   return 0;
  63. }
  64.  
  65. int setgid(int id)
  66. {
  67.   return 0;
  68. }
  69.  
  70. int getuid(void)
  71. {
  72.   return 0;
  73. }
  74.  
  75. int getgid(void)
  76. {
  77.   return 0;
  78. }
  79.  
  80. int chown(char *file, int uid, int gid)
  81. {
  82.   return 0;
  83. }
  84.  
  85. int link(char *old, char *new)
  86. {
  87.   errno = EXDEV;
  88.   return -1;
  89. }
  90.  
  91.  
  92. struct passwd *getpwnam(char *name)
  93. {
  94.   static struct passwd pw;
  95.   static char buffer[256];
  96.   char *ptr, *logname, *fullname, *homedir;
  97.   FILE *passwd;
  98.   int i, found = 0;
  99.  
  100.   strcpy(buffer, uupchome);
  101.   strcat(buffer, "/passwd");
  102.  
  103.   pw.pw_name  = _logname;
  104.   pw.pw_dir   = _homedir;
  105.   pw.pw_gecos = _fullname;
  106.  
  107.   if ( (passwd = fopen(buffer, "r")) == NULL )
  108.     if ( strcmp(name, _logname) == 0 )
  109.       return &pw;
  110.     else
  111.       return NULL;
  112.  
  113.   while ( fgets(buffer, sizeof(buffer), passwd) != NULL )
  114.   {
  115.     buffer[strlen(buffer) - 1] = 0;
  116.  
  117.     if ( buffer[0] == '#' )
  118.       continue;
  119.  
  120.     if ( (ptr = strchr(buffer, ':')) != NULL )
  121.       *ptr++ = 0;
  122.     else
  123.       continue;
  124.  
  125.     if ( stricmp(name, buffer) == 0 )
  126.     {
  127.       logname = buffer;
  128.  
  129.       for ( i = 0; i < 3; i++ )
  130.         if ( (ptr = strchr(ptr, ':')) != NULL )
  131.           *ptr++ = 0;
  132.         else
  133.           continue;
  134.  
  135.       fullname = ptr;
  136.  
  137.       if ( (ptr = strchr(ptr, ':')) != NULL )
  138.         *ptr++ = 0;
  139.       else
  140.         continue;
  141.  
  142.       homedir = ptr;
  143.  
  144.       if (ptr[1] == ';')
  145.     ptr[1] = ':';
  146.  
  147.       if ( ptr[0] && ptr[1] && (ptr = strchr(ptr + 2, ':')) != NULL )
  148.         *ptr++ = 0;   /* skip drive: */
  149.       
  150.       pw.pw_name  = logname;
  151.       pw.pw_gecos = fullname;
  152.       pw.pw_dir   = homedir;
  153.       found = 1;
  154.  
  155.       break;
  156.     }
  157.   }
  158.  
  159.   fclose(passwd);
  160.  
  161.   if ( !found && strcmp(name, _logname) != 0 )
  162.     return NULL;
  163.  
  164.   return &pw;
  165. }
  166.  
  167. struct passwd *getpwuid(int uid)
  168. {
  169.   char *logname = getenv("LOGNAME");
  170.   return getpwnam(logname ? logname : _logname);
  171. }
  172.  
  173.  
  174. int gethostname(char *buf, int size)
  175. {
  176.   strcpy(buf, _domainname /* was: _hostname */ );
  177.   return 0;
  178. }
  179.  
  180. int gethostdomain(char *hostdom, int size)
  181. {
  182.   strcpy(hostdom, _domainname);
  183.   return 0;
  184. }
  185.  
  186. int getfromdomain(char *hostdom, int size)
  187. {
  188.   strcpy(hostdom, _fromdomainname[0] ? _fromdomainname : _domainname);
  189.   return 0;
  190. }
  191.  
  192.  
  193. int tcgetattr(int file, struct termios *buf)
  194. {
  195.   if ( buf )
  196.   {
  197.     buf -> c_cc[0] = 8;
  198.     buf -> c_cc[1] = 127;
  199.   }
  200.   return 0;
  201. }
  202.  
  203. int tcsetattr(int file, int id, struct termios *buf)
  204. {
  205.   return 0;
  206. }
  207.  
  208.  
  209. int readkey(void)
  210. {
  211.   int chr;
  212. #ifdef __EMX__
  213.   #define keycode()    _read_kbd(0, 1, 0)
  214.   #define scancode()   _read_kbd(0, 1, 0)
  215. #else
  216.   KBDKEYINFO ki;
  217.   #define keycode()    (KbdCharIn(&ki, IO_WAIT, 0) ? -1 : ki.chChar)
  218.   #define scancode()   ki.chScan
  219. #endif
  220.  
  221. again:
  222.   chr = keycode();
  223.  
  224.   if ( chr == -1 )
  225.   {
  226.     errno = EINTR;
  227.     return -1;
  228.   }
  229.  
  230.   if ( chr == 0 || chr == 0xE0 )
  231.     switch ( scancode() )
  232.     {
  233.     case 0x48: /* up */
  234.       return 'K';
  235.     case 0x50: /* down */
  236.       return 'J';
  237.     case 0x49: /*page up */
  238.     case 0x4B: /* left */
  239.       return '-';
  240.     case 0x51: /* page down */
  241.     case 0x4D: /* right */
  242.       return '+';
  243.     case 0x47: /* home */
  244.       return '=';
  245.     case 0x4F: /* end */
  246.       return '*';
  247.     case 0x52: /* insert */
  248.       return 'u';
  249.     case 0x92: /* ctrl insert */
  250.       return 'U' - 64;
  251.     case 0x53: /* delete */
  252.       return 'd';
  253.     case 0x93: /* ctrl delete */
  254.       return 'D' - 64;
  255.     case 0x94: /* ctrl tab */
  256.       return 'T' - 64;
  257.     default:
  258.       goto again;
  259.     }
  260.  
  261.   switch ( chr )
  262.   {
  263.     case 0x09: /* tab */
  264.       return 't';
  265.     case 0x1B: /* escape */
  266.       return 'q';
  267.   }
  268.  
  269.   return chr;
  270. }
  271.  
  272.  
  273. void fixline(char *buffer)
  274. {
  275.   char *end;
  276.  
  277.   end = buffer + strlen(buffer);
  278.  
  279.   if ( end - buffer > 1 )
  280.     if ( end[-1] == '\n' && end[-2] == '\r' )
  281.     {
  282.       end[-1] = 0;
  283.       end[-2] = '\n';
  284.     }
  285. }
  286.  
  287.  
  288. void _ScreenSize(lines, columns)
  289. int *lines, *columns;
  290. {
  291. #ifdef __EMX__
  292.   int dst[2];
  293.   _scrsize(dst);
  294.   *lines = dst[1];
  295.   *columns = dst[0];
  296. #else
  297.   VIOMODEINFO vmi;
  298.   vmi.cb = sizeof(vmi);
  299.   VioGetMode(&vmi, 0);
  300.   *lines = vmi.row;
  301.   *columns = vmi.col;
  302. #endif
  303. }
  304.  
  305.  
  306. void unixpath(char *path)
  307. {
  308.   for ( ; *path; path++ )
  309.     if ( *path == '\\' )
  310.       *path = '/';
  311. }
  312.  
  313.  
  314. void os2path(char *path)
  315. {
  316.   for ( ; *path; path++ )
  317.     if ( *path == '/' )
  318.       *path = '\\';
  319. }
  320.  
  321.  
  322. #if 0
  323. /* no longer necessary */
  324. void move_incoming_mail(void)
  325. {
  326.   char inbox[CCHMAXPATH], mailbox[CCHMAXPATH];
  327.  
  328.   if ( stricmp(mailer, "sendmail") == 0 )
  329.   {
  330.     strcpy(inbox, uupchome);
  331.     strcat(inbox, "/mail/inbox.ndx");
  332.     strcpy(mailbox, mailhome);
  333.     strcat(mailbox, _logname);
  334.     movemail(inbox, mailbox);
  335.   }
  336. }
  337. #endif
  338.  
  339.  
  340. void parserc(char *rc)
  341. {
  342.   FILE *fp;
  343.   char buffer[SLEN];
  344.   char *ptr;
  345.  
  346.   if ( rc == NULL )
  347.   {
  348.     printf("Cannot find UUPC system!\n");
  349.     exit(1);
  350.   }
  351.  
  352.   if ( (fp = fopen(rc, "r")) == NULL )
  353.   {
  354.     printf("Cannot find UUPC configuration file '%s'!\n", rc);
  355.     exit(1);
  356.   }
  357.  
  358.   while ( fgets(buffer, sizeof(buffer) - 1, fp) )
  359.   {
  360.     buffer[strlen(buffer) - 1] = 0;
  361.  
  362.     if ( strnicmp(buffer, "NodeName=", 9) == 0 )
  363.       strcpy(_hostname, buffer + 9);
  364.     else if ( strnicmp(buffer, "Domain=", 7) == 0 )
  365.       strcpy(_domainname, buffer + 7);
  366.     else if ( strnicmp(buffer, "FromDomain=", 11) == 0 )
  367.       strcpy(_fromdomainname, buffer + 11);
  368.     else if ( strnicmp(buffer, "Postmaster=", 11) == 0 )
  369.       strcpy(postmaster, buffer + 11);
  370.     else if ( strnicmp(buffer, "ConfDir=", 8) == 0 )
  371.       strcpy(uupchome, buffer + 8);
  372.     else if ( strnicmp(buffer, "SpoolDir=", 9) == 0 )
  373.       strcpy(logdir, buffer + 9);
  374.     else if ( strnicmp(buffer, "TempDir=", 8) == 0 )
  375.       strcpy(tempdir, buffer + 8);
  376.     else if ( strnicmp(buffer, "MailDir=", 8) == 0 )
  377.       strcpy(mailhome, buffer + 8);
  378.     else if ( strnicmp(buffer, "MailExt=", 8) == 0 )
  379.     {
  380.       strcpy(mailext, ".");
  381.       strcat(mailext, buffer + 8);
  382.     }
  383.     else if ( strnicmp(buffer, "Rmail=", 6) == 0 )
  384.       strcpy(mailer, buffer + 6);
  385.     else if ( strnicmp(buffer, "Mailbox=", 8) == 0 )
  386.       strcpy(_logname, buffer + 8);
  387.     else if ( strnicmp(buffer, "Name=", 5) == 0 )
  388.       strcpy(_fullname, buffer + 5);
  389.     else if ( strnicmp(buffer, "ReplyTo=", 8) == 0 )
  390.       strcpy(_reply_to, buffer + 8);
  391.     else if ( strnicmp(buffer, "Home=", 5) == 0 )
  392.       strcpy(_homedir, buffer + 5);
  393.     else if ( strnicmp(buffer, "Editor=", 7) == 0 )
  394.     {
  395.       strcpy(default_editor, buffer + 7);
  396.       if ( (ptr = strchr(default_editor, ' ')) != NULL )
  397.         *ptr = 0;  /* first word only */
  398.     }
  399.     else if ( strnicmp(buffer, "Pager=", 6) == 0 )
  400.     {
  401.       strcpy(default_pager, buffer + 6);
  402.       if ( (ptr = strchr(default_pager, ' ')) != NULL )
  403.         *ptr = 0;
  404.     }
  405.     else if ( strnicmp(buffer, "Options=", 8) == 0 )
  406.     {
  407.       for ( ptr = strtok(buffer + 8, " \t"); ptr; ptr = strtok(NULL, " \t") )
  408.         if ( stricmp(ptr, "directory") == 0 )
  409.           maildir = 1;
  410.         else if ( stricmp(ptr, "background") == 0 )
  411.       background = 1;
  412.     }
  413.   }
  414.  
  415.   fclose(fp);
  416. }
  417.  
  418.  
  419. void initpaths(void)
  420. {
  421.   char *sysrc = getenv("UUPCSYSRC");
  422.   char *usrrc = getenv("UUPCUSRRC");
  423.  
  424.   // putenv("TMP=");
  425.   tzset();
  426.  
  427.   parserc(sysrc);
  428.   if (stricmp(sysrc, usrrc) != 0)
  429.     parserc(usrrc);
  430.  
  431.   strcpy(elmhome, uupchome);
  432.   strcat(elmhome, "/");
  433.   strcat(elmhome, dotelm);
  434.   
  435.   unixpath(elmhome);
  436.   unixpath(tempdir);
  437.   unixpath(mailhome);
  438.  
  439.   os2path(default_editor);
  440.   os2path(default_pager);
  441.  
  442.   if ( tempdir[strlen(tempdir) - 1] != '/' )
  443.     strcat(tempdir, "/");
  444.   if ( mailhome[strlen(mailhome) - 1] != '/' )
  445.     strcat(mailhome, "/");
  446.  
  447. #if 0
  448.   /* no longer necessary, since gethostname returns the FQDN */
  449.   if ( strnicmp(_hostname, _domainname, strlen(_hostname)) == 0 )
  450.     strcpy(_domainname, _domainname + strlen(_hostname));
  451. #endif
  452.  
  453. #if 0
  454.   /* with the addition of rcvmail.exe, this is no longer necessary */
  455.   move_incoming_mail();
  456. #endif
  457. }
  458.