gbool GUrlCache::AddCacheDirectory(const char *dir,CStringArray &readCacheDirs)
{
    CString directory;
    directory = dir;

    directory.TrimRight();
    directory.TrimLeft();

    if (directory.IsEmpty()) return FALSE;

    //
    if (strprefix(directory,"cdrom") ||  strprefix(directory,"CDROM")) { // replace cd rom with found cdroms
        int i=0;

        const char *x = directory;
        x+=strlen("cdrom");

        // get cdrom id
        if (*x >= '0' &&  *x <'9') {
            i = (*x - '0');
            x++;
        }
        // skip the :
        if (*x == ':') x++;



        if (i<cdroms.GetSize()) {
            CString dir = cdroms.GetAt(i);
            // strip of prefix


            if (*x == '\\') x++;
            else if (*x == '/') x++;

            directory = dir  + CString(x);


        }
        else return FALSE; // no cdrom
    }

    // filter out duplicates
    for (int i=0; i<readCacheDirs.GetSize(); i++) {
        if (readCacheDirs.GetAt(i) == directory)
            return FALSE;
    }


    if (ExistsDirectory(directory)) {
        readCacheDirs.Add(directory);
        return TRUE;
    }
    return FALSE;

}
Exemple #2
0
/*
 * Check for non-disk-partition arguments of the form
 *	attribute=argument
 * returning TRUE if one if found
 */
boolean_t dp_parse_argument(char *av)
{
	char *rhs = av;
	__unused static char	here[] = "dp_parse_argument";

	/* Check for '-v' flag */

	if (av[0] == '-' && av[1] == 'v' && av[2] == 0) {
		verbose = TRUE ;
		return TRUE;
	}

	/*
	 * If we find a '=' followed by an argument in the string,
	 * check for known arguments
	 */
	while (*rhs && *rhs != '=')
		rhs++;
	if (*rhs && *++rhs) {
		/* clsize=N pages */
		if (strprefix(av,"cl")) {
			if (!bs_set_default_clsize(d_to_i(rhs)))
				dprintf(("Bad argument (%s) - ignored\n", av));
			return(TRUE);
		}
		/* else if strprefix(av,"another_argument")) {
			handle_another_argument(av);
			return(TRUE);
		} */
	}
	return(FALSE);
}
Exemple #3
0
void umount_cmd(int term, char **args, int argc)
{
	char *current_dir = NULL;

	if(argc != 1)
	{
		term_color_print(term, "\nInvalid parameters.\n", 12);
		term_color_print(term, "Usage: umount path\n", 7);
		return;
	}

	current_dir = get_env("CURRENT_PATH", term);
	
	if(strprefix(args[0], current_dir))
	{
		term_color_print(term, "Cannot umount dir, because it's a prefix of the current path.\n", 12);
		return;
	}

	if(umount(args[0]))
	{
		term_color_print(term, mapioerr(ioerror()), 12);
		term_color_print(term, "\n", 12);
	}
}
Exemple #4
0
int
parse_negative_option(char *opt, struct option_data *data)
{
    size_t i;
    const char *name = strprefix(opt, negative_prefix);
    if (!name) return 0;

    for (i = 0; i < count_of(categories_info); i++) {
        struct extension_category_info *category = &categories_info[i];
        if (strcmp(name, category->option_name)==0) {
            data->extensions &= ~(category->flags);
            return 1;
        }
    }

    for (i = 0; i < count_of(extensions_info); i++) {
        struct extension_info *extension = &extensions_info[i];
        if (strcmp(name, extension->option_name)==0) {
            data->extensions &= ~(extension->flag);
            return 1;
        }
    }

    for (i = 0; i < count_of(html_flags_info); i++) {
        struct html_flag_info *html_flag = &html_flags_info[i];
        if (strcmp(name, html_flag->option_name)==0) {
            data->html_flags &= ~(html_flag->flag);
            return 1;
        }
    }

    return 0;
}
// transform url to a relative file path
gbool GUrlCache::UrlToFilePath(const char *url,CString &relFilePath)
{

    char *FileName = relFilePath.GetBuffer(strlen(url));
    BOOL afterQuestion = FALSE;


    if (strprefix(url,prefix_http))
        strcpy(FileName,&url[strlen(prefix_http)]);
    else strcpy(FileName,url);
    char *ext;
    // extra arguments to CGI ???
    ext =strrchr(FileName,'?');
    if (ext) {
        // *ext = 0;
        // return FALSE; // no cgi-bin
    }

    // strip of argument
    ext =strrchr(FileName,'#');
    if (ext) *ext = 0;

    //test16.wrl_;>_@=&

    ext = FileName;
    while (*ext) {
        switch(*ext) {
        case '/':
            if (afterQuestion) *ext ='_';
            // else keep separator case '/':
#ifdef WIN32
            *ext = '\\'; // convert directory separators
#endif
            break;
        case '?':
            afterQuestion = TRUE;
        case '\\':
        case ':':
        case ';':
        case ',':
        case '>':
        case '<':
        case '|':
        case '~':
        case '*':
            *ext ='_';
            break;
        default :
            break;
        }
        ext ++;

    }

    relFilePath.ReleaseBuffer();

    TRACE("Local filename %s \n",(const char *)relFilePath);
    return TRUE;
}
Exemple #6
0
static void
push_update_digest(md_ctx_t *ctx, struct buffer *buf, const struct options *opt)
{
    char line[OPTION_PARM_SIZE];
    while (buf_parse(buf, ',', line, sizeof(line)))
    {
        /* peer-id might change on restart and this should not trigger reopening tun */
        if (strprefix(line, "peer-id "))
        {
            continue;
        }
        /* tun reopen only needed if cipher change can change tun MTU */
        if (strprefix(line, "cipher ") && !opt->ce.tun_mtu_defined)
        {
            continue;
        }
        md_ctx_update(ctx, (const uint8_t *) line, strlen(line)+1);
    }
}
Exemple #7
0
int a6o_scan_conf_is_white_listed(struct a6o_scan_conf *c, const char *path)
{
	const char **p = directories_white_list(c);

	while (*p != NULL) {
		if (strprefix(path, *p))
			return 1;
		p++;
	}

	return 0;
}
// parse a set of urls
gbool GUrlCache::SetUmelDirectoryPath(const char *dirs)
{

    const char *p;


    // loop over the path

    p= dirs;
    const char *pstart= dirs;

    umelCacheDirs.RemoveAll();

    while (*p != 0) {

        char c = *p++;

        if ( (c == ';')) { // found separator
            CString dir(pstart,p-pstart-1);
            if (strprefix(pstart,prefix_http)) {
                umelHome = dir;
            } else
                AddCacheDirectory(dir,umelCacheDirs);
            pstart = p;

        }
    }

    if (p>pstart) {
        CString dir(pstart,p-pstart);
        if (strprefix(pstart,prefix_http)) {
            umelHome = dir;
        } else
            AddCacheDirectory(dir,umelCacheDirs);
        pstart = p;
    }

    return umelCacheDirs.GetSize()>0;
}
gbool GUrlCache::SetWriteMediaCacheDirectory(const char *mediaDirectory) // Laurent - Jan.2000
{
    writeMediaCacheDir = mediaDirectory;

    writeMediaCacheDir.TrimLeft();
    writeMediaCacheDir.TrimRight();

    if (writeMediaCacheDir.IsEmpty()) {
        writeMediaCacheEnabled = FALSE;
        writeMediaCacheDirValid = FALSE;
        return FALSE;
    }


    if (strlen(mediaDirectory) <=3) {
        writeMediaCacheEnabled = FALSE;
        writeMediaCacheDirValid = FALSE;
        return FALSE;
    }

    CString sysDir;

    UINT s = ::GetSystemDirectory(sysDir.GetBuffer(MAX_PATH+1),MAX_PATH);
    sysDir.ReleaseBuffer();
    if (s>0 && strprefix(writeMediaCacheDir,sysDir)) {
        writeMediaCacheEnabled = FALSE;
        writeMediaCacheDirValid = FALSE;
        return FALSE;
    }




    if (!CreateDirectory(writeMediaCacheDir)) {
        writeMediaCacheEnabled = FALSE;
        writeMediaCacheDirValid = FALSE;
        return FALSE;
    }

    if (writeMediaCacheDir[1]== ':')
        writeMediaCacheDrive = writeMediaCacheDir.Left(3);
    else
        writeMediaCacheDrive = writeMediaCacheDir;


    writeMediaCacheDirValid = TRUE;


    return TRUE;

}
Exemple #10
0
int
parse_category_option(char *opt, struct option_data *data)
{
    size_t i;
    const char *name = strprefix(opt, category_prefix);
    if (!name) return 0;

    for (i = 0; i < count_of(categories_info); i++) {
        struct extension_category_info *category = &categories_info[i];
        if (strcmp(name, category->option_name)==0) {
            data->extensions |= category->flags;
            return 1;
        }
    }

    return 0;
}
Exemple #11
0
int os_file_do_not_scan(const char *path)
{
	const char **p;
	char * real_path;

	real_path =  realpath(path, NULL);
	if( real_path == NULL){
		a6o_log(ARMADITO_LOG_LIB, ARMADITO_LOG_LEVEL_ERROR, "realpath of %s failed : %s.", path, strerror(errno));
		return 1;
	}

	for(p = do_not_scan_paths; *p != NULL; p++) {
		if (strprefix(real_path, *p)){
			free(real_path);
			return 1;
		}
	}

	free(real_path);
	return 0;
}
Exemple #12
0
char *_ll_escape_symbol(const char *in, char *buf)
{
  char *out = buf;

 restart:
  while ( *in == '_' ) {
    *(out ++) = '%';
    in ++;
  }
  while ( *in ) {
    if ( *in == '_' || isupper(*in) ) {
      struct strmap *map = _smap;
      while ( map->in ) {
	const char *e;
	if ( (e = strprefix(in, map->in)) ) {
	  const char *t = map->out;
	  in = e;
	  while ( *t ) {
	    *(out ++) = *(t ++);
	  }

	  if ( ! isalnum(out[-1]) )
	    goto restart;

	  goto next_in;
	}
	map ++;
      }
    }
    *(out ++) = *(in ++);
  next_in:
    ;

  }
  *out = '\0';

  return buf;
}
Exemple #13
0
static void
parseOptions(int argc, char **argv)
{ for( ; argc > 0; argc--, argv++ )
  { char *opt = argv[0];

    if ( dispatchFile(opt) )
      continue;

    if ( streq(opt, "-help") )			/* -help */
    { usage();
    } else if ( streq(opt, "-v") )		/* -v */
    { verbose++;
    } else if ( streq(opt, "--version") )	/* --version */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
      show_version = TRUE;
    } else if ( streq(opt, "-f") )		/* -f */
    { fake++;
    } else if ( streq(opt, "-c") )		/* -c */
    { nolink++;
    } else if ( streq(opt, "-S") )		/* -S */
    { nolink++;
      appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( streq(opt, "-E") )		/* -E */
    { nolink++;
      opt_E = TRUE;
      appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( streq(opt, "-g") )		/* -g */
    { appendArgList(&coptions, OPT_DEBUG);
      appendArgList(&cppoptions, OPT_DEBUG);
#if defined(HOST_TOOLCHAIN_MSC)			/* MSVC DEBUG OPTIONS */
      appendArgList(&coptions, "/ZI");
      appendArgList(&coptions, "/Od");
      appendArgList(&cppoptions, "/ZI");
      appendArgList(&cppoptions, "/Od");
#endif
      appendArgList(&ldoptions, OPT_DEBUG);
#ifdef LIB_PL_DEBUG
      pllib = LIB_PL_DEBUG;
#endif
    } else if ( strprefix(opt, "-pg") )		/* -pg* */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( streq(opt, "-g3") )		/* -g3 */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( strprefix(opt, "gdwarf-") )	/* -gdwarf-* */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( strprefix(opt, "-O") )		/* -O* */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( strprefix(opt, "-Wl,") )	/* -Wl,* */
    { appendArgList(&ldoptions, opt);
    } else if ( strprefix(opt, "-W") )		/* -W* */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( streq(opt, "-build-defaults") )	/* -build-defaults */
    { build_defaults = TRUE;
    } else if ( streq(opt, "-nostate") )	/* -nostate */
    { nostate = TRUE;
    } else if ( streq(opt, "-state") )		/* -state */
    { nostate = FALSE;
    } else if ( streq(opt, "-nolibswipl") )	/* -nolibswipl */
    { nolibswipl = TRUE;
    } else if ( streq(opt, "-dll") ||		/* -dll */
		streq(opt, "-embed-shared") )   /* -embed-shared */
    { embed_shared = TRUE;
#if defined(HOST_TOOLCHAIN_MSC)
      appendArgList(&ldoptions, "/DLL");
#else
#ifdef SO_pic
      appendArgList(&coptions, SO_pic);
      appendArgList(&cppoptions, SO_pic);
#endif
#endif
    } else if ( streq(opt, "-shared") )		/* -shared */
    { shared = TRUE;
      nostate = TRUE;
#ifdef SO_pic
      appendArgList(&coptions, SO_pic);
      appendArgList(&cppoptions, SO_pic);
#endif
    } else if ( streq(opt, "-SHARED") )		/* -SHARED */
    { shared = TRUE;
      nostate = TRUE;
#ifdef SO_PIC
      appendArgList(&coptions, SO_PIC);
      appendArgList(&cppoptions, SO_PIC);
#else
#ifdef SO_pic
      appendArgList(&coptions, SO_pic);
      appendArgList(&cppoptions, SO_pic);
#endif
#endif
    } else if ( streq(opt, "-fpic") )		/* -fpic */
    {
#ifdef SO_pic
      appendArgList(&coptions, SO_pic);
      appendArgList(&cppoptions, SO_pic);
#endif
    } else if ( streq(opt, "-fPIC") )		/* -fPIC */
    {
#ifdef SO_PIC
      appendArgList(&coptions, SO_PIC);
      appendArgList(&cppoptions, SO_PIC);
#endif
    } else if ( streq(opt, "-o") )		/* -o out */
    { if ( argc > 1 )
      { out = argv[1];
	opt_o = TRUE;
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-goal") )		/* -goal goal */
    { if ( argc > 1 )
      { plgoal = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-toplevel") )	/* -toplevel goal */
    { if ( argc > 1 )
      { pltoplevel = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-initfile") )	/* -initfile goal */
    { if ( argc > 1 )
      { plinitfile = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-F") )		/* -F base */
    { if ( argc > 1 )
      { plsysinit = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-class") )		/* -class runtime,kernel,
							  development */
    { if ( argc > 1 )
      { plclass = argv[1];
	if ( !streq(plclass, "runtime") &&
	     !streq(plclass, "kernel") &&
	     !streq(plclass, "development")
	   )
	  usage();
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-pl") )		/* -pl prolog */
    { if ( argc > 1 )
      { pl = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-cc") )		/* -cc compiler */
    { if ( argc > 1 )
      { cc = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-c++") )		/* -c++ compiler */
    { if ( argc > 1 )
      { cxx = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( streq(opt, "-ld") )		/* -ld linker */
    { if ( argc > 1 )
      { ld = argv[1];
	argc--, argv++;
      } else
	usage();
    } else if ( strprefix(opt, "-cc-options") )
    { appendOptions(&coptions, opt+strlen("-cc-options"));
      appendOptions(&cppoptions, opt+strlen("-cc-options"));
    } else if ( strprefix(opt, "-ld-options") )
    { appendOptions(&ldoptions, opt+strlen("-ld-options"));
    } else if ( strprefix(opt, "-pl-options") )
    { appendOptions(&ploptions, opt+strlen("-pl-options"));
    } else if ( strprefix(opt, "-I") )		/* -I<include> */
    { appendArgList(&includedirs, &opt[2]);
    } else if ( strprefix(opt, "-D") )		/* -D<def> */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( strprefix(opt, "-U") )		/* -U<def> */
    { appendArgList(&coptions, opt);
      appendArgList(&cppoptions, opt);
    } else if ( strprefix(opt, "-L") )		/* -L<libdir> */
    { appendArgList(&libdirs, &opt[2]);
    } else if ( streq(opt, "-lccmalloc") )	/* -lccmalloc */
    { appendArgList(&lastlibs, opt);
    } else if ( strprefix(opt, "-l") )		/* -l<lib> */
    { appendArgList(&libs, opt);
    }
  }
}
Exemple #14
0
/* kauth file scope listener 
 * this allows to detect files written to the filesystem
 * arg2 contains a flag KAUTH_FILEOP_CLOSE which is set if a modified file is being closed
 * this way we don't need to trace every close(), only the ones writing to the filesystem
 */
static int
fileop_scope_listener(kauth_cred_t    credential,
                      void *          idata,
                      kauth_action_t  action,
                      uintptr_t       arg0,     /* vnode reference */
                      uintptr_t       arg1,     /* full path to file being closed */
                      uintptr_t       arg2,     /* flags */
                      uintptr_t       arg3)
{
    /* ignore all actions except FILE_CLOSE */
    if (action != KAUTH_FILEOP_CLOSE)
    {
        return KAUTH_RESULT_DEFER;
    }
    
    /* ignore operations with bad data */
    if (credential == NULL || (vnode_t)arg0 == NULL || (char*)arg1 == NULL)
    {
        ERROR_MSG("Arguments contain null pointers!");
        return KAUTH_RESULT_DEFER;
    }
    
    /* ignore closes on folders, character and block devices */
    switch ( vnode_vtype((vnode_t)arg0) )
    {
        case VDIR:
        case VCHR:
        case VBLK:
            return KAUTH_RESULT_DEFER;
        default:
            break;
    }
    
    /* we are only interested when a modified file is being closed */
    if ((int)arg2 != KAUTH_FILEOP_CLOSE_MODIFIED)
    {
        return KAUTH_RESULT_DEFER;
    }
    
    char *file_path = (char*)arg1;
    /* get information from current proc trying to write to the vnode */
    proc_t proc = current_proc();
    pid_t mypid = proc_pid(proc);
    char myprocname[MAXCOMLEN+1] = {0};
    proc_name(mypid, myprocname, sizeof(myprocname));

    /* retrieve the vnode attributes, we can get a lot of vnode information from here */
    struct vnode_attr vap = {0};
    vfs_context_t context = vfs_context_create(NULL);
    /* initialize the structure fields we are interested in
     * reference vn_stat_noauth() xnu/bsd/vfs/vfs_vnops.c
     */
    VATTR_INIT(&vap);
    VATTR_WANTED(&vap, va_mode);
    VATTR_WANTED(&vap, va_type);
    VATTR_WANTED(&vap, va_uid);
    VATTR_WANTED(&vap, va_gid);
    VATTR_WANTED(&vap, va_data_size);
    VATTR_WANTED(&vap, va_flags);
    int attr_ok = 1;
    if ( vnode_getattr((vnode_t)arg0, &vap, context) != 0 )
    {
        /* in case of error permissions and filesize will be bogus */
        ERROR_MSG("failed to vnode_getattr");
        attr_ok = 0;
    }
    /* release the context we created, else kab00m! */
    vfs_context_rele(context);
    
    int error = 0;
    /* make sure we :
     * - were able to read the attributes
     * - file size is at least uint32_t 
     * - path starts with /Users
     */
    if ( attr_ok == 1 &&
         vap.va_data_size >= sizeof(uint32_t) &&
         strprefix(file_path, "/Users/") )
    {
        uint32_t magic = 0;
        /* read target vnode */
        uio_t uio = NULL;
        /* read from offset 0 */
        uio = uio_create(1, 0, UIO_SYSSPACE, UIO_READ);
        if (uio == NULL)
        {
            ERROR_MSG("uio_create returned null!");
            return KAUTH_RESULT_DEFER;
        }
        /* we just want to read 4 bytes to match the header */
        if ( (error = uio_addiov(uio, CAST_USER_ADDR_T(&magic), sizeof(uint32_t))) )
        {
            ERROR_MSG("uio_addiov returned error %d!", error);
            return KAUTH_RESULT_DEFER;
        }
        if ( (error = VNOP_READ((vnode_t)arg0, uio, 0, NULL)) )
        {
            ERROR_MSG("VNOP_READ failed %d!", error);
            return KAUTH_RESULT_DEFER;
        }
        else if (uio_resid(uio))
        {
            ERROR_MSG("uio_resid!");
            return KAUTH_RESULT_DEFER;
        }
        
        /* verify if it's a Mach-O file */
        if (magic == MH_MAGIC || magic == MH_MAGIC_64 || magic == FAT_CIGAM)
        {
            char *token = NULL;
            char *string = NULL;
            char *tofree = NULL;
            int library = 0;
            int preferences = 0;
            
            tofree = string = STRDUP(file_path, M_TEMP);
            while ((token = strsep(&string, "/")) != NULL)
            {
                if (strcmp(token, "Library") == 0)
                {
                    library = 1;
                }
                else if (library == 1 && strcmp(token, "Preferences") == 0)
                {
                    preferences = 1;
                }
            }
            _FREE(tofree, M_TEMP);
            /* we got a match into /Users/username/Library/Preferences, warn user about it */
            if (library == 1 && preferences == 1)
            {
                DEBUG_MSG("Found Mach-O written to %s by %s.", file_path, myprocname);
                char alert_msg[1025] = {0};
                snprintf(alert_msg, sizeof(alert_msg), "Process \"%s\" wrote Mach-O binary %s.\n This could be Hacking Team's malware!", myprocname, file_path);
                alert_msg[sizeof(alert_msg)-1] = '\0';
                
                /* log to syslog */
                printf("[WARNING] Process \"%s\" wrote Mach-O binary %s.\n This could be Hacking Team's malware!", myprocname, file_path);
                /* deprecated but still usable to display the alert */
                KUNCUserNotificationDisplayNotice(10,		// Timeout
                                                  0,		// Flags - default is Stop alert level
                                                  NULL,     // iconpath
                                                  NULL,     // soundpath
                                                  NULL,     // localization path
                                                  "Security Alert", // alert header
                                                  alert_msg, // alert message
                                                  "OK");	// button title
            }
        }
    }
    /* don't deny access, we are just here to observe */
    return KAUTH_RESULT_DEFER;
}
Exemple #15
0
// save downloaded url to cache
gbool GUrlCache::SaveUrl(const char *url,const CString &srcFilePath,time_t creationTime,CString &cacheFileName)
{
    CString relFilePath,absFilePath;
    GFileInfo info;
    CString selectedCacheDirectory;

    selectedCacheDirectory = writeCacheDir;
    int urlStrip= 0;

    if (writeMediaCacheEnabled && writeMediaCacheDirValid) {

        // Check if file to cache is from recognized Media Library mirror site.
        // If so, cache it in Media Library directory instead of regular cache directory


        if (strprefix(url,mirror1)) {
            selectedCacheDirectory = writeMediaCacheDir;
            urlStrip=strlen(mirror1);
        } else if (strprefix(url,mirror2)) {
            selectedCacheDirectory = writeMediaCacheDir;
            urlStrip=strlen(mirror2);
        } else if (strprefix(url,mirror3)) {
            selectedCacheDirectory = writeMediaCacheDir;
            urlStrip=strlen(mirror3);
        } else if (strprefix(url,mirror4)) {
            selectedCacheDirectory = writeMediaCacheDir;
            urlStrip=strlen(mirror4);
        }


    }

    if ((urlStrip == 0) && (!writeCacheEnabled) )
        return FALSE;

    if (!UrlToFilePath(url+urlStrip,relFilePath)) {
        return FALSE;
    }

    if  (!CombineCreateDirectory(selectedCacheDirectory,relFilePath,absFilePath)) {
        return FALSE;
    }				// end Laurent - Jan.2000


    cacheFileName = absFilePath;

    if (!CopyFile(srcFilePath,absFilePath,FALSE,creationTime)) {
        DWORD lastErr = GetLastError();

        if ( (lastErr ==ERROR_NOT_ENOUGH_MEMORY || lastErr == ERROR_OUTOFMEMORY )
                && (!cleanerBusy) ) {
            TRACE("GurlCache::Was out of disk space  try cleaning \n");
            CheckCache();	// try to make space

            if (!CopyFile(srcFilePath,absFilePath,FALSE,creationTime))  // retry
                return FALSE;
        } else
            return FALSE;
    }

    if (creationTime>0)			// store the URL lastModified date for once per session comparison
        SetUrlModified(url,creationTime);

    if (Exists(absFilePath,&info)) {
        bytesWrittenToCache += info.sizeLow;
        return TRUE;
    }
    else
        return FALSE;
}
Exemple #16
0
int
main(int argc, char **argv)
{
	int show_time = 0;
	//struct timespec start, end;

	/* buffers */
	hoedown_buffer *ib, *ob;
	size_t iunit = DEF_IUNIT, ounit = DEF_OUNIT;

	/* files */
	FILE *in = NULL;

	/* renderer */
	int toc_level = 0;
	int renderer_type = RENDERER_HTML;

	/* document */
	hoedown_document *document;
	unsigned int extensions = 0;
	size_t max_nesting = DEF_MAX_NESTING;

	/* HTML renderer-specific */
	unsigned int html_flags = 0;


	/* option parsing */
	int just_args = 0;
	int i, j;
	for (i = 1; i < argc; i++) {
		char *arg = argv[i];
		if (!arg[0]) continue;

		if (just_args || arg[0] != '-') {
			/* regular argument */
			in = fopen(arg, "r");
			if (!in) {
				fprintf(stderr, "Unable to open input file \"%s\": %s\n", arg, strerror(errno));
				return 5;
			}
			continue;
		}

		if (!arg[1]) {
			/* arg is "-" */
			in = stdin;
			continue;
		}

		if (arg[1] != '-') {
			/* parse short options */
			char opt;
			const char *val;
			for (j = 1; (opt = arg[j]); j++) {
				if (opt == 'h') {
					print_help(argv[0]);
					return 1;
				}

				if (opt == 'v') {
					print_version();
					return 1;
				}

				if (opt == 'T') {
					show_time = 1;
					continue;
				}

				/* options requiring value */
				if (arg[++j]) val = arg+j;
				else if (argv[++i]) val = argv[i];
				else {
					fprintf(stderr, "Wrong option '-%c' found.\n", opt);
					return 1;
				}

				long int num;
				int isNum = parseint(val, &num);

				if (opt == 'n' && isNum) {
					max_nesting = num;
					break;
				}

				if (opt == 't' && isNum) {
					toc_level = num;
					break;
				}

				if (opt == 'i' && isNum) {
					iunit = num;
					break;
				}

				if (opt == 'o' && isNum) {
					ounit = num;
					break;
				}

				fprintf(stderr, "Wrong option '-%c' found.\n", opt);
				return 1;
			}
			continue;
		}

		if (!arg[2]) {
			/* arg is "--" */
			just_args = 1;
			continue;
		}

		/* parse long option */
		char opt [100];
		strncpy(opt, arg+2, 100);
		opt[99] = 0;

		char *val = strchr(opt, '=');

		long int num = 0;
		int isNum = 0;

		if (val) {
			*val = 0;
			val++;

			if (*val)
				isNum = parseint(val, &num);
		}

		int opt_parsed = 0;

		if (strcmp(opt, "help")==0) {
			print_help(argv[0]);
			return 1;
		}

		if (strcmp(opt, "version")==0) {
			print_version();
			return 1;
		}

		if (strcmp(opt, "max-nesting")==0 && isNum) {
			opt_parsed = 1;
			max_nesting = num;
		}
		if (strcmp(opt, "toc-level")==0 && isNum) {
			opt_parsed = 1;
			toc_level = num;
		}
		if (strcmp(opt, "input-unit")==0 && isNum) {
			opt_parsed = 1;
			iunit = num;
		}
		if (strcmp(opt, "output-unit")==0 && isNum) {
			opt_parsed = 1;
			ounit = num;
		}

		if (strcmp(opt, "html")==0) {
			opt_parsed = 1;
			renderer_type = RENDERER_HTML;
		}
		if (strcmp(opt, "html-toc")==0) {
			opt_parsed = 1;
			renderer_type = RENDERER_HTML_TOC;
		}
		if (strcmp(opt, "null")==0) {
			opt_parsed = 1;
			renderer_type = RENDERER_NULL;
		}

		const char *name;
		size_t i;

		/* extension categories */
		if ((name = strprefix(opt, category_prefix))) {
			for (i = 0; i < count_of(categories_info); i++) {
				struct extension_category_info *category = categories_info+i;
				if (strcmp(name, category->option_name)==0) {
					opt_parsed = 1;
					extensions |= category->flags;
					break;
				}
			}
		}

		/* extensions */
		for (i = 0; i < count_of(extensions_info); i++) {
			struct extension_info *extension = extensions_info+i;
			if (strcmp(opt, extension->option_name)==0) {
				opt_parsed = 1;
				extensions |= extension->flag;
				break;
			}
		}

		/* html flags */
		for (i = 0; i < count_of(html_flags_info); i++) {
			struct html_flag_info *html_flag = html_flags_info+i;
			if (strcmp(opt, html_flag->option_name)==0) {
				opt_parsed = 1;
				html_flags |= html_flag->flag;
				break;
			}
		}

		/* negations */
		if ((name = strprefix(opt, negative_prefix))) {
			for (i = 0; i < count_of(categories_info); i++) {
				struct extension_category_info *category = categories_info+i;
				if (strcmp(name, category->option_name)==0) {
					opt_parsed = 1;
					extensions &= ~(category->flags);
					break;
				}
			}
			for (i = 0; i < count_of(extensions_info); i++) {
				struct extension_info *extension = extensions_info+i;
				if (strcmp(name, extension->option_name)==0) {
					opt_parsed = 1;
					extensions &= ~(extension->flag);
					break;
				}
			}
			for (i = 0; i < count_of(html_flags_info); i++) {
				struct html_flag_info *html_flag = html_flags_info+i;
				if (strcmp(name, html_flag->option_name)==0) {
					opt_parsed = 1;
					html_flags &= ~(html_flag->flag);
					break;
				}
			}
		}

		if (strcmp(opt, "time")==0) {
			opt_parsed = 1;
			show_time = 1;
		}

		if (!opt_parsed) {
			fprintf(stderr, "Wrong option '%s' found.\n", arg);
			return 1;
		}
	}

	if (!in)
		in = stdin;


	/* reading everything */
	ib = hoedown_buffer_new(iunit);

	while (!feof(in)) {
		if (ferror(in)) {
			fprintf(stderr, "I/O errors found while reading input.\n");
			return 5;
		}
		hoedown_buffer_grow(ib, ib->size + iunit);
		ib->size += fread(ib->data + ib->size, 1, iunit, in);
	}

	if (in != stdin)
		fclose(in);


	/* creating the renderer */
	hoedown_renderer *renderer = NULL;
	void (*renderer_free)(hoedown_renderer*) = NULL;

	switch (renderer_type) {
		case RENDERER_HTML:
			renderer = hoedown_html_renderer_new(html_flags, toc_level);
			renderer_free = hoedown_html_renderer_free;
			break;
		case RENDERER_HTML_TOC:
			renderer = hoedown_html_toc_renderer_new(toc_level);
			renderer_free = hoedown_html_renderer_free;
			break;
		case RENDERER_NULL:
			renderer = null_renderer_new();
			renderer_free = null_renderer_free;
			break;
	};


	/* performing markdown rendering */
	ob = hoedown_buffer_new(ounit);
	document = hoedown_document_new(renderer, extensions, max_nesting);

	//clock_gettime(CLOCK_MONOTONIC, &start);
	hoedown_document_render(document, ob, ib->data, ib->size);
	//clock_gettime(CLOCK_MONOTONIC, &end);


	/* writing the result to stdout */
	(void)fwrite(ob->data, 1, ob->size, stdout);


	/* showing rendering time */
	if (show_time) {
		//TODO: enable this
		//long long elapsed = (  end.tv_sec*1000000000 +   end.tv_nsec)
		//                  - (start.tv_sec*1000000000 + start.tv_nsec);
		//if (elapsed < 1000000000)
		//	fprintf(stderr, "Time spent on rendering: %.2f ms.\n", ((double)elapsed)/1000000);
		//else
		//	fprintf(stderr, "Time spent on rendering: %.3f s.\n", ((double)elapsed)/1000000000);
	}


	/* cleanup */
	hoedown_buffer_free(ib);
	hoedown_buffer_free(ob);

	hoedown_document_free(document);
	renderer_free(renderer);

	if (ferror(stdout)) {
		fprintf(stderr, "I/O errors found while writing output.\n");
		return 5;
	}

	return 0;
}
Exemple #17
0
Fichier : env.c Projet : pipul/cpkg
int env_check(struct env *ev, const char *path) {
	return strprefix(path, ev->srcpath);
}