home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rzsz_3_24_3_src.lzh / os9.c < prev    next >
C/C++ Source or Header  |  1993-11-04  |  5KB  |  233 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <time.h>
  5. #ifdef m6809
  6. #include <sgstat.h>
  7. #include <utime.h>
  8. typedef long time_t;
  9. #endif
  10. #include <direct.h>
  11.  
  12. #define min(x,y) (x < y ? x : y)
  13.  
  14.  
  15. int alarmread(fd,buffer,count,function,timeout)
  16.         register int fd;
  17.         register char *buffer;
  18.         int count;
  19.         int (*function)();
  20.         register int timeout;
  21. {
  22.         register int i;
  23.         int n;
  24.         int noinput, c;
  25.         if((n=_gs_rdy(fd)) > 0)
  26.                 return( read(fd,buffer,min(count,n)));
  27.     errno = 0;
  28.         c=timeout << 2;
  29.         for( i=0, noinput=1; i < c && noinput; i++)
  30.         {
  31.                 if((n=_gs_rdy(fd)) > 0)
  32.                         noinput = 0;
  33.                 else
  34.                 {
  35.             errno = 0;
  36. #ifdef m6809
  37.                         _ss_ssig(fd,SIGWAKE);
  38.                         tsleep(15); /* 60 ticks / second */
  39. #else
  40.             sigmask(1);
  41.                         _ss_ssig(fd,SIGWAKE);
  42.                         tsleep(CLK_TCK / 4);
  43. #endif
  44.                         _ss_rel(fd);
  45.                 }
  46.         }
  47.         if( noinput==0 )
  48.                 return( read(fd,buffer,min(count,n)));
  49.         else
  50.                 return((*function)());
  51. }
  52.  
  53.  
  54. utime (file, timep)
  55. char        *file;
  56. long        timep[2];
  57. {
  58.         struct tm        *localtime ();
  59.         struct tm        *tbuf;
  60.         int        fd;
  61.         struct fildes        buf;
  62.         
  63.         if ((fd = open (file, 3)) < 0)
  64.                 return -1;
  65.                 
  66.         if (_gs_gfd (fd, &buf, sizeof (buf)) < 0)
  67.         {
  68.                 close (fd);
  69.                 return -1;
  70.         }
  71.         
  72.         u2otime(buf.fd_dcr,localtime(&(timep[0])));
  73.         u2otime(buf.fd_date,localtime(&(timep[1])));
  74.         
  75.         if (_ss_pfd (fd, &buf) < 0)
  76.         {
  77.                 close (fd);
  78.                 return -1;
  79.         }
  80.         
  81.         close (fd);
  82.         return 0;
  83. }
  84.  
  85.  
  86. long outime( date)
  87. char        *date;
  88. {
  89.     long o2utime();
  90.         char newdate[6];
  91.         strncpy(newdate,date,5);
  92.         newdate[5]=0;
  93.         
  94.         return(o2utime(newdate));
  95. }
  96.  
  97.  
  98. /*
  99.  *  The two following subroutines convert between UNIX-style and OS-9-style
  100.  *  file attributes.  The UNIX-Style attribute is stored in decimal format,
  101.  *  NOT in octal format, so if it is printed out in %o format, it will have
  102.  *  The correct value.
  103.  */
  104. int u2oattr(attr)
  105.    int attr;
  106. {
  107.    int o_attr=0;
  108.    if (attr & 0x001)  o_attr |= 0x20;  /* Public Execute */
  109.    if (attr & 0x002)  o_attr |= 0x10;  /* Public Write   */
  110.    if (attr & 0x004)  o_attr |= 0x08;  /* Public Read    */
  111.    if (attr & 0x040)  o_attr |= 0x04;  /* Owner  Execute */
  112.    if (attr & 0x080)  o_attr |= 0x02;  /* Owner  Write   */
  113.    if (attr & 0x100)  o_attr |= 0x01;  /* Owner  Read    */
  114.    return(o_attr);
  115. }
  116.  
  117.  
  118. int o2uattr(attr)
  119.    int attr;
  120. {
  121.    int u_attr=0;
  122.    if (attr & 0x20)  u_attr |= (0x001 | 0x008);  /* Public+Group Execute */
  123.    if (attr & 0x10)  u_attr |= (0x002 | 0x010);  /* Public+Group Write   */
  124.    if (attr & 0x08)  u_attr |= (0x004 | 0x020);  /* Public+Group Read    */
  125.    if (attr & 0x04)  u_attr |= (0x040);          /* Owner        Execute */
  126.    if (attr & 0x02)  u_attr |= (0x080);          /* Owner        Write   */
  127.    if (attr & 0x01)  u_attr |= (0x100);          /* Owner        Read    */
  128.    return(u_attr);
  129. }
  130.  
  131.  
  132.  
  133. #ifdef m6809
  134. _ss_sbreak(pn)
  135. int pn;
  136. {
  137.         struct sgbuf old,new;
  138.         short tmp=0;
  139.  
  140.         _gs_opt(pn, &old);
  141.         _strass(&new,&old,sizeof(old));
  142.         new.sg_baud = 0;
  143.         _ss_opt(pn, &new);
  144.         write(pn, &tmp, 1);
  145.         _ss_opt(pn, &old);
  146.  
  147. }
  148. #else
  149.  
  150. /*
  151.  * Convert UNIX time to 5-byte OS-9 time
  152.  */
  153. u2otime(otime, tp)
  154.    char *otime;
  155.    struct tm *tp;
  156. {
  157.    otime[5] = tp->tm_sec;
  158.    otime[4] = tp->tm_min;
  159.    otime[3] = tp->tm_hour;
  160.    otime[2] = tp->tm_mday;
  161.    otime[1] = tp->tm_mon + 1;
  162.    otime[0] = tp->tm_year;
  163. }
  164.  
  165.  
  166. /*
  167.  * Convert 5-byte OS-9 time to UNIX time
  168.  */
  169. long o2utime(otime)
  170.    char *otime;
  171. {
  172.    struct tm t;
  173.    time_t tt;
  174.  
  175.    t.tm_sec  = otime[5];
  176.    t.tm_min  = otime[4];
  177.    t.tm_hour = otime[3];
  178.    t.tm_mday = otime[2];
  179.    t.tm_mon  = otime[1] - 1;
  180.    t.tm_year = otime[0];
  181.  
  182.    tt = mktime(&t);
  183.  
  184.    return(mktime(gmtime(&tt)));
  185. }
  186.  
  187.  
  188. long c4tol(cp)
  189. char *cp;
  190. {
  191.     long n;
  192.  
  193.     n = (cp[0] & 0x7f);
  194.     n = (n << 8) + (cp[1] & 0xff);
  195.     n = (n << 8) + (cp[2] & 0xff);
  196.     n = (n << 8) + (cp[3] & 0xff);
  197.  
  198.     return(n);
  199. }
  200.  
  201.  
  202. char *ltoc4(n)
  203. long n;
  204. {
  205.     char cp[4];
  206.  
  207.     cp[3] = (char) (n % (256));
  208.     n = n >> 8;
  209.     cp[2] = (char) (n % (256));
  210.     n = n >> 8;
  211.     cp[1] = (char) (n % (256));
  212.     n = n >> 8;
  213.     cp[0] = (char) (n % (256));
  214.  
  215.     return(cp);
  216. }
  217.  
  218.  
  219. #asm
  220. I$SetStt        equ             $008E                   ; 8f is get
  221.  
  222. _ss_sbreak:
  223.                 link    a5,#0
  224.                 movem.l D1/D2/A0,-(A7)
  225.                 move.l  D1,D2
  226.                 move.w  #SS_Break,D1
  227.                 os9     I$SetStt
  228.                 bra     _sysret
  229.  
  230. #endasm
  231. #endif /* m6809 */
  232.  
  233.