コード例 #1
0
ファイル: FileStore.cpp プロジェクト: ifzz/FDK
void FileStore::populateCache()
{

  std::string msg;
  Message message;

  FILE* headerFile;
  headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
  if ( headerFile )
  {
    int num, offset, size;
    while ( FILE_FSCANF( headerFile, "%d,%d,%d ", &num, &offset, &size ) == 3 )
      m_offsets[ num ] = std::make_pair( offset, size );
    fclose( headerFile );
  }

  FILE* seqNumsFile;
  seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
  if ( seqNumsFile )
  {
    int sender, target;
    if ( FILE_FSCANF( seqNumsFile, "%d : %d", &sender, &target ) == 2 )
    {
      m_cache.setNextSenderMsgSeqNum( sender );
      m_cache.setNextTargetMsgSeqNum( target );
    }
    fclose( seqNumsFile );
  }

  FILE* sessionFile;
  sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
  if ( sessionFile )
  {
    char time[ 22 ];
#ifdef HAVE_FSCANF_S
    int result = FILE_FSCANF( sessionFile, "%s", time, 22 );
#else
    int result = FILE_FSCANF( sessionFile, "%s", time );
#endif
    if( result == 1 )
    {
      m_cache.setCreationTime( UtcTimeStampConvertor::convert( time, true ) );
    }
    fclose( sessionFile );
  }

  
}
コード例 #2
0
ファイル: huge.c プロジェクト: lucab/gtk-gnutella
/**
 * Add an entry to the persistent cache.
 */
static void
add_persistent_cache_entry(const char *filename, filesize_t size,
	time_t mtime, const struct sha1 *sha1, const struct tth *tth)
{
	char *pathname;
	FILE *f;

	pathname = make_pathname(settings_config_dir(), "sha1_cache");
	f = file_fopen(pathname, "a");
	if (f) {
		filestat_t sb;

		/*
		 * If we're adding the very first entry (file empty), then emit header.
		 */

		if (fstat(fileno(f), &sb)) {
			g_warning("%s(): could not stat \"%s\": %m", G_STRFUNC, pathname);
		} else {
			if (0 == sb.st_size) {
				fputs(sha1_persistent_cache_file_header, f);
			}
			cache_entry_print(f, filename, sha1, tth, size, mtime);
		}
		fclose(f);
	} else {
		g_warning("%s(): could not open \"%s\": %m", G_STRFUNC, pathname);
	}
	HFREE_NULL(pathname);
}
コード例 #3
0
ファイル: bss_file.c プロジェクト: 4ker/openssl
BIO *BIO_new_file(const char *filename, const char *mode)
{
    BIO  *ret;
    FILE *file = file_fopen(filename, mode);
    int fp_flags = BIO_CLOSE;

    if (strchr(mode, 'b') == NULL)
        fp_flags |= BIO_FP_TEXT;

    if (file == NULL) {
        SYSerr(SYS_F_FOPEN, get_last_sys_error());
        ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
        if (errno == ENOENT)
            BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
        else
            BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
        return (NULL);
    }
    if ((ret = BIO_new(BIO_s_file())) == NULL) {
        fclose(file);
        return (NULL);
    }

    BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
                                             * UPLINK */
    BIO_set_fp(ret, file, fp_flags);
    return (ret);
}
コード例 #4
0
ファイル: FileStore.cpp プロジェクト: ifzz/FDK
void FileStore::open( bool deleteFile )
{

  if ( m_msgFile ) fclose( m_msgFile );
  if ( m_headerFile ) fclose( m_headerFile );
  if ( m_seqNumsFile ) fclose( m_seqNumsFile );
  if ( m_sessionFile ) fclose( m_sessionFile );

  m_msgFile = 0;
  m_headerFile = 0;
  m_seqNumsFile = 0;
  m_sessionFile = 0;

  if ( deleteFile )
  {
    file_unlink( m_msgFileName.c_str() );
    file_unlink( m_headerFileName.c_str() );
    file_unlink( m_seqNumsFileName.c_str() );
    file_unlink( m_sessionFileName.c_str() );
  }

  populateCache();
  m_msgFile = file_fopen( m_msgFileName.c_str(), "r+" );
  if ( !m_msgFile ) m_msgFile = file_fopen( m_msgFileName.c_str(), "w+" );
  if ( !m_msgFile ) throw ConfigError( "Could not open body file: " + m_msgFileName );

  m_headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
  if ( !m_headerFile ) m_headerFile = file_fopen( m_headerFileName.c_str(), "w+" );
  if ( !m_headerFile ) throw ConfigError( "Could not open header file: " + m_headerFileName );

  m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
  if ( !m_seqNumsFile ) m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "w+" );
  if ( !m_seqNumsFile ) throw ConfigError( "Could not open seqnums file: " + m_seqNumsFileName );

  bool setCreationTime = false;
  m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r" );
  if ( !m_sessionFile ) setCreationTime = true;
  else fclose( m_sessionFile );

  m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
  if ( !m_sessionFile ) m_sessionFile = file_fopen( m_sessionFileName.c_str(), "w+" );
  if ( !m_sessionFile ) throw ConfigError( "Could not open session file" );
  if ( setCreationTime ) setSession();

  setNextSenderMsgSeqNum( getNextSenderMsgSeqNum() );
  setNextTargetMsgSeqNum( getNextTargetMsgSeqNum() );

  
}
コード例 #5
0
ファイル: main.c プロジェクト: ece492-w16-g7/ece492_w16
/* The audio task is responsible for continuously playing music
 * off the SD card.
 */
void audio_task(void *pdata) {
	int err = 0;

	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_ACTIVE_CTRL, 0x00);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_LEFT_LINE_IN, 0x97);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_RIGHT_LINE_IN, 0x97);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_LEFT_HEADPHONE_OUT, 0x79);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_RIGHT_HEADPHONE_OUT, 0x79);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_ANALOG_AUDIO_PATH_CTRL, 0x12);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_DIGITAL_AUDIO_PATH_CTRL, 0x05);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_POWER_DOWN_CTRL, 0x07);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_AUDIO_DIGITAL_INTERFACE, 0x42);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_SAMPLING_CTRL, 0x22);
	err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_ACTIVE_CTRL, 0x01);
	err += audio_set_headphone_volume(av_dev, DEFAULT_VOLUME);

	if(err < 0)
		printf("Audio Configuration Failed\n");

	alt_up_audio_reset_audio_core(audio_dev);
	alt_up_audio_disable_read_interrupt(audio_dev);
	alt_up_audio_disable_write_interrupt(audio_dev);

	char fileName[10] = {"class.wav\0"};
	if (file_fopen(&readFile, &efsl.myFs, fileName, 'r') != 0)
		printf("Error:\tCould not open file\n");

	int readSize = 0;
	euint32 currentSize = 44;

	euint8 buf[AUDIO_BUF_SIZE];
	int i;

	/* The task is suspended so that it can be played by another task. */
	OSTaskSuspend(AUDIO_TASK_PRIORITY);
	while(1) {
		if (currentSize < readFile.FileSize) {
			int fifospace = alt_up_audio_write_fifo_space(audio_dev, ALT_UP_AUDIO_LEFT);
			if (fifospace > WORD_COUNT) {
				readSize = file_fread(&readFile, currentSize, AUDIO_BUF_SIZE, buf);
				currentSize += readSize;
				i = 0;
				while(i < AUDIO_BUF_SIZE) {
					IOWR_ALT_UP_AUDIO_LEFTDATA(audio_dev->base, (buf[i+1]<<8)|buf[i]);
					IOWR_ALT_UP_AUDIO_RIGHTDATA(audio_dev->base, (buf[i+3]<<8)|buf[i+2]);

					i+=4;
				}
			}
		} else {
			currentSize = 44;
		}
	}

	file_fclose(&readFile);
}
コード例 #6
0
ファイル: tftpserver.c プロジェクト: fanqh/ETH_Mange
int tftp_process_read(struct udp_pcb *upcb, struct ip_addr *to, int to_port, char* FileName)
{
  tftp_connection_args *args = NULL;

  /* If Could not open the file which will be transmitted  */
  if (file_fopen(&file_SD, &efs1.myFs, FileName, 'r') != 0)
  {
    tftp_send_error_message(upcb, to, to_port, TFTP_ERR_FILE_NOT_FOUND);

    tftp_cleanup_rd(upcb, args);

    return 0;
  }

  /* This function is called from a callback,
   * therefore, interrupts are disabled,
   * therefore, we can use regular malloc. */

  args = mem_malloc(sizeof *args);
  /* If we aren't able to allocate memory for a "tftp_connection_args" */
  if (!args)
  {
    /* unable to allocate memory for tftp args  */
    tftp_send_error_message(upcb, to, to_port, TFTP_ERR_NOTDEFINED);

    /* no need to use tftp_cleanup_rd because no 
            "tftp_connection_args" struct has been malloc'd   */
    tftp_cleanup_rd(upcb, args);

    return 0;
  }

  /* initialize connection structure  */
  args->op = TFTP_RRQ;
  args->to_ip.addr = to->addr;
  args->to_port = to_port;
  args->block = 1; /* block number starts at 1 (not 0) according to RFC1350  */
  args->tot_bytes = 0;


  /* set callback for receives on this UDP PCB (Protocol Control Block) */
  udp_recv(upcb, rrq_recv_callback, args);

  /* initiate the transaction by sending the first block of data
   * further blocks will be sent when ACKs are received
   *   - the receive callbacks need to get the proper state    */

  tftp_send_next_block(upcb, args, to, to_port);

  return 1;
}
コード例 #7
0
ファイル: hookers.c プロジェクト: ScriptBasic/ScriptBasic
/*FUNCTION*/
FILE *hook_fopen(pExecuteObject pEo,
                 char *pszFileName,
                 char *pszOpenMode
  ){
/*noverbatim
CUT*/
  int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);

  if( iAccessPermission == 0 )return NULL;
  if( (iAccessPermission&1 && *pszOpenMode == 'r') ||
      (iAccessPermission&2 && ( *pszOpenMode == 'w' || *pszOpenMode == 'a' )) )
    return file_fopen(pszFileName,pszOpenMode);
  return NULL;
  }
コード例 #8
0
ファイル: cpa.c プロジェクト: m3y54m/32bitmicro
int main(int argc, char** argv)
{
	EmbeddedFileSystem efs;
	EmbeddedFile file;	
	unsigned short e;
	unsigned short bufsize;
	signed short ret;
	char *buf;
	FILE *localfile;
	
	if(argc<4){
		fprintf(stderr,"Argument error : cpa <fs> <local_read> <file_append> [bufsize]\n");
		exit(-1);
	}

	if(argc==5)
		bufsize=atoi(argv[4]);
	else
		bufsize=4096;
	buf=malloc(bufsize);
	
	if(efs_init(&efs,argv[1])!=0){
		printf("Could not open filesystem.\n");
		return(-1);
	}
	
	ret=file_fopen(&file,&efs.myFs,argv[3],'a');
	if((ret!=0)){
		printf("Could not open file %s (return value: %d).\n",argv[3],ret);
		return(-2);
	}

	if(!(localfile=fopen(argv[2],"r"))){
		printf("Could not open local file.\n");
		return(-3);
	}
	
	while((e=fread(buf,1,bufsize,localfile))){
		file_write(&file,e,buf);
	}

	file_fclose(&file);
	fclose(localfile);
	fs_umount(&(efs.myFs));

	return(0);
}
コード例 #9
0
ファイル: file.c プロジェクト: MrJoe/gtk-gnutella
/**
 * Open configuration file for writing.  We don't clobber the existing file
 * yet and open a ".new" instead.  Renaming will occur afterwards, when
 * file_config_close() is called.
 *
 * @returns opened FILE if success, NULL on error.
 */
static FILE *
file_config_open(const char *what, const file_path_t *fv)
{
	FILE *out = NULL;
	char *path;

	path = h_strconcat(fv->dir, G_DIR_SEPARATOR_S, fv->name, ".",
				new_ext, (void *) 0);
	g_return_val_if_fail(NULL != path, NULL);

	if (is_absolute_path(path)) {
		out = file_fopen(path, "w");
		if (out == NULL)
			s_warning("unable to persist %s", what);
	}
	HFREE_NULL(path);
	return out;
}
コード例 #10
0
ファイル: bogons.c プロジェクト: qgewfg/gtk-gnutella
/**
 * Watcher callback, invoked when the file from which we read the bogus
 * addresses changed.
 */
static void
bogons_changed(const char *filename, void *unused_udata)
{
	FILE *f;
	char buf[80];
	int count;

	(void) unused_udata;

	f = file_fopen(filename, "r");
	if (f == NULL)
		return;

	bogons_close();
	count = bogons_load(f);

	gm_snprintf(buf, sizeof(buf), "Reloaded %d bogus IP ranges.", count);
	gcu_statusbar_message(buf);
}
コード例 #11
0
ファイル: sdcard-arch.c プロジェクト: gnkarn/Contiki
int
cfs_open (const char *name, int flags)
{
  eint8 mode;
  int fd;
  if (sdcard_efs.myCard.sectorCount == 0) return -1;
  fd = find_free_fd();
  if (fd < 0) return -1;
  if (flags == CFS_READ) {
    mode = MODE_READ;
  } else {
    mode = MODE_APPEND;
  }
  if (file_fopen(&file_descriptors[fd], &sdcard_efs.myFs,
		 (char*)name, mode) < 0) {
    return -1;
  }
  return fd;
}
コード例 #12
0
ファイル: filesystem.c プロジェクト: BigEd/pyldin
static int wrap_fs_open(struct _reent *r, const char *pathname, int flags, int mode)
{
    wrap_fs_check_is_inited();
    mode = mode;
    int fd = wrap_fs_get_fd();
    if (fd >= 0) {
	euint8 m = 0;
	flags &= 0xffff;
	if (flags == O_RDONLY)
	    m = MODE_READ;
	else if (flags & O_APPEND)
	    m = MODE_APPEND;
	else if (flags & O_WRONLY) {
	    if ((flags & O_CREAT) || (flags & O_TRUNC))
		fs_rmfile(fs, (euint8*)pathname);
	    m = MODE_WRITE;
	}

	fprintf(stderr, "open(%s) flag: %08X mode: %c\n", pathname, flags, m);
	if (!m) {
	    fprintf(stderr, "unknown mode\n");
	    r->_errno = EACCES;
	    return -1;
	}

	fd_list[fd].file = (File *)malloc(sizeof(File));
	if (!fd_list[fd].file) {
	    fprintf(stderr, "No memory!\n");
	    r->_errno = ENOMEM;
	    return -1;
	}
	if ( (file_fopen(fd_list[fd].file, fs, (esint8*)pathname, m)) != 0) {
	    fprintf(stderr, "Unable to open the file\n");
	    r->_errno = EACCES;
	    return(-1);
	}
	fd_list[fd].used = 1;
	return fd;
    }
    r->_errno = ENFILE;
    return -1;
}
コード例 #13
0
ファイル: tftpserver.c プロジェクト: fanqh/ETH_Mange
int tftp_process_write(struct udp_pcb *upcb, struct ip_addr *to, int to_port, char *FileName)
{
  tftp_connection_args *args = NULL;

  /* If Could not open the file which will be transmitted  */
  if (file_fopen(&file_CR, &efs2.myFs, FileName, 'w') != 0)
  {
    tftp_send_error_message(upcb, to, to_port, TFTP_ERR_FILE_ALREADY_EXISTS);

    tftp_cleanup_wr(upcb, args);

    return 0;
  }

  /* This function is called from a callback,
   * therefore interrupts are disabled,
   * therefore we can use regular malloc   */
  args = mem_malloc(sizeof *args);
  if (!args)
  {
    tftp_send_error_message(upcb, to, to_port, TFTP_ERR_NOTDEFINED);

    tftp_cleanup_wr(upcb, args);

    return 0;
  }

  args->op = TFTP_WRQ;
  args->to_ip.addr = to->addr;
  args->to_port = to_port;
  /* the block # used as a positive response to a WRQ is _always_ 0!!! (see RFC1350)  */
  args->block = 0;
  args->tot_bytes = 0;

  /* set callback for receives on this UDP PCB (Protocol Control Block) */
  udp_recv(upcb, wrq_recv_callback, args);

  /* initiate the write transaction by sending the first ack */
  tftp_send_ack_packet(upcb, to, to_port, args->block);

  return 0;
}
コード例 #14
0
ファイル: geo_ip.c プロジェクト: MrJoe/gtk-gnutella
/**
 * Watcher callback, invoked when the file from which we read the
 * geographic IP mappings changed.
 */
static void
gip_changed(const char *filename, void *idx_ptr)
{
	FILE *f;
	char buf[80];
	uint count;
	unsigned idx = pointer_to_uint(idx_ptr);

	f = file_fopen(filename, "r");
	if (f == NULL)
		return;

	count = gip_load(f, idx);
	fclose(f);

	str_bprintf(buf, sizeof buf, "Reloaded %u geographic IPv%c ranges.",
		count, GIP_IPV4 == idx ? '4' : '6');

	gcu_statusbar_message(buf);
}
コード例 #15
0
ファイル: spam.c プロジェクト: Haxe/gtk-gnutella
/**
 * Watcher callback, invoked when the file from which we read the spam 
 * changed.
 */
static void
spam_changed(const char *filename, gpointer unused_udata)
{
	FILE *f;

	(void) unused_udata;

	f = file_fopen(filename, "r");
	if (f) {
		char buf[80];
		gulong count;

		spam_close();
		count = spam_load(f);
		fclose(f);

		gm_snprintf(buf, sizeof(buf), "Reloaded %lu spam items.", count);
		gcu_statusbar_message(buf);
	}
}
コード例 #16
0
ファイル: efs.c プロジェクト: samkrew/nxp-lpc
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_open(struct dfs_fd* file)
{
	File* efsfile;
	DirList* efsdir;
	efsl_fs* efsfs = (efsl_fs*)(file->fs->data);
	int result = 0;
	
	/* parameter check */
	if ( file == RT_NULL || file->fs == RT_NULL || 
		file->fs->data == RT_NULL ) 
	{	
		dfs_log(DFS_DEBUG_INFO, ("Invalid argument"));		
		return -DFS_STATUS_EINVAL;
	}

	/* open directory */
	if(file->flags & DFS_O_DIRECTORY)
	{
		/* write directory is not supported */
		if(file->flags & DFS_O_WRONLY || file->flags & DFS_O_RDWR)
		{
			dfs_log(DFS_DEBUG_INFO, ("write directory isn't supported"));			
			return -DFS_STATUS_EISDIR;
		}
		
		/* create directory */
		if(file->flags & DFS_O_CREAT)
		{
			dfs_log(DFS_DEBUG_INFO, ("create directory"));
			result = mk_dir(&efsfs->filesystem, file->path);
			if(result < 0) 
			{
				dfs_log(DFS_DEBUG_INFO, ("directory %s has existed", file->path));
				return -DFS_STATUS_EEXIST;
			}	
		}
		
		efsdir = (DirList*)rt_malloc(sizeof(DirList));
		if(efsdir == RT_NULL) 
		{
			dfs_log(DFS_DEBUG_INFO, ("memory alloc failed"));
			return -DFS_STATUS_ENOMEM;		
		}
		
		result = ls_openDir(efsdir, &efsfs->filesystem, file->path);
		if(result < 0)
		{
			dfs_log(DFS_DEBUG_INFO, ("open directory %s failed", file->path));
			rt_free(efsdir);
		}	
		else 
		{
			file->data = efsdir;
		}	
	}
	/* open file */
	else
	{
		efsfile = (File *)rt_malloc(sizeof(File));
		if (efsfile == RT_NULL) 
		{
			dfs_log(DFS_DEBUG_INFO, ("memory alloc failed"));			
			return -DFS_STATUS_ENOMEM;		
		}

		result = file_fopen(efsfile, &efsfs->filesystem, file->path, file->flags);
		if(result < 0)	
		{	
			dfs_log(DFS_DEBUG_INFO, ("open file %s failed", file->path));
			rt_free(efsfile);
		}	
		else
		{
			file->pos  = efsfile->FilePtr;
			file->size = efsfile->FileSize;
			file->data = efsfile;
		}
	}

	return result;
}
コード例 #17
0
ファイル: bss_file.c プロジェクト: JackDanger/openssl
static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
{
    long ret = 1;
    FILE *fp = (FILE *)b->ptr;
    FILE **fpp;
    char p[4];

    switch (cmd) {
    case BIO_C_FILE_SEEK:
    case BIO_CTRL_RESET:
        if (b->flags & BIO_FLAGS_UPLINK)
            ret = (long)UP_fseek(b->ptr, num, 0);
        else
            ret = (long)fseek(fp, num, 0);
        break;
    case BIO_CTRL_EOF:
        if (b->flags & BIO_FLAGS_UPLINK)
            ret = (long)UP_feof(fp);
        else
            ret = (long)feof(fp);
        break;
    case BIO_C_FILE_TELL:
    case BIO_CTRL_INFO:
        if (b->flags & BIO_FLAGS_UPLINK)
            ret = UP_ftell(b->ptr);
        else
            ret = ftell(fp);
        break;
    case BIO_C_SET_FILE_PTR:
        file_free(b);
        b->shutdown = (int)num & BIO_CLOSE;
        b->ptr = ptr;
        b->init = 1;
#  if BIO_FLAGS_UPLINK!=0
#   if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
#    define _IOB_ENTRIES 20
#   endif
#   if defined(_IOB_ENTRIES)
        /* Safety net to catch purely internal BIO_set_fp calls */
        if ((size_t)ptr >= (size_t)stdin &&
            (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
            BIO_clear_flags(b, BIO_FLAGS_UPLINK);
#   endif
#  endif
#  ifdef UP_fsetmod
        if (b->flags & BIO_FLAGS_UPLINK)
            UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
        else
#  endif
        {
#  if defined(OPENSSL_SYS_WINDOWS)
            int fd = _fileno((FILE *)ptr);
            if (num & BIO_FP_TEXT)
                _setmode(fd, _O_TEXT);
            else
                _setmode(fd, _O_BINARY);
#  elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
            int fd = fileno((FILE *)ptr);
            /* Under CLib there are differences in file modes */
            if (num & BIO_FP_TEXT)
                setmode(fd, O_TEXT);
            else
                setmode(fd, O_BINARY);
#  elif defined(OPENSSL_SYS_MSDOS)
            int fd = fileno((FILE *)ptr);
            /* Set correct text/binary mode */
            if (num & BIO_FP_TEXT)
                _setmode(fd, _O_TEXT);
            /* Dangerous to set stdin/stdout to raw (unless redirected) */
            else {
                if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
                    if (isatty(fd) <= 0)
                        _setmode(fd, _O_BINARY);
                } else
                    _setmode(fd, _O_BINARY);
            }
#  elif defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
            int fd = fileno((FILE *)ptr);
            if (num & BIO_FP_TEXT)
                setmode(fd, O_TEXT);
            else
                setmode(fd, O_BINARY);
#  endif
        }
        break;
    case BIO_C_SET_FILENAME:
        file_free(b);
        b->shutdown = (int)num & BIO_CLOSE;
        if (num & BIO_FP_APPEND) {
            if (num & BIO_FP_READ)
                BUF_strlcpy(p, "a+", sizeof p);
            else
                BUF_strlcpy(p, "a", sizeof p);
        } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
            BUF_strlcpy(p, "r+", sizeof p);
        else if (num & BIO_FP_WRITE)
            BUF_strlcpy(p, "w", sizeof p);
        else if (num & BIO_FP_READ)
            BUF_strlcpy(p, "r", sizeof p);
        else {
            BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
            ret = 0;
            break;
        }
#  if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
        if (!(num & BIO_FP_TEXT))
            strcat(p, "b");
        else
            strcat(p, "t");
#  endif
#  if defined(OPENSSL_SYS_NETWARE)
        if (!(num & BIO_FP_TEXT))
            strcat(p, "b");
        else
            strcat(p, "t");
#  endif
        fp = file_fopen(ptr, p);
        if (fp == NULL) {
            SYSerr(SYS_F_FOPEN, get_last_sys_error());
            ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
            BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
            ret = 0;
            break;
        }
        b->ptr = fp;
        b->init = 1;
        BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
                                               * UPLINK */
        break;
    case BIO_C_GET_FILE_PTR:
        /* the ptr parameter is actually a FILE ** in this case. */
        if (ptr != NULL) {
            fpp = (FILE **)ptr;
            *fpp = (FILE *)b->ptr;
        }
        break;
    case BIO_CTRL_GET_CLOSE:
        ret = (long)b->shutdown;
        break;
    case BIO_CTRL_SET_CLOSE:
        b->shutdown = (int)num;
        break;
    case BIO_CTRL_FLUSH:
        if (b->flags & BIO_FLAGS_UPLINK)
            UP_fflush(b->ptr);
        else
            fflush((FILE *)b->ptr);
        break;
    case BIO_CTRL_DUP:
        ret = 1;
        break;

    case BIO_CTRL_WPENDING:
    case BIO_CTRL_PENDING:
    case BIO_CTRL_PUSH:
    case BIO_CTRL_POP:
    default:
        ret = 0;
        break;
    }
    return (ret);
}
コード例 #18
0
int main()
{
	int8_t res;
    uint32_t n, m, p, cnt;
    uint32_t cclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU);
    uint32_t filesize = 0;
    uint32_t time_end;

//	SystemInit();
    SysTick_Config(cclk/1000 - 1); /* Generate interrupt each 1 ms   */

	debug_frmwrk_init(); // UART0
    xfunc_out = put_char;
	xfunc_in  = get_char; 

    xprintf("%s",mciFsMenu);

	xprintf("\nMMC/SD Card Filesystem Test (P:LPC1788 L:EFSL)\n");

	xprintf("\nCARD init...");

	// init file system
	if ( ( res = efs_init( &efs, 0 ) ) != 0 ) {
		xprintf("failed with %d\n",res);
	}
	else 
	{
		xprintf("ok\n");

        xprintf("Card type: ");
        switch (CardConfig.CardType)
        {
            case MCI_MMC_CARD:
                xprintf("MMC\n");
                break;
            case MCI_SDSC_V1_CARD:
                xprintf("Version 1.x Standard Capacity SD card.\n");
                break;
            case MCI_SDSC_V2_CARD:
                xprintf("Version 2.0 or later Standard Capacity SD card.\n");
                break;
            case MCI_SDHC_SDXC_CARD:
                xprintf("Version 2.0 or later High/eXtended Capacity SD card.\n");
                break;
            default:
                break;            
        }
        xprintf("Sector size: %d bytes\n", CardConfig.SectorSize);
        xprintf("Sector count: %d\n", CardConfig.SectorCount);
        xprintf("Block size: %d sectors\n", CardConfig.BlockSize);
        xprintf("Card capacity: %d MByte\n\n", (((CardConfig.SectorCount >> 10) * CardConfig.SectorSize)) >> 10);
		xprintf("\nDirectory of 'root':\n");
		
		/* list files in root directory */
		ls_openDir( &list, &(efs.myFs) , "/");
		while ( ls_getNext( &list ) == 0 ) {
			// list.currentEntry is the current file
			list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0';
			xprintf("%s, 0x%x bytes\n", list.currentEntry.FileName, list.currentEntry.FileSize ) ;
		}
#if READ_TEST_ENABLED!=0
        /* Read test */
        xprintf("\nFile read test:\n");
        xprintf("Open file %s ...", FILE_NAME_R);
        xmemset(Buff,0,sizeof(Buff));
        if (file_fopen( &filer, &efs.myFs , FILE_NAME_R , 'r' ) == 0 )
        {
            xprintf(" OK. \nReading %lu bytes ...\n", FILE_RW_SIZE);

            n=FILE_RW_SIZE; 
            m = 0;
            Timer = 0;
            xprintf("File's content:\n");
            while (n)
            {
                if (n>=blen) {cnt = blen; n -= blen;}
                else         {cnt = n; n = 0;}

                p =  file_read( &filer, cnt, &Buff[m] );
                xprintf("%s",&Buff[m]);
                m += p;
                if (p != cnt) break;                
            }
            filesize = m;
            time_end = Timer;
            xprintf("\n%lu bytes read in %lu milisec.\n", m, time_end);
            file_fclose( &filer ); 

        } else
        {
            xprintf (" Failed.\n");    
        }
#endif
#if WRITE_TEST_ENABLED!=0
        /* Write test*/  
        xprintf("\nFile write test:\n");
        xprintf("Open file %s ...", FILE_NAME_W);
        if (file_fopen( &filew, &efs.myFs , FILE_NAME_W , 'a' ) == 0 )
        {
            xprintf(" OK. \nWriting %lu bytes ...\n", filesize);
            n=filesize;
            m = 0;
            Timer = 0;
            while (n)
            {
                if (n>=blen) {
                    cnt = blen;
                    n -= blen;
                } else {
                    cnt = n;
                    n = 0;
                }
                p = file_write( &filew, cnt, &Buff[m] );
                m += p;
                if (p != cnt) break;
            }
            time_end = Timer;
            xprintf("%lu bytes written in %lu milisec.\n", m, time_end);

            file_fclose( &filew );                          

        } else {
            xprintf (" Failed.\n");
        }
#endif
#if READ_TEST_ENABLED!=0
        /* Read test */
        xprintf("\nFile read test:\n");
        xprintf("Open file %s ...", FILE_NAME_W);
        xmemset(Buff,0,sizeof(Buff));
        if (file_fopen( &filer, &efs.myFs , FILE_NAME_W , 'r' ) == 0 )
        {
            xprintf(" OK. \nReading %lu bytes ...\n", FILE_RW_SIZE);

            n=FILE_RW_SIZE; 
            m = 0;
            Timer = 0;
            xprintf("File's content:\n");
            while (n)
            {
                if (n>=blen) {cnt = blen; n -= blen;}
                else         {cnt = n; n = 0;}

                p =  file_read( &filer, cnt, &Buff[m] );
                xprintf("%s",&Buff[m]);
                m += p;
                if (p != cnt) break;                
            }
            filesize = m;
            time_end = Timer;
            xprintf("\n%lu bytes read in %lumiliseconds.\n", m, time_end);
            file_fclose( &filer ); 

        } else
        {
            xprintf (" Failed.\n");    
        }
#endif
        /* close file system */
	    fs_umount( &efs.myFs ) ;
    }

	xprintf("\nEFSL test complete.\n");

	while (1);
}
コード例 #19
0
ファイル: shell_commands.c プロジェクト: swen3027/APB_R2
void write_file_cli(int argc, char** argv)
{
	char* filename;
	char a,b,c;
	int quit =1;
	signed char retval;
	File output;
	MESSAGE("opening file\n\r");
	retval = file_fopen(&output, &(efs.myFs), argv[1], 'w');
	MESSAGE("opened file\n\r");
	switch(retval)
	{
		case -2: //File already exists, open it for appending
			MESSAGE("File already exists, append to it?(Y/N):");
			c = UART0_Getchar();
			if(c == 'Y' || c == 'y')
			{
				if(file_fopen(&output, &(efs.myFs), argv[1], 'a') == -3)
				{
					MESSAGE("No more space!\n\r");
					return;
				}
				else
				{
					MESSAGE("appended to %s\n\r", argv[1]);
				}
			
			}
			else
				return;
			break;
		case -3:
			MESSAGE("No more space!\n\r");
			return;

		case 0:
			MESSAGE("file opened successfully\n\r");
			break;
		default:
			MESSAGE("unknown error occured\n\r");
	}
	MESSAGE("chars are being entered into the file until ~`! is recieved:\n\r");
	a = 0;
	b = 0;
	c = 0;
	
	while(quit)
	{
		a = UART0_Getchar();
		if(a == '~')
		{
			b = UART0_Getchar();
			if(b == '`')
			{
				c = UART0_Getchar();
				if(c == '!')
				{
					quit = 1;
					MESSAGE("quit recieved\n\r");
					file_fclose(&output);
					return;
				}
				else
				{
					file_write(&output, 1, &a);
					file_write(&output, 1, &b);
					file_write(&output, 1, &c);
				}
			}
			else
			{
				file_write(&output, 1, &a);
				file_write(&output, 1, &b);
			}
	
		}
		else
		{

			file_write(&output, 1, &a);

		}
		

	}

}
コード例 #20
0
ファイル: main.c プロジェクト: 003900107/realboard-lpc4088
int main()
{
	int8_t res;
    uint32_t n, m, p, cnt;

//	SystemInit();
    SysTick_Config(SystemCoreClock/1000 - 1); /* Generate interrupt each 1 ms   */

	LPC17xx_UART_Init(115200); // UART0
    xfunc_out = LPC17xx_UART_PutChar;
	xfunc_in  = LPC17xx_UART_GetChar; 

	xprintf("\nMMC/SD Card Filesystem Test (P:LPC1768 L:EFSL)\n");

	xprintf("\nCARD init...");

	// init file system
	if ( ( res = efs_init( &efs, 0 ) ) != 0 ) {
		xprintf("failed with %i\n",res);
	}
	else 
	{
		xprintf("ok\n");

        xprintf("Card type: ");
        switch (CardType)
        {
            case CARDTYPE_MMC:
                xprintf("MMC\n");
                break;
            case CARDTYPE_SDV1:
                xprintf("Version 1.x Standard Capacity SD card.\n");
                break;
            case CARDTYPE_SDV2_SC:
                xprintf("Version 2.0 or later Standard Capacity SD card.\n");
                break;
            case CARDTYPE_SDV2_HC:
                xprintf("Version 2.0 or later High/eXtended Capacity SD card.\n");
                break;
            default:
                break;            
        }
        xprintf("Sector size: %d bytes\n", CardConfig.sectorsize);
        xprintf("Sector count: %d\n", CardConfig.sectorcnt);
        xprintf("Block size: %d sectors\n", CardConfig.blocksize);
        xprintf("Card capacity: %d MByte\n\n", (((CardConfig.sectorcnt >> 10) * CardConfig.sectorsize)) >> 10);

		xprintf("\nDirectory of 'root':\n");
		
		/* list files in root directory */
		ls_openDir( &list, &(efs.myFs) , "/");
		while ( ls_getNext( &list ) == 0 ) {
			// list.currentEntry is the current file
			list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0';
			xprintf("%s, 0x%x bytes\n", list.currentEntry.FileName, list.currentEntry.FileSize ) ;
		}

#if WRITE_TEST_ENABLED!=0
        /* Write test*/  
        xprintf("\nFile write test:\n");
        xprintf("Open file %s ...", FILE_NAME);
        if (file_fopen( &filew, &efs.myFs , FILE_NAME , 'a' ) == 0 )
        {
            xprintf(" OK. \nWriting %lu bytes ...\n", FILE_RW_SIZE);

            xmemset(Buff, 0xAA, blen);
            n=FILE_RW_SIZE;
            m = 0;
            Timer = 0;
            while (n)
            {
                if (n>=blen) {
                    cnt = blen;
                    n -= blen;
                } else {
                    cnt = n;
                    n = 0;
                }
                p = file_write( &filew, cnt, Buff );
                m += p;
                if (p != cnt) break;
            }
            xprintf("%lu bytes written with %lu kB/sec.\n", m, Timer ? (m / Timer) : 0);

            file_fclose( &filew );                          

        } else {
            xprintf (" Failed.\n");
        }
#endif

#if READ_TEST_ENABLED!=0
        /* Read test */
        xprintf("\nFile read test:\n");
        xprintf("Open file %s ...", FILE_NAME);
        if (file_fopen( &filer, &efs.myFs , FILE_NAME , 'r' ) == 0 )
        {
            xprintf(" OK. \nReading %lu bytes ...\n", FILE_RW_SIZE);

            n=FILE_RW_SIZE; 
            m = 0;
            Timer = 0;
            while (n)
            {
                if (n>=blen) {cnt = blen; n -= blen;}
                else         {cnt = n; n = 0;}

                p =  file_read( &filer, cnt, Buff );
                m += p;
                if (p != cnt) break;                
            }
            xprintf("%lu bytes read with %lu kB/sec.\n", m, Timer ? (m / Timer) : 0);

            file_fclose( &filer ); 

        } else
        {
            xprintf (" Failed.\n");    
        }
#endif

        /* close file system */
	    fs_umount( &efs.myFs ) ;
    }

	xprintf("\nEFSL test complete.\n");

	while (1);
}
コード例 #21
0
ファイル: main.c プロジェクト: chargen/robotvisionexp
int main(void)
{
	int ch;
	int8_t res;
	
	systemInit();
	gpioInit();
	
	uart1Init(UART_BAUD(BAUD), UART_8N1, UART_FIFO_8); // setup the UART
		
	uart1Puts("\r\nMMC/SD Card Filesystem Test (P:LPC2138 L:EFSL)\r\n");
	uart1Puts("efsl LPC2000-port and this Demo-Application\r\n");
	uart1Puts("done by Martin Thomas, Kaiserslautern, Germany\r\n");
	
	/* init efsl debug-output */
	lpc2000_debug_devopen(uart1Putch);
		
	ledToggle();
	
	rprintf("CARD init...");
	if ( ( res = efs_init( &efs, 0 ) ) != 0 ) {
		rprintf("failed with %i\n",res);
	}
	else {
		rprintf("ok\n");
		rprintf("Directory of 'root':\n");
		ls_openDir( &list, &(efs.myFs) , "/");
		while ( ls_getNext( &list ) == 0 ) {
			list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0';
			rprintf( "%s ( %li bytes )\n" ,
				list.currentEntry.FileName,
				list.currentEntry.FileSize ) ;
		}
		
		if ( file_fopen( &filer, &efs.myFs , LogFileName , 'r' ) == 0 ) {
			rprintf("File %s open. Content:\n", LogFileName);
			while ( ( e = file_read( &filer, 512, buf ) ) != 0 ) {
				buf[e]='\0';
				uart1Puts((char*)buf);
			}
			rprintf("\n");
			file_fclose( &filer );
		}
		
		if ( file_fopen( &filew, &efs.myFs , LogFileName , 'a' ) == 0 ) {
			rprintf("File %s open for append. Appending...", LogFileName);
			strcpy((char*)buf, "Martin hat's angehaengt\r\n");
			if ( file_write( &filew, strlen((char*)buf), buf ) == strlen((char*)buf) ) {
				rprintf("ok\n");
			}
			else {
				rprintf("fail\n");
			}
			file_fclose( &filew );
		}
		
		if ( file_fopen( &filer, &efs.myFs , LogFileName , 'r' ) == 0 ) {
			rprintf("File %s open. Content:\n", LogFileName);
			while ( ( e = file_read( &filer, 512, buf ) ) != 0 ) {
				buf[e]='\0';
				uart1Puts((char*)buf);
			}
			rprintf("\n");
			file_fclose( &filer );
		}
		
		fs_umount( &efs.myFs ) ;
	}
	
	while(1) {
		if ((ch = uart1Getch()) >= 0) {
			uart1Puts("You pressed : ");
			uart1Putch(ch);
			uart1Puts("\r\n");
			if ( ch == 'M' ) {
				rprintf("Creating FS\n");
				mkfs_makevfat(&efs.myPart);
			}
			ledToggle();
		}
	}
	
	return 0; /* never reached */
}
コード例 #22
0
ファイル: reader.c プロジェクト: ScriptBasic/ScriptBasic
/* three functions to call the posix standard file open, read, close functions */
static void *_MyOpenFile(char *FileName, void *p){
  return (void *)file_fopen(FileName,"r");
  }
コード例 #23
0
/**
********************************************************************************************
 Function Name : fopen()

 Description   :	

 Input         :	

 Output        : Void

 Note          :
********************************************************************************************
*/
U8 fopen(File* file, S8 *filename,U8 open_mode)
{
  return file_fopen( file, &efs.myFs ,filename, open_mode);
}