Example #1
0
void			complete_word(t_line **line, t_le *le)
{
	char		**path_and_word;
	char		*buffer;
	t_arbre		*arbre;
	t_arbre		*tmp;
	int			i;

	i = -1;
	(void)le;
	arbre = NULL;
	path_and_word = get_actual_word(*line);
	buffer = ft_memalloc(sizeof(char) * 1024);
	creer_arbre(&arbre, path_and_word[0]);
	tmp = arbre;
	arbre = recherche(&arbre, path_and_word[1]);
	completion_tree(arbre, &buffer, 0);
	while (buffer[++i])
		add_to_line(le, line, buffer[i]);
	if (is_folder(path_and_word[0], path_and_word[1], buffer))
		add_to_line(le, line, '/');
	free_arbre(&tmp);
	free(buffer);
	free(path_and_word[0]);
	free(path_and_word[1]);
}
std::vector<std::string> folder_wildcard (const std::string& directory, const std::string& wild, bool subdirs, bool files)
{
  std::string dir = directory.empty() ? std::string(".") : directory;
  std::vector<std::string> results;
#ifdef _WIN32
  std::string wildcard = create_filespec(dir, wild);
  long handle = -1;
  _finddata_t fileinfo;
  for (bool OK = (handle = _findfirst((char*)wildcard.c_str(), &fileinfo)) != -1; OK; OK = (_findnext(handle, &fileinfo)==0))
  {
    std::string strentry = fileinfo.name;
    if (strentry.compare(".")!=0 && strentry.compare("..")!=0)
      if ((subdirs && (fileinfo.attrib & _A_SUBDIR)) || (files && !(fileinfo.attrib & _A_SUBDIR)))
        results.push_back(strentry);
  }
  _findclose(handle);
#else
  DIR* d = opendir(dir.c_str());
  if (d)
  {
    for (dirent* entry = readdir(d); entry; entry = readdir(d))
    {
      std::string strentry = entry->d_name;
      if (strentry.compare(".")!=0 && strentry.compare("..")!=0)
      {
        std::string subpath = create_filespec(dir, strentry);
        if (((subdirs && is_folder(subpath)) || (files && is_file(subpath))) && (match_wildcard(wild, strentry)))
          results.push_back(strentry);
      }
    }
    closedir(d);
  }
#endif
  return results;
}
Example #3
0
int init(int argc, char **argv, struct config *status)
{
	status->port = NULL;
	status->root_dir = NULL;
	status->backlog = 1024;
	static struct option long_options[] = {
		{"help", no_argument, 0, 'h'},
		{"port", required_argument, 0, 'p'},
		{"root-dir", required_argument, 0,  'r' },
		{"backlog", required_argument, 0, 'b'},
		{0, 0, 0, 0}
	};
	int c, option_index;
	while ((c = getopt_long(argc, argv, "hp:r:b:", long_options, &option_index)) != -1) {
		switch (c) {
		case 'h': {
			printf("Usage: %s [OPTIONS]\n", argv[0]);
			printf("\t--help    \tDisplay this message\n");
			printf("\t--port    \tSet the port of the server\n");
			printf("\t--root-dir\tSet the root directory of the server\n");
			printf("\t--backlog\tSet the max incoming connections to listen to\n");
			exit(EXIT_SUCCESS);
		}
		case 'b': {
				check_digit(optarg);
				status->backlog = (int)strtol(optarg, NULL, 10);
				break;
			}
		case 'p': {
			check_digit(optarg);
			status->port = strdup(optarg);
			break;
		}
		case 'r': {
			if (is_folder(optarg) == -1) {
				fprintf(stderr, "\"%s\" is not a directory\n", optarg);
				exit(EXIT_FAILURE);
			}
			if (status->root_dir) {
				free(status->root_dir);
			}
			status->root_dir = strdup(optarg);
			break;
		}
		default:
			break;
		}
	}
	if (status->port == NULL) {
		uid_t uid = geteuid();
		status->port = strdup(uid == 0 ? "80" : "8080");
	}
	if (status->root_dir == NULL) {
		status->root_dir = get_current_dir_name();
	}
	return 0;
}
Example #4
0
/** Inline helper to get the parent folder and length of a the parent quickly */
static inline bool get_dirname(wchar_t* sExecutable, size_t lpszExecutable, wchar_t* sParentOut, size_t* lpszParentOut)
{
	if(!lstrcpynW(sParentOut, sExecutable, (int)lpszExecutable)) return false;
	*lpszParentOut = lpszExecutable;
	while(sParentOut[--*lpszParentOut] != L'\\' && *lpszParentOut) sParentOut[*lpszParentOut] = L'\0';
	
	// Remove the trailing new line.
	if(*lpszParentOut) sParentOut[*lpszParentOut] = L'\0';
	return (*lpszParentOut > 0) && is_folder(sParentOut);
}
Example #5
0
void list_all_files(char* directory)
{
	DIR *dir;
	struct dirent *ent;
	
	unsigned char temp;
	
	#if MAX_LENGH < 255
		unsigned char i;
	#elif MAX_LENGH < 65000
		unsigned short i;
	#else
		unsigned long i;
	#endif

	short pc;
	
	/* Reset all the stored files to zero */
	for(i=0;i<MAX_LENGH;i++)
	{
		strcpy(file_name[i], "");
	}
	
	i = 0;
	temp = 0;
	pc = -1;
	numb_files = 0;
	
	if ((dir = opendir (directory)) != NULL) 
	{
		while ( (ent = readdir (dir)) != NULL ) 
		{
			/* Add the .. string and then after, reject them all*/
			if (i == 0)
			{
				strcpy(file_name[i], "..");
				file_type[i] = TUR_C;
				i++;
				numb_files++;
			}
			
			/* Finds .tns occurence */
#ifdef _TINSPIRE
			char* pch = strstr (ent->d_name,".tns");
#else
			char* pch = strstr (ent->d_name,FORMAT_FILE);
#endif
			
			/* Reject these two signs and the executable itself */
			char* pch2 = strstr (ent->d_name,"..");
			char* pch3 = strstr (ent->d_name,EXECUTABLE_NAME);
			pc = strncmp (ent->d_name, ".", 2);
			
			/* Check if file in question is a folder */
			temp = is_folder(ent->d_name);
			
			/* If file has ".tns" extension, is a folder and is not ".." and "." */
			if ((pch2 == NULL && pch3 == NULL && pc != 0))
			{
				/* Copy string cotent from ent->d_name to file_name[i]*/
				strcpy(file_name[i], ent->d_name);
				
				if (pch != NULL)
				{
					file_type[i] = BLUE_C;
				}
				else if (temp)
				{
					file_type[i] = F_C;
				}
				else
				{
					file_type[i] = GREEN_C;
				}

				i++;
				numb_files++;
			}
			
		}
		closedir (dir);
	} 
	else 
	{
		perror ("Not a directory");
	}
	
	numb_files = numb_files - 1;
}
Example #6
0
/**
 * Simple inline helper to check if a given path exists, and
 * represent a file.
 */
static inline bool is_file(wchar_t* path)
{
	return exists(path) && !is_folder(path);
}
bool folder_exists (const std::string& directory)
{
  return is_folder(directory);
}
Example #8
0
int main(int argc, char* argv[]) 
{
	/* Buffer to hold =>the current directory */
	char* buf = NULL;
	unsigned char exit_app = 0;
#ifdef EXECUTE_APP
	unsigned char file_chosen;
	unsigned char done = 1;
#endif
	
	/* Temporary array, used for starting executable*/
	char file_to_start[MAX_LENGH];
	
	/* Init video and variables */
	init();
	
	clear_entirescreen();
	
	/* Set it to the current directory. */
	currentdir = getcwd(buf, MAX_LENGH);
	
	/* List 12 files to be shown on screen (here, it is the first chunck) */
	list_all_files(currentdir);
	
	update_entirescreen();
	
	/* Refresh everything on screen */
	refresh_cursor(1);
	
	while (exit_app==0) 
	{
		while (button_state[6]<1)
		{
			/* Call function for input */
			controls();
			
				/* If Up button is pressed down... (or Left button held) */
				if (button_state[0] == 1 || button_state[2] > 0)
				{
					if (choice > 0) 
					{
						choice--;
						refresh_cursor(0);
						set_fileid();
					}
					else if (scroll_choice > 0)
					{
						choice = 11;
						scroll_choice = scroll_choice - 1;
						refresh_cursor(3);
						set_fileid();
					}
				}
				/* If Down button is pressed down... (or Right button held) */
				else if (button_state[1] == 1 || button_state[3] > 0)
				{
					/* Don't let the user to scroll more than there are files... */
					if (fileid_selected < numb_files)
					{
						if (choice < 11) 
						{
							choice++;	
							refresh_cursor(0);
							set_fileid();
						}
						/* If the user wants to go down and there are more files, change the files to shown to the next one (thanks to scroll_choice) */
						else if (numb_files > 10)
						{
							scroll_choice = scroll_choice + 1;
							choice = 0;
							set_fileid();
							refresh_cursor(3);
						}
					}
				}
			
				if (button_state[6] == 1)
				{
					#ifdef EXECUTE_APP
						file_chosen = 0;
					#endif
					exit_app = 1;
				}
			
			
				/* If Control/Return button is pressed... */
				if (button_state[4]==1 || button_state[5]==1)
				{
					/* If file is a tns file then launch it */
					
					if (file_type[fileid_selected] == BLUE_C) 
					{
						snprintf(file_to_start, MAX_LENGH, "%s/%s", currentdir, file_name[fileid_selected]);
						#ifdef EXECUTE_APP
							file_chosen = 1;
							button_state[6] = 1;
						#elif defined(_TINSPIRE)
							nl_exec(file_to_start, 0, NULL);
						#endif
					}
					/* If not then it is a folder, if thats the case then go to that folder */
					else if (file_type[fileid_selected] == F_C || choice == 0) 
					{
						goto_folder();
					}
				}
				
				if (button_state[8]==1)
				{
					if (!is_folder(file_name[fileid_selected]))
						remove_file();
				}
				
			
			/* Don't waste CPU cycles */
			#ifndef ndlib
				SDL_Delay(16);
			#endif
		}
	
		#ifdef EXECUTE_APP
			if (file_chosen == 1)
			{
				#ifdef ndlib
					deinitBuffering();
				#else
					if (gui_screen) SDL_FreeSurface(gui_screen);
				#endif
					
				while(done == 1)
				{
					tostart(file_to_start);
					done = 0;
				}
					
				done = 1;
				file_chosen = 0;
				
				init();
				
				clear_entirescreen();
				currentdir = getcwd(buf, MAX_LENGH);
				list_all_files(currentdir);
				update_entirescreen();
				refresh_cursor(2);
			}
			else
			{
				exit_app = 1;
			}
		#endif
	}


#ifdef ndlib
	clearBufferB();
	deinitBuffering();
#else
	if (gui_screen != NULL) SDL_FreeSurface(gui_screen);
	SDL_QuitSubSystem(SDL_INIT_VIDEO);
	SDL_Quit();
#endif

	exit(0);
}