Example #1
0
int
syscmd_copy_file(char *src_file, char *dst_file)
{
    FILE *src_fp = NULL, *dst_fp = NULL;
    char buf[BUFFER_COUNT];
    int ret = 0, count;
    int w_count = 0;
    int amount = 0;

    if (!sal_strcmp(src_file, dst_file))
    {
        return 0;
    }

    src_fp = sal_fopen(src_file, "r");
    if (NULL == src_fp)
    {
        ret = -1;
        goto error;
    }

    dst_fp = sal_fopen(dst_file, "w+");
    if (NULL == dst_fp)
    {
        ret = -2;
        goto error;
    }

    while ((count = sal_fread(buf, sizeof(char), BUFFER_COUNT, src_fp)) > 0)
    {
        w_count = sal_fwrite(buf, sizeof(char), count, dst_fp);

        if (w_count < count)
        {
            ret = -2;
            goto error;
        }

        /* check the memory on 1M boundary */
        amount += count;
        if(amount & 0x100000)
        {
            ret = -3;
            goto error;
        }
    }
error:
    if (NULL != src_fp)
    {
        sal_fclose(src_fp);
    }
    if (NULL != dst_fp)
    {
        sal_fclose(dst_fp);
    }
    memmgr_free_cached_mem();

    return ret;
}
Example #2
0
File: io.c Project: ariavie/bcm
int printk_file_enable(int enable)
{
#ifdef NO_FILEIO
    return 0;
#else
    int old_enable = (file_fp != NULL);

    if (! old_enable && enable) {
	/* Enable */
	if (! file_nm) {
	    printk("Logging file not opened, can't enable\n");
	    return -1;
	}
	if ((file_fp = sal_fopen(file_nm, "a")) == 0) {
	    perror("Error opening file");
	    return -1;
	}
    }

    if (old_enable && ! enable) {
	if (file_fp) {
	    sal_fclose(file_fp);
	    file_fp = 0;
	}
	/* Note: file_nm remains valid; output can still be re-enabled */
    }
    return old_enable;
#endif
}
Example #3
0
File: config.c Project: ariavie/bcm
int
sal_config_save(char *fname, char *pat, int append)
{
    int   rv = 0;
#ifndef SAL_CONFIG_FILE_DISABLE
    FILE  *new_fp;
    sc_t  *sc;
    int   pat_len = 0;
    int   i;

    if (fname == NULL) {
        return 0; /* No config file */
    }

    if(append) {
        new_fp = sal_fopen(fname, "a");
    } else {
        new_fp = sal_fopen(fname, "w");
    }
    
    if (new_fp == NULL) {
        rv = -1;
        goto done;
    }

    if (pat) {
        pat_len = strlen(pat);
    }
    /* Write out the current values */
    for (i = 0; i < MAX_CONFIG_HASH_COUNT; i++) {
        for (sc = sal_config_list[i]; sc != NULL; sc = sc->sc_next) {
            if ((pat == NULL) || (sal_strncasecmp(sc->sc_name, pat, pat_len) == 0)) {
                sal_fprintf(new_fp, "%s=%s\n", sc->sc_name, sc->sc_value);
            }
        }
    }

    sal_fclose(new_fp);

 done:
    if (rv < 0) {
        sal_printf("sal_config_flush: variables not saved\n");
    }
#endif /* SAL_CONFIG_FILE_DISABLE */

    return rv;
}
Example #4
0
File: io.c Project: ariavie/bcm
int printk_file_open(char *filename, int append)
{
#ifndef NO_FILEIO
    if (file_nm) {
	printk_file_close();
    }
    if ((file_fp = sal_fopen(filename, append ? "a" : "w")) == 0) {
	perror("Error opening file");
	return -1;
    }
    file_nm = sal_strcpy((char *)sal_alloc(sal_strlen(filename) + 1, __FILE__), filename);
#endif /* NO_FILEIO */
    return 0;
}
Example #5
0
cmd_result_t
cmd_cint(int unit, args_t* a)
{
    char* s; 
    int cmp;
    int argc = 0; 
    char* argv[16]; 

    cmd_cint_initialize(); 
    
    s = ARG_CUR(a);
    if(s) {
        cmp = sal_strcmp(s, "allow_file_info");
        if(!cmp) {
            argv[argc] = ARG_GET(a);
            argc++;
        }
    }

    if(ARG_CUR(a)) {
        /* Load file */
        FILE* fp; 
        s = ARG_GET(a); 
        
        if((fp = sal_fopen(s, "r")) != NULL) {
                

                
            sal_memset(argv, 0, sizeof(argv)); 
            while( (argv[argc] = ARG_GET(a)) ) {
                argc++; 
            }   

            cint_interpreter_parse(fp, NULL, argc, argv); 
            sal_fclose(fp); 
        }   
        else {
            cli_out("error: could not open file '%s'\n", s); 
        }       
    }   
    else {
        cli_out("Entering C Interpreter. Type 'exit;' to quit.\n\n"); 
        cint_interpreter_parse(NULL, "cint> ", argc, argv); 
    }
    return 0; 
}
Example #6
0
File: config.c Project: ariavie/bcm
int
sal_config_flush(void)
{
    int   rv = 0;
#ifndef SAL_CONFIG_FILE_DISABLE
    FILE  *old_fp, *new_fp;
    sc_t  *sc, *new_sc;
    char  str[SAL_CONFIG_STR_MAX], *c;
    int   line = 0;
    char  *fname, *tname;
    int i;

    fname = (sal_config_file_name != NULL ?
       sal_config_file_name : SAL_CONFIG_FILE);

    if (fname[0] == 0) {
        return 0; /* No config file */
    }

    tname = (sal_config_temp_name != NULL ?
       sal_config_temp_name : SAL_CONFIG_TEMP);

    assert(tname != NULL && tname[0] != 0);

    /* Attempt to create temp file */

    if ((new_fp = sal_fopen(tname, "w")) == NULL) {
        rv = -1;
        goto done;
    }

    /* old_fp can be NULL if creating config file for first time */

    old_fp = sal_fopen(fname, "r");

    /* Initialize the "flushed" flag */
    for (i = 0; i < MAX_CONFIG_HASH_COUNT; i++) {
        for (sc = sal_config_list[i]; sc != NULL; sc = sc->sc_next) {
            sc->sc_flag = SC_NOT_FLUSHED;
        }
    }

    /* Read the entire file  - parsing as we go */

    while (old_fp != NULL && sal_fgets(str, sizeof(str), old_fp)) {
        char *s;
      
        line++;
      
        /* Strip trailing newline/blanks */
      
        c = str + strlen(str);
        while (c > str && isspace((unsigned) c[-1])) {
            c--;
        }
      
        *c = '\0';
      
        /*
         * Transfer blank lines and comment lines, but NOT commented-out
         * variable settings (yet)
         */
      
        if (str[0] == 0 || (str[0] == '#' && strchr(str, '=') == NULL)) {
            sal_fprintf(new_fp, "%s\n", str);
            continue;
        }
      
        s = str;
        if (*s == '#') {
            s++;
        }
      
        if ((sc = sal_config_parse(s)) == NULL) {
            sal_printf("sal_config_flush: format error "
                 "in %s on line %d (removed)\n",
                 fname, line);
            continue;
        }
      
        /* Write new value (or comment) for this entry */
      
        if ((new_sc = sal_config_find(sc->sc_name)) == NULL ||
            new_sc->sc_flag == SC_FLUSHED) {
            /* Not found or a dup, write out commented assignment */
            sal_fprintf(new_fp, "#%s=%s\n", sc->sc_name, sc->sc_value);
        } else {
            sal_fprintf(new_fp, "%s=%s\n", new_sc->sc_name, new_sc->sc_value);
            new_sc->sc_flag = SC_FLUSHED;
        }
      
        FREE_SC(sc);
    }

    /* Write out the current values that were not in the old_fp file */
    for (i = 0; i < MAX_CONFIG_HASH_COUNT; i++) {
        for (sc = sal_config_list[i]; sc != NULL; sc = sc->sc_next) {
            if (sc->sc_flag == SC_NOT_FLUSHED) {
                sal_fprintf(new_fp, "%s=%s\n", sc->sc_name, sc->sc_value);
            }
        }
    }

    sal_fclose(new_fp);

    if (old_fp != NULL) {
        sal_fclose(old_fp);
    }

    if (rv == 0) {      /* No error, rename file */
        rv = sal_rename(tname, fname);
    }

    if (rv != 0) {      /* Error, remove temp file */
        (void)sal_remove(tname);
    }

 done:
    if (rv < 0) {
        sal_printf("sal_config_flush: variables not saved\n");
    }
#endif /* SAL_CONFIG_FILE_DISABLE */

    return rv;
}
Example #7
0
File: config.c Project: ariavie/bcm
/*
 * Function:
 *  sal_config_refresh
 * Purpose:
 *      Refresh default (compiled-in) configuration.
 *  Refresh the memory image from the configuration file,
 *      clobbering default values (if the config file exists).
 * Parameters:
 *  None
 * Returns:
 *  0 - success
 *  -1 - failed.
 */
int
sal_config_refresh(void)
{
    sc_t  *sc, *sc_tmp;
    int i;
#ifndef SAL_CONFIG_FILE_DISABLE
    FILE  *fp;
    sc_t  *found_sc;
    char  str[SAL_CONFIG_STR_MAX], *c;
    int   line = 0;
    char  *fname;
    int   suppress_unknown_warnings = 0;
#endif

    /* Clear all previous state */
    for (i = 0; i < MAX_CONFIG_HASH_COUNT; i++) {
        sc = sal_config_list[i];
        sal_config_list[i] = NULL;
        while (sc != NULL) {
            sc_tmp = sc->sc_next;
            FREE_SC(sc);
            sc = sc_tmp;
        }  
    }
    /* load precompiled values from platform_defines.c */
    sal_config_init_defaults();

#ifndef SAL_CONFIG_FILE_DISABLE
    fname = (sal_config_file_name != NULL ?
         sal_config_file_name : SAL_CONFIG_FILE);

    if (fname[0] == 0) {
        return 0;   /* No config file */
    }

    /* Try to load config file ... */
    if ((fp = sal_fopen(fname, "r")) == NULL) {
#if defined(BROADCOM_DEBUG)
        sal_printf("sal_config_refresh: cannot read file: %s, "
               "variables not loaded\n",
               fname);
#endif /* BROADCOM_DEBUG */
        return -1;
    }

    /*
     * Add new file-based variables, superseding any matching compiled
     * variables.  Find end-of-list, and initialize the default-or-file
     * flag.
     */
    for (i = 0; i < MAX_CONFIG_HASH_COUNT; i++) {
        for (sc = sal_config_list[i]; sc != NULL; sc = sc->sc_next) {
            sc->sc_flag = SC_COMPILE_VALUE;
        }
    }
    /* Read the entire file  - parsing as we go */

    while (sal_fgets(str, sizeof(str), fp)) {

        line++;
      
        /* Skip comment lines */
      
        if (str[0] == '#') {
            continue;
        }
      
        /* Strip trailing newline/blanks */
      
        c = str + strlen(str);
        while (c > str && isspace((unsigned) c[-1])) {
            c--;
        }
      
        *c = '\0';
      
        /* Skip blank lines */
      
        if (str[0] == 0) {
            continue;
        }
      
        if ((sc = sal_config_parse(str)) == NULL) {
            sal_printf("sal_config_refresh: format error "
                 "in %s on line %d (ignored)\n",
                 fname, line);
            continue;
        }
      
        /* Check for pre-existing default or duplicates in the file */        
        found_sc = sal_config_list[sc->sc_hash];
        while (found_sc != NULL) {
            if (sal_strcmp(found_sc->sc_name, sc->sc_name) == 0) {
                break;
            }
            found_sc = found_sc->sc_next;
        }
        
        if (found_sc != NULL) {
            if (found_sc->sc_flag != SC_COMPILE_VALUE) {
                sal_printf("sal_config_refresh: ignoring duplicate entry "
               "\"%s\"\n"
               "                    %s line %d "
               "(first defined on line %d)\n",
               sc->sc_name, fname,
               line, found_sc->sc_flag);
            }
            else {
                /* Clobber the compiled-in default value */
                char *temp = sc->sc_value;         /* New value */
                sc->sc_value = found_sc->sc_value; /* Old, to be freed */
                found_sc->sc_value = temp;
                found_sc->sc_flag = line; /* Line # of 1st definition */
            }
            FREE_SC(sc);
            continue;
        }

        /* 
         * Scan for "suppress_unknown_prop_warnings" directly as we go
         * instead of sal_config_find() because it's much faster
         */
        if (sal_strcasecmp("suppress_unknown_prop_warnings", 
                           sc->sc_name) == 0) {
            suppress_unknown_warnings = _shr_ctoi(sc->sc_value);
        }

        if (!suppress_unknown_warnings) {
            if (sal_config_prop_is_known(sc) == FALSE) {
                sal_printf("sal_config_refresh: unknown entry \"%s\""
                           " on %s line %d\n", sc->sc_name, fname, line);
            }
        }
        
        sc->sc_flag = line; /* Line # of 1st definition */
        sc->sc_next = sal_config_list[sc->sc_hash];
        sal_config_list[sc->sc_hash] = sc;  
    } /* parse config file */
    sal_fclose(fp);
#endif /* SAL_CONFIG_FILE_DISABLE */

    return 0;
}
Example #8
0
/*******************************************************************************
 * Name:    copy_file_with_progress
 * Purpose: copy file and show progress
 * Input:
 *   src_file: source file
 *   dst_file: dest file
 * Output:
 * Return:
 *   success: 0
 *   failed : -1
 * Note:
 ******************************************************************************/
#define HASHBYTES (50 * 1024)
#define MEMCHECK  (4 * 1024 *1024) /*4M*/
int
copy_file_with_progress(char *src_file, char *dst_file)
{
    FILE *src_fp = NULL, *dst_fp = NULL;
    char buf[BUFFER_COUNT];
    int ret = 0, count;
    int amount = 0;
    int hashbytes = 0;
    int memcheck = 0;
    struct timeval tstart;
    struct timeval tstop;

    if (!sal_strcmp(src_file, dst_file))
    {
        return 0;
    }

    src_fp = sal_fopen(src_file, "r");
    if (NULL == src_fp)
    {
        ret = -1;
        goto error;
    }

    dst_fp = sal_fopen(dst_file, "w+");
    if (NULL == dst_fp)
    {
        ret = -2;
        goto error;
    }

    (void)gettimeofday(&tstart, NULL);
    while ((count = sal_fread(buf, sizeof(char), BUFFER_COUNT, src_fp)) > 0)
    {
        ret = sal_fwrite(buf, sizeof(char), count, dst_fp);
        if (ret <= 0)
        {
            (void)gettimeofday(&tstop, NULL);
            goto error;
        }
        amount += count;

        if(amount >= memcheck)
        {
            if((syslimit_mem_threshold_check() < 0))
            {
                ret = -3;
                goto error;
            }
            memcheck += MEMCHECK;
        }

        while (amount >= hashbytes)
        {
            ctc_cli_out(".");
            (void) fflush(stdout);
            hashbytes += HASHBYTES;
        }
    }
    (void)gettimeofday(&tstop, NULL);

error:
    if (NULL != src_fp)
    {
        sal_fclose(src_fp);
    }
    if (NULL != dst_fp)
    {
        sal_fclose(dst_fp);
    }
    if (amount && ( ret >= 0))
    {
        double  delta;

        /* compute delta in 1/10's second units */
        delta = ((tstop.tv_sec * 10.) + (tstop.tv_usec / 100000)) -
                ((tstart.tv_sec * 10.) + (tstart.tv_usec / 100000));
        delta = delta / 10.;    /* back to seconds */
        ctc_cli_out("\n%lu bytes in %.1f seconds, %.0f kbytes/second\n",
                amount, delta, (amount * 1.) / (1024 * delta));
    }
    return ret;
}
Example #9
0
cmd_result_t
iprocread_cmd(int unit, args_t *args)
{
    cmd_result_t rv = CMD_OK;
    parse_table_t  pt;
    char *c, *filename = NULL;
    uint32 addr = 0;
    int ce = 0;
    int len=1;
#ifndef NO_FILEIO
#ifndef NO_CTRL_C
    jmp_buf ctrl_c;
#endif
    FILE * volatile fp = NULL;
#endif

    if (ARG_CNT(args) < 1) {
        return(CMD_USAGE);
    }

    parse_table_init(unit, &pt);
    parse_table_add(&pt, "ChangeEndian", PQ_DFL|PQ_BOOL,
                    0, &ce,  NULL);
    if (parse_arg_eq(args, &pt) < 0) {
        printk("%s: Error: Unknown option: %s\n", ARG_CMD(args), ARG_CUR(args));
        parse_arg_eq_done(&pt);
        return(CMD_USAGE);
    }

    c = ARG_GET(args);
    if (!isint(c)) {
        printk("%s: Error: Address not specified\n", ARG_CMD(args));
        return(CMD_USAGE);
    }
    addr = parse_address(c);

    if (ARG_CNT(args) > 0) {
        c = ARG_GET(args);
        if (isint(c)) {
            len = parse_address(c);
            if (ARG_CNT(args) > 0) {
                filename = ARG_GET(args);
            }
        } else {
            filename = c;
        }
    }

    if (filename == NULL) {
        /* Just dump to screen */
        rv = _iproc_dump(unit, ce, NULL, addr, len);
    } else {
#ifdef NO_FILEIO
        printk("no filesystem\n");
#else
        /* Dump to file */        
  #ifndef NO_CTRL_C
        if (!setjmp(ctrl_c)) {
            sh_push_ctrl_c(&ctrl_c);
  #endif

            fp = sal_fopen(filename, "w");
            if (!fp) {
                printk("%s: Error: Unable to open file: %s\n",
                   ARG_CMD(args), filename);
                rv = CMD_FAIL;
  #ifndef NO_CTRL_C
                sh_pop_ctrl_c();
  #endif
                return(rv);
            } else {
                soc_cm_debug(DK_VERBOSE, "Dump to file %s\n", filename);
                rv = _iproc_dump(unit, ce, fp, addr, len);
                sal_fclose((FILE *)fp);
                fp = NULL;
            }

  #ifndef NO_CTRL_C
        } else if (fp) {
            sal_fclose((FILE *)fp);
            fp = NULL;
            rv = CMD_INTR;
        }

        sh_pop_ctrl_c();
  #endif
        sal_usleep(10000);
#endif /* NO_FILEIO */
    }

    return(rv);
}
Example #10
0
cmd_result_t
iprocwrite_cmd(int unit, args_t *args)
{
    cmd_result_t rv = CMD_OK;
    parse_table_t  pt;
    char *c, *filename = NULL;
    int ce = 0;
    uint32 addr = 0;
#ifndef NO_FILEIO
  #ifndef NO_CTRL_C
    jmp_buf ctrl_c;
  #endif
    FILE * volatile fp = NULL;
#endif

    if (ARG_CNT(args) < 1) {
        return(CMD_USAGE);
    }

    parse_table_init(unit, &pt);
    parse_table_add(&pt, "ChangeEndian", PQ_DFL|PQ_BOOL,
                    0, &ce,  NULL);
    if (parse_arg_eq(args, &pt) < 0) {
        printk("%s: Error: Unknown option: %s\n", ARG_CMD(args), ARG_CUR(args));
        parse_arg_eq_done(&pt);
        return(CMD_USAGE);
    }

    c = ARG_GET(args);
    if (!isint(c)) {
        printk("%s: Error: Address not specified\n", ARG_CMD(args));
        return(CMD_USAGE);
    }
    addr = parse_address(c);

    if (ARG_CNT(args) <= 0) {
        printk("%s: Error: Data / Filename not specified\n", ARG_CMD(args));
        return(CMD_USAGE);
    }

    c = ARG_GET(args);

    if (!isint(c)) {
        filename = c;
    }

    if (filename == NULL) {
        ARG_PREV(args);
        rv = _iproc_write_from_args(unit, ce, addr, args);
    } else {
#ifdef NO_FILEIO
        printk("no filesystem\n");
#else
        /* Read from file */        
  #ifndef NO_CTRL_C
          if (!setjmp(ctrl_c)) {
              sh_push_ctrl_c(&ctrl_c);
  #endif
    
              fp = sal_fopen(filename, "r");
              if (!fp) {
                  printk("%s: Error: Unable to open file: %s\n",
                     ARG_CMD(args), filename);
                  rv = CMD_FAIL;
  #ifndef NO_CTRL_C
                  sh_pop_ctrl_c();
  #endif
                  return(rv);
              } else {
                  rv = _iproc_write_from_bin_file(unit, ce, addr, fp);
                  sal_fclose((FILE *)fp);
                  fp = NULL;
              }

  #ifndef NO_CTRL_C
          } else if (fp) {
              sal_fclose((FILE *)fp);
              fp = NULL;
              rv = CMD_INTR;
          }
    
          sh_pop_ctrl_c();
  #endif
#endif
    }

    return(rv);
}