- 博客访问: 1097476
- 博文数量: 155
- 博客积分: 2525
- 博客等级: 大尉
- 技术积分: 2242
- 用 户 组: 普通用户
- 注册时间: 2011-05-05 20:52
分类: LINUX
2012-06-23 13:29:26
- #include <stdarg.h>
- #include <errno.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <errno.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <resolv.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <getopt.h>
- #define DEFAULTIP "127.0.0.1"
- #define DEFAULTPORT "80"
- #define DEFAULTBACK "10"
- #define DEFAULTDIR "/home"
- #define DEFAULTLOG "/tmp/das-server.log"
- void prterrmsg(char *msg);
- #define prterrmsg(msg) { perror(msg); abort(); }
- void wrterrmsg(char *msg);
- #define wrterrmsg(msg) { fputs(msg, logfp); fputs(strerror(errno), logfp);fflush(logfp); abort(); }
- void prtinfomsg(char *msg);
- #define prtinfomsg(msg) { fputs(msg, stdout); }
- void wrtinfomsg(char *msg);
- #define wrtinfomsg(msg) { fputs(msg, logfp); fflush(logfp);}
- #define MAXBUF 1024
- char buffer[MAXBUF + 1];
- char *host = 0;
- char *port = 0;
- char *back = 0;
- char *dirroot = 0;
- char *logdir = 0;
- unsigned char daemon_y_n = 0;
- FILE *logfp;
- #define MAXPATH 150
- /*----------------------------------------
- *--- dir_up - 查找dirpath所指目录的上一级目录
- *----------------------------------------
- */
- char *dir_up(char *dirpath)
- {
- static char Path[MAXPATH];
- int len;
- strcpy(Path, dirpath);
- len = strlen(Path);
- if (len > 1 && Path[len - 1] == '/')
- len--;
- while (Path[len - 1] != '/' && len > 1)
- len--;
- Path[len] = 0;
- return Path;
- }
- /*------------------------------------------------------
- *--- AllocateMemory - 分配空间并把d所指的内容复制
- *------------------------------------------------------
- */
- void AllocateMemory(char **s, int l, char *d)
- {
- *s = malloc(l + 1);
- bzero(*s, l + 1);
- memcpy(*s, d, l);
- }
- /************关于本文档********************************************
- *filename: das-server.c
- *purpose: 这是在Linux下用C语言写的目录访问服务器,支持目录浏览和文件下载
- *wrote by: zhoulifa(zhoulifa@163.com) 周立发(http://zhoulifa.bokee.com)
- Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言
- *date time:2007-01-26 19:32
- *Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途
- * 但请遵循GPL
- *Thanks to: Google.com
- *Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力
- * 科技站在巨人的肩膀上进步更快!感谢有开源前辈的贡献!
- *********************************************************************/
- /*------------------------------------------------------
- *--- 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%d - %s"
- "Linux 下目录访问服务器
100%%\">
"
- "
100%%\">", errno,
- strerror(errno));
- fprintf(client_sock,
- "
CC0000\" size=+2>请向管理员咨询为何出现如下错误提示:\n%s %s
",
- 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%s"
- "Linux 下目录访问服务器
100%%\">
"
- "
100%%\">", Path);
- fprintf(client_sock,
- "目录 %s\n",
- Path);
- fprintf(client_sock,
- "
名称 | 大小 | 修改时间 |
\n");
- if (dir == 0) {
- fprintf(client_sock,
- "
CC0000\" size=+2>%s",
- 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, "
");
- 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,
- "
http://%s%s%s\">(parent) | ",
- host, atoi(port) == 80 ? "" : nport,
- dir_up(Path));
- else
- fprintf(client_sock,
- "
http://%s%s%s\">%s | ",
- host, atoi(port) == 80 ? "" : nport, Filename,
- dirent->d_name);
- if (S_ISDIR(info.st_mode))
- fprintf(client_sock, "
目录 | ");
- else if (S_ISREG(info.st_mode))
- fprintf(client_sock, "
%d | ", info.st_size);
- else if (S_ISLNK(info.st_mode))
- fprintf(client_sock, "
链接 | ");
- else if (S_ISCHR(info.st_mode))
- fprintf(client_sock, "
字符设备 | ");
- else if (S_ISBLK(info.st_mode))
- fprintf(client_sock, "
块设备 | ");
- else if (S_ISFIFO(info.st_mode))
- fprintf(client_sock, "
FIFO | ");
- else if (S_ISSOCK(info.st_mode))
- fprintf(client_sock, "
Socket | ");
- else
- fprintf(client_sock, "
(未知) | ");
- fprintf(client_sock, "
%s | ", ctime(&info.st_ctime));
- }
- fprintf(client_sock, "
\n");
- free(realFilename);
- }
- fprintf(client_sock, "