/*!
  \fn char * print_temp_b_sec6 (char **sec6, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 6 of part B of a TEMP report
  \param sec6 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec6
*/
char * print_temp_b_sec6 ( char **sec6, size_t lmax, struct temp_chunks *t )
{
  size_t i;
  char *c = *sec6, *c0 = *sec6;

  if ( check_len ( sec6, 6 ) )
    {
      c += sprintf ( c, " 21212" );
    }

  for ( i = 0; i < t->b.s6.n && i < TEMP_NMAX_POINTS ; i++ )
    {
      if ( check_len ( sec6, 12 ) )
        {
          c += sprintf ( c, " %s%s", t->b.s6.wd[i].nini, t->b.s6.wd[i].PnPnPn );
          c += sprintf ( c, " %s", t->b.s6.wd[i].dndnfnfnfn );
        }
    }

  if ( c != c0 )
    {
      *sec6 = c;
    }
  return *sec6;
}
/*!
  \fn char * print_temp_c_sec3 (char **sec3, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 3 of part C of a TEMP report
  \param sec3 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec3
*/
char * print_temp_c_sec3 ( char **sec3, size_t lmax, struct temp_chunks *t )
{
  size_t i;
  char *c = *sec3, *c0 = *sec3;

  if ( t->c.s3.n == 0 )
    {
      if ( check_len ( sec3, 6 ) )
        {
          c += sprintf ( c, " 88999" );
        }
    }
  else
    {
      for ( i = 0; i < t->c.s3.n ; i++ )
        {
          if ( check_len ( sec3, 18 ) )
            {
              c += sprintf ( c, " 88%s", t->c.s3.trop[i].PnPnPn );
              c += sprintf ( c, " %s%s", t->c.s3.trop[i].TnTnTan, t->c.s3.trop[i].DnDn );
              c += sprintf ( c, " %s", t->c.s3.trop[i].dndnfnfnfn );
            }
        }
    }

  if ( c != c0 )
    {
      *sec3 = c;
    }
  return *sec3;

}
/*!
  \fn char * print_temp_a_sec2 (char **sec2, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 2 of part A of a TEMP report
  \param sec2 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec2
*/
char * print_temp_a_sec2 ( char **sec2, size_t lmax, struct temp_chunks *t )
{
  size_t i;
  char *c = *sec2, *c0 = *sec2;

  //Surface level
  if ( check_len ( sec2, 18 ) )
    {
      c += sprintf ( c, " 99%s", t->a.s2.lev0.PnPnPn );
      c += sprintf ( c, " %s%s", t->a.s2.lev0.TnTnTan, t->a.s2.lev0.DnDn );
      c += sprintf ( c, " %s", t->a.s2.lev0.dndnfnfnfn );
    }
  for ( i = 0; i < t->a.s2.n ; i++ )
    {
      if ( check_len ( sec2, 18 ) )
        {
          c += sprintf ( c, " %s%s", t->a.s2.std[i].PnPn, t->a.s2.std[i].hnhnhn );
          c += sprintf ( c, " %s%s", t->a.s2.std[i].TnTnTan, t->a.s2.std[i].DnDn );
          c += sprintf ( c, " %s", t->a.s2.std[i].dndnfnfnfn );
        }
    }

  if ( c != c0 )
    {
      *sec2 = c;
    }
  return *sec2;

}
Beispiel #4
0
void os_flush (void)
{
  if (Window == 0)
    return;
  if (TextBufferPtr < 1)
    return;

  static int flushing = 0;

  if (flushing != 0)
    return;
  flushing = 1;

  char *ptr, *space, *lastspace;
  int searching;

  *(TextBuffer+TextBufferPtr) = ' ';
  ptr = TextBuffer;
  while (check_len (ptr, TextBufferPtr))
  {
    space = ptr;
    lastspace = space;
    searching = 1;
    while (searching)
    {
      while (*space != ' ')
	space++;
      if (check_len (ptr, space - ptr))
      {
	space = lastspace;
	text (ptr, space - ptr);
	os_printchar ('\r');
	space++;
	if (*space == ' ')
	  space++;
	TextBufferPtr -= space - ptr;
	ptr = space;
	searching = 0;
      }
      else
	lastspace = space;
      space++;
    }
  }
  if (TextBufferPtr > 0)
    text (ptr, TextBufferPtr);
  TextBufferPtr = 0;

  flushing = 0;
}
/*!
  \fn char * print_temp_a_sec4 (char **sec4, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 4 of part A of a TEMP report
  \param sec4 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec1
*/
char * print_temp_a_sec4 ( char **sec4, size_t lmax, struct temp_chunks *t )
{
  size_t i;
  char *c = *sec4, *c0 = *sec4;

  if ( t->a.s4.n == 0 )
    {
      if ( check_len ( sec4, 6 ) )

        {
          c += sprintf ( c, " 77999" );
        }
    }
  else
    {
      for ( i = 0; i < t->a.s4.n ; i++ )
        {
          if ( check_len ( sec4, 18 ) )
            {
              if ( t->a.s4.windx[i].no_last_wind )
                {
                  c += sprintf ( c, " 77%s", t->a.s4.windx[i].PmPmPm );
                }
              else
                {
                  c += sprintf ( c, " 66%s", t->a.s4.windx[i].PmPmPm );
                }
              c += sprintf ( c, " %s", t->a.s4.windx[i].dmdmfmfmfm );

              if ( t->a.s4.windx[i].vbvb[0] && t->a.s4.windx[i].vava[0] )
                {
                  c += sprintf ( c, " 4%s%s", t->a.s4.windx[i].vbvb, t->a.s4.windx[i].vava );
                }
            }
        }
    }


  if ( c != c0 )
    {
      *sec4 = c;
    }
  return *sec4;

}
/*!
  \fn char * print_temp_d_sec1 (char **sec1, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 1 of part D of a TEMP report
  \param sec1 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec1
*/
char * print_temp_d_sec1 ( char **sec1, size_t lmax, struct temp_chunks *t )
{
  char *c = *sec1, *c0 = *sec1;

  c += sprintf ( c, "%s", t->t.datime );

  c += sprintf ( c, " %s%s", t->d.s1.MiMi, t->d.s1.MjMj );

  if ( t->d.s1.D_D[0] && t->a.s1.II[0] == 0 )
    {
      c += sprintf ( c, " %s", t->d.s1.D_D );
    }

  c += sprintf ( c, " %s/", t->d.s1.YYGG );

  // print IIiii
  if ( check_len ( sec1,6 ) && t->d.s1.II[0] )
    {
      c += sprintf ( c, " %s%s", t->d.s1.II, t->d.s1.iii );
    }
  else
    {
      if ( t->d.s1.LaLaLa[0] )
        {
          c += sprintf ( c, " 99%s", t->d.s1.LaLaLa );
        }
      else
        {
          c += sprintf ( c, " 99///" );
        }

      if ( t->d.s1.Qc[0] && t->d.s1.LoLoLoLo[0] )
        {
          c += sprintf ( c, " %s%s", t->d.s1.Qc, t->d.s1.LoLoLoLo );
        }
      else
        {
          c += sprintf ( c, " /////" );
        }

      if ( t->d.s1.MMM[0] && t->d.s1.Ula && t->d.s1.Ulo )
        {
          c += sprintf ( c, " %s%s%s", t->d.s1.MMM, t->d.s1.Ula, t->d.s1.Ulo );
        }

      if ( t->d.s1.h0h0h0h0[0] )
        {
          c += sprintf ( c, " %s%s", t->d.s1.h0h0h0h0, t->d.s1.im );
        }
    }

  if ( c != c0 )
    {
      *sec1 = c;
    }
  return *sec1;
}
/*!
  \fn char * print_temp_c_sec7 (char **sec7, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 7 of part C of a TEMP report
  \param sec7 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec7
*/
char * print_temp_c_sec7 ( char **sec7, size_t lmax, struct temp_chunks *t )
{
  char *c = *sec7, *c0 = *sec7;

  if ( check_len ( sec7, 18 ) )
    {
      c += sprintf ( c, " 31313" );
      c += sprintf ( c, " %s%s%s", t->c.s7.sr, t->c.s7.rara, t->c.s7.sasa );
      c += sprintf ( c, " 8%s%s", t->c.s7.GG, t->c.s7.gg );
    }
  if ( t->c.s7.TwTwTw[0] && check_len ( sec7, 6 ) )
    {
      c += sprintf ( c, " 9%s%s", t->c.s7.sn, t->c.s7.TwTwTw );
    }

  if ( c != c0 )
    {
      *sec7 = c;
    }
  return *sec7;
}
Beispiel #8
0
/**
 * do some general checks on the multihash for validity
 * @param mh the multihash
 * @param len the length of the multihash
 * @returns errors or MH_E_NO_ERROR(0)
 */
static int check_multihash(const unsigned char mh[], size_t len) {
	int err;

	if (len < 3)
		return MH_E_TOO_SHORT;

	if (mh[0] & VARINT_MASK) {
		// This value is a varint, but there are currently no supported
		// values that require more than a single byte to represent.
		return MH_E_VARINT_NOT_SUPPORTED;
	} else if (mh[1] & VARINT_MASK) {
		return MH_E_VARINT_NOT_SUPPORTED;
	}

	err = check_len(mh[1]);

	return err;
}
Beispiel #9
0
int cw_in_generic(struct conn *conn, struct cw_action_in *a, uint8_t * data, int len,
		  struct sockaddr *from)
{

	if (!check_len(conn, a, data, len, from))
		return 0;


	mbag_t itemstore;
///	if (!a->target)
		itemstore = conn->incomming;
//	else
//		itemstore = a->target(conn, a);


	return do_save(itemstore, conn, a, data, len, from);



}
Beispiel #10
0
/*!
  \fn char * print_temp_d_sec5 (char **sec5, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 5 of part D of a TEMP report
  \param sec5 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec5
*/
char * print_temp_d_sec5 ( char **sec5, size_t lmax, struct temp_chunks *t )
{
  size_t i;
  char *c = *sec5, *c0 = *sec5;

  for ( i = 0; i < t->d.s5.n && i < TEMP_NMAX_POINTS ; i++ )
    {
      if ( check_len ( sec5, 12 ) )
        {
          c += sprintf ( c, " %s%s", t->d.s5.th[i].nini, t->d.s5.th[i].PnPnPn );
          c += sprintf ( c, " %s%s", t->d.s5.th[i].TnTnTan, t->d.s5.th[i].DnDn );
        }
    }

  if ( c != c0 )
    {
      *sec5 = c;
    }
  return *sec5;
}
Beispiel #11
0
int cw_in_radio_generic(struct conn *conn, struct cw_action_in *a, uint8_t * data,
                        int len, struct sockaddr *from)
{
    if (!check_len(conn, a, data, len, from))
        return 0;

    int rid = cw_get_byte(data);
    mbag_t radio = mbag_i_get_mbag(conn->radios, rid, NULL);
    if (!radio) {
        if (a->vendor_id != 0
                || ( (a->vendor_id == 0) && (a->msg_id != CW_MSG_DISCOVERY_REQUEST
                                             && a->msg_id != CW_MSG_JOIN_REQUEST) )) {
            cw_dbg(DBG_ELEM_ERR, "Radio not found %d", rid);
            return 0;
        }
        mbag_i_set_mbag(conn->radios,rid,mbag_create());
    }



    return 1;
}
Beispiel #12
0
static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
			    struct sockaddr *from)
{
	struct cw_action_in as, *af, *afm;

	int offset = cw_get_hdr_msg_offset(rawmsg);

	uint8_t *msg_ptr = rawmsg + offset;

	int elems_len = cw_get_msg_elems_len(msg_ptr);

	int payloadlen = len - offset;


	/* pre-check message */
	if (payloadlen - 8 != elems_len) {

		if (conn->strict_hdr) {
			cw_dbg(DBG_MSG_ERR,
			       "Discarding message from %s, msgelems len=%d, payload len=%d, (Strict CAPWAP) ",
			       sock_addr2str(&conn->addr), elems_len, payloadlen - 8);
			errno = EAGAIN;
			return -1;
		}


		if (elems_len < payloadlen - 8) {
			cw_dbg(DBG_RFC,
			       "Packet from from %s has %d bytes of extra data, ignoring.",
			       sock_addr2str(&conn->addr), payloadlen - 8 - elems_len);
			elems_len = len - 8;
		}

		if (elems_len > payloadlen - 8) {

			cw_dbg(DBG_RFC,
			       "Packet from from %s has msgelems len of %d bytes, but has only %d bytes of data, truncating.",
			       sock_addr2str(&conn->addr), elems_len, payloadlen - 8);
			elems_len = payloadlen - 8;
		}
	}



	if (!conn->detected) {
		//struct mod_ac *mod;
		struct cw_actiondef *ad = load_mods(conn, rawmsg, len, elems_len, from);
		if (!ad) {
			cw_log(LOG_ERR, "Error");
			errno = EAGAIN;
			return -1;
		}
		conn->actions = ad;
		conn->detected = 1;

	}



	/* prepare struct for search operation */
	as.capwap_state = conn->capwap_state;
	as.msg_id = cw_get_msg_id(msg_ptr);
	as.vendor_id = 0;
	as.elem_id = 0;
	as.proto = 0;


	/* Search for state/message combination */
	afm = cw_actionlist_in_get(conn->actions->in, &as);

	if (!afm) {
		/* Throw away unexpected response messages */
		if (!(as.msg_id & 1)) {
			cw_dbg(DBG_MSG_ERR,
			       "Message type %d (%s) unexpected/illegal in %s State, discarding.",
			       as.msg_id, cw_strmsg(as.msg_id),
			       cw_strstate(conn->capwap_state));
			errno = EAGAIN;
			return -1;
		}

		/* Request message not found in current state, check if we know 
		   anything else about this message type */
		const char *str = cw_strheap_get(conn->actions->strmsg, as.msg_id);
		int result_code = 0;
		if (str) {
			/* Message found, but it was in wrong state */
			cw_dbg(DBG_MSG_ERR,
			       "Message type %d (%s) not allowed in %s State.", as.msg_id,
			       cw_strmsg(as.msg_id), cw_strstate(as.capwap_state));
			result_code = CW_RESULT_MSG_INVALID_IN_CURRENT_STATE;
		} else {
			/* Message is unknown */
			cw_dbg(DBG_MSG_ERR, "Message type %d (%s) unknown.",
			       as.msg_id, cw_strmsg(as.msg_id),
			       cw_strstate(as.capwap_state));
			result_code = CW_RESULT_MSG_UNRECOGNIZED;

		}
		cw_send_error_response(conn, rawmsg, result_code);
		errno = EAGAIN;
		return -1;
	}

	
	if (conn->msg_start){
		conn->msg_start(conn, afm, rawmsg, len, from);
	}

	/* Execute start processor for message */
	if (afm->start) {
		afm->start(conn, afm, rawmsg, len, from);
	}

	uint8_t *elems_ptr = cw_get_msg_elems_ptr(msg_ptr);
	uint8_t *elem;

	/* Create an avltree to catch the found mandatory elements */
	conn->mand = stravltree_create();

	int unrecognized = 0;

	/* iterate through message elements */
	cw_foreach_elem(elem, elems_ptr, elems_len) {

		as.elem_id = cw_get_elem_id(elem);
		int elem_len = cw_get_elem_len(elem);


		af = cw_actionlist_in_get(conn->actions->in, &as);

		if (!af) {
			unrecognized++;
			cw_dbg(DBG_ELEM_ERR,
			       "Element %d (%s) not allowed in msg of type %d (%s), ignoring.",
			       as.elem_id, cw_strelemp(conn->actions, as.elem_id),
			       as.msg_id, cw_strmsg(as.msg_id));
			continue;
		}

		if (!check_len(conn, af, cw_get_elem_data(elem), elem_len, from)) {
			continue;
		}
		cw_dbg_elem(DBG_ELEM, conn, as.msg_id, as.elem_id, cw_get_elem_data(elem),
			    elem_len);



		int afrc = 1;
		if (af->start) {
			afrc =
			    af->start(conn, af, cw_get_elem_data(elem), elem_len, from);

		}

		if (af->mand && afrc) {
			/* add found mandatory message element 
			   to mand list */
			stravltree_add(conn->mand, af->item_id);
		}

		if(conn->elem_end){
			afrc = conn->elem_end(conn,af,afrc,cw_get_elem_data(elem), elem_len,from);
		}

	}
Beispiel #13
0
/*!
  \fn char * print_temp_b_sec8 (char **sec8, size_t lmax, struct temp_chunks *t)
  \brief Prints the section 8 of part B of a TEMP report
  \param sec8 the pointer where to print section
  \param lmax max length permited
  \param t pointer to s atruct \ref temp_chunks where the parse results are set

  returns the string sec8
*/
char * print_temp_b_sec8 ( char **sec8, size_t lmax, struct temp_chunks *t )
{
  char *c = *sec8, *c0 = *sec8;

  if ( check_len ( sec8, 12 ) && t->b.s8.h[0] )
    {
      c += sprintf ( c, " 41414 " );
      if ( t->b.s8.Nh[0] )
        {
          c += sprintf ( c, "%s", t->b.s8.Nh );
        }
      else
        {
          c += sprintf ( c, "/" );
        }

      if ( t->b.s8.Cl[0] )
        {
          c += sprintf ( c, "%s", t->b.s8.Cl );
        }
      else
        {
          c += sprintf ( c, "/" );
        }

      if ( t->b.s8.h[0] )
        {
          c += sprintf ( c, "%s", t->b.s8.h );
        }
      else
        {
          c += sprintf ( c, "/" );
        }

      if ( t->b.s8.Cm[0] )
        {
          c += sprintf ( c, "%s", t->b.s8.Cm );
        }
      else
        {
          c += sprintf ( c, "/" );
        }

      if ( t->b.s8.Ch[0] )
        {
          c += sprintf ( c, "%s", t->b.s8.Ch );
        }
      else
        {
          c += sprintf ( c, "/" );
        }

      //c += sprintf ( c, " %s%s%s%s%s", t->b.s8.Nh, t->b.s8.Cl, t->b.s8.h, t->b.s8.Cm, t->b.s8.Ch );
    }

  if ( c != c0 )
    {
      *sec8 = c;
    }
  return *sec8;
}
Beispiel #14
0
int main(int argc, char** argv) {

  int fd = -1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
  if(argc != 2){                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    printf("Usage: ./write [filename]\n");                                                                                                                                                                                                                                                                                                                                                                                                                                                 
    return 1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
  }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

  /* input key */
  printf("Enter key: ");
  char ekey[256];
  int elen;
  char filename[256];
  strcpy(filename, argv[1]);
  int j = 0, c;
  while ((c = getc(stdin)) != EOF) {
    if (c == '\n')
      break;
    ekey[j++] = c;
  }
  //  fgets(ekey, 256, stdin);                                                                                                                                                                                                                                                                                                                                                                                                                                                             
  if ((elen = strlen(ekey)) == 0) {
    printf("The key is empty\n");
    return;
  }

  //  printf("%s\n", ekey);                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  //  printf("%d\n", elen);                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  pid_t pid = getpid();
  //  printf("pid: %d\n", pid);                                                                                                                                                                                                                                                                                                                                                                                                                                                            

  /* eopen file */
  if ((fd = syscall(NR_eopen, ekey, elen, filename, O_RDWR|O_CREAT, 0640, pid)) == -1) {
    perror("open");
    return 2;
  } else {
    //    printf("open success\n");                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  }

  /* user input */
  /*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  char *userinput;                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  int inputc = 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  int iter = 1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  userinput = malloc(sizeof(char)*BUF_SIZE);                                                                                                                                                                                                                                                                                                                                                                                                                                               
  while ((c = getc(stdin)) != EOF) {                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    if (c == '\n')                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
      break;                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    *(userinput+inputc) = c;                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    inputc++;                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    if (inputc == (BUF_SIZE * iter)) {                                                                                                                                                                                                                                                                                                                                                                                                                                                     
      iter++;                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
      userinput = (char *) realloc(userinput, BUF_SIZE * iter);                                                                                                                                                                                                                                                                                                                                                                                                                            
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
  }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  printf("\t\tUsr input length: %d\n", inputc);                                                                                                                                                                                                                                                                                                                                                                                                                                            
  printf("\t\t%s\n", userinput);                                                                                                                                                                                                                                                                                                                                                                                                                                                           
  */

  const int filelen = check_len(fd, 0);
  //  printf("file lenght is: %d\n", filelen);                                                                                                                                                                                                                                                                                                                                                                                                                                             
  /*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  char input[filelen+BUF_SIZE];                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  memset(input, '\0', filelen+BUF_SIZE);                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  lseek(fd, 0, SEEK_SET);                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  int retlen = read(fd, input, filelen);                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  */
  int wmode = 0;
  while (!wmode) {
    printf("Select writing mode: \n");
    printf("1. Sequential write\n");
    printf("2. Random access\n");
    printf("Your option: ");
    scanf("%d", &wmode);
    getchar();
    if (wmode == 1) { /* Sequential write */
      /* User input */
      printf("Write something: ");
      char *userinput;
      int inputc = 0;
      int iter = 1;
      userinput = malloc(sizeof(char)*BUF_SIZE);
      while ((c = getc(stdin)) != EOF) {
        *(userinput+inputc) = c;
        inputc++;
        if (inputc == (BUF_SIZE * iter)) {
          iter++;
          userinput = (char *) realloc(userinput, BUF_SIZE * iter);
        }
      }
      //      printf("\t\tUsr input length: %d\n", inputc);                                                                                                                                                                                                                                                                                                                                                                                                                                
      //      printf("\t\t%s\n", userinput);                                                                                                                                                                                                                                                                                                                                                                                                                                               

      /* Copy user input to input array */
      int i = filelen, ch;
      char input[filelen + inputc];
      lseek(fd, 0, SEEK_SET);
      int retlen = read(fd, input, filelen);
      //      printf("Original len: %d\n", retlen);                                                                                                                                                                                                                                                                                                                                                                                                                                        
      //      printf("Original string: %s\n", input);                                                                                                                                                                                                                                                                                                                                                                                                                                      
      for (j = 0; j < inputc; j++) {
        input[i++] = *(userinput+j);
      }

      //      printf("%s\n", input);                                                                                                                                                                                                                                                                                                                                                                                                                                                       
      //      printf("i is %d\n", i);                                                                                                                                                                                                                                                                                                                                                                                                                                                      

      /* write */
      int writelength;
      lseek(fd, 0, SEEK_SET); // re-write the whole file                                                                                                                                                                                                                                                                                                                                                                                                                                   
      if ((writelength = write(fd, input, i)) != i)
        printf("WRITIG ERROR\n");
      //      printf("%d\n",writelength);                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    }

    else if (wmode == 2) { /* Random access */
      printf("Enter offset (if offset exceeds file length, append after the end of the file): ");
      int offset;
      scanf("%d", &offset);
      getchar();
      if(offset > filelen) /* if offset exceeds file length, append after the end */
        offset = filelen;

      /* User input */
      printf("Write something: ");
      char *userinput;
      int inputc = 0;
      int iter = 1;
      userinput = malloc(sizeof(char)*BUF_SIZE);
      while ((c = getc(stdin)) != EOF) {
        *(userinput+inputc) = c;
        inputc++;
        if (inputc == (BUF_SIZE * iter)) {
          iter++;
          userinput = (char *) realloc(userinput, BUF_SIZE * iter);
        }
      }
      //      printf("\t\tUsr input length: %d\n", inputc);                                                                                                                                                                                                                                                                                                                                                                                                                                
      //      printf("\t\t%s\n", userinput);                                                                                                                                                                                                                                                                                                                                                                                                                                               

      /* Copy user input to input array */
      char input[filelen + inputc];
      lseek(fd, 0, SEEK_SET);
      int retlen = read(fd, input, filelen);
      //      printf("Original len: %d\n", retlen);                                                                                                                                                                                                                                                                                                                                                                                                                                        
      //      printf("Original string: %s\n", input);                                                                                                                                                                                                                                                                                                                                                                                                                                      

      /* get the string after offset */
      int remain_len = check_len(fd, offset);
      char str[remain_len];
      for (j=offset; j<filelen; j++) {
        str[j-offset] = input[j];
      }
      //      printf("%s\n", str);                                                                                                                                                                                                                                                                                                                                                                                                                                                         


      int i = offset, ch;
      for (j = 0; j < inputc; j++) {
        input[i++] = *(userinput+j);
      }
      /*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
      while ((ch = getc(stdin)) != EOF) {                                                                                                                                                                                                                                                                                                                                                                                                                                                  
        input[i++] = ch; /* starting writing from offset                                                                                                                                                                                                                                                                                                                                                                                                                                   
      }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
      */



      /* move the string after offset backwards */
      for (j = 0; j < remain_len; j++) {
        input[i++] = str[j];
      }
      //      printf("%s\n", input);                                                                                                                                                                                                                                                                                                                                                                                                                                                       
      //      printf("i is %d\n", i);                                                                                                                                                                                                                                                                                                                                                                                                                                                      

      /* write */
      int writelength;
      lseek(fd, 0, SEEK_SET); // re-write the whole file                                                                                                                                                                                                                                                                                                                                                                                                                                   
      if ((writelength = write(fd, input, i)) != i)
        printf("WRITIG ERROR\n");
      printf("%d\n",writelength);
    } else { /* Wrong selection */
      printf("Input error. Retry!\n");
      wmode = 0;
    }
  }

  /* close the file */
  if (syscall(NR_eclose, fd) == -1) {
    perror("close");
    return 2;
  } else
    printf("\nSuccessfully close\n");

  return 0;
}
Beispiel #15
0
int main(void)
{
	struct sockaddr_in server;
    struct timeval tv;
	int s, i, count_servers, file = -1;
    message_t msg;
    server_data_t server_data;
    list_data_server_t list_data_server;
	socklen_t slen = sizeof(server);
    char *filename;
    off_t flen, recv_len;
    fd_set rfds;
    
    filename = malloc(sizeof(char) * 30);
    
	if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
		fprintf(stderr, "Error create sock");
	}

    server_data.main_port = 2009;
    strcpy(server_data.main_ip_address, "127.0.0.1");
    
	bzero(&server, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(server_data.main_port);
	server.sin_addr.s_addr = inet_addr(server_data.main_ip_address);
    
    printf("Hello, wating conecting to server ...\n");
    
    get_msg("Conect client", &msg, &server_data);
	sendto(s, &msg, sizeof(msg), 0, (struct sockaddr *)&server, sizeof(server));
    
    recvfrom(s, &count_servers, sizeof(int), 0, (struct sockaddr*)&server, &slen);
    printf("Count servers     :%d\n", count_servers);
    recvfrom(s, &list_data_server, sizeof(list_data_server), 0, (struct sockaddr*)&server, &slen);
    for (i = 0; i < count_servers; i++) {
        printf("%d) %s:%d\n", i + 1, list_data_server.s_addr[i], list_data_server.s_port[i]);
    }
    
    while (1) {
        recvfrom(s, &msg, sizeof(msg), 0, (struct sockaddr*)&server, &slen);
        if (0 == strcmp("File open good", msg.buf))
            break;
        printf("%s ", msg.buf);
        scanf("%s", filename);
    
        get_msg(filename, &msg, &server_data);
        sendto(s, &msg, sizeof(msg), 0, (struct sockaddr*)&server, sizeof(server));
    }
    
    recvfrom(s, &flen, sizeof(flen), 0, (struct sockaddr*)&server, &slen);

    file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (file < 0)
        printf("Error open file\n");
    else
        printf("File %s open\n", filename);
    
    msg.size = sizeof(msg.buf);
    
    fprintf(stdout, "Start download ...\n [");
    fflush (stdout);
    
    tv.tv_sec = 6;
    tv.tv_usec = 0;
    
    FD_ZERO(&rfds);
    FD_SET(s, &rfds);
    
    while (1) {
        bzero(&msg.buf, sizeof(msg.buf));
        select(s + 1, &rfds, NULL, NULL, &tv);
        if (FD_ISSET(s, &rfds)){
            recvfrom(s, &msg, sizeof(msg), 0, (struct sockaddr*)&server, &slen);
            recv_len += sizeof(msg.buf);
            check_len(flen, &recv_len);
            if (msg.size == 512) {
                write(file, &msg.buf, sizeof(msg.buf));
            } else {
                write(file, &msg.buf, msg.size);
                break;
            }
        } else {
            fprintf(stderr, "\nError connect\n");
            exit(-1);
        }
    }
    printf("]\nDownload finish\n");
    close(s);
	return 0;
}