예제 #1
0
/*
 * rw_dungeon
 * セーブデータからのダンジョン情報の保存と復元
 */
void
rw_dungeon(FILE *fp, boolean rw)
{
    short i, j;
    char buf[ROGUE_COLUMNS];

    if (rw) {    // 書き込み時実行
	for (i = 0; i < ROGUE_LINES; i++) {
	    r_write(fp, (char *) dungeon[i], (ROGUE_COLUMNS * sizeof(dungeon[0][0])));
	    for (j = 0; j < ROGUE_COLUMNS; j++) {
		buf[j] = mvinch_rogue(i, j);
	    }
	    r_write(fp, buf, ROGUE_COLUMNS);
	}
	return;
    } else {    // 読み込み時実行
	for (i = 0; i < ROGUE_LINES; i++) {
	    r_read(fp, (char *) dungeon[i], (ROGUE_COLUMNS * sizeof(dungeon[0][0])));
	    r_read(fp, buf, ROGUE_COLUMNS);

	    if ( i < ROGUE_LINES - 1 ) {
		for (j = 0; j < ROGUE_COLUMNS; j++) {
		    mvaddch_rogue(i, j, (unsigned char) buf[j]);
		}
	    } else {
		print_stats(STAT_ALL);
	    }
	}
	return;
    }
}
예제 #2
0
void
read_string(char *s, FILE *fp)
{
    short n;

    r_read(fp, (char *) &n, sizeof(short));
    r_read(fp, s, n);
    xxxx(s, n);
}
예제 #3
0
UWORD RES_read_entry(RF_class *RF, ULONG entry, void *dest, RF_entry_hdr *RHDR,
		UWORD type) {
	static RF_entry_hdr header;
	ULONG len, hl;
	UWORD tl, dl, n;
	BYTE *tag;
	DICT_entry *cur;

	if (RF_flags(RF, entry) & (SA_UNUSED | SA_DELETED))
		return (0);

	header = *RF_header(RF, entry);
	if (RHDR != NULL)
		*RHDR = header;

	len = header.data_size;

	switch (type) {
	case RTYP_DICTIONARY:
		r_read(RF->file, &n, sizeof(UWORD));
		len -= ((ULONG) sizeof(UWORD) * 2L);

		hl = (ULONG) n * (ULONG) sizeof(ULONG);
		len -= hl;

		lseek(RF->file, hl, SEEK_CUR);

		while (len) {
			r_read(RF->file, &tl, sizeof(tl));
			len -= (ULONG) (sizeof(tl) + tl);

			if (!tl)
				continue;

			tag = (BYTE*) mem_alloc(tl);
			r_read(RF->file, tag, (UWORD) tl);

			cur = DICT_enter((DICT_class*) dest, tag, D_DEFHEAP);
			mem_free(tag);

			r_read(RF->file, &dl, sizeof(dl));
			len -= (ULONG) (sizeof(dl) + dl);

			if (dl) {
				cur->def = mem_alloc((ULONG) dl);
				r_read(RF->file, cur->def, (UWORD) dl);
			} else
				cur->def = NULL;
		}
		break;

	default:
		return (0);
	}

	return (1);
}
예제 #4
0
int main(void) {
   int audiofd;
   char *bp;
   char buffer[BLKSIZE];
   unsigned bytesneeded;
   int bytesread;
 
   if ((audiofd = open(AUDIO_DEVICE, O_NONBLOCK | O_RDWR)) == -1) {
      perror("Failed to open audio device");
      return 1;
    }
 
    bp = buffer;
    bytesneeded = BLKSIZE;
    while(bytesneeded != 0) {
       bytesread = r_read(audiofd, bp, bytesneeded);
       if ((bytesread == -1) && (errno != EAGAIN))
          break;
       if (bytesread > 0) {
          bp += bytesread;
          bytesneeded -= bytesread;
       } 
    }
    fprintf(stderr, "%d bytes read\n", BLKSIZE - bytesneeded);
    return 0;
}
예제 #5
0
void
read_pack(object *pack, FILE *fp, boolean is_rogue)
{
    object read_obj, *new_obj;

    for (;;) {
	r_read(fp, (char *) &read_obj, sizeof(object));
	if (read_obj.ichar == 0) {
	    pack->next_object = (object *) 0;
	    break;
	}
	new_obj = alloc_object();
	*new_obj = read_obj;
	if (is_rogue) {
	    if (new_obj->in_use_flags & BEING_WORN) {
		do_wear(new_obj);
	    } else if (new_obj->in_use_flags & BEING_WIELDED) {
		do_wield(new_obj);
	    } else if (new_obj->in_use_flags & (ON_EITHER_HAND)) {
		do_put_on(new_obj, ((new_obj->in_use_flags & ON_LEFT_HAND)
				    ? 1 : 0));
	    }
	}
	pack->next_object = new_obj;
	pack = new_obj;
    }
}
예제 #6
0
int main(int argc, char *argv[]) {
    int i;
    char reqbuf[1];
    int reqfd, seqfd;
    long seqnum;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s requestfifo sequencefifo\n", argv[0]);
        return 1;
    }

    if ((reqfd = open(argv[REQUEST_FIFO], O_WRONLY)) == -1 ||
        ((seqfd = open(argv[SEQUENCE_FIFO], O_RDONLY)) == -1)) {
        perror("Client failed to open a FIFO");
        return 1;
    }
    for (i = 0; i < REPEAT_MAX; i++) {
        reqbuf[0] = OK_CHAR;
        sleep((int) (SLEEP_MAX * drand48()));
        if (r_write(reqfd, reqbuf, 1) == -1) {
            perror("Client failed to write request");
            break;
        }
        if (r_read(seqfd, &seqnum, sizeof(seqnum)) != sizeof(seqnum)) {
            fprintf(stderr, "Client failed to read full sequence number\n");
            reqbuf[0] = ERROR_CHAR;
            r_write(reqfd, reqbuf, 1);
            break;
        }
        fprintf(stderr, "[%ld]:received sequence number %ld\n",
                (long) getpid(), seqnum);
    }
    return 0;
}
예제 #7
0
파일: resfile.cpp 프로젝트: psi29a/thirdeye
void RF_delete_entry(RF_class *RF, ULONG entry) {
	RF_entry_hdr RHDR;
	ULONG blknum;
	UWORD i;
	OD_link *link;

	blknum = (entry / (ULONG) OD_SIZE);      // blknum = directory block #
	i = (UWORD) (entry % (ULONG) OD_SIZE);   // i = entry # within block

	link = RF->root;
	while (blknum--) {
		link = link->next;
		if (link == NULL)
			return;
	}

	if (link->blk.flags[i] & (SA_UNUSED | SA_DELETED))
		return;

	lseek(RF->file, link->blk.index[i], SEEK_SET);
	r_read(RF->file, &RHDR, sizeof(RF_entry_hdr));

	link->blk.flags[i] |= SA_DELETED;
	link->touched = 1;

	RF->hdr.lost_space += RHDR.data_size;
	RF->touched = 1;

	RHDR.data_size = 0L;

	lseek(RF->file, link->blk.index[i], SEEK_SET);
	r_write(RF->file, &RHDR, sizeof(RF_entry_hdr));
}
예제 #8
0
파일: compile.c 프로젝트: liancheng/rose
rsexp r_compile_from_port (RState* r, rsexp port)
{
    rsexp reader;
    rsexp datum;
    rsexp program;

    ensure (reader = r_reader_new (r, port));

    program = R_NULL;
    datum = R_UNDEFINED;

    while (TRUE) {
        datum = r_read (reader);

        if (r_failure_p (datum))
            return R_FAILURE;

        if (r_eof_object_p (datum))
            break;

        ensure (program = r_cons (r, datum, program));
    }

    return r_compile (r, program);
}
예제 #9
0
/* Fork a chain of processes */
int main(int argc, char *argv[])
{
	char buf[] = "g";
	pid_t child = 0;
	int fd[2];
	int i, n;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s processes\n", argv[0]);
		return 1;
	}
	n = atoi(argv[1]);
	if (pipe(fd) == -1) {	/* create pipe */
		perror("Failed to create pipe");
		return 1;
	}
	for (i = 0; i < n; i++)	/* create fan of processes */
		if ((child = fork()) <= 0)
			break;
	if (child > 0) {	/* write synchronization chars to pipe */
		for (i = 0; i < n; i++)
			if (r_write(fd[1], buf, 1) != 1)
				perror("Failed to write sync characters");
	}
	if (r_read(fd[0], buf, 1) != 1) /* synchronize here */
		perror("Failed to read sync characters");
	
	fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld\n",
		i, (long)getpid(), (long)getppid(), (long)child);

	return 0;
}
예제 #10
0
/**
 * attempts to read at most nbyte bytes from the open file descriptor fd into the
 * buffer buf. The r_readtimed function behaves the same as r_read unless no bytes
 * are available in a number of seconds given by seconds. If no bytes are available
 * within the timeout period, r_readtimed returns –1 and sets errno to ETIME. If
 * interrupted by a signal, r_readtimed restarts but maintains the original ending
 * timeout.
 */
ssize_t r_readtimed(int fd, void *buf, size_t nbyte, double seconds) {
	struct timeval timedone;
	timedone = r_add2currenttime(seconds);
	if (r_waitfdtimed(fd, timedone) == -1)
		return (ssize_t)(-1);
	return r_read(fd, buf, nbyte);
}
예제 #11
0
ssize_t readtimed(int fd, void *buf, size_t nbytes, double seconds)
{
	struct timeval timedone;

	timedone = add2currenttime(seconds);
	if (waitfdtimed(fd, timedone) == -1)
		return -1;
	return r_read(fd, buf, nbytes);
}
예제 #12
0
파일: resfile.cpp 프로젝트: psi29a/thirdeye
ULONG RF_write_entry(RF_class *RF, ULONG entry, void *source,
		RF_entry_hdr *RHDR, UWORD type) {
	RF_entry_hdr cur, dummy;
	ULONG blknum;
	UWORD i;
	OD_link *link;

	blknum = (entry / (ULONG) OD_SIZE);      // blknum = directory block #
	i = (UWORD) (entry % (ULONG) OD_SIZE);   // i = entry # within block

	link = RF->root;
	while (blknum--) {
		link = link->next;
		if (link == NULL)
			return ((ULONG) -1L);
	}
	if (link->blk.flags[i] & SA_UNUSED)
		return ((ULONG) -1L);

	RF->touched = 1;

	if (RHDR == NULL) {
		dummy.data_size = 0L;
		dummy.data_attrib = DA_TEMPORARY;
		RHDR = &dummy;
	}

	if (type != RTYP_HOUSECLEAN) {
		if (link->blk.flags[i] & SA_DELETED) {
			link->blk.flags[i] &= (~SA_DELETED);
			link->touched = 1;
		}
		RHDR->timestamp = RF->hdr.modify_time = current_time();
	}

	lseek(RF->file, link->blk.index[i], SEEK_SET);

	if (link->blk.index[i] == RF->hdr.file_size) {
		RF->hdr.file_size += (sizeof(RF_entry_hdr) + RHDR->data_size);
		return (RES_store_resource(RF, entry, source, RHDR, type));
	}

	r_read(RF->file, &cur, sizeof(RF_entry_hdr));

	if (RHDR->data_size > cur.data_size) {
		RF->hdr.lost_space += (cur.data_size + sizeof(RF_entry_hdr));
		link->blk.index[i] = RF->hdr.file_size;
		link->touched = 1;
		lseek(RF->file, 0L, SEEK_END);
		RF->hdr.file_size += (sizeof(RF_entry_hdr) + RHDR->data_size);
	} else {
		RF->hdr.lost_space += (cur.data_size - RHDR->data_size);
		lseek(RF->file, link->blk.index[i], SEEK_SET);
	}

	return (RES_store_resource(RF, entry, source, RHDR, type));
}
예제 #13
0
void
rw_rooms(FILE *fp, boolean rw)
{
    short i;

    for (i = 0; i < MAXROOMS; i++) {
	rw ? r_write(fp, (char *) (rooms + i), sizeof(room)) :
	    r_read(fp, (char *) (rooms + i), sizeof(room));
    }
}
예제 #14
0
void
rw_id(struct id id_table[], FILE *fp, int n, boolean wr)
{
    short i;

    for (i = 0; i < n; i++) {
	if (wr) {
	    r_write(fp, (char *) &(id_table[i].value), sizeof(short));
	    r_write(fp, (char *) &(id_table[i].id_status),
		    sizeof(unsigned short));
	    write_string(id_table[i].title, fp);
	} else {
	    r_read(fp, (char *) &(id_table[i].value), sizeof(short));
	    r_read(fp, (char *) &(id_table[i].id_status),
		   sizeof(unsigned short));
	    read_string(id_table[i].title, fp);
	}
    }
}
예제 #15
0
/**
 * reads at most PIPE_BUF bytes from open file descriptor fromfd and writes the
 * bytes read to the open file descriptor tofd. If successful, r_readwrite returns the
 * number of bytes copied. If r_readwrite reaches end-of-file on fromfd, it returns
 * 0. If unsuccessful, r_readwrite returns –1 and sets errno.
 */
int r_readwrite(int fromfd, int tofd) {
	char buf[BLKSIZE];
	int bytesread;
	if ((bytesread = r_read(fromfd, buf, BLKSIZE)) < 0)
		return -1;
	if (bytesread == 0)
		return 0;if (r_write(tofd, buf, bytesread) < 0)
			return -1;
		return bytesread;
}
예제 #16
0
/*
 * rend_send_message
 *
 * Send a rendezvous message
 */
int rend_send_message(REND_MESSAGE *pmsg) {
    int retval;

    if(r_write(rend_pipe_to[WR_SIDE],pmsg,sizeof(REND_MESSAGE)) == -1)
	return -1;

    if((retval=r_read(rend_pipe_from[RD_SIDE],&retval,sizeof(int)) == -1))
	return -1;

    return retval;
}
예제 #17
0
void process_or_do_work(int fd1, int fd2) {
   char buf[1024];
   ssize_t bytesread;

   for ( ; ; ) {
      bytesread = r_read(fd1, buf, sizeof(buf));
      if ((bytesread == -1) && (errno != EAGAIN))
         return;                                    /* a real error on fd1 */
      else if (bytesread > 0) {
         docommand(buf, bytesread);
         continue;
      }
      bytesread = r_read(fd2, buf, sizeof(buf));
      if ((bytesread == -1) && (errno != EAGAIN))
         return;                                    /* a real error on fd2 */
      else if (bytesread > 0) 
         docommand(buf, bytesread);
      else
         dosomething();          /* input not available, do something else */
   }
}
예제 #18
0
/*
This function is used to execute in a thread.
It will connect a server with hostname and port number.
It will send a full path of the named filename under the current working directory
to the server.
The server will return back a point to a number, if successful is 0, failed is 1.
The mutex lock is used to lock the communication process.
*/
void *threadconnect(void *arg) {
    char flag[1];
    int flag_num;
    int bytescopied;
    int communfd;
    int error;
    char compilepath[BLKSIZE];
    int i =0;
    const char *str_name;

    struct connectpara *cp;
    cp = (struct connectpara *)(arg);

    u_port_t pn = 0;
    char *hn = NULL;
    char *fn = NULL;

    pn = (*cp).portnumber;
    hn = (*cp).hostname;
    fn = (*cp).filename;

    if ((communfd = u_connect(pn, hn)) == -1) {
        perror("Failed to establish connection");
        return NULL;
    }
    fprintf(stderr, "[%ld]:connection made to %s\n", (long)getpid(), hn);

    str_name = fn;

    getcwd(compilepath, BLKSIZE);

    strcat(compilepath, "/");
    strcat(compilepath, str_name);
    strcat(compilepath, "\n");

    if(r_write(communfd, compilepath, strlen(compilepath)) < 0) {
        fprintf(stderr, "Could not write to communfd\n");
        return NULL;
    }

    if ((i = r_read(communfd, flag, 1)) < 0) {		//read flag from STDOUT_FILENO
        fprintf(stderr, "Could not read flag from communfd.\n");
        return NULL;
    }

    if(flag_num = (int) atoi(flag)) {
        return &one;
    }
    else {
        return &zero;
    }
}
예제 #19
0
static bool_t xdr_std_read(xdr_s_type *xdr)
{
    xdr->in_len = r_read(xdr->fd, (void *)xdr->in_msg, RPCROUTER_MSGSIZE_MAX);
    if (xdr->in_len < 0) return FALSE;

    if (xdr->in_len < (RPC_OFFSET+2)*4) {
        xdr->in_len = -1;
        return FALSE;
    }

    xdr->in_next = (RPC_OFFSET+2)*4;
    return TRUE;
}
예제 #20
0
int copyfile(int fromfd, int tofd) {
   char buf[BLKSIZE]; 
   int bytesread, byteswritten;
   int totalbytes = 0;

   for (  ;  ;  ) {
      if ((bytesread = r_read(fromfd, buf, BLKSIZE)) <= 0)
         break;     
      if ((byteswritten = r_write(tofd, buf, bytesread)) == -1)
         break;
      totalbytes += byteswritten;
   }
   return totalbytes;
}
예제 #21
0
char *shell::passToLocator(const char*command){
    //fprintf(stderr,"passToLocator\n");
    int bytesSent, bytesRead=-1, myError;
    char buffer[MAXBUFFER];
    char *newCommand;
    sem_wait(locatorAccessSemaphore);       //I need you tonight.
    //fprintf(stderr,"Access Semaphore received.\n");
    bytesSent=r_write(processToLocatorThreadPipe[1], (void*)command, (std::strlen(command)+1));
    bytesRead=r_read(locatorThreadToProcessPipe[0], (void*)buffer, MAXBUFFER);
    //fprintf(stderr,"Bytes read: %d!\n", bytesRead);
    sem_post(locatorAccessSemaphore);       //Let's go our separate ways.
    //fprintf(stderr,"Access Semaphore returned.\n");
    newCommand=new char[std::strlen(buffer)+1];
    std::strcpy(newCommand,buffer);
    return newCommand;
}
예제 #22
0
void monitorselect(int fd[], int numfds) {
   char buf[BUFSIZE];
   int bytesread;
   int i;
   int maxfd;
   int numnow, numready;
   fd_set readset;

   maxfd = 0;                  /* set up the range of descriptors to monitor */
   for (i = 0; i < numfds; i++) {
       if ((fd[i] < 0) || (fd[i] >= FD_SETSIZE))
          return;
       if (fd[i] >= maxfd)
          maxfd = fd[i] + 1;
   }
   numnow = numfds;
   while (numnow > 0) {            /* continue monitoring until all are done */
      FD_ZERO(&readset);                  /* set up the file descriptor mask */
      for (i = 0; i < numfds; i++)
         if (fd[i] >= 0)
            FD_SET(fd[i], &readset);
      numready = select(maxfd, &readset, NULL, NULL, NULL);  /* which ready? */
      if ((numready == -1) && (errno == EINTR))     /* interrupted by signal */
         continue;
      else if (numready == -1)                          /* real select error */
         break;
      for (i = 0; (i < numfds) && (numready > 0); i++) { /* read and process */
         if (fd[i] == -1)                         /* this descriptor is done */
            continue;
         if (FD_ISSET(fd[i], &readset)) {        /* this descriptor is ready */
            bytesread = r_read(fd[i], buf, BUFSIZE);
            numready--;
            if (bytesread > 0)
               docommand(buf, bytesread);
            else  {           /* error occurred on this descriptor, close it */
               r_close(fd[i]);
               fd[i] = -1;
               numnow--;
            }
         }
      }
   }
   for (i = 0; i < numfds; i++)
       if (fd[i] >= 0)
           r_close(fd[i]);
}
예제 #23
0
static void alarm_handle_ev_cb(int fd,short event_type,void *arg)
{

    int ret;
    
    char buff[ALARM_BUFF_SIZE];

      
    struct ISIL_EV_TASK * ev_task = (struct ISIL_EV_TASK *)arg;

    if(ev_task) {
        
        ret = r_read(fd,(char *)buff,ALARM_BUFF_SIZE);
        
        if(ret < 0 ) {

            
            if( errno == ENOMEM) {
                //TODO:
                
                return ;
            }
            fprintf(stderr,"Read alarm data err .\n");
            ev_task->release(ev_task);
            return ;

        }
        else if( ret == 0 ) {

            fprintf(stderr,"Get alarm date err .\n");
            ev_task->release(ev_task);
            DEBUG_FUNCTION();
            return ;
        }
        else{
            /*put here*/
            if(ev_task->hook) {
                ev_task->hook( (void *)buff,ev_task);
            }
        }


    }

    return ;
}
예제 #24
0
파일: rtp.c 프로젝트: dulton/hm-platform
static void
rtp_session_fill_cb(gpointer unused_data, gpointer session_p)
{
    RTP_session *rtp_s = (RTP_session*)session_p;
    Resource *resource = rtp_s->track->parent;
    BufferQueue_Consumer *consumer = rtp_s->consumer;
    gulong unseen;

	unseen = bq_consumer_unseen(consumer);
	if (unseen >= MAX_RND_PACKAGES)
		return;

    while ((unseen = bq_consumer_unseen(consumer)) < 3*MAX_RND_PACKAGES)
    {
    	if (r_read(resource) != RESOURCE_OK)
            break;
    }
}
예제 #25
0
static int scrape_contents(pid_t pid, int fd, int size, void* data)
{
    int offset, retval;

    retval = 0;
    offset = r_lseek(pid, fd, 0, SEEK_CUR);
    if (offset == -1)
	goto out_err;
    if (r_lseek(pid, fd, 0, SEEK_SET) < 0)
	goto out_err;
    if (r_read(pid, fd, data, size) < 0)
	goto out_err_seek;
    retval = 1;
	
out_err_seek:
    r_lseek(pid, fd, offset, SEEK_SET);
out_err:
    return retval;
}
예제 #26
0
void *shell::locatorThread(void   *arguments){
    int bytesRead,bytesSent=-1, myError;
    char buffer[MAXBUFFER];
    std::string fullCommandName;
    while(running){
        bytesRead=r_read(processToLocatorThreadPipe[0], buffer, MAXBUFFER);
        //fprintf(stderr,"Bytes read: %d!\n", bytesRead);
        //fprintf(stderr,"Buffer contents: %s\n", buffer);
        buffer[bytesRead-1]=0;  //Cap it with a null, just to make sure
        fullCommandName=locatorFindFile(buffer);
        //bytesRead=r_write(locatorThreadToProcessPipe[0], (void*)fullCommandName.c_str(), (fullCommandName.size()+1));
        //fprintf(stderr,"File Location: %s\n", fullCommandName.c_str());
        std::strcpy(buffer, fullCommandName.c_str());
        //fprintf(stderr,"Buffer contents: %s\n", buffer);
        bytesSent=r_write(locatorThreadToProcessPipe[1], buffer, std::strlen(buffer)+1);
        //fprintf(stderr,"Bytes Sent: %d\n", bytesSent);
        fullCommandName.clear();    
    }
    return NULL;
}
예제 #27
0
/* Read from infd and write to outfd until an error or end-of-file occurs */
static void readwriteall(int infd, int outfd) {
   char buf[RWBUFSIZE];
   int bytes_read;

   while ((bytes_read = r_read(infd, buf, RWBUFSIZE)) > 0) {
      if (r_write(outfd, buf, bytes_read) != bytes_read) {
         if (ldebug_flag)
            fprintf(stderr, "Pipe write error\n");
         close(infd);
         close(outfd);
         return;
      }
   }
   if (bytes_read < 0) {
      if (ldebug_flag)
         fprintf(stderr, "Pipe read error\n");
   }
   close(infd);
   close(outfd);
}
예제 #28
0
파일: rtp.c 프로젝트: phully/feng_build
/**
 * @brief Do the work of filling an RTP session with data
 *
 * @param unued_data Unused
 * @param session_p The session parameter from rtp_session_fill
 *
 *
 * @internal This function is used to initialize @ref
 *           RTP_session::fill_pool.
 */
static void rtp_session_fill_cb(ATTR_UNUSED gpointer unused_data,
                                gpointer session_p)
{
    RTP_session *session = (RTP_session*)session_p;
    Resource *resource = session->track->parent;
    BufferQueue_Consumer *consumer = session->consumer;
    gulong unseen;
    const gulong buffered_frames = resource->srv->srvconf.buffered_frames;

    while ( (unseen = bq_consumer_unseen(consumer)) < buffered_frames ) {
#if 0
        fprintf(stderr, "calling read_packet from %p for %p[%s] (%u/%d)\n",
                session,
                resource, resource->info->mrl,
                unseen, buffered_frames);
#endif

        if ( r_read(resource) != RESOURCE_OK )
            break;
    }
}
예제 #29
0
파일: resfile.cpp 프로젝트: psi29a/thirdeye
RF_entry_hdr *RF_header(RF_class *RF, ULONG entry) {
	static RF_entry_hdr RHDR;
	ULONG blknum;
	UWORD i;
	OD_link *link;

	blknum = (entry / (ULONG) OD_SIZE);      // blknum = directory block #
	i = (UWORD) (entry % (ULONG) OD_SIZE);   // i = entry # within block

	link = RF->root;
	while (blknum--) {
		link = link->next;
		if (link == NULL)
			return (NULL);
	}
	if (link->blk.flags[i] & SA_UNUSED)
		return (NULL);

	lseek(RF->file, link->blk.index[i], SEEK_SET);
	r_read(RF->file, &RHDR, sizeof(RF_entry_hdr));

	return (&RHDR);
}
예제 #30
0
ssize_t
rRead (struct r_file *file, char *buffer, ssize_t size) {

        int status = 0 , ret = -1 , siz = 0 , flage = 0;
        unsigned long temp_size = 0 , bytes_read = 0 , temp_size1 = 0;
        unsigned long counter = 0;

        temp_size = size;
        if (temp_size > 0) {
                if (file != NULL) {

			if (file->write_flag == 1) {
				printf ("ERROR: File is Open in Write mode\n");
				goto out;
			}

                        /*if size is greater then 1024, then we have to
                        * repeatedly call the r_read function as server can
                        * send only 1024 bytes per request*/
                        if (temp_size > 1020) {
                                temp_size1 = size;
                                flage = 1;
loop:
                                if (temp_size > 1020) {
                                        size = 1020;
                                        temp_size -= 1020;
                                } else {
                                        size = temp_size;
                                        temp_size = 0;
                                }
                        }

                        siz = r_read (file , buffer , size);

                        if (siz == -1) {
                                /*checking the enum to print the error*/
                                /*if error occures the buffer will only
                                 * contain error enum*/
                                status = atoi(buffer);
                                if (status == file_not_open) {
                                        printf ("ERROR: File Not Open\n");
                                        printf ("\tRead failed\n");
                                } else if (status == not_enough_content) {
                                        if (flage == 1)
                                                goto message;
                                        printf ("ERROR: Not Enough Content\n");

                                } else
                                        printf ("ERROR: Read Failed");
                                ret = -1;
                                goto out;
                        } else if (siz < size) {
                                if (flage != 1) {

                                        printf ("File Contains only:");
                                        printf("%d bytes\n" , siz);
                                        printf ("File Content :\n\n%s\n\n",
                                        buffer);

                                        ret = 0;
                                        goto out;
                                } else if (flage == 1) {
                                        printf ("%s" , buffer);
                                        counter += siz;

                                        ret = 0;
                                        goto message;
                                }
                        } else {
                                if (flage == 1) {
                                        printf ("%s" , buffer);
                                        counter += siz;
                                        if (counter == temp_size1) {
                                                ret = 0;
                                                goto message;
                                        } else {
                                                goto loop;
                                        }
                                } else {
                                        printf ("File Content :\n\n%s\n\n"
								, buffer);
                                        ret = 0;
                                        goto out;
                                }
                        }
                } else
                        printf ("ERROR: File Not Open\n\n");
        } else
                printf ("ERROR: Please Enter a Valid Size\n\n");

message:
        if (flage == 1) {
                if (counter == temp_size1)
                        printf ("Total Contents read = %lu" , counter);
                else
                        printf ("File only Contain  : %lu bytes\n" , counter);
                flage = 0;
        }

out:
        memset(buffer , 0 , sizeof(buffer));
        return ret;
}