Example #1
0
File: ls.c Project: imbyungjun/TIL
int main(int argc, char **argv) {
	int i;
	int ch, opt = 0;

	/* option handler */
	while ((ch = getopt(argc, argv, "alR")) != -1) {
		switch(ch) {
			case 'a':
				opt |= FLAG_A;
				break;
			case 'l':
				opt |= FLAG_L;
				break;
			case 'R':
				opt |= FLAG_R;
				break;
			default:
				print_usage();
		}
	}

	if ((opt && argc < 3) || (!opt && argc < 2))
		my_ls(".", opt);
	else {
		/*
		 * i = 1,		option is not specified
		 * i = 2,		option is specified
		 */
		for ((opt!=0)?(i=2):(i=1); i < argc; i++)
			my_ls(argv[i], opt);
	}
	printf(!(opt & FLAG_L)?"\n":"");
}
Example #2
0
void    other_input(DIR *dirp, int argc, char option, char **argv)
{
  int   i;

  i = 1;
  while (i < argc)
    {
      if (argv[i][0] != '-')
        {
          if ((dirp = opendir(argv[i])) != NULL)
            {
              if (option == 'l')
                my_ls_el(dirp, argv, i);
              else
		{
		  my_ls(dirp, argv, i, option);
		  my_putchar('\n');
		  closedir(dirp);
		}
            }
          else
            my_printf("ls: cannot access %s: Not a directory\n", argv[i]);
          i++;
        }
      else
        i++;
    }
}
Example #3
0
void		launch_ls(t_list *f_list, t_list *ptr, char *options, int cpt)
{
  while (cpt < my_list_size(f_list))
    {
      if (my_list_size(f_list) > 2 && find_in_tab(options, 'd') != 0)
	my_printf("%s:\n", ptr->name);
      {
	my_ls(ptr->name, options);
      }
      if (cpt + 1 < my_list_size(f_list) &&
	  (my_list_size(f_list) > 2 && find_in_tab(options, '1') == 0
	   && find_in_tab(options, 'd') != 0 || options[0] == '\0'))
	{
	  my_putstr("\n");
	}
      if (cpt + 1 == my_list_size(f_list) && find_in_tab(options, 'd') == 0 &&
	  find_in_tab(options, 'l') != 0)
	{
	  my_putstr("\n");
	}
      if (cpt + 1 != my_list_size(f_list) && find_in_tab(options, 'l') == 0 &&
	  find_in_tab(options, 'd') != 0 && find_in_tab(options, '1') != 0)
	my_putstr("\n");
      ptr = ptr->next;
      cpt = cpt + 1;
    }
}
Example #4
0
int		main(int argc, char **argv)
{
  t_options	*options;
  t_list_file	*list_file;
  int		check_option;
  int		i;

  i = 1;
  check_option = 0;
  list_file = NULL;
  if ((init_options(&options)) == 1)
    return (1);
  while (i < argc)
  {
    if (argv[i][0] == '-')
      check_option = get_option(argv[i], &options);
    else
      check_option += push_file(options, &list_file, argv[i], 0);
    if (check_option != 0)
      return (1);
    i = i + 1;
  }
  if ((my_ls(options, list_file)) == -1)
    return (1);
  free_list_file(&list_file);
  free(options);
  return (0);
}
Example #5
0
int main(int argc, const char *argv[])
{
    char path[] = "./";

    my_ls(path);
    
    return 0;
}
Example #6
0
int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		printf("%s\n","wrong usage.");
		exit(1);
	}

	my_ls(argv[1]);

	exit(0);
}
Example #7
0
int main(int argc ,char * argv[]){
        int model;
        char target_path[max_len];
        //char* buf;
        //system("./terminal.sh");
        //buf=getenv("VAR_CLOUMS");
        //printf("%s\n",buf);
        init_screen();
        get_model(argc,argv,&model,target_path);
        my_ls(target_path,model);
        return 0;
}
Example #8
0
File: ls.c Project: imbyungjun/TIL
/* 
 * read directory and get each file name,
 * and call my_ls function with file name as argument
 */
void print_dir(const char *name, int opt) {
	struct dirent *dir;
	struct stat stat_buf;
	DIR *dp;
	char *perm;
	ssize_t cnt = 0;		/* count for file sizes */

	if (strcmp(name, ".")) {
		chdir(name);
		strcpy(name, ".");
	}

	if ((dp = opendir(name)) == NULL) {
		warn("%s", name);
		return;
	}

	/*
	 * get total blocks and print it 
	 * before other informations
	 */
	if (opt & FLAG_L) {
		while ((dir = readdir(dp))) {
			if (dir->d_ino != 0) {
				if (!(opt & FLAG_A) && (dir->d_name[0] == '.'))
					continue;

				if (lstat(dir->d_name, &stat_buf) < 0) {
					warn("%s", dir->d_name);
					return;
				}
				cnt += stat_buf.st_blocks;
			}
		}
		printf("total %d\n", cnt);
	}

	/* go back to the beginning of the directory */
	rewinddir(dp);

	/* print informations by call my_ls function */
	while ((dir = readdir(dp))) {
		if (dir->d_ino != 0) {
			if (!(opt & FLAG_A) && (dir->d_name[0] == '.'))
				continue;
			
			my_ls(dir->d_name, opt);
		}
	}
	closedir(dp);
}
Example #9
0
int	execute(int sc)
{
  int	nb_read;
  char	buff[500];

  nb_read = read(sc, buff, 500);
  if (strncmp(buff, "ls", 2) == 0)
    my_ls(sc);
  else if (strncmp(buff, "pwd", 3) == 0)
    my_pwd(sc);
  else if (strncmp(buff, "quit", 4) == 0)
    return (0);
  else
    write(sc, "Commande not found", strlen("Commande not found"));
  return (1);
}
Example #10
0
int		main(int ac, char **av)
{
  char		*options;
  t_list	*f_list;
  t_list	*ptr;

  f_list = NULL;
  ptr = malloc(sizeof(*ptr));
  options = malloc(sizeof(*options) * 4);
  options = "ldr1";
  options = find_options(av, options, 0, 1);
  f_list = create_list(f_list, av, 0, 1);
  ptr = f_list;
  if (my_list_size(f_list) == 1)
    {
      my_ls(".", options);
      if (find_in_tab(options, 'd') == 0)
	my_putchar('\n');
    }
  else
    launch_ls(f_list, ptr, options, 1);
  //  free(options);
  return (0);
}