示例#1
0
my_qsort(struct Edge* arr, int l , int h)
{
  int pivot, i , j;
  struct Edge temp;
  pivot = l;
  i = l;
  j = h;
  if(i < j)
  {
    while(i < j)
    {
      while(arr[i].wt <= arr[pivot].wt)
        i++;

      while(arr[j].wt > arr[pivot].wt)
        j--;

      if(i < j)
      {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }

    temp = arr[j];
    arr[j] = arr[pivot];
    arr[pivot] = temp;

    my_qsort(arr, l , j-1);
    my_qsort(arr, j+1, h);
  }

}
int send_variant_2_list(MEM_ROOT *mem_root, Protocol *protocol,
			List<String> *names,
			const char *cat, String *source_name)
{
  DBUG_ENTER("send_variant_2_list");

  String **pointers= (String**)alloc_root(mem_root,
					  sizeof(String*)*names->elements);
  String **pos;
  String **end= pointers + names->elements;

  List_iterator<String> it(*names);
  for (pos= pointers; pos!=end; (*pos++= it++)) ;

  my_qsort(pointers,names->elements,sizeof(String*),string_ptr_cmp);

  for (pos= pointers; pos!=end; pos++)
  {
    protocol->prepare_for_resend();
    if (source_name)
      protocol->store(source_name);
    protocol->store(*pos);
    protocol->store(cat,1,&my_charset_latin1);
    if (protocol->write())
      DBUG_RETURN(-1);
  }

  DBUG_RETURN(0);
}
示例#3
0
/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int threeSumClosest(int* nums, int numsSize, int target) {
	int result = 0x7fffffff, bestResidual = 0x7fffffff;

	if (numsSize <= 3) {
		result = 0;
		for (int i = 0; i < numsSize; ++i) result += nums[i];
		return result;
	}

	my_qsort(nums, 0, numsSize - 1);

	for (int i = 0; i < numsSize - 2; ++i) {
		int j = i + 1, k = numsSize - 1;
		while (j < k) {
			int sum = nums[i] + nums[j] + nums[k];
			if (sum > target) {
				if (sum - target < bestResidual) {
					bestResidual = sum - target;
					result = sum;
				}
				--k;
			}
			else if (sum < target) {
				if (target - sum < bestResidual) {
					bestResidual = target - sum;
					result = sum;
				}
				++j;
			}
			else return target;
		}
	}
	return result;
}
示例#4
0
文件: test.c 项目: mzhaolz/hpc
Point run(void* arri_1, void* arri_2, size_t num, size_t size, int (*compar) (const void*, const void*))
{
	clock_t start;
	//assert(are_equal(arri_1, arri_2, num, size, compar));
	clock_t difference;	clock_t difference_std;
	start = clock();
	qsort(arri_2, num, size, compar);	
	difference_std = clock() - start;
	//assert(!is_ordered(arri_2, num, size, compar));
	//time the standard library
	start = clock();
	my_qsort(arri_1, num, size, compar);
	difference = clock() - start;	
	//we're just testing for speed now
	//print_int_array(arri_1, num);
	//print_int_array(arri_2, num);
	assert(are_equal(arri_1, arri_2, num, size, compar));
	free(arri_1); free(arri_2);
	Point p = { .x = difference * 1000/CLOCKS_PER_SEC, .y = difference_std * 1000/CLOCKS_PER_SEC};
	return p;
}

Point test_int_array(size_t num, size_t size) 
{
	void* compar = &compar_int;
	int* arri_1 = random_int_array(num);
	int* arri_2 = (int*) duplicate_array(arri_1, num, size);
	return run(arri_1, arri_2, num, size, compar);
}
示例#5
0
void my_qsort(void *v[], int left, int right, int (*comp)(void *, void *, ...)) {
	int i, last;
	void swap(void *v[], int, int);
	
	if (left >= right)
		return;
		
	swap(v, left, (left + right)/2);
	last = left;
	for (i = left+1; i <= right; i++)
		if ((*comp)(v[i], v[left]) < 0)
			swap(v, ++last, i);
	swap(v, left, last);
	my_qsort(v, left, last-1, comp);
	my_qsort(v, last+1, right, comp);
}
示例#6
0
void my_qsort(char * plines[], int left, int right)
{
	int last;

	if (left >= right)
		return;

	swap(&plines[left], &plines[(left+right)/2]);
	last = left;

	int i;
	for(i=left+1; i<=right; ++i)
		if (strcmp(plines[left],plines[i]) > 0)
			swap(&plines[++last], &plines[i]);

	swap(&plines[left], &plines[last]);
	my_qsort(plines, left, last-1);
	my_qsort(plines, last+1, right);
}
示例#7
0
void my_qsort (int array[], int a, int b)
{
        int bit = 0;
	int mid, p, q, tmp, i, j;
	if (b - a <= 24)
	{
		for (i = a + 1; i <= b; i++)
		{
			tmp = array[i];
			j = i - 1;
			while (array[j] > tmp && j >= a)
			{
				array[j + 1] = array[j];
				array[j] = tmp;
				j--;
			}
		}
		return;
	}
	init_mid (array, a, b);
	p = q = a;
	mid = array[b];
	while (q != b)
	{
		if (bit)
			bit = 0;
		else
			bit = 1;
		if (array[q] < mid + bit)
		{
			tmp = array[p];
			array[p] = array[q];
			array[q] = tmp;
			p++;
		}
		q++;
	}
	array[b] = array[p];
	array[p] = mid;
	my_qsort (array, a, p - 1);
	my_qsort (array, p + 1, b);
}
示例#8
0
文件: qsort.c 项目: AshKash/kit-sink
void my_qsort(int a[], int begin, int end)
{
    if (end > begin) {
        int pivot = a[rand() * (end - begin) / RAND_MAX + begin];
        int l = begin + 1;
        int r = end;
        while (l<r) {
            iter++;
            if (a[l] <= pivot)
                l++;
            else {
                r--;
                SWAP(a[l], a[r]);
            }
        }
        l--;
        SWAP(a[begin], a[l]);
        my_qsort(a, begin, l);
        my_qsort(a, r, end);
    }
}
示例#9
0
int main(int argc, char *argv[])
{
 // sscanf(argv[1],"%d",&n);
 //array = (int*)malloc(n*sizeof(int));


  int i;
	
  my_qsort(ray,0,99);
  for (i=0; i<100; i++){
    printf("%x\n",ray[i]);
  }  	
  return 0;
}
示例#10
0
void my_qsort(int a[], int head, int tail) {
	int i = head, j = tail, temp;
	while (i < j) {
		while (a[i] <= a[j] && i < j)
			++i;
		if (i < j) {
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
			--j;
		}
		while (a[i] <= a[j] && i < j)
			--j;
		if (i < j) {
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
			++i;
		}
	}
	if (head < i - 1) my_qsort(a, head, i - 1);
	if (j + 1 < tail) my_qsort(a, j + 1, tail);
}
示例#11
0
文件: test.c 项目: mzhaolz/hpc
double test_array(void* arri_1, size_t num, size_t size, int (*compar) (const void*, const void*)) 
{
	void* arri_2 = duplicate_array(arri_1, num, size);
	clock_t difference;	
	clock_t start = clock();
	my_qsort(arri_1, num, size, compar);
	difference = clock() - start;	
	//clock_t start = clock();
	qsort(arri_2, num, size, compar);	
	//difference = clock() - start;
	assert(are_equal(arri_1, arri_2, num, size, compar));
	free(arri_2);
	return difference * 1000/CLOCKS_PER_SEC;
}
示例#12
0
void print(part* parts, int num_parts)
{
    printf("Part Number    Part Name                Quantity on Hand\n");
    int number_array[MAX_PARTS] = {0};
    for (int i = 0; i < num_parts; i++)
    {
        number_array[i] = parts[i].number;
    }
    my_qsort (number_array, number_array + num_parts - 1);
    for (int i = 0; i < num_parts; i++)
    {
        int index = find_part(parts, num_parts, number_array[i]);
        printf("%7d        %-25s%11d\n", parts[index].number, parts[index].name, parts[index].on_hand);
    }
    return;
}
示例#13
0
int main()
{
	int nlines; /* Number of lines */
	char * plines[MAX_LINES]; /* Pointers to lines */
	
	if ((nlines = read_lines(plines, MAX_LINES)) < 0)
	{
		fprintf(stderr, "Error: Lack of memory. Input is too large.\n");
		return 1;
	}

	my_qsort(plines, 0, nlines-1);
	write_lines(plines, nlines);

	return 0;
}
示例#14
0
static void collapse_adjacent_pages(void)
{
    int i, j;

    /* collapse adjacent: */
    my_qsort(blockfree, BLOCKFREE_CACHE_SIZE, sizeof(Free_Block), compare_free_block);
    j = 0;
    for (i = 1; i < BLOCKFREE_CACHE_SIZE; i++) {
        if ((blockfree[j].start + blockfree[j].len) == blockfree[i].start) {
            blockfree[j].len += blockfree[i].len;
            blockfree[i].start = NULL;
            blockfree[i].len = 0;
            if (!blockfree[i].zeroed)
                blockfree[j].zeroed = 0;
        } else
            j = i;
    }
}
示例#15
0
int main() {
    char * tab[10000];

    char string[40];
    int n = 0;
    while(gets(string) != NULL) {
        tab[n] = (char*) malloc (40);
        strcpy(tab[n], string);
        n++;
    }

    my_qsort(tab, n);

    for(int i=n-1;i>=0;i--) {
        printf("%s\n", tab[i]);
    }

    return 0;
}
示例#16
0
static void alloc_cache_collapse_pages(FreeBlock *blockfree)
{
  int i;
  int j;

  /* sort by FreeBlock->start */
  my_qsort(blockfree, BLOCKFREE_CACHE_SIZE, sizeof(FreeBlock), free_block_compare);

  /* collapse adjacent: */
  j = 0;
  for (i = 1; i < BLOCKFREE_CACHE_SIZE; i++) {
    if ((blockfree[j].start + blockfree[j].len) == blockfree[i].start) {
      blockfree[j].len += blockfree[i].len;
      blockfree[i].start = NULL;
      blockfree[i].len = 0;
      if (!blockfree[i].zeroed)
        blockfree[j].zeroed = 0;
    } else
      j = i;
  }
}
示例#17
0
文件: main.c 项目: dtbinh/LearnCpp
int main(int argc, char * argv[])
{
    int nlines;
    int numeric = 0;

    if (argc > 1 && strcmp(argv[1], "-n") == 0)
        numeric = 1;
    if ((nlines = readlines(lineptr, MAXLINES)) >= 0)
    {
        my_qsort((void *) lineptr, 0, nlines - 1,
                (int (*)(void *, void *))(numeric ? numcmp : strcmp));
        writelines(lineptr, nlines);
        return 0;
    }
    else
    {
        printf("imput too big to sort\n");
        return 1;
    }

}
示例#18
0
文件: qsort.c 项目: AshKash/kit-sink
int main()
{
    int a[1000000];
    int len = sizeof(a) / sizeof(a[0]);
    int i;

    for (i=0; i<len; i++) {
        a[i] = rand();
        /*printf("%d\n", a[i]);*/
    }

    printf("UNSORTED\n\n");

    my_qsort(a, 0, len);

    for (i=0; i<len; i++) {
        /*printf("%d\n", a[i]);*/
    }
    printf("DONE: %d\n\n", iter);

}
示例#19
0
int kruskalMST(struct Graph *graph)
{
  int v;
  int V = graph->V;
  struct Edge result[V];
  int e = 0 ; // edge counter
  int i = 0 ;     // index for sorted edges
  //step 1: sort the edges in the ascending order
  my_qsort(graph->edge, 0 ,graph->E-1); 
  printfEdges(graph->edge, graph->E);
  //allocate memory for creating subsets
  struct subset *subsets = (struct subset*)malloc(sizeof(struct subset)*V);
  for(v = 0 ; v < V ; v++)
  {
    subsets[v].parent = v;
    subsets[v].rank = 0;
  }

  while(e < V-1)
  {
    struct Edge next_edge = graph->edge[i++];

    int x = find(subsets, next_edge.src);
    int y = find(subsets, next_edge.dest);
    if(x !=  y)
    {
      result[e++] = next_edge;
      Union(subsets,x,y);
    }
  }

  // print the contents of result[] to display the built MST
  printf("Following are the edges in the constructed MST\n");
  for (i = 0; i < e; ++i)
    printf("%d -- %d == %d\n", result[i].src, result[i].dest,
        result[i].wt);
  return 0;

}
示例#20
0
/* sort input lines */
int main(int argc, char *argv[])
{
	int nlines;
	int numeric = 0;
	int rev = 1;
	char c;

	while (--argc > 0 && (*++argv)[0] == '-') { 
		while ((c = *++argv[0])) {
			switch(c) {
				case 'n':
					numeric = 1;
					break;
				case 'r':
					rev = -1;
					break;
				default:
					printf("sortlines: illegal option %c\n", c);
					argc = -1;
					break;
			}
		}
	}
	if (argc != 0)
		printf("Usage: sortlines -n -r");
	else {

		if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
			my_qsort((void **) lineptr, 0, nlines-1, rev,
					(int (*)(void*,void*))(numeric ? numcmp : strcmp));
			writelines(lineptr, nlines);
			return 0;
		} else {
			printf("input too big to sort\n");
			return 1;
		}
	}
}
示例#21
0
int main(int __unused argc, char *argv[])
{
	int nlines;
	int numeric = 0;
	int comp_factor = 1;

	while (*++argv) {
		if (strcmp(argv[0], "-n") == 0)
			numeric = 1;
		if (strcmp(argv[0], "-r") == 0)
			comp_factor = -1;
	}

	if ((nlines = readlines(lineptr, allocbuf, MAXLINES)) >= 0) {
		my_qsort((void **) lineptr, 0, nlines - 1,
			  (int (*)(void*,void*))(numeric ? numcmp : strcmp), comp_factor);
		writelines(lineptr, nlines);
		return 0;
	} else {
		printf("error: input too big to sort\n");
		return 1;
	}
}
示例#22
0
MY_DIR	*my_dir(const char *path, myf MyFlags)
{
  char          *buffer;
  MY_DIR        *result= 0;
  FILEINFO      finfo;
  DYNAMIC_ARRAY *dir_entries_storage;
  MEM_ROOT      *names_storage;
  DIR		*dirp;
  struct dirent *dp;
  char		tmp_path[FN_REFLEN+1],*tmp_file;
#ifdef THREAD
  char	dirent_tmp[sizeof(struct dirent)+_POSIX_PATH_MAX+1];
#endif
  DBUG_ENTER("my_dir");
  DBUG_PRINT("my",("path: '%s' MyFlags: %d",path,MyFlags));

#if defined(THREAD) && !defined(HAVE_READDIR_R)
  mysql_mutex_lock(&THR_LOCK_open);
#endif

  dirp = opendir(directory_file_name(tmp_path,(char *) path));
#if defined(__amiga__)
  if ((dirp->dd_fd) < 0)			/* Directory doesn't exists */
    goto error;
#endif
  if (dirp == NULL || 
      ! (buffer= my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) + 
                           ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
                           sizeof(MEM_ROOT), MyFlags)))
    goto error;

  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); 
  names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
                             ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
  
  if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
                            ENTRIES_START_SIZE, ENTRIES_INCREMENT))
  {
    my_free(buffer);
    goto error;
  }
  init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
  
  /* MY_DIR structure is allocated and completly initialized at this point */
  result= (MY_DIR*)buffer;

  tmp_file=strend(tmp_path);

#ifdef THREAD
  dp= (struct dirent*) dirent_tmp;
#else
  dp=0;
#endif
  
  while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp)))
  {
    if (!(finfo.name= strdup_root(names_storage, dp->d_name)))
      goto error;
    
    if (MyFlags & MY_WANT_STAT)
    {
      if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage, 
                                               sizeof(MY_STAT))))
        goto error;
      
      bzero(finfo.mystat, sizeof(MY_STAT));
      (void) strmov(tmp_file,dp->d_name);
      (void) my_stat(tmp_path, finfo.mystat, MyFlags);
      if (!(finfo.mystat->st_mode & MY_S_IREAD))
        continue;
    }
    else
      finfo.mystat= NULL;

    if (push_dynamic(dir_entries_storage, (uchar*)&finfo))
      goto error;
  }

  (void) closedir(dirp);
#if defined(THREAD) && !defined(HAVE_READDIR_R)
  mysql_mutex_unlock(&THR_LOCK_open);
#endif
  result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
  result->number_off_files= dir_entries_storage->elements;
  
  if (!(MyFlags & MY_DONT_SORT))
    my_qsort((void *) result->dir_entry, result->number_off_files,
          sizeof(FILEINFO), (qsort_cmp) comp_names);
  DBUG_RETURN(result);

 error:
#if defined(THREAD) && !defined(HAVE_READDIR_R)
  mysql_mutex_unlock(&THR_LOCK_open);
#endif
  my_errno=errno;
  if (dirp)
    (void) closedir(dirp);
  my_dirend(result);
  if (MyFlags & (MY_FAE | MY_WME))
    my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,my_errno);
  DBUG_RETURN((MY_DIR *) NULL);
} /* my_dir */
示例#23
0
MY_DIR	*my_dir(const char *path, myf MyFlags)
{
  char          *buffer;
  MY_DIR        *result= 0;
  FILEINFO      finfo;
  DYNAMIC_ARRAY *dir_entries_storage;
  MEM_ROOT      *names_storage;
  DIR		*dirp;
  struct dirent *dp;
  char		tmp_path[FN_REFLEN + 2], *tmp_file;
  char	dirent_tmp[sizeof(struct dirent)+_POSIX_PATH_MAX+1];

  DBUG_ENTER("my_dir");
  DBUG_PRINT("my",("path: '%s' MyFlags: %d",path,MyFlags));

#if !defined(HAVE_READDIR_R)
  mysql_mutex_lock(&THR_LOCK_open);
#endif

  dirp = opendir(directory_file_name(tmp_path,(char *) path));
  if (dirp == NULL || 
      ! (buffer= my_malloc(key_memory_MY_DIR,
                           ALIGN_SIZE(sizeof(MY_DIR)) + 
                           ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
                           sizeof(MEM_ROOT), MyFlags)))
    goto error;

  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); 
  names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
                             ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
  
  if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
                            NULL,               /* init_buffer */
                            ENTRIES_START_SIZE, ENTRIES_INCREMENT))
  {
    my_free(buffer);
    goto error;
  }
  init_alloc_root(key_memory_MY_DIR, names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
  
  /* MY_DIR structure is allocated and completly initialized at this point */
  result= (MY_DIR*)buffer;

  tmp_file=strend(tmp_path);

  dp= (struct dirent*) dirent_tmp;
  
  while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp)))
  {
    if (!(finfo.name= strdup_root(names_storage, dp->d_name)))
      goto error;
    
    if (MyFlags & MY_WANT_STAT)
    {
      if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage, 
                                               sizeof(MY_STAT))))
        goto error;
      
      memset(finfo.mystat, 0, sizeof(MY_STAT));
      (void) my_stpcpy(tmp_file,dp->d_name);
      (void) my_stat(tmp_path, finfo.mystat, MyFlags);
      if (!(finfo.mystat->st_mode & MY_S_IREAD))
        continue;
    }
    else
      finfo.mystat= NULL;

    if (insert_dynamic(dir_entries_storage, (uchar*)&finfo))
      goto error;
  }

  (void) closedir(dirp);
#if !defined(HAVE_READDIR_R)
  mysql_mutex_unlock(&THR_LOCK_open);
#endif
  result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
  result->number_off_files= dir_entries_storage->elements;
  
  if (!(MyFlags & MY_DONT_SORT))
    my_qsort((void *) result->dir_entry, result->number_off_files,
          sizeof(FILEINFO), (qsort_cmp) comp_names);
  DBUG_RETURN(result);

 error:
#if !defined(HAVE_READDIR_R)
  mysql_mutex_unlock(&THR_LOCK_open);
#endif
  my_errno=errno;
  if (dirp)
    (void) closedir(dirp);
  my_dirend(result);
  if (MyFlags & (MY_FAE | MY_WME))
  {
    char errbuf[MYSYS_STRERROR_SIZE];
    my_error(EE_DIR, MYF(0), path,
             my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
  }
  DBUG_RETURN((MY_DIR *) NULL);
} /* my_dir */
示例#24
0
MY_DIR	*my_dir(const char *path, myf MyFlags)
{
  char          *buffer;
  MY_DIR        *result= 0;
  FILEINFO      finfo;
  DYNAMIC_ARRAY *dir_entries_storage;
  MEM_ROOT      *names_storage;
#ifdef __BORLANDC__
  struct ffblk       find;
#else
  struct _finddata_t find;
#endif
  ushort	mode;
  char		tmp_path[FN_REFLEN],*tmp_file,attrib;
#ifdef _WIN64
  __int64       handle;
#else
  long		handle;
#endif
  DBUG_ENTER("my_dir");
  DBUG_PRINT("my",("path: '%s' stat: %d  MyFlags: %d",path,MyFlags));

  /* Put LIB-CHAR as last path-character if not there */
  tmp_file=tmp_path;
  if (!*path)
    *tmp_file++ ='.';				/* From current dir */
  tmp_file= strnmov(tmp_file, path, FN_REFLEN-5);
  if (tmp_file[-1] == FN_DEVCHAR)
    *tmp_file++= '.';				/* From current dev-dir */
  if (tmp_file[-1] != FN_LIBCHAR)
    *tmp_file++ =FN_LIBCHAR;
  tmp_file[0]='*';				/* Windows needs this !??? */
  tmp_file[1]='.';
  tmp_file[2]='*';
  tmp_file[3]='\0';

  if (!(buffer= my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) + 
                          ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
                          sizeof(MEM_ROOT), MyFlags)))
    goto error;

  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); 
  names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
                             ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
  
  if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
                            ENTRIES_START_SIZE, ENTRIES_INCREMENT))
  {
    my_free(buffer);
    goto error;
  }
  init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
  
  /* MY_DIR structure is allocated and completly initialized at this point */
  result= (MY_DIR*)buffer;

#ifdef __BORLANDC__
  if ((handle= findfirst(tmp_path,&find,0)) == -1L)
#else
  if ((handle=_findfirst(tmp_path,&find)) == -1L)
#endif
  {
    DBUG_PRINT("info", ("findfirst returned error, errno: %d", errno));
    if  (errno != EINVAL)
      goto error;
    /*
      Could not read the directory, no read access.
      Probably because by "chmod -r".
      continue and return zero files in dir
    */
  }
  else
  {

    do
    {
#ifdef __BORLANDC__
      attrib= find.ff_attrib;
#else
      attrib= find.attrib;
      /*
        Do not show hidden and system files which Windows sometimes create.
        Note. Because Borland's findfirst() is called with the third
        argument = 0 hidden/system files are excluded from the search.
      */
      if (attrib & (_A_HIDDEN | _A_SYSTEM))
        continue;
#endif
#ifdef __BORLANDC__
      if (!(finfo.name= strdup_root(names_storage, find.ff_name)))
        goto error;
#else
      if (!(finfo.name= strdup_root(names_storage, find.name)))
        goto error;
#endif
      if (MyFlags & MY_WANT_STAT)
      {
        if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage,
                                                 sizeof(MY_STAT))))
          goto error;

        bzero(finfo.mystat, sizeof(MY_STAT));
#ifdef __BORLANDC__
        finfo.mystat->st_size=find.ff_fsize;
#else
        finfo.mystat->st_size=find.size;
#endif
        mode= MY_S_IREAD;
        if (!(attrib & _A_RDONLY))
          mode|= MY_S_IWRITE;
        if (attrib & _A_SUBDIR)
          mode|= MY_S_IFDIR;
        finfo.mystat->st_mode= mode;
#ifdef __BORLANDC__
        finfo.mystat->st_mtime= ((uint32) find.ff_ftime);
#else
        finfo.mystat->st_mtime= ((uint32) find.time_write);
#endif
      }
      else
        finfo.mystat= NULL;

      if (push_dynamic(dir_entries_storage, (uchar*)&finfo))
        goto error;
    }
#ifdef __BORLANDC__
    while (findnext(&find) == 0);
#else
    while (_findnext(handle,&find) == 0);

    _findclose(handle);
#endif
  }

  result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
  result->number_off_files= dir_entries_storage->elements;

  if (!(MyFlags & MY_DONT_SORT))
    my_qsort((void *) result->dir_entry, result->number_off_files,
          sizeof(FILEINFO), (qsort_cmp) comp_names);
  DBUG_PRINT("exit", ("found %d files", result->number_off_files));
  DBUG_RETURN(result);
error:
  my_errno=errno;
#ifndef __BORLANDC__
  if (handle != -1)
      _findclose(handle);
#endif
  my_dirend(result);
  if (MyFlags & MY_FAE+MY_WME)
    my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,errno);
  DBUG_RETURN((MY_DIR *) NULL);
} /* my_dir */
示例#25
0
文件: my_lib.c 项目: isleon/Jaxer
MY_DIR	*my_dir(const char* path, myf MyFlags)
{
  char          *buffer;
  MY_DIR        *result= 0;
  FILEINFO      finfo;
  DYNAMIC_ARRAY *dir_entries_storage;
  MEM_ROOT      *names_storage;
  struct find_t find;
  ushort	mode;
  char		tmp_path[FN_REFLEN],*tmp_file,attrib;
  DBUG_ENTER("my_dir");
  DBUG_PRINT("my",("path: '%s' stat: %d  MyFlags: %d",path,MyFlags));

  /* Put LIB-CHAR as last path-character if not there */

  tmp_file=tmp_path;
  if (!*path)
    *tmp_file++ ='.';				/* From current dir */
  tmp_file= strmov(tmp_file,path);
  if (tmp_file[-1] == FN_DEVCHAR)
    *tmp_file++= '.';				/* From current dev-dir */
  if (tmp_file[-1] != FN_LIBCHAR)
    *tmp_file++ =FN_LIBCHAR;
  tmp_file[0]='*';				/* MSDOS needs this !??? */
  tmp_file[1]='.';
  tmp_file[2]='*';
  tmp_file[3]='\0';

  if (_dos_findfirst(tmp_path,_A_NORMAL | _A_SUBDIR, &find))
    goto error;

  if (!(buffer= my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) + 
                          ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
                          sizeof(MEM_ROOT), MyFlags)))
    goto error;

  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); 
  names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
                             ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
  
  if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
                            ENTRIES_START_SIZE, ENTRIES_INCREMENT))
  {
    my_free((gptr) buffer,MYF(0));
    goto error;
  }
  init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
  
  /* MY_DIR structure is allocated and completly initialized at this point */
  result= (MY_DIR*)buffer;
 
  do
  {
    if (!(finfo.name= strdup_root(names_storage, find.name)))
      goto error;
    
    if (MyFlags & MY_WANT_STAT)
    {
      if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage, 
                                               sizeof(MY_STAT))))
        goto error;
      
      bzero(finfo.mystat, sizeof(MY_STAT));
      finfo.mystat->st_size= find.size;
      mode= MY_S_IREAD; attrib= find.attrib;
      if (!(attrib & _A_RDONLY))
	mode|= MY_S_IWRITE;
      if (attrib & _A_SUBDIR)
	mode|= MY_S_IFDIR;
      finfo.mystat->st_mode= mode;
      finfo.mystat->st_mtime= ((uint32) find.wr_date << 16) + find.wr_time;
    }
    else
      finfo.mystat= NULL;

    if (push_dynamic(dir_entries_storage, (gptr)&finfo))
      goto error;
  
  } while (_dos_findnext(&find) == 0);
  
  result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
  result->number_off_files= dir_entries_storage->elements;
  
  if (!(MyFlags & MY_DONT_SORT))
    my_qsort((void *) result->dir_entry, result->number_off_files,
          sizeof(FILEINFO), (qsort_cmp) comp_names);
  DBUG_RETURN(result);

error:
  my_dirend(result);
  if (MyFlags & MY_FAE+MY_WME)
    my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,errno);
  DBUG_RETURN((MY_DIR *) NULL);
} /* my_dir */