static void
process_readdir(u_int32_t id)
{
	DIR *dirp;
	struct dirent *dp;
	char *path;
	int handle;

	handle = get_handle();
	debug("request %u: readdir \"%s\" (handle %d)", id,
	    handle_to_name(handle), handle);
	dirp = handle_to_dir(handle);
	path = handle_to_name(handle);
	if (dirp == NULL || path == NULL) {
		send_status(id, SSH2_FX_FAILURE);
	} else {
		struct stat st;
		char pathname[MAXPATHLEN];
		Stat *stats;
		int nstats = 10, count = 0, i;

		stats = xcalloc(nstats, sizeof(Stat));
		while ((dp = readdir(dirp)) != NULL) {
			if (count >= nstats) {
				nstats *= 2;
				stats = xrealloc(stats, nstats, sizeof(Stat));
			}
/* XXX OVERFLOW ? */
			snprintf(pathname, sizeof pathname, "%s%s%s", path,
			    strcmp(path, "/") ? "/" : "", dp->d_name);
			if (lstat(pathname, &st) < 0)
				continue;
			stat_to_attrib(&st, &(stats[count].attrib));
			stats[count].name = xstrdup(dp->d_name);
			stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
			count++;
			/* send up to 100 entries in one message */
			/* XXX check packet size instead */
			if (count == 100)
				break;
		}
		if (count > 0) {
			send_names(id, count, stats);
			for (i = 0; i < count; i++) {
				free(stats[i].name);
				free(stats[i].long_name);
			}
		} else {
			send_status(id, SSH2_FX_EOF);
		}

#ifdef NERSC_MOD
 	char* t1buf = encode_string(path, strlen(path));
 	s_audit("sftp_process_readdir_3", "count=%i int=%d uristring=%s",
 		get_client_session_id(), (int)getppid(), t1buf);
#endif

		free(stats);
	}
}
Exemplo n.º 2
0
int ls(char *name) {
	printf("ls: %s\n", name);
	struct stat mystat, *sp;
	int r;
	char *s;
	char fullName[256], cwd[256];
	s = name;
	if (strcmp(name, "") == 0) //there is no filName input
		s = "./";

	sp = &mystat;
	if (r = lstat(s, sp) < 0) {
		printf("no such file %s\n", s); //exit(1);
	} else {
		strcpy(fullName, s);
		if (s[0] != '/') { // name is relative : get CWD path
			getcwd(cwd, 256);
			strcpy(fullName, cwd);
			strcat(fullName, "/");
			strcat(fullName, s);
		}

		if (S_ISDIR(sp->st_mode))
			ls_dir(fullName);
		else
			ls_file(fullName);

		printf("ls %s ok\n", fullName);
	}
	return 0;
}
Exemplo n.º 3
0
static void
process_readdir(u_int32_t id)
{
	DIR *dirp;
	struct dirent *dp;
	char *path;
	int r, handle;

	if ((r = get_handle(iqueue, &handle)) != 0)
		fatal("%s: buffer error: %s", __func__, ssh_err(r));

	debug("request %u: readdir \"%s\" (handle %d)", id,
	    handle_to_name(handle), handle);
	dirp = handle_to_dir(handle);
	path = handle_to_name(handle);
	if (dirp == NULL || path == NULL) {
		send_status(id, SSH2_FX_FAILURE);
	} else {
		struct stat st;
		char pathname[PATH_MAX];
		Stat *stats;
		int nstats = 10, count = 0, i;

		stats = xcalloc(nstats, sizeof(Stat));
		while ((dp = readdir(dirp)) != NULL) {
			if (count >= nstats) {
				nstats *= 2;
				stats = xreallocarray(stats, nstats, sizeof(Stat));
			}
/* XXX OVERFLOW ? */
			snprintf(pathname, sizeof pathname, "%s%s%s", path,
			    strcmp(path, "/") ? "/" : "", dp->d_name);
			if (lstat(pathname, &st) < 0)
				continue;
			stat_to_attrib(&st, &(stats[count].attrib));
			stats[count].name = xstrdup(dp->d_name);
			stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
			count++;
			/* send up to 100 entries in one message */
			/* XXX check packet size instead */
			if (count == 100)
				break;
		}
		if (count > 0) {
			send_names(id, count, stats);
			for (i = 0; i < count; i++) {
				free(stats[i].name);
				free(stats[i].long_name);
			}
		} else {
			send_status(id, SSH2_FX_EOF);
		}
		free(stats);
	}
}
static void
process_readdir(void)
{
	DIR *dirp;
	struct dirent *dp;
	char *path;
	int handle;
	u_int32_t id;

	id = get_int();
	handle = get_handle();
	TRACE("readdir id %u handle %d", id, handle);
	dirp = handle_to_dir(handle);
	path = handle_to_name(handle);
	if (dirp == NULL || path == NULL) {
		send_status(id, SSH2_FX_FAILURE);
	} else {
		struct stat st;
		char pathname[1024];
		Stat *stats;
		int nstats = 10, count = 0, i;

		stats = xmalloc(nstats * sizeof(Stat));
		while ((dp = readdir(dirp)) != NULL) {
			if (count >= nstats) {
				nstats *= 2;
				stats = xrealloc(stats, nstats * sizeof(Stat));
			}
/* XXX OVERFLOW ? */
			snprintf(pathname, sizeof pathname, "%s%s%s", path,
			    strcmp(path, "/") ? "/" : "", dp->d_name);
			if (lstat(pathname, &st) < 0)
				continue;
			stat_to_attrib(&st, &(stats[count].attrib));
			stats[count].name = xstrdup(dp->d_name);
			stats[count].long_name = ls_file(dp->d_name, &st);
			count++;
			/* send up to 100 entries in one message */
			/* XXX check packet size instead */
			if (count == 100)
				break;
		}
		if (count > 0) {
			send_names(id, count, stats);
			for (i = 0; i < count; i++) {
				xfree(stats[i].name);
				xfree(stats[i].long_name);
			}
		} else {
			send_status(id, SSH2_FX_EOF);
		}
		xfree(stats);
	}
}
Exemplo n.º 5
0
static void	ls_folder(t_lsprop *prop, t_list *il, int print_title)
{
	t_file	*file;
	t_list	*new_list;

	file = (t_file *)il->content;
	if (print_title)
		ft_printf("%s:\n", file->fullpath);
	new_list = get_file_in_dir_info(prop, file->fullpath);
	ls_file(prop, &new_list, print_title);
}
Exemplo n.º 6
0
static void	ls_recursive(t_lsprop *prop, t_list *elem)
{
	t_file	*file;
	t_list	*new_list;

	reset_print_prop(prop->pp);
	file = (t_file *)elem->content;
	if (!is_curr_prev_folder(file))
		ft_printf("%s:\n", file->fullpath);
	if (!OPT_A && file->name[0] != '.')
	{
		new_list = get_file_in_dir_info(prop, file->fullpath);
		ls_file(prop, &new_list, HIDE_TITLE);
	}
	else
		if (!is_curr_prev_folder(file))
		{
			new_list = get_file_in_dir_info(prop, file->fullpath);
			ls_file(prop, &new_list, HIDE_TITLE);
		}
}
Exemplo n.º 7
0
int ls_dir(char *dname)
{
  DIR *dp;
  struct dirent *ep;
  dp = opendir(dname);
  while ( ep=readdir(dp)){
  ls_file(ep->d_name);

  //printf("%s \n", ep->d_name);
  }
  write(newsock,"ls complete",MAX); 

}
Exemplo n.º 8
0
//from KC's class notes #8
 int myls(char dname[128])
{
  printf("directory name: %s\n", dname);
  struct stat mystat, *sp;
  int r;
  
  sp = &mystat;
  if (r = lstat(dname, sp) < 0){
     printf("no such file %s\n", dname); return(1);
  }
  if (S_ISDIR(sp->st_mode)) {ls_dir(dname);}
  else {ls_file(dname);}
  return 0;
}
Exemplo n.º 9
0
int ls_dir(char *dname) {
	printf("ls_dir: %s\n", dname);
	DIR *dirp;
	struct dirent *dp;

	dirp = opendir(dname); // open the dir
	if (dirp == NULL) {
		printf("open dir %s was failed.\n", dname);
	} else {
		// loop thru every file inside the dir
		while ((dp = readdir(dirp)) != NULL) {
			ls_file(dp->d_name); // ls every single file
		}

		closedir(dirp); // close the dir stream
	}
}
Exemplo n.º 10
0
void SFTP::process_readdir(void)
{
  int handle;
	u_int32_t id;

	id = get_int();
	handle = get_handle();
	debug("request %u: readdir \"%s\" (handle %d)", id,
    toUTF8(handle_to_name(handle)).c_str (), handle);
	
  HANDLE dh = handle_to_dir(handle);
  const SFTPFilePath dirPath (handle_to_path (handle));
	if (dh == INVALID_HANDLE_VALUE 
      || dirPath.to_string ().length () == 0
      ) 
  {
		send_status(id, SSH2_FX_FAILURE);
	} 
  else 
  {
		WIN32_FIND_DATA st;
		//char pathname[MAXPATHLEN];
		Stat *stats;
		int nstats = 10, count = 0, i;

    stats = (Stat*) xcalloc(nstats, sizeof(Stat));
    HANDLE& searchHandle = handles[handle].searchHandle;
    bool& searchDone = handles[handle].searchDone;
    if (!searchDone 
        && searchHandle == INVALID_HANDLE_VALUE)
    {
      assert (!searchDone);
      searchHandle = ::FindFirstFile
        (dirPath.get_mask_for_dir_search ().c_str (),
         &st
         );
    }
    if (!searchDone 
        && searchHandle != INVALID_HANDLE_VALUE
) 
    {
      searchDone = true;
      while (::FindNextFileW (searchHandle, &st))
      {
			  if (count >= nstats) {
				  nstats *= 2;
				  stats = (Stat*) xrealloc(stats, nstats, sizeof(Stat));
			  }
			  stat_to_attrib(&st, &(stats[count].attrib));
        stats[count].name = xstrdup
          (toUTF8(st.cFileName).c_str ());
        stats[count].long_name = xstrdup
          (toUTF8 (ls_file(st.cFileName, &st, 0)).c_str ());
			  count++;
			  /* send up to 100 entries in one message */
			  /* XXX check packet size instead */
			  if (count == 100) {
          searchDone = false;
          break;
        }
      }
      /*if (searchDone)
        ::GetLastError (); // read error of FindNextFile*/
		}
    if (searchDone 
        && searchHandle != INVALID_HANDLE_VALUE)
    {
      ::FindClose (searchHandle);
      searchHandle = 0;
    }

		if (count > 0) {
			send_names(id, count, stats);
			for (i = 0; i < count; i++) {
				xfree(stats[i].name);
				xfree(stats[i].long_name);
			}
		} else {
      searchHandle = INVALID_HANDLE_VALUE;
      searchDone = false;
			send_status(id, SSH2_FX_EOF);
		}
		xfree(stats);
	}
}
Exemplo n.º 11
0
int excute (int myargc, char* myargv[])//command will not b null when called
{
  char answer[MAX];
  struct stat fstat, *sp;
  char *cmd;
  char *op = 0;
  sp = &fstat;
  bzero(answer, MAX);
  cmd  = myargv[0];
  char pwd[MAX];
  //printf("Hi\n");
  if(myargc > 1)
  {
    op = myargv[1];
  }
  
  if(strcmp(cmd, "pwd") == 0)
  {
    write(newsock, "1", MAX);//sending sucess message
    getcwd(cwd, MAX);
    write(newsock, cwd, MAX);
    printf("client request pwd: %s\n", cwd);
    return 1;
  } 
  if(strcmp(cmd, "ls") == 0)
  {
    //printf("Hi\n")  ;
    printf("myargc = %d\n",myargc );
    printf("myargv[0] = %s\n", myargv[0]);
    printf("myargv[1] = %s\n", myargv[1]);
    write(newsock, "1", MAX);
    if((myargc > 1) && lstat(myargv[1], &fstat) < 0)
    { 
      write(newsock, "0", 2);
      printf("lstat %s failed\n", myargv[1]);
      
      return 0;
    }
    else if(myargc > 1)
    {
      if(S_ISDIR(sp->st_mode))
      {
        //ls_dir(myargv[1]);
        strcat(answer, "dir");        
        write(newsock, answer, MAX);
        ls_dir(myargv[1]);
        return 0;
      }else{
        
        strcat(answer, "file");
        write(newsock, answer, MAX);
        ls_file(myargv[1]);
        //write(newsock,"done",MAX);
        return 0;
      }
    }

    if(myargc == 1)
    {
      strcat(answer, "dir");
      write(newsock, answer, MAX);
      ls_dir("./");
      printf("ls current directory OK");
      return 0;
    }
    
    return 1;
  } 
  if(strcmp(cmd, "cd") == 0)
  {
    printf("myargc =%d\n",myargc );
    printf("op = %s\n",op );
    write(newsock, "1", MAX);//start message
    if(myargc>1){
      getcwd(pwd, MAX);
      strcat(pwd, "/");
      strcat(pwd,op);
      printf("myargv[1] = %s\n",myargv[1] );
      printf("pwd = %s\n", pwd);
      if(!(chdir(pwd) < 0)){
        printf("client request: cd OK\n");
        strcat(answer, "cd OK");
        write(newsock, answer, MAX);
        printf("cd OK\n");
      }
    }
    // printf("myargc =%d\n",myargc );
    // printf("op = %s\n",op );
    // write(newsock, "1", MAX);//start message
    // if(!(chdir(pwd) < 0))//cd sucessed
    // {
      
    //   printf("client request: cd OK\n");
    //   strcat(answer, "cd OK");
    //   write(newsock, answer, MAX);
    //   printf("cd OK\n");
    // }
    if(myargc == 1)
    {
      
      printf("cd to HOME dir\n");
      chdir("/home/haoran");
      strcat(answer, "cd to HOME dir");
      write(newsock, answer, MAX);
      printf("cd to HOME dir\n");
      return 0;
    }
    else
    {
      printf("client request: cd FAILED\n");
      strcat(answer, "cd FAILED");
      write(newsock, answer, MAX);
      printf("cd FAILED\n");
    }
    return 1;
  } 
  if(strcmp(cmd, "mkdir") == 0)
  {
    printf("op = %s\n",op );
    write(newsock, "1", MAX);
    if(mkdir(op, 0777) < 0)
    {
      printf("mkdir FAIL \n");
      strcat(answer, "mkdir FAIL");
      write(newsock, answer, MAX);

      
    }else
    {
      printf("mkdir %s OK\n", op);
      strcat(answer, "mkdir OK");
      write(newsock, answer, MAX);
      printf("mkdir OK\n");
    }
    return 1;
  } 
  if(strcmp(cmd, "rmdir") == 0)
  {
    write(newsock, "1", MAX);
    if(rmdir(op) < 0)
    {
      
      printf("rmdir FAIL \n");
      strcat(answer, "rmdir FAIL ");
      write(newsock, answer, MAX);
      
      
    }else
    {
      printf("rmdir %s OK\n", op);
      strcat(answer, "rmdir OK");
      write(newsock, answer, MAX);
      printf("rmdir OK\n");

      //
    }
    return 1;
  } 
  if(strcmp(cmd, "rm") == 0)
  {
    write(newsock, "1", MAX);
    if(unlink(op) < 0)
    {
      printf("rm FAIL");
      strcat(answer, "rm FAIL");
      write(newsock, answer, MAX);

    }
    else
    {
      printf("rm %s OK\n", op);
      strcat(answer, "rm OK");
      write(newsock, answer, MAX);
      printf("rm OK\n");
    }
    return 1;
  } 
  if(strcmp(cmd, "get") == 0)
  {
    getCmd(myargc, myargv);
    return 2;//need to work with server
  } 
  if(strcmp(cmd, "put") == 0)
  {
    putCmd(myargc, myargv);   
    return 2;
  } 
  if(strcmp(cmd, "cat") == 0)
  {
    write(newsock, "1", MAX);
    strcat(answer, "Only cat local file");
    write(newsock, answer, MAX);
    
  }
  if(strcmp(cmd, "quit") == 0)//should never get this
  {
    exit(1);
  }

  return 0;
}
Exemplo n.º 12
0
/* sftp ls.1 replacement which handles path globs */
static int
do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
    int lflag)
{
	glob_t g;
	u_int i, c = 1, colspace = 0, columns = 1;
	Attrib *a = NULL;

	memset(&g, 0, sizeof(g));

	if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
	    NULL, &g) || (g.gl_pathc && !g.gl_matchc)) {
		if (g.gl_pathc)
			globfree(&g);
		error("Can't ls: \"%s\" not found", path);
		return (-1);
	}

	if (interrupted)
		goto out;

	/*
	 * If the glob returns a single match and it is a directory,
	 * then just list its contents.
	 */
	if (g.gl_matchc == 1) {
		if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) {
			globfree(&g);
			return (-1);
		}
		if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
		    S_ISDIR(a->perm)) {
			int err;

			err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
			globfree(&g);
			return (err);
		}
	}

	if (!(lflag & LS_SHORT_VIEW)) {
		u_int m = 0, width = 80;
		struct winsize ws;

		/* Count entries for sort and find longest filename */
		for (i = 0; g.gl_pathv[i]; i++)
			m = MAX(m, strlen(g.gl_pathv[i]));

		if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
			width = ws.ws_col;

		columns = width / (m + 2);
		columns = MAX(columns, 1);
		colspace = width / columns;
	}

	for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) {
		char *fname;

		fname = path_strip(g.gl_pathv[i], strip_path);

		if (lflag & LS_LONG_VIEW) {
			char *lname;
			struct stat sb;

			/*
			 * XXX: this is slow - 1 roundtrip per path
			 * A solution to this is to fork glob() and
			 * build a sftp specific version which keeps the
			 * attribs (which currently get thrown away)
			 * that the server returns as well as the filenames.
			 */
			memset(&sb, 0, sizeof(sb));
			if (a == NULL)
				a = do_lstat(conn, g.gl_pathv[i], 1);
			if (a != NULL)
				attrib_to_stat(a, &sb);
			lname = ls_file(fname, &sb, 1);
			printf("%s\n", lname);
			xfree(lname);
		} else {
			printf("%-*s", colspace, fname);
			if (c >= columns) {
				printf("\n");
				c = 1;
			} else
				c++;
		}
		xfree(fname);
	}

	if (!(lflag & LS_LONG_VIEW) && (c != 1))
		printf("\n");

 out:
	if (g.gl_pathc)
		globfree(&g);

	return (0);
}
Exemplo n.º 13
0
/* sftp ls.1 replacement for directories */
static int
do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
{
	int n;
	u_int c = 1, colspace = 0, columns = 1;
	SFTP_DIRENT **d;

	if ((n = do_readdir(conn, path, &d)) != 0)
		return (n);

	if (!(lflag & LS_SHORT_VIEW)) {
		u_int m = 0, width = 80;
		struct winsize ws;
		char *tmp;

		/* Count entries for sort and find longest filename */
		for (n = 0; d[n] != NULL; n++) {
			if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
				m = MAX(m, strlen(d[n]->filename));
		}

		/* Add any subpath that also needs to be counted */
		tmp = path_strip(path, strip_path);
		m += strlen(tmp);
		xfree(tmp);

		if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
			width = ws.ws_col;

		columns = width / (m + 2);
		columns = MAX(columns, 1);
		colspace = width / columns;
		colspace = MIN(colspace, width);
	}

	if (lflag & SORT_FLAGS) {
		for (n = 0; d[n] != NULL; n++)
			;	/* count entries */
		sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
		qsort(d, n, sizeof(*d), sdirent_comp);
	}

	for (n = 0; d[n] != NULL && !interrupted; n++) {
		char *tmp, *fname;

		if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
			continue;

		tmp = path_append(path, d[n]->filename);
		fname = path_strip(tmp, strip_path);
		xfree(tmp);

		if (lflag & LS_LONG_VIEW) {
			if (lflag & LS_NUMERIC_VIEW) {
				char *lname;
				struct stat sb;

				memset(&sb, 0, sizeof(sb));
				attrib_to_stat(&d[n]->a, &sb);
				lname = ls_file(fname, &sb, 1);
				printf("%s\n", lname);
				xfree(lname);
			} else
				printf("%s\n", d[n]->longname);
		} else {
			printf("%-*s", colspace, fname);
			if (c >= columns) {
				printf("\n");
				c = 1;
			} else
				c++;
		}

		xfree(fname);
	}

	if (!(lflag & LS_LONG_VIEW) && (c != 1))
		printf("\n");

	free_sftp_dirents(d);
	return (0);
}
Exemplo n.º 14
0
Arquivo: ls.c Projeto: z88dk/z88dk
int main(int argc, char **argv)
{
   static unsigned char *name;
   static unsigned char cwd[ESX_PATHNAME_MAX + 1];
   
   static struct esx_stat es;
   static struct esx_dirent ed;
   static struct esx_dirent_slice *slice;

   // initialization
   
   old_cpu_speed = ZXN_READ_REG(REG_TURBO_MODE);
   ZXN_NEXTREG(REG_TURBO_MODE, RTM_14MHZ);
   
   atexit(cleanup);
   
   // if a filename is not listed use the current working directory
   
   name = strrstrip(strstrip(argv[1]));
   
   if ((argc == 1) || (strcmp(name, ".") == 0))
   {
      if (esx_f_getcwd(cwd) == 0xff) exit(errno);
      name = cwd;
   }
   
   // try to open as a directory
   
   if ((fin = esx_f_opendir(name)) != 0xff)
   {
      // directory

      while (esx_f_readdir(fin, &ed) == 1)
      {
         slice = esx_slice_dirent(&ed);
         
         ls_name = ed.name;
         ls_size = &slice->size;
         
         tm_from_dostm(&ls_tm, &slice->time);
         
         (ed.attr & ESX_DIR_A_DIR) ? ls_dir() : ls_file();
      }
      
      esx_f_close(fin);
      fin = 0xff;
   }
   else
   {
      // file
      
      if (esx_f_stat(name, &es)) exit(errno);
      
      ls_name = name;
      ls_size = &es.size;
      
      tm_from_dostm(&ls_tm, &es.time);
      
      (es.attr & ESX_DIR_A_DIR) ? ls_dir() : ls_file();
   }
   
   return 0;
}