home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / MMSRC029.ZIP / mmail-0.29 / interfac / mysystem.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-27  |  3.9 KB  |  212 lines

  1. /*
  2.  * MultiMail offline mail reader
  3.  * some low-level routines common to both sides
  4.  
  5.  Copyright (c) 1999 William McBrine <wmcbrine@clark.net>
  6.  
  7.  Distributed under the GNU General Public License.
  8.  For details, see the file COPYING in the parent directory. */
  9.  
  10. /* Most non-ANSI, non-curses stuff is here. */
  11.  
  12. #include "interfac.h"
  13. #include "error.h"
  14.  
  15. extern "C" {
  16. #include <dirent.h>
  17. #include <unistd.h>
  18. #include <sys/stat.h>
  19. #include <sys/utsname.h>
  20.  
  21. #ifdef __EMX__
  22. int _chdir2(const char *);
  23. char *_getcwd2(char *, int);
  24. #endif
  25. }
  26.  
  27. static DIR *Dir;
  28. static struct dirent *entry;
  29.  
  30. void fatalError(const char *description);
  31.  
  32. int mysystem(const char *cmd)
  33. {
  34.     if (!isendwin())
  35.         endwin();
  36. #if defined (__PDCURSES__) && !defined (XCURSES)
  37.     // Restore original cursor
  38.     PDC_set_cursor_mode(curs_start, curs_end);
  39. #endif
  40.     int result = system(cmd);
  41.  
  42.     // Non-zero result = error; pause so it can (maybe) be read
  43.     if (result)
  44.         sleep(2);
  45.  
  46.     keypad(stdscr, TRUE);
  47. #if defined (__PDCURSES__) && defined (__RSXNT__)
  48.     typeahead(-1);
  49. #endif
  50.     return result;
  51. }
  52.  
  53. void mytmpnam(char *name)
  54. {
  55. /* EMX doesn't return an absolute pathname from tmpnam(), so we create one
  56.    ourselves. Otherwise, use the system's version, and make sure it hasn't
  57.    run out of names.
  58. */
  59. #ifdef __EMX__
  60.     const char *tmp = getenv("TEMP");
  61.  
  62.     if (!tmp) {
  63.         tmp = getenv("TMP");
  64.         if (!tmp)
  65.             tmp = mm.resourceObject->get(mmHomeDir);
  66.     }
  67.     sprintf(name, "%s/%s", tmp, tmpnam(0));
  68. #else
  69.     if (!tmpnam(name))
  70.         fatalError("Out of temporary filenames");
  71. #endif
  72. }
  73.  
  74. int mychdir(const char *pathname)
  75. {
  76.     return
  77. #ifdef __EMX__
  78.         _chdir2(pathname);
  79. #else
  80.         chdir(pathname);
  81. #endif
  82. }
  83.  
  84. int mymkdir(const char *pathname)
  85. {
  86.     return mkdir(pathname, S_IRWXU);
  87. }
  88.  
  89. void myrmdir(const char *pathname)
  90. {
  91.     rmdir(pathname);
  92. }
  93.  
  94. void mygetcwd(char *pathname)
  95. {
  96. #ifdef __EMX__
  97.     _getcwd2(pathname, 255);
  98. #else
  99.     getcwd(pathname, 255);
  100. #endif
  101. }
  102.  
  103. bool readable(const char *filename)
  104. {
  105.     return !access(filename, R_OK);
  106. }
  107.  
  108. bool writeable(const char *filename)
  109. {
  110.     return !access(filename, R_OK | W_OK);
  111. }
  112.  
  113. // system name -- results of uname()
  114. const char *sysname()
  115. {
  116.     static struct utsname buf;
  117.  
  118.     if (!buf.sysname[0])
  119. #if defined(__WIN32__) && defined(__RSXNT__)
  120.         // uname() returns "MS-DOS" in RSXNT, so hard-wire it here
  121.         strcpy(buf.sysname, "Win32");
  122. #else
  123.         uname(&buf);
  124. #endif
  125.     return buf.sysname;
  126. }
  127.  
  128. bool myopendir(const char *dirname)
  129. {
  130.     return (Dir = opendir(dirname)) ? !mychdir(dirname) : false;
  131. }
  132.  
  133. const char *myreaddir()
  134. {
  135.     entry = readdir(Dir);
  136.     if (!entry) {
  137.         closedir(Dir);
  138.         return 0;
  139.     } else
  140.         return entry->d_name;
  141. }
  142.  
  143. void clearDirectory(const char *DirName)
  144. {
  145.     const char *fname;
  146.  
  147.     if (myopendir(DirName))
  148.         while ((fname = myreaddir()))
  149.             if (fname[0] != '.')
  150.                 remove(fname);
  151. }
  152.  
  153. #if defined (__MSDOS__) || defined (__EMX__)
  154.  
  155. /* Convert pathnames to "canonical" form (change slashes to backslashes).
  156.    The "nospace" stuff leaves any parameters unconverted.
  157.    Don't call this twice in a row without first copying the result! D'oh!
  158. */
  159.  
  160. const char *canonize(const char *sinner)
  161. {
  162.     static char saint[256];
  163.     int i;
  164.     bool nospace = true;
  165.  
  166.     for (i = 0; sinner[i]; i++) {
  167.         saint[i] = (nospace && (sinner[i] == '/')) ?
  168.             '\\' : sinner[i];
  169.         if (nospace && (saint[i] == ' '))
  170.             nospace = false;
  171.     }
  172.     saint[i] = '\0';
  173.     return saint;
  174. }
  175.  
  176. /* Command shell routine -- currently only used in the DOSish ports */
  177.  
  178. void shellout()
  179. {
  180.     static const char *oldprompt = getenv("PROMPT");
  181.  
  182.     if (!oldprompt)
  183.         oldprompt = "$p$g";
  184.  
  185.     int len = strlen(oldprompt) + 13;
  186.     char *prompt = new char[len];
  187.  
  188.     sprintf(prompt, "PROMPT=%s[MM] ", oldprompt);
  189.     putenv(prompt);
  190.  
  191.     mychdir(error.getOrigDir());
  192.     touchwin(stdscr);
  193.     refresh();
  194.     mysystem(getenv("COMSPEC"));
  195.  
  196.     delete[] prompt;
  197.  
  198.     interface->redraw();
  199. }
  200.  
  201. #endif
  202.  
  203. bool mystat::init(const char *fname)
  204. {
  205.     struct stat fileStat;
  206.     bool retval = !stat(fname, &fileStat);
  207.     size = retval ? fileStat.st_size : -1;
  208.     date = retval ? fileStat.st_mtime : -1;
  209.     isdir = !(!S_ISDIR(fileStat.st_mode));
  210.     return retval;
  211. }
  212.