void tseg_set_text(tseg_text_t *ts, char *string ) { m_clear( ts->text ); m_write( ts->text, 0, string, strlen(string)+1 ); int ms = ts->m_seg; int ma = ts->m_attr; text_segment_t seg; char *s = string; int a,b,x,attr = 0; while( isspace(*s) ) s++; m_clear( ms ); while(1) { memset(&seg,0,sizeof seg); if( strlist_cut_word(s,&a,&b)) break; if( b-a <= 0 ) break; if( s[b] == '\n' ) seg.format = T_HARD_BREAK; if( s[a] == '@' ) /* attribute prefix found */ { a++; if( s[a] != '@' ) { if( (x = tseg_get_format(ma,s,&a,&b))) seg.format |= x; if( (x=tseg_get_attr(ma, s, &a, &b )) >= 0 ) attr = x; } } seg.start = (s-string)+a; seg.end = (s-string)+b-1; seg.attribute = attr; s+=b; m_put( ms, &seg ); } }
void write(const T* source, std::size_t n, system::error_code& ec) // Requires: is_open() // Effects: As if, POSIX write(), except will finish partial write. // Length of write is n * sizeof(boost::remove_extent<T>::type). Sets ec to 0 // if no error, otherwise sets ec to the system error code. { BOOST_ASSERT(is_open()); m_write(source, n * sizeof(typename boost::remove_extent<T>::type), ec); }
void write(const T* source, std::size_t n) // Requires: is_open() // Effects: As if, POSIX write(), except will finish partial write. // Length of write is n * sizeof(typename boost::remove_extent<T>::type). // Throws: On error. { BOOST_ASSERT(is_open()); m_write(source, n * sizeof(typename boost::remove_extent<T>::type)); }
void write(const T& source, system::error_code& ec) // Requires: is_open() // Effects: As if, POSIX write(), except will finish partial write. // Length of write is sizeof(T). Sets ec to 0 if no error, otherwise // sets ec to the system error code. { BOOST_ASSERT(is_open()); m_write(&source, sizeof(T), ec); }
void write(const T& source) // Requires: is_open() // Effects: As if, POSIX write(), except will finish partial write. // Length of write is sizeof(T). // Throws: On error. { BOOST_ASSERT(is_open()); m_write(&source, sizeof(T)); }
int main(int argc, char *argv[]) { int fd = -1; char cmd; if (2 != argc){ printf("check your input pls!\n"); return 0; } do { printf("[o-open|w-write|r-read|c-clear|s-seek|q-quit]:"); cmd = getchar(); getchar(); switch (cmd) { case 'o': if (-1 == fd) fd = open(argv[1], O_RDWR); break; case 'w': if (-1 == fd) { printf("you should run command o first!\n"); break; } m_write(fd); break; case 'r': if (-1 == fd) { printf("you should run command o first!\n"); break; } m_read(fd); break; case 'q': if (-1 != fd) close(fd); break; case 'c': if (-1 == fd) { printf("you should run command o first!\n"); break; } ioctl(fd, 0); break; case 's': if (-1 == fd) { printf("you should run command o first!\n"); break; } m_seek(fd); break; default: ; } }while('q' != cmd); return 0; }
int main (void) { /* const char i_path[] = "/etc/motd"; */ const char message[] = "the file contents\n"; const char *m_ptr = message; const char o_path[] = "file1"; const int mode = 0664; /* int i_fd; */ int o_fd; int f_size; int i; /* if ((i_fd = open (i_path, O_RDONLY)) == -1) */ /* { */ /* perror ("open"); */ /* goto error; */ /* } */ if ((o_fd = open (o_path, O_RDWR | O_CREAT, (mode_t)mode)) == -1) { perror ("open"); goto error; } /* f_size = (strlen (message) * sizeof (char)) + 1; */ /* if ((f_size = get_fsize (i_fd)) == -1) */ /* { */ /* perror ("fstat"); */ /* goto error; */ /* } */ /* printf ("size: \t%d bytes\n", f_size); */ if ((i = m_write (o_fd, message)) == -1) { perror ("m_write"); goto error; } printf ("mapped: \t%d bytes\n", i); /* close (i_fd); */ close (o_fd); return 0; error: /* close (i_fd); */ close (o_fd); return 1; }
void Log::m_log(const char *fmt, ...) { va_list va_alist; if (fmt == NULL || strlen(fmt) == 0) return; va_start(va_alist, fmt); m_write(fmt, va_alist); va_end(va_alist); }
void Log::m_error(const char *header, const char* fmt, ...) { va_list va_alist; if (fmt == NULL || strlen(fmt) == 0) return; va_start(va_alist, fmt); std::string format(std::string(header) + fmt); m_write(format.c_str(), va_alist, true); va_end(va_alist); }
void monitor_carpdemote(void *v) { u_int32_t mtype = MONITOR_CARPINC; if (carp_demoted) return; if (m_write(m_state.s, &mtype, sizeof mtype) < 1) log_msg(1, "monitor_carpdemote: unable to write to monitor"); else carp_demoted = 1; }
void Log::m_debug(const char *header, const char *fmt, ...) { #ifdef DEBUG va_list va_alist; if (fmt == NULL || strlen(fmt) == 0) return; va_start(va_alist, fmt); std::string format(std::string(header) + fmt); m_write(format.c_str(), va_alist); va_end(va_alist); #endif }
void Log::m_hexdump(const void *addr, size_t len, const char *desc, ...) { size_t i; unsigned char buff[17]; unsigned char *pc = (unsigned char*) addr; // Output description if given. if (desc != nullptr && strlen(desc) > 0) { va_list va_alist; va_start(va_alist, desc); m_write(desc, va_alist); va_end(va_alist); m_log(":\n"); } // Process every byte in the data. for (i = 0; i < len; i++) { // Multiple of 16 means new line (with line offset). if ((i % 16) == 0) { // Just don't print ASCII for the zeroth line. if (i != 0) m_log(" %s\n", buff); // Output the offset. m_log(" %04x ", i); } // Now the hex code for the specific character. m_log(" %02x", pc[i]); // And store a printable ASCII character for later. if (isprint(pc[i])) buff[i % 16] = pc[i]; else buff[i % 16] = '.'; buff[(i % 16) + 1] = '\0'; } // Pad out last line if not exactly 16 characters. while ((i % 16) != 0) { m_log(" "); i++; } // And print the final ASCII bit. m_log(" %s\n", buff); }
/* Privileged */ static void m_priv_pfkey_snap(int s) { u_int8_t *sadb_buf = 0, *spd_buf = 0; size_t sadb_buflen = 0, spd_buflen = 0, sz; int mib[5]; u_int32_t v; mib[0] = CTL_NET; mib[1] = PF_KEY; mib[2] = PF_KEY_V2; mib[3] = NET_KEY_SADB_DUMP; mib[4] = 0; /* Unspec SA type */ /* First, fetch SADB data */ if (sysctl(mib, sizeof mib / sizeof mib[0], NULL, &sz, NULL, 0) == -1 || sz == 0) { sadb_buflen = 0; goto try_spd; } sadb_buflen = sz; if ((sadb_buf = malloc(sadb_buflen)) == NULL) { log_err("m_priv_pfkey_snap: malloc"); sadb_buflen = 0; goto out; } if (sysctl(mib, sizeof mib / sizeof mib[0], sadb_buf, &sz, NULL, 0) == -1) { log_err("m_priv_pfkey_snap: sysctl"); sadb_buflen = 0; goto out; } /* Next, fetch SPD data */ try_spd: mib[3] = NET_KEY_SPD_DUMP; if (sysctl(mib, sizeof mib / sizeof mib[0], NULL, &sz, NULL, 0) == -1 || sz == 0) { spd_buflen = 0; goto out; } spd_buflen = sz; if ((spd_buf = malloc(spd_buflen)) == NULL) { log_err("m_priv_pfkey_snap: malloc"); spd_buflen = 0; goto out; } if (sysctl(mib, sizeof mib / sizeof mib[0], spd_buf, &sz, NULL, 0) == -1) { log_err("m_priv_pfkey_snap: sysctl"); spd_buflen = 0; goto out; } out: /* Return SADB data */ v = (u_int32_t)sadb_buflen; if (m_write(s, &v, sizeof v) == -1) { log_err("m_priv_pfkey_snap: write"); return; } if (sadb_buflen) { if (m_write(s, sadb_buf, sadb_buflen) == -1) log_err("m_priv_pfkey_snap: write"); memset(sadb_buf, 0, sadb_buflen); free(sadb_buf); } /* Return SPD data */ v = (u_int32_t)spd_buflen; if (m_write(s, &v, sizeof v) == -1) { log_err("m_priv_pfkey_snap: write"); return; } if (spd_buflen) { if (m_write(s, spd_buf, spd_buflen) == -1) log_err("m_priv_pfkey_snap: write"); memset(spd_buf, 0, spd_buflen); free(spd_buf); } return; }
int monitor_get_pfkey_snap(u_int8_t **sadb, u_int32_t *sadbsize, u_int8_t **spd, u_int32_t *spdsize) { u_int32_t v; ssize_t rbytes; v = MONITOR_GETSNAP; if (m_write(m_state.s, &v, sizeof v) < 1) return -1; /* Read SADB data. */ *sadb = *spd = NULL; *spdsize = 0; if (m_read(m_state.s, sadbsize, sizeof *sadbsize) < 1) return -1; if (*sadbsize) { *sadb = (u_int8_t *)malloc(*sadbsize); if (!*sadb) { log_err("monitor_get_pfkey_snap: malloc()"); monitor_drain_input(); return -1; } rbytes = m_read(m_state.s, *sadb, *sadbsize); if (rbytes < 1) { memset(*sadb, 0, *sadbsize); free(*sadb); return -1; } } /* Read SPD data */ if (m_read(m_state.s, spdsize, sizeof *spdsize) < 1) { if (*sadbsize) { memset(*sadb, 0, *sadbsize); free(*sadb); } return -1; } if (*spdsize) { *spd = (u_int8_t *)malloc(*spdsize); if (!*spd) { log_err("monitor_get_pfkey_snap: malloc()"); monitor_drain_input(); if (*sadbsize) { memset(*sadb, 0, *sadbsize); free(*sadb); } return -1; } rbytes = m_read(m_state.s, *spd, *spdsize); if (rbytes < 1) { memset(*spd, 0, *spdsize); free(*spd); if (*sadbsize) { memset(*sadb, 0, *sadbsize); free(*sadb); } return -1; } } log_msg(2, "monitor_get_pfkey_snap: got %u bytes SADB, %u bytes SPD", *sadbsize, *spdsize); return 0; }