Example #1
0
/*DirListing - read the directory and output an HTML table. Used partially from source above*/
void DirListing(FILE* FP, char* Path) {
	struct dirent *dirent;
	struct stat info;
	char Filename[MAXPATH];
	DIR* dir = opendir(Path);
	fprintf(FP, "<html><head><title>Directory Lister</title></body>"
		"<body><font size=+4>CS410 Web Server</font><br><hr width=\"100%%\"><br><center>"
		"<table border cols=4 width=\"100%%\" bgcolor=\"009999\">");
	fprintf(FP, "<caption><font size=+3>Directory of %s</font></caption>\n", Path);
	if ( dir == 0 )
	{
		fprintf(FP, "</table><font color=\"white\" size=+2>%s</font></body></html>", strerror(errno));
		return;
	}
	while ( (dirent = readdir(dir)) != 0 )
	{
		if ( strcmp(Path, "/") == 0 )
			sprintf(Filename, "/%s", dirent->d_name);
		else
			sprintf(Filename, "%s/%s", Path, dirent->d_name);
		fprintf(FP, "<tr>");
		if ( stat(Filename, &info) == 0 )
		{
			fprintf(FP, "<td>%s</td>", dirent->d_name);
			if ( S_ISDIR(info.st_mode) )
			{
				if ( strcmp(dirent->d_name, "..") == 0 )
					fprintf(FP, "<td><a href=\"http://%s%s\">(parent)</a></td>", Host, dir_up(Path));
				else
					fprintf(FP, "<td><a href=\"http://%s%s\">%s</a></td>", Host, Filename, dirent->d_name);
				fprintf(FP, "<td>Directory</td>");
			}
			else if ( S_ISREG(info.st_mode) )
			{
				fprintf(FP, "<td><a href=\"http://%s%s\">%s</a></td>", Host, Filename, dirent->d_name);
				fprintf(FP, "<td>%d</td>", info.st_size);
			}
			else if ( S_ISLNK(info.st_mode) )
				fprintf(FP, "<td>Link</td>");
			else if ( S_ISCHR(info.st_mode) )
				fprintf(FP, "<td>Character Device</td>");
			else if ( S_ISBLK(info.st_mode) )
				fprintf(FP, "<td>Block Device</td>");
			else if ( S_ISFIFO(info.st_mode) )
				fprintf(FP, "<td>FIFO</td>");
			else if ( S_ISSOCK(info.st_mode) )
				fprintf(FP, "<td>Socket</td>");
			else
				fprintf(FP, "<td>(unknown)</td>");
			fprintf(FP, "<td>%s</td>", strtrim(ctime(&info.st_ctime)));
		}
		else
			perror(Path);
		fprintf(FP, "</tr>\n");
	}
	fprintf(FP, "</table></center></body></html>");
}
Example #2
0
static void cmd_CDUP_func(ClientInfo *client)
{
	char path[PATH_MAX];
	/* Path without the prefix */
	const char *normal_path = strchr(client->cur_path, '/');
	strcpy(path, normal_path);
	dir_up(path);
	sprintf(client->cur_path, "%s%s", FTP_DEFAULT_PREFIX, path);
	client_send_ctrl_msg(client, "200 Command okay.\n");
}
Example #3
0
    /*------------------------------------------------------
    *--- GiveResponse - 把Path所指的内容发送到client_sock去
    *-------------------如果Path是一个目录,则列出目录内容
    *-------------------如果Path是一个文件,则下载文件
    *------------------------------------------------------
    */
    void GiveResponse(FILE * client_sock, char *Path)
    {
        struct dirent *dirent;
        struct stat info;
        char Filename[MAXPATH];
        DIR *dir;
        int fd, len, ret;
        char *p, *realPath, *realFilename, *nport;

        /* 获得实际工作目录或文件 */
        len = strlen(dirroot) + strlen(Path) + 1;
        realPath = malloc(len + 1);
        bzero(realPath, len + 1);
        sprintf(realPath, "%s/%s", dirroot, Path);

        /* 获得实际工作端口 */
        len = strlen(port) + 1;
        nport = malloc(len + 1);
        bzero(nport, len + 1);
        sprintf(nport, ":%s", port);

        /* 获得实际工作目录或文件的信息以判断是文件还是目录 */
        if (stat(realPath, &info)) {
            fprintf(client_sock,
                    "HTTP/1.1 200 OK\r\nServer: DAS by ZhouLifa\r\nConnection: close\r\n\r\n<html><head><title>%d - %s</title></head>"
                    "<body><font size=+4>Linux 下目录访问服务器</font><br><hr width=\"100%%\"><br><center>"
                    "<table border cols=3 width=\"100%%\">", errno,
                    strerror(errno));
            fprintf(client_sock,
                    "</table><font color=\"CC0000\" size=+2>请向管理员咨询为何出现如下错误提示:\n%s %s</font></body></html>",
                    Path, strerror(errno));
            goto out;
        }
        /* 处理浏览文件请求,即下载文件 */
        if (S_ISREG(info.st_mode)) {
            fd = open(realPath, O_RDONLY);
            len = lseek(fd, 0, SEEK_END);
            p = (char *) malloc(len + 1);
            bzero(p, len + 1);
            lseek(fd, 0, SEEK_SET);
            ret = read(fd, p, len);
            close(fd);
            fprintf(client_sock,
                    "HTTP/1.1 200 OK\r\nServer: DAS by ZhouLifa\r\nConnection: keep-alive\r\nContent-type: application/*\r\nContent-Length:%d\r\n\r\n",
                    len);
            fwrite(p, len, 1, client_sock);
            free(p);
        } else if (S_ISDIR(info.st_mode)) {
            /* 处理浏览目录请求 */
            dir = opendir(realPath);
            fprintf(client_sock,
                    "HTTP/1.1 200 OK\r\nServer: DAS by ZhouLifa\r\nConnection: close\r\n\r\n<html><head><title>%s</title></head>"
                    "<body><font size=+4>Linux 下目录访问服务器</font><br><hr width=\"100%%\"><br><center>"
                    "<table border cols=3 width=\"100%%\">", Path);
            fprintf(client_sock,
                    "<caption><font size=+3>目录 %s</font></caption>\n",
                    Path);
            fprintf(client_sock,
                    "<tr><td>名称</td><td>大小</td><td>修改时间</td></tr>\n");
            if (dir == 0) {
                fprintf(client_sock,
                        "</table><font color=\"CC0000\" size=+2>%s</font></body></html>",
                        strerror(errno));
                return;
            }
            /* 读取目录里的所有内容 */
            while ((dirent = readdir(dir)) != 0) {
                if (strcmp(Path, "/") == 0)
                    sprintf(Filename, "/%s", dirent->d_name);
                else
                    sprintf(Filename, "%s/%s", Path, dirent->d_name);
                fprintf(client_sock, "<tr>");
                len = strlen(dirroot) + strlen(Filename) + 1;
                realFilename = malloc(len + 1);
                bzero(realFilename, len + 1);
                sprintf(realFilename, "%s/%s", dirroot, Filename);
                if (stat(realFilename, &info) == 0) {
                    if (strcmp(dirent->d_name, "..") == 0)
                        fprintf(client_sock,
                                "<td><a href=\"http://%s%s%s\">(parent)</a></td>",
                                host, atoi(port) == 80 ? "" : nport,
                                dir_up(Path));
                    else
                        fprintf(client_sock,
                                "<td><a href=\"http://%s%s%s\">%s</a></td>",
                                host, atoi(port) == 80 ? "" : nport, Filename,
                                dirent->d_name);
                    if (S_ISDIR(info.st_mode))
                        fprintf(client_sock, "<td>目录</td>");
                    else if (S_ISREG(info.st_mode))
                        fprintf(client_sock, "<td>%d</td>", info.st_size);
                    else if (S_ISLNK(info.st_mode))
                        fprintf(client_sock, "<td>链接</td>");
                    else if (S_ISCHR(info.st_mode))
                        fprintf(client_sock, "<td>字符设备</td>");
                    else if (S_ISBLK(info.st_mode))
                        fprintf(client_sock, "<td>块设备</td>");
                    else if (S_ISFIFO(info.st_mode))
                        fprintf(client_sock, "<td>FIFO</td>");
                    else if (S_ISSOCK(info.st_mode))
                        fprintf(client_sock, "<td>Socket</td>");
                    else
                        fprintf(client_sock, "<td>(未知)</td>");
                    fprintf(client_sock, "<td>%s</td>", ctime(&info.st_ctime));
                }
                fprintf(client_sock, "</tr>\n");
                free(realFilename);
            }
            fprintf(client_sock, "</table></center></body></html>");
        } else {
            /* 既非常规文件又非目录,禁止访问 */
            fprintf(client_sock,
                    "HTTP/1.1 200 OK\r\nServer: DAS by ZhouLifa\r\nConnection: close\r\n\r\n<html><head><title>permission denied</title></head>"
                    "<body><font size=+4>Linux 下目录访问服务器</font><br><hr width=\"100%%\"><br><center>"
                    "<table border cols=3 width=\"100%%\">");
            fprintf(client_sock,
                    "</table><font color=\"CC0000\" size=+2>你访问的资源'%s'被禁止访问,请联系管理员解决!</font></body></html>",
                    Path);
        }
      out:
        free(realPath);
        free(nport);
    }
Example #4
0
void GiveResponse(FILE * client_sock, char *Path)
{
	struct dirent *dirent;
	struct stat info;
	char Filename[MAXPATH];
	DIR *dir;
	int fd, len, ret;
	char *p, *realPath, *realFilename, *nport;

	len = strlen(dirroot) + strlen(Path) + 1;
	realPath = malloc(len + 1);
	bzero(realPath, len + 1);
	sprintf(realPath, "%s/%s", dirroot, Path);

	len = strlen(port) + 1;
	nport = malloc(len + 1);
	bzero(nport, len + 1);
    sprintf(nport, ":%s", port);
	
	if (stat(realPath, &info)) {
		fprintf(client_sock,
					"HTTP/1.1 200 OK\r\nServer: Chester Test FTP \r\nConnection: close\r\n\r\n<html><head><title>%d - %s</title><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>"
					"<body><font size=+4>FTP Directory</font><br><hr width=\"100%%\"><br><center>"
					"<table border cols=3 width=\"100%%\">", errno,
					strerror(errno));
		fprintf(client_sock,
					"</table><font color=\"CC0000\" size=+2>Server Error :\n%s %s</font></body></html>",
					Path, strerror(errno));
		goto out;
	}
	
	if (S_ISREG(info.st_mode)) {
		fd = open(realPath, O_RDONLY);
		len = lseek(fd, 0, SEEK_END);
		p = (char *) malloc(len + 1);
		bzero(p, len + 1);
		lseek(fd, 0, SEEK_SET);
		ret = read(fd, p, len);
		close(fd);
		fprintf(client_sock,
					"HTTP/1.1 200 OK\r\nServer: Chester Test FTP \r\nConnection: keep-alive\r\nContent-type: application/*\r\nContent-Length:%d\r\n\r\n",
					len);
		fwrite(p, len, 1, client_sock);
		free(p);
	} else if (S_ISDIR(info.st_mode)) {
		
		dir = opendir(realPath);
		fprintf(client_sock,
					"HTTP/1.1 200 OK\r\nServer: Chester Test FTP\r\nConnection: close\r\n\r\n<html><head><title>%s</title><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>"
					"<body><font size=+4>FTP Directory</font><br><hr width=\"100%%\"><br><center>"
					"<table border cols=3 width=\"100%%\">", Path);
		fprintf(client_sock,
					"<caption><font size=+3>Dir %s</font></caption>\n",
					Path);
		fprintf(client_sock,
					"<tr><td>name</td><td>size</td><td>Modification time</td></tr>\n");
		if (dir == 0) {
			fprintf(client_sock,
						"</table><font color=\"CC0000\" size=+2>%s</font></body></html>",
						strerror(errno));
			return;
		}
		
		while ((dirent = readdir(dir)) != 0) {
			if (strcmp(Path, "/") == 0)
			  sprintf(Filename, "/%s", dirent->d_name);
			else
			  sprintf(Filename, "%s/%s", Path, dirent->d_name);
			fprintf(client_sock, "<tr>");
			len = strlen(dirroot) + strlen(Filename) + 1;
			realFilename = malloc(len + 1);
			bzero(realFilename, len + 1);
			sprintf(realFilename, "%s/%s", dirroot, Filename);
			if (stat(realFilename, &info) == 0) {
				if (strcmp(dirent->d_name, "..") == 0)
				  fprintf(client_sock,
							  "<td><a href=\"http://%s%s%s\">(parent)</a></td>",
							  host, atoi(port) == 80 ? "" : nport,
							  dir_up(Path));
				else
				  fprintf(client_sock,
							  "<td><a href=\"http://%s%s%s\">%s</a></td>",
							  host, atoi(port) == 80 ? "" : nport, Filename,
							  dirent->d_name);
	            if (S_ISDIR(info.st_mode))
				  fprintf(client_sock, "<td>dir</td>");
				else if (S_ISREG(info.st_mode))
				  fprintf(client_sock, "<td>%d</td>", info.st_size);
				else if (S_ISLNK(info.st_mode))
				  fprintf(client_sock, "<td>link</td>");
				else if (S_ISCHR(info.st_mode))
				  fprintf(client_sock, "<td>char device</td>");
				else if (S_ISBLK(info.st_mode))
				  fprintf(client_sock, "<td>device</td>");
				else if (S_ISFIFO(info.st_mode))
				  fprintf(client_sock, "<td>FIFO</td>");
				else if (S_ISSOCK(info.st_mode))
				  fprintf(client_sock, "<td>Socket</td>");
				else
				  fprintf(client_sock, "<td>(unknown)</td>");
				fprintf(client_sock, "<td>%s</td>", ctime(&info.st_ctime));
			}
			fprintf(client_sock, "</tr>\n");
			free(realFilename);
		}
		fprintf(client_sock, "</table></center></body></html>");
	} else {
		
		fprintf(client_sock,
					"HTTP/1.1 200 OK\r\nServer: Chester Test Server\r\nConnection: close\r\n\r\n<html><head><title>permission denied</title></head>"
					"<body><font size=+4>FTP Directory</font><br><hr width=\"100%%\"><br><center>"
					"<table border cols=3 width=\"100%%\">");
		fprintf(client_sock,
					"</table><font color=\"CC0000\" size=+2>Forbidden.</font></body></html>",
					Path);
	}
out:
	free(realPath);
	free(nport);
}
Example #5
0
File: cmod.c Project: imclab/cmod
int main(int argc, char *argv[])
{
  char *buff;
  int ch;
  int i;

  initscr();
  cbreak();
  noecho();
  nonl();
  curs_set(0);
  // nodelay(stdscr, TRUE);
  intrflush(stdscr, FALSE);
  keypad(stdscr, TRUE);

  if((color = has_colors())) {
    start_color();
    init_pair(1, COLOR_WHITE, COLOR_BLUE);
    init_pair(2, COLOR_CYAN, COLOR_BLUE);
  }

  signal(SIGINT, cleanup);

  init_windows();
  init_bass();

  create_playlist(&playlist_h, &playlist_t);

  if(argc>1) {
    if(strncmp("/", argv[1], 1) != 0) {
      error("argument must be an absolute path", FALSE);
    }
    if(dirname(opendir(argv[1]))) { // is a dir
      if(add(&playlist_h, argv[1], &playlist_t)) {
        p_p++;
        c_p_p++;
        nsongs++;
        active_win = PLAYLIST_WIN;
        bass_loop = BASS_MUSIC_LOOP;
        play(argv[1], PLAYING_PLAYLIST);
      }
      current_path = strdup((char*)dirname(argv[1]));
    } else {
      error("argument not valid", FALSE);
    }
  } else {
    current_path = (char*)malloc(sizeof(char)*PATH_MAX);
    current_path = (char*)getcwd(current_path, PATH_MAX);
  }

  entries = refresh_browser(current_path, 0);

  mvprintw(0, 0, "%s", current_path);

  repaint_windows();

  while((ch = getch()) != 'q') {
    switch(ch) {
      case 'a': // add song to playlist
          if(active_win == BROWSER_WIN) {
            if(!is_dir(current_path, entries[p_b])) {
              buff = mystrcat(current_path,entries[p_b],1);
              if(add(&playlist_h, buff, &playlist_t)) {
                if(p_p == -1) p_p++;
                nsongs++;
                repaint_windows();
              }
              free(buff);
            }
          }
        break;
      case 'e': // add all songs in current dir
          if(active_win == BROWSER_WIN) {
            for(i=0; i<nfiles; i++) {
              if(!is_dir(current_path, entries[i])) {
                buff = mystrcat(current_path,entries[i],1);
                if(add(&playlist_h, buff, &playlist_t)) {
                  if(p_p == -1) p_p++;
                  nsongs++;
                }
                free(buff);
              }
            }
          }
          repaint_windows();
        break;
      case 'c': // recursive add
          if(active_win == BROWSER_WIN) {
            //printw("---%s+++", current_path);
            add_recursive(current_path, entries, nfiles);
            repaint_windows();
            //printw("---%s", current_path);
          }
        break;
      case 'd': // remove one song from playlist
          if(active_win == PLAYLIST_WIN) {
            if(del(&playlist_h, p_p, &playlist_t)) {
              nsongs--;
              if(p_p > nsongs - 1) {
                p_p = nsongs - 1;
              }
              if(p_p < c_p_p) {
                c_p_p--;
              }
              //stop();
              repaint_windows();
            }
          }
        break;
      case 'r': // remove all songs from playlist
          if(del_all(&playlist_h, &playlist_t)) {
            p_p = -1;
            c_p_p = -1;
            nsongs = 0;
            repaint_windows();
          }
        break;
      case ' ': // start playback or browse filesystem
      case 13: //enter key
          if(active_win == BROWSER_WIN) {
            if(strcmp(entries[p_b], "..\0") == 0) {
              current_path = dirname(current_path);
              p_b = 0;
              entries = refresh_browser(current_path, 0);
            } else {
              if(is_dir(current_path, entries[p_b])) {
                current_path = dir_up(current_path, entries[p_b]);
                p_b = 0;
                entries = refresh_browser(current_path, 0);
              } else {
                buff = mystrcat(current_path,entries[p_b],1);
                play(buff, PLAYING_BROWSER);
                free(buff);
                c_p_p = -1;
                if(p_b < nfiles-1) {
                  p_b++;
                }
              }
            }
            refresh_path();
          } else { // PLAYLIST_WIN
            if(nsongs != 0) {
              play(get(&playlist_h, p_p, &playlist_t)->file, PLAYING_PLAYLIST);
              c_p_p = p_p;
              if(p_p < nsongs - 1) {
                p_p++;
              }
            }
          }
          repaint_windows();
        break;

      case 's': // stop playback
          stop();
          c_p_p = -1;
          repaint_windows();
          clear_statusbar(NULL);
        break;

      case 'n': // next song
          next();
        break;

      case 'p': // prev song
          prev();
        break;

      case 'g': // go to current song
          p_p = c_p_p;
          repaint_windows();
        break;

      case 'l': // toggle loop
          loop();
          stop();
          c_p_p = -1;
          repaint_windows();
        break;

      case 'z': // toggle random
          random_mode();
        break;

      case '.': // seek forward
          seek(1);
        break;

      case ',': // seek backward
          seek(-1);
        break;

      case '+':
          volume_up();
        break;

      case '-':
          volume_down();
        break;

      case '>':
          amplify_up();
        break;

      case '<':
          amplify_down();
        break;

      case KEY_UP:
        if(active_win == BROWSER_WIN) {
          if(p_b > 0) {
            p_b--;
          }
        } else {
          if(p_p > 0) {
            p_p--;
          }
        }
        repaint_windows();
        break;

      case KEY_DOWN:
        if(active_win == BROWSER_WIN) {
          if(p_b < nfiles-1) {
            p_b++;
          }
        } else {
          if(p_p < nsongs - 1) {
            p_p++;
          }
        }
        repaint_windows();
        break;

      case KEY_LEFT:
      case KEY_RIGHT:
          switch_activewindow();
          repaint_windows();
        break;

      case KEY_HOME:
          if(active_win == BROWSER_WIN) {
            p_b = 0;
          } else {
            if(p_p != -1) p_p = 0;
          }
          repaint_windows();
        break;

      case KEY_END:
          if(active_win == BROWSER_WIN) {
            p_b = nfiles-1;
          } else {
            if(p_p != -1) p_p = nsongs - 1;
          }
          repaint_windows();
        break;

      case KEY_NPAGE:
        if(active_win == BROWSER_WIN) {
          if((p_b + (browser_win->_maxy - 2)) < nfiles-1 ) {
            p_b += browser_win->_maxy - 1;
          } else {
            p_b = nfiles-1;
          }
        } else {
          if((p_p + (playlist_win->_maxy - 2)) < nsongs) {
            p_p += playlist_win->_maxy - 1;
          } else {
            p_p = nsongs - 1;
          }
        }
        repaint_windows();
        break;

      case KEY_PPAGE:
        if(active_win == BROWSER_WIN) {
          if((p_b - (browser_win->_maxy - 2)) > 0) {
            p_b -= browser_win->_maxy - 1;
          } else {
            p_b = 0;
          }
        } else {
          if((p_p - (playlist_win->_maxy - 2)) > 0) {
            p_p -= playlist_win->_maxy - 1;
          } else {
            if(p_p != -1) p_p = 0;
          }
        }
        repaint_windows();
        break;
    }

    //usleep(10);

    //update_status();

  }

  cleanup();

  return 0;

}
Example #6
0
int file_choose(const char *start_path, char *chosen_file, const char *title, const char *supported_ext[])
{
	SceCtrlData pad, old_pad;
	unsigned int keys_down;
	int i;
	int selected = 0;
	char cur_path[PATH_MAX];

	pad.buttons = old_pad.buttons = 0;

	strcpy(cur_path, start_path);

	file_list list;
	file_list_entry *entry;

	file_list_build(cur_path, &list, supported_ext);

	while (1) {
		sceCtrlPeekBufferPositive(0, &pad, 1);
		if (pad.buttons & PSP2_CTRL_SELECT) break;
		keys_down = pad.buttons & ~old_pad.buttons;

		if (keys_down & PSP2_CTRL_UP) {
			selected--;
			if (selected < 0) {
				selected = list.length - 1;
			}
		} else if (keys_down & PSP2_CTRL_DOWN) {
			selected++;
			if (selected == list.length) {
				selected = 0;
			}
		}

		if (keys_down & (PSP2_CTRL_CROSS | PSP2_CTRL_START)) {
			file_list_entry *entry = file_list_get_nth_entry(&list, selected);

			if (entry->is_dir) {
				if (strcmp(entry->name, "..") == 0) {
					dir_up(cur_path);
				} else {
					char new_path[PATH_MAX];
					sprintf(new_path, "%s/%s", cur_path, entry->name);
					strcpy(cur_path, new_path);
				}
				file_list_empty(&list);
				file_list_build(cur_path, &list, supported_ext);
				selected = 0;
			} else if (entry->supported) {
				sprintf(chosen_file, "%s/%s", cur_path, entry->name);
				file_list_empty(&list);
				return 1;
			}
		} else if (keys_down & PSP2_CTRL_CIRCLE) {
			dir_up(cur_path);
			file_list_empty(&list);
			file_list_build(cur_path, &list, supported_ext);
			selected = 0;
		}


		vita2d_start_drawing();
		vita2d_clear_screen();

		font_draw_stringf(10, 10, WHITE, title);

		entry = list.head;
		for (i = 0; i < list.length; i++) {

			font_draw_stringf(
				10,
				40 + i*20,
				(!entry->is_dir && entry->supported) ? GREEN : WHITE,
				"%s %s",
				(selected == i) ? ">" : "",
				entry->name);

			entry = entry->next;
		}

		old_pad = pad;
		vita2d_end_drawing();
		vita2d_swap_buffers();
	}

	return 0;
}
Example #7
0
int file_choose(const char *start_path, char *chosen_file, const char *title, const char *supported_ext[])
{
	SceCtrlData pad, old_pad;
	unsigned int keys_down;
	int i;
	int selected = 0;
	char cur_path[PATH_MAX];

	pad.buttons = old_pad.buttons = 0;

	strcpy(cur_path, start_path);

	file_list list;
	file_list_entry *entry;

	file_list_build(cur_path, &list, supported_ext);

	while (1) {
		sceCtrlPeekBufferPositive(0, &pad, 1);
		if (pad.buttons & SCE_CTRL_SELECT) break;
		keys_down = pad.buttons & ~old_pad.buttons;

		if (keys_down & SCE_CTRL_UP) {
			selected--;
			if (selected < list.scroll) {
				list.scroll--;
			}
			if (selected < 0) {
				selected = list.length - 1;
				list.scroll = max(0, list.length - LIST_MAX_ONSCREEN);
			}
		} else if (keys_down & SCE_CTRL_DOWN) {
			selected++;
			if (selected == list.scroll + LIST_MAX_ONSCREEN) {
				list.scroll++;
			}
			if (selected == list.length) {
				selected = 0;
				list.scroll = 0;
			}
		}

		if (keys_down & (SCE_CTRL_CROSS | SCE_CTRL_START)) {
			file_list_entry *entry = file_list_get_nth_entry(&list, selected);

			if (entry->is_dir) {
				if (strcmp(entry->name, "..") == 0) {
					dir_up(cur_path);
				} else {
					char new_path[PATH_MAX];
					// no valid elf with new toolchain with sprintf(new_path, "%s/%s", cur_path, entry->name);
					sceClibSnprintf(new_path, PATH_MAX, "%s/%s", cur_path, entry->name);
					strcpy(cur_path, new_path);
				}
				file_list_empty(&list);
				file_list_build(cur_path, &list, supported_ext);
				selected = 0;
			} else if (entry->supported) {
				// no valid elf with new toolchain with sprintfsprintf(chosen_file, "%s/%s", cur_path, entry->name);
				sceClibSnprintf(chosen_file,PATH_MAX, "%s/%s", cur_path, entry->name);
				
				file_list_empty(&list);
				return 1;
			}
		} else if (keys_down & SCE_CTRL_CIRCLE) {
			dir_up(cur_path);
			file_list_empty(&list);
			file_list_build(cur_path, &list, supported_ext);
			selected = 0;
		}


		vita2d_start_drawing();
		vita2d_clear_screen();

		font_draw_stringf(10, 10, WHITE, title);

		entry = file_list_get_nth_entry(&list, list.scroll);
		for (i = list.scroll; i < min(list.length, list.scroll + LIST_MAX_ONSCREEN); i++) {

			font_draw_stringf(
				10,
				40 + (i - list.scroll)*20,
				(!entry->is_dir && entry->supported) ? GREEN : WHITE,
				"%s %s",
				(selected == i) ? ">" : "",
				entry->name);

			entry = entry->next;
		}

		old_pad = pad;
		vita2d_end_drawing();
		vita2d_swap_buffers();
	}

	return 0;
}
Example #8
0
/*
   以html响应客户端的请求,若请求是目录,则列出目录信息,若是文件,则将文件内容传送给客户端
   */
void GiveResponse(FILE *client_sock,char *path){
	struct dirent *dirent;
	struct stat fileinfo;
	char Filename[MAXPATH];
	DIR *dir;
	int fd,len,ret,i=0;
	//FILE *fp;
	char *p,*realpath,*realFilename,*nport;
	struct passwd *p_passwd;
	struct group *p_group;
	char chinese[128];
	extern char home_dir[],ip[],port[];
	//目前未容许选择可上传到的路径,统一放在/upload目录下
	char uploadHtml[]="<form method=\"POST\" action=\"/upload\" enctype=\"multipart/form-data\">上传文件"
		"<input name=\"image\" type=\"file\" />"
		"<input type=\"submit\" value=\"Upload\" /></form>";
	//the action will exist in the head:POST /dir HTTP...
	//将中文名还原成主机能够识别的编码
	for(ret=0,len=strlen(path);ret<len;ret++){
		if(path[ret]=='%') {
			path[ret+2]=hexstr2int(path[ret+1],
					path[ret+2]);
			ret+=2;
		}
	}
	for(ret=0;ret<len;ret++){
		if(path[ret]=='%'){
			chinese[i++]=path[ret+2];
			ret+=2;
		}else
			chinese[i++]=path[ret];
	}
	chinese[i]='\0';
	if(i>0) strcpy(path,chinese);
	//因请求的文件是以服务器主目录为根目录如/var/www/,因此需要加该根目录才是绝对路径
	len=strlen(home_dir)+strlen(path)+1;
	realpath=malloc(len+1);
	bzero(realpath,len+1);
	sprintf(realpath,"%s/%s",home_dir,path);
	//get port
	len=strlen(port)+1;
	nport=malloc(len+1);
	bzero(nport,len+1);
	sprintf(nport,":%s",port);
	//file state
	if(stat(realpath,&fileinfo)){//fail
		fprintf(client_sock,"HTTP/1.1 200 OK\r\nServer:Test http server\r\nConnection:close\r\n\r\n"//两个\r\n,头部结束,HTTP1.0默认close,1.1默认keep-alive
			"<html lang=\"zh-cn\"><html><head><meta charset=\"utf-8\"><title>%d - %s</title></head>"
			"<body><font size=+4>Linux HTTP server</font><br><hr width=\"100%%\"><br><center><table border cols=3 width=\"100%%\"></table><font color=\"CC0000\" size=+2> connect to administrator,error path is: \n%s %s</font></body></html>",errno,strerror(errno),path,strerror(errno));
		goto out;
	}
	if(S_ISREG(fileinfo.st_mode)){//send file content
		fd=open(realpath,O_RDONLY);
		len=lseek(fd,0,SEEK_END);
		p=(char *)malloc(len+2048);//plus 2048 is due to i when display the text,need extra space:'<' changes to &#60;
		bzero(p,len+2048);
		lseek(fd,0,SEEK_SET);
		ret=read(fd,p,len);//一次性读取文件,对于大文件会出错,有时间再改,分批读取和传送文件
		char logmsg[30];
		if(ret<0) info("read fail");
		/*else {
			sprintf(logmsg,"read len=%d,the real len is %d\n",ret,len);
			info(logmsg);
		}*/
		close(fd);
		char *type=postfix(path);
		if(strcmp("c",type)==0
				||strcmp("txt",type)==0){
		//<的转义序列为&lt; or &#60;,> &gt; &#62;,&的转义为&amp; or &#38; 不转义的话<stdio.h>被当作标签,但不显示
			fprintf(client_sock,"HTTP/1.1 200 OK\r\nServer:Test http server\r\nConnection:close\r\n\r\n<html lang=\"zh-cn\"><html><head><meta charset=\"utf-8\"><title>content of %s</title></head><body><font size=+4>康哥's file</font><br><hr width=\"100%%\"><br>%s</body></html>"//<center>
					,path,formatText(p));
		}else if(strcmp("jpg",type)==0||strcmp("png",type)==0){
			fprintf(client_sock,"HTTP/1.1 200 OK\r\nServer:Test http server\r\nConnection:keep-alive\r\nContent-type:image\r\nContent-Length: %d\r\n\r\n",len);
			fwrite(p,len,1,client_sock);//send file content
			
		}else if(strcmp("html",type)==0){
			fprintf(client_sock,"HTTP/1.1 200 OK\r\nServer:Test http server\r\nConnection:close\r\n\r\n");
			fwrite(p,len,1,client_sock);//send file content
		}else{
			fprintf(client_sock,"HTTP/1.1 200 OK\r\nServer:Test http server\r\nConnection:keep-alive\r\nContent-type:application/*\r\nContent-Length:%d\r\n\r\n",len);
			//for transport file,so keep-alive
			fwrite(p,len,1,client_sock);//send file content
		}
		free(p);
	}else if(S_ISDIR(fileinfo.st_mode)){
		dir=opendir(realpath);
		fprintf(client_sock,"HTTP/1.1 200 OK\r\nServer:Test http server\r\nConnection:close\r\n\r\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html lang=\"zh-cn\"><html><head><meta charset=\"utf-8\"><title>%s</title></head><body><font size=+4>康哥's file</font><br><hr width=\"100%%\"><br>%s<center>"
			"<table border cols=3 width=\"100%%\">",path
				,uploadHtml);
		fprintf(client_sock,"<caption><font size=+3> Directory %s</font></caption>\n",path);//表格头信息,便于显示
		fprintf(client_sock,"<tr><td>name</td><td>type</td><td>owner</td><td>group</td><td>size</td><td>modify time</td></tr>\n");
		if(dir==NULL){//打开目录失败
			fprintf(client_sock,"</table><font color=\"CC0000\" size=+2>%s</font></body></html>",strerror(errno));
			return ;
		}
		fprintf(client_sock,"<td><a href=\"http://%s%s%s\">..parent..</a></td><br>",ip,atoi(port)==80?"":nport,dir_up(path));//go to parent dir
		while((dirent=readdir(dir))!=NULL){
			if(strcmp(path,"/")==0)//website root,no display parent fold
				sprintf(Filename,"/%s",dirent->d_name);
				//create web absolute dir
			else sprintf(Filename,"%s/%s",path,dirent->d_name);
			if(dirent->d_name[0]=='.')
				continue;//隐藏文件不列出,以及..和.
			fprintf(client_sock,"<tr>");
			len=strlen(home_dir)+strlen(Filename)+1;
			realFilename=malloc(len+1);
			bzero(realFilename,len+1);
			sprintf(realFilename,"%s/%s",home_dir,Filename);//主机上的绝对路径
			if(stat(realFilename,&fileinfo)==0){
				 fprintf(client_sock,"<td><a href=\"http://%s%s%s\">%s</a></td><br>",ip,atoi(port)==80?"":nport,Filename,dirent->d_name);
				//p_time=ctime(&info.st_mtime);
				p_passwd=getpwuid(fileinfo.st_uid);//文件拥有者
				p_group=getgrgid(fileinfo.st_gid);//拥有者组

				fprintf(client_sock,"<td>%c</td>",file_type(fileinfo.st_mode));
				fprintf(client_sock,"<td>%s</td>",p_passwd->pw_name);//the file hoster
				fprintf(client_sock,"<td>%s</td><td> %d </td><td>%s</td><br>",p_group->gr_name,(int)fileinfo.st_size,ctime(&fileinfo.st_ctime));
			}
			fprintf(client_sock,"</tr>\n");
			free(realFilename);
		}//while end
		fprintf(client_sock,"</table></center></body></html>");
	}else{//neither file or dir,forbid access
		fprintf(client_sock,"HTTP/1.1 200 OK\r\nServer:Test http server\r\nConnection:close\r\n\r\n<html lang=\"zh-cn\"><html><head><meta charset=\"utf-8\"><title>Permission denied</title></head><body><font size=+4>康哥's HTTP server</font><br><hr width=\"100%%\"><br><table border cols=3 width=\"100%%\">");
		fprintf(client_sock,"</table><font color=\"CC0000\" size=+2> you access resource '%s' forbid to access,communicate with the admintor </font></body></html>",path);
	}
out:
	free(realpath);
	free(nport);
}