コード例 #1
0
ファイル: busca.c プロジェクト: bejar37/Busca
int f_type(struct dirent *dp, opt_t *opt)
{
	struct stat statbuf;
	char *crit = opt->criteria;
	char *nombre = malloc(strlen(dp->d_name) + strlen(opt->dir_busca) + 2);
	int si_fich;

	int correcto = 0;

	if ((strcmp("f", crit)) && (strcmp("d", crit))){
		sal_err(OPTERR);
	}

	si_fich = !strcmp("f", crit);
	formar(nombre, dp->d_name, opt->dir_busca);
	stat(nombre, &statbuf);

	if (S_ISREG(statbuf.st_mode) && si_fich)
		correcto = 1;

	else if (S_ISDIR(statbuf.st_mode) && !si_fich)
		correcto = 1;

	free(nombre);

	return correcto;
}
コード例 #2
0
ファイル: busca.c プロジェクト: bejar37/Busca
int permissions(struct dirent *dp, opt_t *opt)
{
	int ret;

	char *crit = opt->criteria;
	char *nombre = malloc(strlen(dp->d_name) + strlen(opt->dir_busca) + 2);
	formar(nombre, dp->d_name, opt->dir_busca);

	if (!(strcmp(crit, "r")))
		ret = euidaccess(nombre, R_OK);
	else if (!(strcmp(crit, "x")))
		ret = euidaccess(nombre, X_OK);
	else if (!(strcmp(crit, "w")))
		ret = euidaccess(nombre, W_OK);
	else
		sal_err(OPTERR);

	if (!ret)
		ret = 1; /*euidaccess returns 0 if access granted, else returns
			   -1*/

	free(nombre);

	return ret;
}
コード例 #3
0
ファイル: main.c プロジェクト: fferegrino/analizando-algo
long long fibonacciCola(int n) {
    Cola cola;
    if (n == 0) return 0;
    if (n == 1) return 1;
    creaLista(&cola);
    formar(&cola, 0);
    formar(&cola, 1);
    int i;
    for (i = 2; i <= n; i++) {
        // Quitamos el elemento menos reciente
        long long fi1 = atender(&cola);
        // Obtenemos el valor del siguiente más reciente
        long long fi2 = valorPrincipio(&cola);
        // Almacenamos el resultado en la cola
        formar(&cola, fi1 + fi2);
    }
    return valorFinal(&cola);
}
コード例 #4
0
ファイル: busca.c プロジェクト: bejar37/Busca
int parse_dir(opt_t *opciones, filtfunc filter, char **nombres)
{
	/* LOOK INTO THE fts_open function instead - 30/3/2011 */
	int fret = 0;
	DIR *dirp;
	struct dirent *dp;
	char *nombre;

	int numdir = 0;

	int i;
	char *busc = (*opciones).dir_busca;

	if (!(dirp = opendir(busc))){
		sal_err(DIRERR);
		exit(2);
	}

	dp = readdir(dirp);

	while(dp){
		numdir++;
		dp = readdir(dirp);
	}

	rewinddir(dirp);

	i = 0;
	dp = readdir(dirp);

	while(dp){

		if (i > 255)
			break;
		if (dp->d_name[0] != '.')
			fret = (*filter) (dp, opciones);

		if (fret > 0){
			nombre = malloc(strlen(dp->d_name) +
					strlen(opciones->dir_busca) + 2);
			formar(nombre, dp->d_name, opciones->dir_busca);
			nombres[i++] = strdup(nombre);
			free(nombre);
		}
		dp = readdir(dirp);
		fret = 0;
	}
	closedir(dirp);
	return i;
}
コード例 #5
0
ファイル: busca.c プロジェクト: bejar37/Busca
int f_map(struct dirent *dp, opt_t *opt)
{
	struct stat statbuf;
	int ret = 0;
	char *crit = opt->criteria;
	char *nombre = malloc(strlen(dp->d_name) + strlen(opt->dir_busca) + 2);
	char *f1;
	char *cont;
	int fd;
	
	formar(nombre, dp->d_name, opt->dir_busca);

	if ((fd = open(nombre, O_RDONLY)) < 0){
		sal_err(FILERR);
		free(nombre);
		return ret;
	}

	stat(nombre, &statbuf);

	if (S_ISDIR(statbuf.st_mode)){
		free(nombre);
		return ret;
	}

	f1 = (char *) mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);

	if (f1 == (caddr_t) -1){
		free(nombre);
		return ret;
	}


	cont = strstr(f1, crit);


	if (cont != NULL)
		ret = 1;

	munmap(f1, statbuf.st_size);
	close(fd);
	free(nombre);

	return ret;
}
コード例 #6
0
ファイル: busca.c プロジェクト: bejar37/Busca
int f_read(struct dirent *dp, opt_t *opt)
{
	int ret = 0;
	char *crit = opt->criteria;
	char *nombre = malloc(strlen(dp->d_name) + strlen(opt->dir_busca) + 2);
	FILE *f;
	int lkahead;
	char *cmp;
	struct stat statbuf;

	formar(nombre, dp->d_name, opt->dir_busca);

	f = fopen(nombre, "r");
	stat(nombre, &statbuf);
	lkahead = statbuf.st_size;
	cmp = (char *) malloc(lkahead);

	if (f == NULL){
		if (S_ISDIR(statbuf.st_mode))
			sal_err(DIRERR);
		sal_err(FILERR);
		free(cmp);
		free(nombre);
		return ret;
	}

		
	
	while(fgets(cmp, lkahead, f) != NULL){
		if (strstr(cmp, crit) != NULL){
			ret = 1;
			free(cmp);
			free(nombre);
			fclose(f);
			return ret;
		}
	}

	free(cmp);
	free(nombre);
	fclose(f);
	return 0;
}