void erase_arrow( void) { int line = INDEX_TOP + currmenu->curr - currmenu->first; if (!currmenu->max) return; MoveCursor(line, 0); if (tinrc.draw_arrow) my_fputs(" ", stdout); else { #ifdef USE_CURSES char buffer[BUFSIZ]; char *s = screen_contents(line, 0, buffer); #else char *s; if (line - INDEX_TOP < 0) /* avoid underruns */ line = INDEX_TOP; s = screen[line - INDEX_TOP].col; #endif /* USE_CURSES */ EndInverse(); my_fputs(s, stdout); if (s[MARK_OFFSET] == tinrc.art_marked_selected) { MoveCursor(line, MARK_OFFSET); StartInverse(); my_fputc(s[MARK_OFFSET], stdout); EndInverse(); } } }
void draw_arrow_mark( int line) { MoveCursor(line, 0); if (tinrc.draw_arrow) my_fputs("->", stdout); else { #ifdef USE_CURSES char buffer[BUFSIZ]; char *s = screen_contents(line, 0, buffer); #else char *s = screen[line - INDEX_TOP].col; #endif /* USE_CURSES */ StartInverse(); my_fputs(s, stdout); EndInverse(); if (s[MARK_OFFSET] == tinrc.art_marked_selected) { MoveCursor(line, MARK_OFFSET); EndInverse(); my_fputc(s[MARK_OFFSET], stdout); } } stow_cursor(); }
/* * Output a string to the notes file. * This is the only function that references that file. */ void output_note(char *final_note) { PHYSFS_file *fff; char basename[13]; char buf[1024]; /* Hack -- extract first 8 characters of name and append an extension */ (void)strnfmt(basename, sizeof(basename), "%.8s.nte", player_base); basename[sizeof(basename) - 1] = '\0'; /* Build the path */ path_build(buf, sizeof(buf), TENGINE_DIR_NOTE, basename); /* Open notes file */ fff = my_fopen(buf, "a"); /* Failure */ if (!fff) return; /* Add note, and close note file */ my_fputs(fff, final_note); /* Close the handle */ my_fclose(fff); /* Done */ return; }
/* * Print a formatted colour message at the bottom of the screen, wait a while */ void wait_message( unsigned int sdelay, const char *fmt, ...) { va_list ap; va_start(ap, fmt); clear_message(); #ifdef HAVE_COLOR fcol(tinrc.col_message); #endif /* HAVE_COLOR */ vsnprintf(mesg, sizeof(mesg), fmt, ap); my_fputs(mesg, stdout); #ifdef HAVE_COLOR fcol(tinrc.col_normal); #endif /* HAVE_COLOR */ cursoron(); my_flush(); (void) sleep(sdelay); /* clear_message(); would be nice, but tin doesn't expect this yet */ va_end(ap); }
/* * Print a formatted message to stderr, no colour is added. * Interesting - this function implicitly clears 'errno' */ void error_message( const char *fmt, ...) { va_list ap; va_start(ap, fmt); errno = 0; clear_message(); vsnprintf(mesg, sizeof(mesg), fmt, ap); my_fputs(mesg, stderr); /* don't use my_fprintf() here due to %format chars */ my_fflush(stderr); if (cmd_line) { my_fputc('\n', stderr); fflush(stderr); } else { stow_cursor(); (void) sleep(2); clear_message(); } va_end(ap); }
int main() { char buf[10]; my_fputs(my_fgets(buf, 10, stdin), stdout); my_getline(buf, 10); printf("%s", buf); return 0; }
void log_error(char *message) { int file; time_t rowtime; time(&rowtime); file = my_fopen("error.log", O_APPEND, 0777); if(file == 0) { my_putstr("log file can't be writed"); return; } my_fputs(ctime(&rowtime), file); my_fputs (message, file); my_fputs(": ", file); my_fputs(strerror(errno), file); my_fputs ("\n", file); close(file); }
/* * emit a newline, reset and redraw prompt and current input line */ static void gl_redraw( void) { if (gl_init_done == -1) { my_fputc('\n', stdout); my_fputs(gl_prompt, stdout); gl_pos = 0; gl_fixup(0, BUF_SIZE); } }
/* * Send "QUIT" command and close the connection to the server * * Side effects: Closes the connection to the server. * You can't use "put_server" or "get_server" after this * routine is called. * * TODO: remember servers response string and if it contains anything else * than just "." (i.e. transfer statistics) present it to the user? * */ void close_server( void) { if (nntp_wr_fp == NULL || nntp_rd_fp == NULL) return; if (!batch_mode || verbose) my_fputs(_(txt_disconnecting), stdout); nntp_command("QUIT", OK_GOODBYE, NULL, 0); quitting = TRUE; /* Don't reconnect just for this */ (void) s_fclose(nntp_wr_fp); (void) s_fclose(nntp_rd_fp); s_end(); nntp_wr_fp = nntp_rd_fp = NULL; }
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n = 0; char sendline[MAXLINE]; char recvline[MAXLINE + 1]; //!!! while (my_fgets(sendline, MAXLINE, fp) != NULL) { my_sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); n = my_recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); recvline[n] = 0; my_fputs(recvline, stdout); } }
void show_title( const char *title) { int col; col = (cCOLS - (int) strlen(_(txt_type_h_for_help))) + 1; if (col) { MoveCursor(0, col); #ifdef HAVE_COLOR fcol(tinrc.col_title); #endif /* HAVE_COLOR */ /* you have mail message in */ my_fputs((mail_check() ? _(txt_you_have_mail) : _(txt_type_h_for_help)), stdout); #ifdef HAVE_COLOR fcol(tinrc.col_normal); #endif /* HAVE_COLOR */ } center_line(0, TRUE, title); /* wastes some space on the left */ }
void tu8_9dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n = 0; char sendline[MAXLINE]; char recvline[MAXLINE]; socklen_t len ; struct sockaddr *preply_addr; //用于返回的地址, sockaddr!!! preply_addr = my_malloc(servlen); while (my_fgets(sendline, MAXLINE, fp) != NULL) { my_sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); // printf("sendto %s : %s\n", // my_sock_ntop(preply_addr, len), sendline); len = servlen ; n = my_recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len); // printf("recvfrom %s : %s\n", // my_sock_ntop(preply_addr, len), recvline); if (len != servlen || memcmp(pservaddr, preply_addr, len) != 0) { printf("reply from %s (ignored)\n", my_sock_ntop(preply_addr, len)); continue; } recvline[n] = 0; my_fputs(recvline, stdout); } }
int main(int argc, char **argv) { int sockfd = 0; int n = 0; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; struct in_addr **pptr = NULL; // struct in_addr *inetaddrp[2]; // struct in_addr inetaddr; // struct hostent *hp = NULL; struct servent *sp = NULL; if ( argc != 3 ) { err_quit("usage: daytimetcpcli <hostname> <service>"); } if ( (hp = gethostbyname(argv[1])) == NULL ) { if (inet_aton(argv[1], &inetaddr) == 0) { err_quit("hostname error for %s: %s\n", argv[1], hstrerror(h_errno)); } else { inetaddrp[0] = &inetaddr; inetaddrp[1] = NULL; pptr = inetaddrp; //注意是有p的. } } else { pptr = (struct in_addr **) hp->h_addr_list; } //获得了相应服务的端口号什么的 if ( (sp = getservbyname(argv[2], "tcp")) == NULL ) { err_quit("getservbyname error for %s\n", argv[2]); } for ( ; *pptr != NULL; pptr++ ) { sockfd = my_socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = sp->s_port; //帅 memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr)); printf("trying %s\n", my_sock_ntop((SA *) &servaddr, sizeof(servaddr))); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) == 0) { break; //success } err_ret("connect error"); close(sockfd); } if (*pptr == NULL) //这里不是很懂 { err_quit("unable to connect"); } while ((n= my_read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; my_fputs(recvline, stdout); } exit (0); }
void center_line( int line, t_bool inverse, const char *str) { char buffer[256]; int pos; int len; #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) int width; wchar_t wbuffer[256]; /* needs same number of elements as buffer */ wchar_t wbuffer2[256]; wchar_t suffix_buf[6]; /* space for TRUNC_TAIL */ #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ STRCPY(buffer, str); /* protect terminal... */ convert_to_printable(buffer); #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) if ((len = mbstowcs(wbuffer, buffer, ARRAY_SIZE(wbuffer) - 1)) <= 0) len = strlen(buffer); else wbuffer[ARRAY_SIZE(wbuffer) - 1] = (wchar_t) '\0'; if ((width = wcswidth(wbuffer, ARRAY_SIZE(wbuffer) - 1)) <= 0) width = len; #else len = strlen(buffer); #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ if (!cmd_line) { #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) if (cCOLS >= width) pos = (cCOLS - width) / 2; #else if (cCOLS >= len) pos = (cCOLS - len) / 2; #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ else pos = 1; MoveCursor(line, pos); if (inverse) { StartInverse(); my_flush(); } } #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) if (width >= cCOLS) { wcspart(wbuffer2, wbuffer, cCOLS - 6, ARRAY_SIZE(wbuffer2) - 5, TRUE); mbstowcs(suffix_buf, TRUNC_TAIL, ARRAY_SIZE(suffix_buf) - 1); wcsncat(wbuffer2, suffix_buf, 4); wcstombs(buffer, wbuffer2, sizeof(buffer) - 1); } #else if (len >= cCOLS) { char *buf; buf = my_strdup(buffer); snprintf(buffer, sizeof(buffer), "%-.*s%s", cCOLS - 6, buf, TRUNC_TAIL); free(buf); } #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ my_fputs(buffer, stdout); if (cmd_line) my_flush(); else { if (inverse) EndInverse(); } }
char * tin_getline( const char *prompt, int number_only, /* 1=positive numbers only, 2=negative too */ const char *str, int max_chars, t_bool passwd, int which_hist) { int c, i, loc, tmp, gl_max; #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) wint_t wc; #else char *buf = gl_buf; #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ is_passwd = passwd; set_xclick_off(); if (prompt == NULL) prompt = ""; gl_buf[0] = 0; /* used as end of input indicator */ gl_fixup(-1, 0); /* this resets gl_fixup */ gl_width = cCOLS - strlen(prompt); gl_prompt = prompt; gl_pos = gl_cnt = 0; if (max_chars == 0) { if (number_only) gl_max = 6; else gl_max = BUF_SIZE; } else gl_max = max_chars; my_fputs(prompt, stdout); cursoron(); my_flush(); if (gl_in_hook) { loc = gl_in_hook(gl_buf); if (loc >= 0) gl_fixup(0, BUF_SIZE); } if (!cmd_line && gl_max == BUF_SIZE) CleartoEOLN(); #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) if (str != NULL) { wchar_t wbuf[LEN]; if (mbstowcs(wbuf, str, ARRAY_SIZE(wbuf) - 1) != (size_t) -1) { wbuf[ARRAY_SIZE(wbuf) - 1] = (wchar_t) '\0'; for (i = 0; wbuf[i]; i++) gl_addwchar(wbuf[i]); } } while ((wc = ReadWch()) != WEOF) { if ((gl_cnt < gl_max) && iswprint(wc)) { if (number_only) { if (iswdigit(wc)) { gl_addwchar(wc); /* Minus */ } else if (number_only == 2 && gl_pos == 0 && wc == (wint_t) '-') { gl_addwchar(wc); } else { ring_bell(); } } else gl_addwchar(wc); } else { c = (int) wc; switch (wc) { #else if (str != NULL) { for (i = 0; str[i]; i++) gl_addchar(str[i]); } while ((c = ReadCh()) != EOF) { c &= 0xff; if ((gl_cnt < gl_max) && my_isprint(c)) { if (number_only) { if (isdigit(c)) { gl_addchar(c); /* Minus */ } else if (number_only == 2 && gl_pos == 0 && c == '-') { gl_addchar(c); } else { ring_bell(); } } else gl_addchar(c); } else { switch (c) { #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ case ESC: /* abort */ #ifdef HAVE_KEY_PREFIX case KEY_PREFIX: #endif /* HAVE_KEY_PREFIX */ switch (get_arrow_key(c)) { case KEYMAP_UP: case KEYMAP_PAGE_UP: hist_prev(which_hist); break; case KEYMAP_PAGE_DOWN: case KEYMAP_DOWN: hist_next(which_hist); break; case KEYMAP_RIGHT: gl_fixup(-1, gl_pos + 1); break; case KEYMAP_LEFT: gl_fixup(-1, gl_pos - 1); break; case KEYMAP_HOME: gl_fixup(-1, 0); break; case KEYMAP_END: gl_fixup(-1, gl_cnt); break; case KEYMAP_DEL: gl_del(0); break; case KEYMAP_INS: #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) gl_addwchar((wint_t) ' '); #else gl_addchar(' '); #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ break; default: return (char *) 0; } break; case '\n': /* newline */ case '\r': gl_newline(which_hist); #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) wcstombs(buf, gl_buf, BUF_SIZE - 1); #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ return buf; case CTRL_A: gl_fixup(-1, 0); break; case CTRL_B: gl_fixup(-1, gl_pos - 1); break; case CTRL_D: if (gl_cnt == 0) { gl_buf[0] = 0; my_fputc('\n', stdout); #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) wcstombs(buf, gl_buf, BUF_SIZE - 1); #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ return buf; } else gl_del(0); break; case CTRL_E: gl_fixup(-1, gl_cnt); break; case CTRL_F: gl_fixup(-1, gl_pos + 1); break; case CTRL_H: case DEL: gl_del(-1); break; case TAB: if (gl_tab_hook) { tmp = gl_pos; loc = gl_tab_hook(gl_buf, strlen(gl_prompt), &tmp); if (loc >= 0 || tmp != gl_pos) gl_fixup(loc, tmp); } break; case CTRL_W: gl_kill_back_word(); break; case CTRL_U: gl_fixup(-1, 0); /* FALLTHROUGH */ case CTRL_K: gl_kill(); break; case CTRL_L: case CTRL_R: gl_redraw(); break; case CTRL_N: hist_next(which_hist); break; case CTRL_P: hist_prev(which_hist); break; default: ring_bell(); break; } } } #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) wcstombs(buf, gl_buf, BUF_SIZE - 1); #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ return buf; } /* * adds the character c to the input buffer at current location if * the character is in the allowed template of characters */ static void #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) gl_addwchar( wint_t wc) #else gl_addchar( int c) #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ { int i; /* * Crashing is always the worst solution IMHO. So as a quick hack, * ignore characters silently, if buffer is full. To allow a final * newline, leave space for one more character. Just a hack too. * This was the original code: * if (gl_cnt >= BUF_SIZE - 1) { error_message("tin_getline: input buffer overflow"); giveup(); } */ if (gl_cnt >= BUF_SIZE - 2) return; for (i = gl_cnt; i >= gl_pos; i--) gl_buf[i + 1] = gl_buf[i]; #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) gl_buf[gl_pos] = (wchar_t) wc; #else gl_buf[gl_pos] = c; #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ gl_fixup(gl_pos, gl_pos + 1); } /* * Cleans up entire line before returning to caller. A \n is appended. * If line longer than screen, we redraw starting at beginning */ static void gl_newline( int w) { int change = gl_cnt; int len = gl_cnt; int loc = gl_width - 5; /* shifts line back to start position */ if (gl_cnt >= BUF_SIZE - 1) { /* * Like above: avoid crashing if possible. gl_addchar() now * leaves one space left for the newline, so this part of the * code should never be reached. A proper implementation is * desirable though. */ error_message("tin_getline: input buffer overflow"); giveup(); } hist_add(w); /* only adds if nonblank */ if (gl_out_hook) { change = gl_out_hook(gl_buf); #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) len = wcslen(gl_buf); #else len = strlen(gl_buf); #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ } if (loc > len) loc = len; gl_fixup(change, loc); /* must do this before appending \n */ #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) gl_buf[len] = (wchar_t) '\0'; #else gl_buf[len] = '\0'; #endif /* MULTIBYTE_ABLE && !NO_LOCALE */ } /* * Delete a character. The loc variable can be: * -1 : delete character to left of cursor * 0 : delete character under cursor */ static void gl_del( int loc) { int i; if ((loc == -1 && gl_pos > 0) || (loc == 0 && gl_pos < gl_cnt)) { for (i = gl_pos + loc; i < gl_cnt; i++) gl_buf[i] = gl_buf[i + 1]; gl_fixup(gl_pos + loc, gl_pos + loc); } else ring_bell(); }