int main(int argc, char *argv[]) { long nlines = DEFAULT_N_LINES; int opt; while ((opt = getopt_long(argc, argv, "n:", longopts, NULL)) != -1) { switch (opt) { case 'n': nlines = atoi(optarg); break; case 'h': fprintf(stdout, "Usage: %s [-n LINES] [FILE ...]\n", argv[0]); exit(0); case '?': fprintf(stdout, "Usage: %s [-n LINES] [FILE ...]\n", argv[0]); exit(1); } } if (optind == argc) { do_head(stdin, nlines); } else { for (int i = optind; i < argc; i++) { FILE *f; f = fopen(argv[i], "r"); if (!f) { perror(argv[i]); exit(1); } do_head(f, nlines); fclose(f); } } exit(0); }
int main (int argc, char *argv[]) { long nlines; if (argc < 2) { fprintf(stderr, "Usage: %s n [file file ...]\n", argv[0]); exit(1); } nlines = atol (argv [1]); if (argc == 2) { do_head(stdin, nlines); } else { int i; for (i = 2; i < argc; i++) { FILE *f; f = fopen(argv[i], "r"); if (!f) { perror (argv[i]); exit(1); } do_head(f, nlines); fclose(f); } } exit (0); }
void on_write(struct ev_loop *loop, struct ev_io *watcher, int revents) { Request *request = (Request *)watcher; RequestLine *request_line = &(request->request_line); int fd = watcher->fd; char *method = request_line->method; char *uri = request_line->uri; if (!strncasecmp(method,"GET",MAXLINE)) { /* get不区分大小写 */ do_get(fd, uri); ev_io_stop(loop, watcher); return; } else if (!strncasecmp(method,"HEAD",MAXLINE)) { do_head(fd, uri); ev_io_stop(loop, watcher); return; } else if (!strncasecmp(method,"POST",MAXLINE)) { do_post(fd, uri); ev_io_stop(loop, watcher); return; } send_error(fd, method, "501","Not Implemented"); ev_io_stop(loop, watcher); }
int main(int argc, char *argv[]) { //エラー処理 引数がない場合はプログラムを終了させる if(argc < 2){ fprintf(stderr, "Usage: %s n\n", argv[0]); exit(1); } do_head(stdin, atol(argv[1])); exit(0); }
static void maintainr_projectbox_init (MaintainrProjectbox *item) { GtkWidget *mainbox; item->priv = MAINTAINR_PROJECTBOX_GET_PRIVATE (item); gtk_container_set_border_width (GTK_CONTAINER (item), 10); gtk_notebook_set_show_tabs (GTK_NOTEBOOK (item), FALSE); mainbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); gtk_notebook_append_page (GTK_NOTEBOOK (item), mainbox, NULL); gtk_box_pack_start (GTK_BOX (mainbox), do_head (item), FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (mainbox), do_todos (item), TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (mainbox), do_buttons (item), FALSE, FALSE, 0); gtk_notebook_append_page (GTK_NOTEBOOK (item), do_config (item), NULL); }
static void *response_thread(void *args) { pthread_t thread_id; int has_method, sockfd; unsigned int n; ghd_task_t* task; ghd_method method; ghd_request_fields_t Connection_field; volatile p_read_data received_data = NULL; // 注册线程意外退出回调函数 // 强制类型转换,make gcc happy. pthread_cleanup_push((void (*)(void *))pthread_mutex_unlock, (void *)&mutex); thread_id = pthread_self(); syslog(LOG_INFO, "Thread ID: %ld", thread_id); /* 处理线程就在这个while 里循环, * 等待信号, 有信号就处理,处理完毕继续等待 */ while ( 1 ) { /* 在使用前一定要确保mutex已初始化 */ pthread_mutex_lock(&mutex); while(task_queue_head == NULL) pthread_cond_wait(&cond, &mutex); sockfd = task_queue_head->sockfd; task = task_queue_head; task_queue_head = task_queue_head->next; if (task_queue_head == NULL) task_queue_tail = NULL; geekhttpd_task_free(task); pthread_mutex_unlock(&mutex); received_data = alloc_read_data(); if (received_data == NULL) { syslog(LOG_INFO, "Cannot alloc memory for received_data"); goto closefd; } has_method = 0; /* 读取从客户端发来的清求 */ while (1) { /* 重置errno */ errno = 0; if ( received_data->len < MAXHEADERLEN) n = read(sockfd, received_data->data + received_data->len, MAXHEADERLEN - received_data->len); else break; if (n < 0) { if ( errno == EINTR) { continue; } /* 处理有问题的链接 */ syslog(LOG_INFO, "brower closed connection"); goto closefd; } else if (n == 0) { break; } else { if ( errno == EAGAIN ) break; received_data->len += n; if (!has_method && received_data->len > 7){ get_request_method(received_data->data, &method); has_method = 1; } } } syslog(LOG_INFO, "[Request] Size %d, Content: %s", received_data->len, \ (char *)received_data->data); /* * 根据方法响应请求,向浏览器发送消息和数据 */ switch (method) { case GET: do_get(sockfd, received_data); break; case POST: do_post(sockfd, received_data); break; case HEAD: do_head(sockfd, received_data); break; case PUT: case DELETE: case TRACE: case CONNECT: case OPTIONS: do_not_implement(sockfd); break; case UNKNOWN: /* send 405 */ do_unknown_method(sockfd); break; default: break; } /* TODO: * Connection: Keep-Alive * 没有处理 */ closefd: close(sockfd); /* free read_data */ free_read_data(received_data); received_data = NULL; } pthread_cleanup_pop(0); }
void * thread_load( void * position_data ) { printf("come in.\n"); seed_t * seed = ( seed_t * ) position_data; int sock_fd = socket( AF_INET, SOCK_STREAM, 0 ); if( sock_fd == -1 ) { perror("socket"); exit( 1 ); } int ret = connect( sock_fd, ( sockaddr_t * )&url.addr, ADDR_SIZE ); if( ret == -1 ) { perror("connect"); exit( 1 ); } printf("have connect.\n"); int start_position = seed->start_position; int data_len = seed->data_len; char * request = malloc( 1400 ); sprintf( request, "GET %s HTTP/1.1\r\nHost: %s:%d\r\nRange: %d-\r\n\r\n", url.file_path, inet_ntoa( url.addr.sin_addr ), PORT_NUM, start_position ); printf("request = %s\n", request ); send( sock_fd, request, strlen(request)+1, 0 ); char * data_buf = malloc( DATA_BUF_SIZE ); if( data_buf == NULL ) { perror("malloc"); exit( 1 ); } unsigned read_num = 0; unsigned write_position = start_position; unsigned total_read_size = 0; int data_length; ret = do_head( sock_fd, data_buf, &data_length ); if( ret > 0 ) { pwrite( fd, data_buf, ret, write_position ); } write_position += ret; while( 1 ) { ret = recv( sock_fd, data_buf + read_num, 1400, 0 ); if( ret < 0 ) { perror("recv"); break; } else if( ret == 0 ) { break; } read_num += ret; total_read_size += ret; if( total_read_size >= data_len ) { read_num = ( read_num - ( total_read_size - data_len ) ); pthread_mutex_lock( &mutex ); pwrite( fd, data_buf, read_num, write_position ); pthread_mutex_unlock( &mutex ); break; } if( read_num >= DATA_BUF_SIZE - 1400 ) { pthread_mutex_lock( &mutex ); pwrite( fd, data_buf, read_num, write_position ); printf("load.\n"); pthread_mutex_unlock( &mutex ); write_position += read_num; read_num = 0; } } pthread _exit( 0 ); }
/* *主函数 */ int main( int argc, char ** argv ) { if( argc != 2 ) { /* 从键盘接收输入的线程数 */ printf( "Usage: %s thread_num.\n", argv[0] ); exit( 1 ); } thread_num = atoi( argv[1] ); /* 把字符串转换为整数 */ int sock_fd = socket( AF_INET, SOCK_STREAM, 0 ); if( sock_fd == -1 ) { perror("socket"); exit( 1 ); } int ret; printf("Input URL: "); gets( url.url ); /* 从键盘输入要下载的文件的URL */ ret = parse_path(); /* 解析输入的URL地址 */ if( ret == -1 ) { printf("parse path error.\n"); exit( 1 ); } /* 1.连接到服务器 */ int addr_len = ADDR_SIZE; ret = connect( sock_fd, ( sockaddr_t * )( &url.addr), addr_len ); if( ret == -1 ) { perror("connect error"); exit( 1 ); } print_url_info(); /* 打印出URL信息 */ char * request = malloc( 1000 ); sprintf( request, "GET %s HTTP/1.1\r\nHost: %s:%d\r\n\r\n", url.file_path, inet_ntoa( url.addr.sin_addr ), PORT_NUM ); printf("request: %s\n", request ); /* 2.向服务器发送请求 */ printf("send request ...\n"); send( sock_fd, request, strlen(request)+1, 0 ); fd = open( url.file_name, O_CREAT | O_TRUNC | O_WRONLY ); if( fd == -1 ) { perror( "open"); exit( 1 ); } char recv_buf[1400]; int data_length; printf("Receive data from %s ... \n", url.hostname ); ret = do_head( sock_fd, recv_buf, &data_length ); if( ret > 0 ) { write( fd, recv_buf, ret ); } printf("File size: %d\n", data_length ); char end = 0; lseek( fd, data_length -1 , SEEK_SET ); write( fd, &end, 1 ); /* 3.关闭套接口 */ close( sock_fd ); int i; seed_t seed[ thread_num ]; pthread_t thread[ thread_num ]; int per_thread_load_size = data_length / thread_num; /* 每一个线程需要下载的大小 */ /* 4.创建线程开始下载 */ for( i=0; i<thread_num; ++i ) { printf("Have creat %d threads\n", i ); seed[i].start_position = per_thread_load_size * i; /* */ if( i == ( thread_num - 1 ) ) seed[i].data_len = per_thread_load_size + data_length % thread_num; else seed[i].data_len = per_thread_load_size; pthread_create( &thread[i], NULL, thread_load, &seed[i] ); } for( i=0; i<thread_num; ++i ) pthread_join( thread[i], NULL ); close( fd ); return 0; }
int command_head (int argc, char **argv) { int i; char *ptr; progname = argv[0]; in = stdin; for (i = 1; i < argc; ++i) { ptr = argv[i]; if (*ptr == '-') { switch (*++ptr) { case '\0': in = stdin; continue; case '-': if (!strcmp (ptr, "-help")) puts (help_text); else if (!strcmp (ptr, "-version")) puts ("head: version "VERSION); else terror ("invalid option `%s'", argv[i]); return (EXIT_SUCCESS); case 'c': if (*++ptr != '\0') ; else if (++i >= argc) ptr = argv[i]; else terror ("missing operand to `-c' option"); bytes = getnum_ul (ptr); flag_lines = 0; continue; case 'n': if (*++ptr != '\0') ; else if (++i < argc) ptr = argv[i]; else terror ("missing operand to `-n' option"); /* Fall through to get the number */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': lines = getnum_ul (ptr); flag_lines = 1; continue; } } else in = xfopen (argv[i], "r"); do_head (); fclose (in); } if (flag_done == 0) do_head (); return (EXIT_SUCCESS); }