home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rzsz_3_36_3_src.lzh / os9.c < prev    next >
C/C++ Source or Header  |  1996-01-08  |  6KB  |  299 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <time.h>
  5. #ifdef m6809
  6. #define localtime localtim
  7. #include <sgstat.h>
  8. #include <utime.h>
  9. typedef long time_t;
  10. #endif
  11. #include <direct.h>
  12.  
  13. #define min(x,y) (x < y ? x : y)
  14.  
  15. int alarmread(fd,buffer,count,function,timeout)
  16. int fd;
  17. char *buffer;
  18. int count;
  19. int (*function)();
  20. int timeout;
  21. {
  22.     register int i = 0;
  23.     register int noinput = 1;
  24.     register int n, c;
  25.     int value;
  26.     
  27.     errno = 0;
  28.         if((n=_gs_rdy(fd)) > 0)
  29.         return(read(fd,buffer,min(count,n)));
  30.         c = timeout << 2;
  31.     do
  32.         {
  33.                 if((n=_gs_rdy(fd)) > 0)
  34.                         noinput = 0;
  35.                 else
  36.                 {
  37.             errno = 0;
  38. #ifdef m6809
  39.                         _ss_ssig(fd,SIGWAKE);
  40.                         tsleep(15); /* 60 ticks / second */
  41. #else
  42.             sigmask(1);
  43.                         _ss_ssig(fd,SIGWAKE);
  44.                         tsleep(CLK_TCK / 4);
  45. #endif
  46.                         _ss_rel(fd);
  47.                 }
  48.         }
  49.     while (noinput && (i++ < c));
  50.  
  51.         if( !noinput )
  52.         return(read(fd,buffer,min(count,n)));
  53.     else
  54.             return((*function)());
  55. }
  56.  
  57.  
  58.  
  59. utime (file, timep)
  60. char        *file;
  61. long        timep[2];
  62. {
  63.         struct tm *localtime();
  64.         struct tm *tbuf;
  65.         int fd;
  66.         struct fildes buf;
  67.         
  68.         if ((fd = open (file, 3)) < 0)
  69.                 return -1;
  70.                 
  71.         if (_gs_gfd (fd, &buf, sizeof (buf)) < 0)
  72.         {
  73.                 close (fd);
  74.                 return -1;
  75.         }
  76.         
  77.         u2otime(buf.fd_dcr,localtime(&(timep[0])));
  78.         u2otime(buf.fd_date,localtime(&(timep[1])));
  79.         
  80.         if (_ss_pfd (fd, &buf) < 0)
  81.         {
  82.                 close (fd);
  83.                 return -1;
  84.         }
  85.         
  86.         close (fd);
  87.         return 0;
  88. }
  89.  
  90.  
  91. long outime( date)
  92. char        *date;
  93. {
  94.     long o2utime();
  95.         char newdate[6];
  96.         strncpy(newdate,date,5);
  97.         newdate[5]=0;
  98.         
  99.         return(o2utime(newdate));
  100. }
  101.  
  102.  
  103. /*
  104.  *  The two following subroutines convert between UNIX-style and OS-9-style
  105.  *  file attributes.  The UNIX-Style attribute is stored in decimal format,
  106.  *  NOT in octal format, so if it is printed out in %o format, it will have
  107.  *  The correct value.
  108.  */
  109. int u2oattr(attr)
  110. int attr;
  111. {
  112.    int o_attr=0;
  113.    if (attr & 0x001)  o_attr |= 0x20;  /* Public Execute */
  114.    if (attr & 0x002)  o_attr |= 0x10;  /* Public Write   */
  115.    if (attr & 0x004)  o_attr |= 0x08;  /* Public Read    */
  116.    if (attr & 0x040)  o_attr |= 0x04;  /* Owner  Execute */
  117.    if (attr & 0x080)  o_attr |= 0x02;  /* Owner  Write   */
  118.    if (attr & 0x100)  o_attr |= 0x01;  /* Owner  Read    */
  119.    return(o_attr);
  120. }
  121.  
  122.  
  123. int o2uattr(attr)
  124. int attr;
  125. {
  126.    int u_attr=0;
  127.    if (attr & 0x20)  u_attr |= (0x001 | 0x008);  /* Public+Group Execute */
  128.    if (attr & 0x10)  u_attr |= (0x002 | 0x010);  /* Public+Group Write   */
  129.    if (attr & 0x08)  u_attr |= (0x004 | 0x020);  /* Public+Group Read    */
  130.    if (attr & 0x04)  u_attr |= (0x040);          /* Owner        Execute */
  131.    if (attr & 0x02)  u_attr |= (0x080);          /* Owner        Write   */
  132.    if (attr & 0x01)  u_attr |= (0x100);          /* Owner        Read    */
  133.    return(u_attr);
  134. }
  135.  
  136.  
  137.  
  138. #ifdef m6809
  139. _ss_sbreak(pn)
  140. int pn;
  141. {
  142.         struct sgbuf old,new;
  143.         short tmp=0;
  144.  
  145.         _gs_opt(pn, &old);
  146.         _strass(&new,&old,sizeof(old));
  147.         new.sg_baud = 0;
  148.         _ss_opt(pn, &new);
  149.         write(pn, &tmp, 1);
  150.         _ss_opt(pn, &old);
  151.  
  152. }
  153. #else /* !m6809 */
  154.  
  155. /*
  156.  * Convert UNIX time to 5-byte OS-9 time
  157.  */
  158. u2otime(otime, tp)
  159. char *otime;
  160. struct tm *tp;
  161. {
  162.    otime[5] = tp->tm_sec;
  163.    otime[4] = tp->tm_min;
  164.    otime[3] = tp->tm_hour;
  165.    otime[2] = tp->tm_mday;
  166.    otime[1] = tp->tm_mon + 1;
  167.    otime[0] = tp->tm_year;
  168. }
  169.  
  170.  
  171. /*
  172.  * Convert 5-byte OS-9 time to UNIX time
  173.  */
  174. long o2utime(otime)
  175. char *otime;
  176. {
  177.    struct tm t;
  178.    time_t tt;
  179.  
  180.    t.tm_sec  = otime[5];
  181.    t.tm_min  = otime[4];
  182.    t.tm_hour = otime[3];
  183.    t.tm_mday = otime[2];
  184.    t.tm_mon  = otime[1] - 1;
  185.    t.tm_year = otime[0];
  186.  
  187.    tt = mktime(&t);
  188.  
  189.    return(mktime(gmtime(&tt)));
  190. }
  191.  
  192.  
  193. long c4tol(cp)
  194. char *cp;
  195. {
  196.     return (
  197.         ((cp[0] & 0x7f) << 24)|
  198.         ((cp[1] & 0xff) << 16) |
  199.         ((cp[2] & 0xff) << 8) |
  200.          (cp[3] & 0xff)
  201.         );
  202. }
  203.  
  204.  
  205. char *ltoc4(n)
  206. long n;
  207. {
  208.     char cp[4];
  209.  
  210.     cp[3] = (char) (n % (256));
  211.     n = n >> 8;
  212.     cp[2] = (char) (n % (256));
  213.     n = n >> 8;
  214.     cp[1] = (char) (n % (256));
  215.     n = n >> 8;
  216.     cp[0] = (char) (n % (256));
  217.  
  218.     return(cp);
  219. }
  220.  
  221.  
  222. #asm
  223. I$SetStt        equ             $008E                   ; 8f is get
  224.  
  225. _ss_sbreak:
  226.                 link    a5,#0
  227.                 movem.l D1/D2/A0,-(A7)
  228.                 move.l  D1,D2
  229.                 move.w  #SS_Break,D1
  230.                 os9     I$SetStt
  231.                 bra     _sysret
  232. #endasm
  233.  
  234. /* The following unix compatable signal routines are from blarslib.l */
  235. /* #include <signal.h> */
  236.  
  237. typedef int (*pfi)();
  238.  
  239. #define SIGHANDS 32
  240.  
  241. static int siginit;
  242. static int nhands;
  243. static struct sighand {int signo; pfi action;} sighands[SIGHANDS];
  244.  
  245. static int catch();
  246.  
  247. pfi signal(sig, func)
  248. int sig;
  249. pfi func;
  250. {
  251.     register struct sighand *sp;
  252.     pfi p = SIG_DFL;
  253.  
  254.     if(!siginit) {
  255.         siginit = 1;
  256.     intercept(catch);
  257.     }
  258.     for(sp = &sighands[0]; sp < &sighands[SIGHANDS]; sp++) {
  259.         if(sp->signo == sig) {
  260.         p = sp->action;
  261.         sp->action = func;
  262.         return p;
  263.     }
  264.     }
  265.     for(sp = &sighands[0]; sp < &sighands[SIGHANDS]; sp++) {
  266.         if(sp->action == SIG_DFL) {
  267.         sp->signo = sig;
  268.         sp->action = func;
  269.         return SIG_DFL;
  270.     }
  271.     }
  272.     return (pfi)-1;
  273. }
  274.  
  275. static catch(sig)
  276. int sig;
  277. {
  278.     register struct sighand *sp;
  279.  
  280.     for(sp = &sighands[0]; sp < &sighands[SIGHANDS]; sp++) {
  281.         if(sig == sp->signo) {
  282.         /* note the sys V bug of removing the handler is not done */
  283.         /* (bsd does not have this bug) */
  284.         if((int)sp->action == SIG_IGN) return(0);
  285.         if((int)sp->action == SIG_DFL) break;
  286.         (*sp->action)();
  287.         return(0);
  288.     }
  289.     }
  290.     switch(sig) {
  291.     case SIGWAKE:
  292.         return(0);
  293.     default:
  294.         exit(sig);
  295.     }
  296. }
  297. #endif /* !m6809 */
  298.  
  299.