home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / misc / zero / zero.c < prev    next >
C/C++ Source or Header  |  1995-01-07  |  4KB  |  151 lines

  1. /* zero-handler - source of 0-bytes, like /dev/zero in Unix
  2.  *
  3.  * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation.  This software is provided "as is" without express or
  10.  * implied warranty.
  11.  *
  12.  * V1.0: 23/Oct/94 first version
  13.  * V1.1: 06/Jan/95 added bytecount
  14.  */
  15. #define THIS_PROGRAM    "zero-handler"
  16. #define THIS_VERSION    "1.1"
  17.  
  18. static char amiga_version[] = "\0$VER: " THIS_PROGRAM " " THIS_VERSION " (" __COMMODORE_DATE__ ")";
  19.  
  20.  
  21. #include <exec/types.h>
  22. #include <exec/nodes.h>
  23. #include <exec/lists.h>
  24. #include <exec/ports.h>
  25. #include <exec/memory.h>
  26. #include <dos/dos.h>
  27. #include <dos/dosextens.h>
  28. #include <dos/filehandler.h>
  29. #include <proto/exec.h>
  30. #include <proto/dos.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <clib/alib_protos.h>
  34.  
  35. static char * BSTR2C(BPTR bstr);
  36.  
  37.  
  38. static struct MinList mylist;
  39. typedef struct {
  40.     struct MinNode  node;
  41.     LONG            bytesleft;
  42. } NODE;
  43.  
  44.  
  45. static char *
  46. BSTR2C(bstr)
  47.     BPTR bstr;
  48. {
  49.     static char cstr[256];
  50.     int len;
  51.     UBYTE *p = (UBYTE *)BADDR(bstr);
  52.  
  53.     if( p ) {
  54.         len = (int) *p++;
  55.         strncpy(cstr, (char *)p, len);
  56.         cstr[len] = '\0';
  57.         return cstr;
  58.     }
  59.     return (char *)0;
  60. }
  61.  
  62.  
  63. void _main()
  64. {
  65.     struct DeviceNode *devnode;
  66.     struct Process *myproc;
  67.     struct DosPacket *pkt;
  68.     NODE *node;
  69.     struct FileHandle *fh;
  70.     char *name;
  71.     LONG err1, err2;
  72.  
  73.     NewList((struct List *)&mylist);
  74.  
  75.     myproc = (struct Process *)FindTask(NULL);
  76.  
  77.     /* get the startup message */
  78.     pkt = WaitPkt();
  79.     devnode = (struct DeviceNode *)BADDR(pkt->dp_Arg3);
  80.     devnode->dn_Task = &(myproc->pr_MsgPort);
  81.     ReplyPkt(pkt, DOSTRUE, 0);
  82.  
  83.     for(;;) {
  84.         pkt = WaitPkt();
  85.  
  86.         err1 = DOSTRUE; err2 = 0;
  87.         switch( pkt->dp_Type ) {
  88.             case ACTION_FINDINPUT:
  89.                 fh = (struct FileHandle *)BADDR(pkt->dp_Arg1);
  90.                 fh->fh_Port = NULL;
  91.  
  92.                 if( name = BSTR2C(pkt->dp_Arg3) ) {
  93.                     char *p; LONG nbytes;
  94.                     if( p = strchr(name, ':') )
  95.                         name = ++p;
  96.                     nbytes = strtol(name, &p, 10);
  97.                     if( *p ) {             /* "zero:xxx" */
  98.                         err1 = DOSFALSE;
  99.                         err2 = ERROR_OBJECT_NOT_FOUND;
  100.                     }
  101.                     else
  102.                     if( name == p )             /* "zero:\0" */
  103.                         fh->fh_Arg1 = (LONG)0;
  104.                     else                        /* "zero:<number>" */
  105.                     if( node = AllocMem(sizeof(NODE), MEMF_ANY|MEMF_CLEAR) ) {
  106.                         node->bytesleft = strtol(name, &p, 10);
  107.                         AddTail((struct List *)&mylist, (struct Node *)node);
  108.                         fh->fh_Arg1 = (LONG)node;
  109.                     }
  110.                     else {
  111.                         err1 = DOSFALSE;
  112.                         err2 = ERROR_NO_FREE_STORE;
  113.                     }
  114.                 }
  115.                 else
  116.                     fh->fh_Arg1 = (LONG)0;
  117.             break;
  118.  
  119.             case ACTION_END:
  120.                 if( node = (NODE *)(pkt->dp_Arg1) ) {
  121.                     Remove((struct Node *)node);
  122.                     FreeMem(node, sizeof(NODE));
  123.                 }
  124.             break;
  125.  
  126.             case ACTION_READ:
  127.                 err1 = pkt->dp_Arg3;
  128.                 bzero((void *)(pkt->dp_Arg2), err1);
  129.  
  130.                 if( node = (NODE *)(pkt->dp_Arg1) ) {
  131.                     if( err1 > node->bytesleft )
  132.                         err1 = node->bytesleft;
  133.                     node->bytesleft -= err1;
  134.                 }
  135.             break;
  136.  
  137.             case ACTION_IS_FILESYSTEM:
  138.                 err1 = DOSFALSE;
  139.                 /* err2 = 0; */
  140.             break;
  141.  
  142.             default:
  143.                 err1 = DOSFALSE;
  144.                 err2 = ERROR_ACTION_NOT_KNOWN;
  145.             break;
  146.         }
  147.         ReplyPkt(pkt, err1, err2);
  148.     }
  149. }
  150.  
  151.