本文整理汇总了C++中puts函数的典型用法代码示例。如果您正苦于以下问题:C++ puts函数的具体用法?C++ puts怎么用?C++ puts使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了puts函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main (
int argc,
char *argv [])
{
char *fname = "/etc/apache2/conf.d/php.conf.yaml";
FILE *fh;
if (argc > 1)
fname = argv [1];
fh = fopen(fname, "r");
printf ("Openned :%s:\n", fname);
yaml_parser_t parser;
yaml_token_t token; /* New variable */
/* Initialize parser */
if(!yaml_parser_initialize(&parser))
fputs("Failed to initialize parser!\n", stderr);
if(fh == NULL)
fputs("Failed to open file!\n", stderr);
/* Set input file */
yaml_parser_set_input_file(&parser, fh);
/* START new code */
do {
yaml_parser_scan(&parser, &token);
switch(token.type)
{
/* Stream start/end */
case YAML_STREAM_START_TOKEN:
puts("STREAM START");
break;
case YAML_STREAM_END_TOKEN:
puts("STREAM END");
break;
/* Token types (read before actual token) */
case YAML_KEY_TOKEN:
printf("(Key token) ");
break;
case YAML_VALUE_TOKEN:
printf("(Value token) ");
break;
/* Block delimeters */
case YAML_BLOCK_SEQUENCE_START_TOKEN:
puts("<b>Start Block (Sequence)</b>");
break;
case YAML_BLOCK_ENTRY_TOKEN:
puts("<b>Start Block (Entry)</b>");
break;
case YAML_BLOCK_END_TOKEN:
puts("<b>End block</b>");
break;
/* Data */
case YAML_BLOCK_MAPPING_START_TOKEN:
puts("[Block mapping]");
break;
case YAML_SCALAR_TOKEN:
printf("scalar %s \n", token.data.scalar.value);
break;
/* Others */
default:
printf("Got token of type %d\n", token.type);
}
if(token.type != YAML_STREAM_END_TOKEN)
yaml_token_delete(&token);
} while (token.type != YAML_STREAM_END_TOKEN);
yaml_token_delete(&token);
/* END new code */
/* Cleanup */
yaml_parser_delete(&parser);
fclose(fh);
return 0;
}
示例2: main
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
示例3: main
//.........这里部分代码省略.........
exit(1);
}
//printf("RECV [%d bytes]: invitation message\n", msg_size);
output(buffer);
char space;
bzero(name, sizeof(name));
bzero(buffer, sizeof(buffer));
bzero(author, sizeof(author));
bzero(command, sizeof(command));
bzero(content, sizeof(content));
scanf("%5s%1c", command, &space);
if ((msg_size = sendto(sock, command, strlen(command), 0, (struct sockaddr *)&client, sizeof(client))) < 0)
{
perror("SEND command failed");
exit(1);
}
//printf("SEND [%d bytes]: command '%s'\n", msg_size, path);
if (!strcmp(command, ":exit"))
{
bzero(buffer, sizeof(buffer));
if ((msg_size = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&client, (socklen_t *)&len)) < 0) // Receive the content of file
{
perror("RECV file or directory content failed");
exit(1);
}
//printf("RECV [%d bytes]: file or directory content\n", msg_size);
output(buffer);
break;
}
if (recv_report(sock) < 0)
{
puts("!No such command");
send_report(sock, SUCCESS);
}
if (!strcmp(command, "add"))
{
char str[SIZE_ARG];
fgets(name, sizeof(name), stdin);
if (name[strlen(name) - 1] == '\n')
name[strlen(name) - 1] = '\0';
if ((msg_size = sendto(sock, name, strlen(name), 0, (struct sockaddr *)&client, sizeof(client))) < 0)
{
perror("SEND command failed");
exit(1);
}
//printf("SEND [%d bytes]: title of article '%s'\n", msg_size, name);
if (recv_report(sock) < 0)
{
puts("!Such file already exist");
send_report(sock, SUCCESS);
}
else
{
int length = sizeof(content) - sizeof(author) - sizeof(name);
printf("Input author: ");
fgets(author, sizeof(author), stdin);
if (author[strlen(author) - 1] == '\n')
author[strlen(author) - 1] = '\0';
printf("name's read: %s [%d bytes]\n", name, msg_size);
printf("author's read: %s [%d bytes]\n", author, msg_size);
puts("Put content:");
printf("[%d of %d] ", (strlen(content)+strlen(str)), length);
示例4: while
void *threadfunc()
{
int cnt,cnt1;
char str[100];
char copy[100],tmpstr[100],tmpfinal[100];
int z;int i;
char restartFile[100],rstart[100];
int pos;
char processID[100],check[400],context[100];
FILE *fp;
while(1)
{
if(read(sockdes,buff,sizeof(buff)))
{
puts(buff);
if(!strcmp(buff,"CP"))
{
FILE *procs;
procs = popen("ps -e -o pid,cmd","r");
fscanf(procs,"%s",processID);
fgets(str,100,procs);
while(!feof(procs))
{
fscanf(procs,"%s",processID);
fgets(str,100,procs);
if(strstr(str,"a.out")!=NULL)
{
sprintf(str,"cr_checkpoint --save-all --kill %s",processID);
strcpy(tmpstr,"/home/cse4119/context_files/bal/context.");
strcat(tmpstr,processID);
printf("\nHere\n");
for(cnt=0;strcmp(tmpstr,processIDs[cnt])!=0;cnt++)
{
// printf("\n%s --> %s",processIDs[cnt],tmpstr);
}
printf("\nHere\n");
for(cnt1=(cnt+1);cnt1<noOfProcess;cnt1++)
strcpy(processIDs[cnt1-1],processIDs[cnt1]);
noOfProcess--;
strcpy(check,"\0");
fp=popen(str,"r");
fgets(check,100,fp);
fclose(fp);
if(strstr(check,"Checkpoint")!=NULL)
{
printf("\nThe selected file could not be checkpointed");
}
else
{
printf("\nForced Checkpoint due to overload successful\n");
sprintf(context,"chmod 777 context.%s",processID);
system(context);
sprintf(context,"mv context.%s /home/cse4119/context_files/redist/context.%s",processID,processID);
system(context);
break;
}
}
}
}
else if(strstr(buff,"FAIL")!=NULL)
{
for(z=strlen(buff);buff[z]!='/';z--);
z++;
pos=0;
for(;z<=strlen(buff);z++)
restartFile[pos++]=buff[z];
printf("\nTo be restarted: %s",restartFile);
if(strstr(buff,"FAIL")!=NULL)
{
for(i=5;i<=strlen(buff);i++)
{
rstart[i-5]=buff[i];
}
}
// sprintf(copy,"mv %s %s",rstart,restartFile);
// system(copy);
// sleep(3);
printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&\n&&&&&&&&&&&&&&&&&&&&\nRESTART\n&&&&&&&&&&&&&&&&&&&&&&&&&&&\n&&&&&&&&&&&&&&&&&&&&\n");
pthread_create(&restartThread,NULL,restartFunc,buff);
pthread_create(&restartThreadRem,NULL,restartFuncDel,rstart);
}
else if(strstr(buff,"context")!=NULL)
{
sprintf(copy,"mv /home/cse4119/context_files/redist/%s %s",buff,buff);
system(copy);
sprintf(tmpfinal,"/home/cse4119/context_files/redist/%s",buff);
// printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&\n&&&&&&&&&&&&&&&&&&&&\nRESTART\n&&&&&&&&&&&&&&&&&&&&&&&&&&&\n&&&&&&&&&&&&&&&&&&&&\n");
//.........这里部分代码省略.........
示例5: au_do_update
int
au_do_update(int idx, long sz)
{
image_header_t *hdr;
char *addr;
long start, end;
int off, rc;
uint nbytes;
hdr = (image_header_t *)LOAD_ADDR;
#if defined(CONFIG_FIT)
if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
puts ("Non legacy image format not supported\n");
return -1;
}
#endif
/* disable the power switch */
*CPLD_VFD_BK |= POWER_OFF;
/* execute a script */
if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
addr = (char *)((char *)hdr + image_get_header_size ());
/* stick a NULL at the end of the script, otherwise */
/* parse_string_outer() runs off the end. */
addr[image_get_data_size (hdr)] = 0;
addr += 8;
parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
return 0;
}
start = aufl_layout[FIDX_TO_LIDX(idx)].start;
end = aufl_layout[FIDX_TO_LIDX(idx)].end;
/* unprotect the address range */
/* this assumes that ONLY the firmware is protected! */
if (idx == IDX_FIRMWARE) {
#undef AU_UPDATE_TEST
#ifdef AU_UPDATE_TEST
/* erase it where Linux goes */
start = aufl_layout[1].start;
end = aufl_layout[1].end;
#endif
flash_sect_protect(0, start, end);
}
/*
* erase the address range.
*/
debug ("flash_sect_erase(%lx, %lx);\n", start, end);
flash_sect_erase(start, end);
wait_ms(100);
/* strip the header - except for the kernel and ramdisk */
if (image_check_type (hdr, IH_TYPE_KERNEL) ||
image_check_type (hdr, IH_TYPE_RAMDISK)) {
addr = (char *)hdr;
off = image_get_header_size ();
nbytes = image_get_image_size (hdr);
} else {
addr = (char *)((char *)hdr + image_get_header_size ());
#ifdef AU_UPDATE_TEST
/* copy it to where Linux goes */
if (idx == IDX_FIRMWARE)
start = aufl_layout[1].start;
#endif
off = 0;
nbytes = image_get_data_size (hdr);
}
/* copy the data from RAM to FLASH */
debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
rc = flash_write(addr, start, nbytes);
if (rc != 0) {
printf("Flashing failed due to error %d\n", rc);
return -1;
}
/* check the dcrc of the copy */
if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
image_get_dcrc (hdr)) {
printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
return -1;
}
/* protect the address range */
/* this assumes that ONLY the firmware is protected! */
if (idx == IDX_FIRMWARE)
flash_sect_protect(1, start, end);
return 0;
}
示例6: translate_messages
int /* O - 1 on success, 0 on error */
translate_messages(cups_array_t *cat, /* I - Message catalog */
const char *lang) /* I - Output language... */
{
/*
* Google provides a simple translation/language tool for translating
* from one language to another. It is far from perfect, however it
* can be used to get a basic translation done or update an existing
* translation when no other resources are available.
*
* Translation requests are sent as HTTP POSTs to
* "http://translate.google.com/translate_t" with the following form
* variables:
*
* Name Description Value
* -------- ---------------------------------- ----------------
* hl Help language? "en"
* ie Input encoding "UTF8"
* langpair Language pair "en|" + language
* oe Output encoding "UTF8"
* text Text to translate translation string
*/
int ret; /* Return value */
_cups_message_t *m; /* Current message */
int tries; /* Number of tries... */
http_t *http; /* HTTP connection */
http_status_t status; /* Status of POST request */
char *idptr, /* Pointer into msgid */
buffer[65536], /* Input/output buffer */
*bufptr, /* Pointer into buffer */
*bufend, /* Pointer to end of buffer */
length[16]; /* Content length */
int bytes; /* Number of bytes read */
/*
* Connect to translate.google.com...
*/
puts("Connecting to translate.google.com...");
if ((http = httpConnect("translate.google.com", 80)) == NULL)
{
perror("Unable to connect to translate.google.com");
return (0);
}
/*
* Scan the current messages, requesting a translation of any untranslated
* messages...
*/
for (m = (_cups_message_t *)cupsArrayFirst(cat), ret = 1;
m;
m = (_cups_message_t *)cupsArrayNext(cat))
{
/*
* Skip messages that are already translated...
*/
if (m->str && m->str[0])
continue;
/*
* Encode the form data into the buffer...
*/
snprintf(buffer, sizeof(buffer),
"hl=en&ie=UTF8&langpair=en|%s&oe=UTF8&text=", lang);
bufptr = buffer + strlen(buffer);
bufend = buffer + sizeof(buffer) - 5;
for (idptr = m->id; *idptr && bufptr < bufend; idptr ++)
if (*idptr == ' ')
*bufptr++ = '+';
else if (*idptr < ' ' || *idptr == '%')
{
sprintf(bufptr, "%%%02X", *idptr & 255);
bufptr += 3;
}
else if (*idptr != '&')
*bufptr++ = *idptr;
*bufptr++ = '&';
*bufptr = '\0';
sprintf(length, "%d", (int)(bufptr - buffer));
/*
* Send the request...
*/
printf("\"%s\" = ", m->id);
fflush(stdout);
tries = 0;
do
{
//.........这里部分代码省略.........
示例7: spi_xfer
int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
void *din, unsigned long flags)
{
volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
unsigned int tmpdout, tmpdin, event;
int numBlks = bitlen / 32 + (bitlen % 32 ? 1 : 0);
int tm, isRead = 0;
unsigned char charSize = 32;
debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
if (flags & SPI_XFER_BEGIN)
spi_cs_activate(slave);
spi->event = 0xffffffff; /* Clear all SPI events */
/* handle data in 32-bit chunks */
while (numBlks--) {
tmpdout = 0;
charSize = (bitlen >= 32 ? 32 : bitlen);
/* Shift data so it's msb-justified */
tmpdout = *(u32 *) dout >> (32 - charSize);
/* The LEN field of the SPMODE register is set as follows:
*
* Bit length setting
* len <= 4 3
* 4 < len <= 16 len - 1
* len > 16 0
*/
spi->mode &= ~SPI_MODE_EN;
if (bitlen <= 16) {
if (bitlen <= 4)
spi->mode = (spi->mode & 0xff0fffff) |
(3 << 20);
else
spi->mode = (spi->mode & 0xff0fffff) |
((bitlen - 1) << 20);
} else {
spi->mode = (spi->mode & 0xff0fffff);
/* Set up the next iteration if sending > 32 bits */
bitlen -= 32;
dout += 4;
}
spi->mode |= SPI_MODE_EN;
spi->tx = tmpdout; /* Write the data out */
debug("*** spi_xfer: ... %08x written\n", tmpdout);
/*
* Wait for SPI transmit to get out
* or time out (1 second = 1000 ms)
* The NE event must be read and cleared first
*/
for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) {
event = spi->event;
if (event & SPI_EV_NE) {
tmpdin = spi->rx;
spi->event |= SPI_EV_NE;
isRead = 1;
*(u32 *) din = (tmpdin << (32 - charSize));
if (charSize == 32) {
/* Advance output buffer by 32 bits */
din += 4;
}
}
/*
* Only bail when we've had both NE and NF events.
* This will cause timeouts on RO devices, so maybe
* in the future put an arbitrary delay after writing
* the device. Arbitrary delays suck, though...
*/
if (isRead && (event & SPI_EV_NF))
break;
}
if (tm >= SPI_TIMEOUT)
puts("*** spi_xfer: Time out during SPI transfer");
debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin);
}
if (flags & SPI_XFER_END)
spi_cs_deactivate(slave);
return 0;
}
示例8: misc_init_r
int misc_init_r (void)
{
/* adjust flash start and size as well as the offset */
gd->bd->bi_flashstart = 0 - flash_info[0].size;
gd->bd->bi_flashoffset= flash_info[0].size - CONFIG_SYS_MONITOR_LEN;
#if 0
volatile unsigned short *fpga_mode =
(unsigned short *)((ulong)CONFIG_SYS_FPGA_BASE_ADDR + CONFIG_SYS_FPGA_CTRL);
volatile unsigned char *duart0_mcr =
(unsigned char *)((ulong)DUART0_BA + 4);
volatile unsigned char *duart1_mcr =
(unsigned char *)((ulong)DUART1_BA + 4);
bd_t *bd = gd->bd;
char * tmp; /* Temporary char pointer */
unsigned char *dst;
ulong len = sizeof(fpgadata);
int status;
int index;
int i;
unsigned long CPC0_CR0Reg;
dst = malloc(CONFIG_SYS_FPGA_MAX_SIZE);
if (gunzip (dst, CONFIG_SYS_FPGA_MAX_SIZE, (uchar *)fpgadata, &len) != 0) {
printf ("GUNZIP ERROR - must RESET board to recover\n");
do_reset (NULL, 0, 0, NULL);
}
status = fpga_boot(dst, len);
if (status != 0) {
printf("\nFPGA: Booting failed ");
switch (status) {
case ERROR_FPGA_PRG_INIT_LOW:
printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
break;
case ERROR_FPGA_PRG_INIT_HIGH:
printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
break;
case ERROR_FPGA_PRG_DONE:
printf("(Timeout: DONE not high after programming FPGA)\n ");
break;
}
/* display infos on fpgaimage */
index = 15;
for (i=0; i<4; i++) {
len = dst[index];
printf("FPGA: %s\n", &(dst[index+1]));
index += len+3;
}
putc ('\n');
/* delayed reboot */
for (i=20; i>0; i--) {
printf("Rebooting in %2d seconds \r",i);
for (index=0;index<1000;index++)
udelay(1000);
}
putc ('\n');
do_reset(NULL, 0, 0, NULL);
}
puts("FPGA: ");
/* display infos on fpgaimage */
index = 15;
for (i=0; i<4; i++) {
len = dst[index];
printf("%s ", &(dst[index+1]));
index += len+3;
}
putc ('\n');
free(dst);
/*
* Reset FPGA via FPGA_DATA pin
*/
SET_FPGA(FPGA_PRG | FPGA_CLK);
udelay(1000); /* wait 1ms */
SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
udelay(1000); /* wait 1ms */
#endif
#if 0
/*
* Enable power on PS/2 interface
*/
*fpga_mode |= CONFIG_SYS_FPGA_CTRL_PS2_RESET;
/*
* Enable interrupts in exar duart mcr[3]
*/
*duart0_mcr = 0x08;
*duart1_mcr = 0x08;
#endif
return (0);
}
示例9: LoadDatabase
//.........这里部分代码省略.........
if(fscanf(f,"%hu %hu %hu %hu %hu %hu %hu %hu",
&ap->Vocab,
&ap->Condition[0],
&ap->Condition[1],
&ap->Condition[2],
&ap->Condition[3],
&ap->Condition[4],
&ap->Action[0],
&ap->Action[1])!=8)
{
printf("Bad action line (%d)\n",ct);
exit(1);
}
ap++;
ct++;
}
ct=0;
if(loud)
printf("Reading %d word pairs.\n",nw);
while(ct<nw+1)
{
Verbs[ct]=ReadString(f);
Nouns[ct]=ReadString(f);
ct++;
}
ct=0;
rp=Rooms;
if(loud)
printf("Reading %d rooms.\n",nr);
while(ct<nr+1)
{
if(fscanf(f,"%hd %hd %hd %hd %hd %hd",
&rp->Exits[0],&rp->Exits[1],&rp->Exits[2],
&rp->Exits[3],&rp->Exits[4],&rp->Exits[5])!=6)
{
printf("Bad room line (%d)\n",ct);
exit(1);
}
rp->Text=ReadString(f);
ct++;
rp++;
}
ct=0;
if(loud)
printf("Reading %d messages.\n",mn);
while(ct<mn+1)
{
Messages[ct]=ReadString(f);
ct++;
}
ct=0;
if(loud)
printf("Reading %d items.\n",ni);
ip=Items;
while(ct<ni+1)
{
ip->Text=ReadString(f);
ip->AutoGet=strchr(ip->Text,'/');
/* Some games use // to mean no auto get/drop word! */
if(ip->AutoGet && strcmp(ip->AutoGet,"//") && strcmp(ip->AutoGet,"/*"))
{
char *t;
*ip->AutoGet++=0;
t=strchr(ip->AutoGet,'/');
if(t!=NULL)
*t=0;
}
if(fscanf(f,"%hd",&lo)!=1)
{
printf("Bad item line (%d)\n",ct);
exit(1);
}
ip->Location=(unsigned char)lo;
ip->InitialLoc=ip->Location;
ip++;
ct++;
}
ct=0;
/* Discard Comment Strings */
while(ct<na+1)
{
free(ReadString(f));
ct++;
}
if(fscanf(f,"%d",&ct)!=1)
{
puts("Cannot read version");
exit(1);
}
if(loud)
printf("Version %d.%02d of Adventure ",
ct/100,ct%100);
if(fscanf(f,"%d",&ct)!=1)
{
puts("Cannot read adventure number");
exit(1);
}
if(loud)
printf("%d.\nLoad Complete.\n\n",ct);
}
示例10: main
int main(int argc, char **argv) {
struct nfq_handle *h;
struct nfq_q_handle *qh;
struct nfnl_handle *nh;
struct termios tio, old_tio;
fd_set readfds;
int fd;
int rv;
int ret;
char buf[4096] __attribute__ ((aligned));
int run = 1;
// opening library handle
h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()\n");
exit(1);
}
// unbinding existing nf_queue handler for AF_INET (if any)
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()\n");
exit(1);
}
// binding nfnetlink_queue as nf_queue handler for AF_INET
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()\n");
exit(1);
}
// binding this socket to queue '1'
qh = nfq_create_queue(h, 1, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
exit(1);
}
// setting copy_packet mode
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "can't set packet_copy mode\n");
exit(1);
}
/* get the terminal settings for stdin */
tcgetattr(STDIN_FILENO, &tio);
old_tio = tio;
/* disable canonical mode (buffered i/o) and local echo */
tio.c_lflag &=(~ICANON & ~ECHO);
/* set the new settings immediately */
tcsetattr(STDIN_FILENO,TCSANOW,&tio);
fd = nfq_fd(h);
while(run) {
FD_ZERO(&readfds);
FD_SET(0, &readfds);
FD_SET(fd, &readfds);
ret = select( fd + 1, &readfds, NULL, NULL, NULL);
if(ret >= 0) {
if(FD_ISSET(0, &readfds)) {
int ch = fgetc(stdin);
// q - quit
if(ch == 'q') {
puts("quit");
run = 0;
}
// i - ignore request
if((ch == 'i') && pending) {
puts("ignored");
free(pending);
pending = NULL;
}
// a - accept request
if((ch == 'a') && pending) {
puts("accepted");
allowed = realloc(allowed, (allowed_len+1) * sizeof(device_t));
allowed[allowed_len++] = *pending;
free(pending);
pending = NULL;
}
// d - deny request
if((ch == 'd') && pending) {
puts("denied");
denied = realloc(denied, (denied_len+1) * sizeof(device_t));
denied[denied_len++] = *pending;
free(pending);
pending = NULL;
}
// s - show
if(ch == 's') {
puts("");
//.........这里部分代码省略.........
示例11: do_test
static int
do_test (void)
{
pthread_t th;
int err;
printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
puts ("parent: get mutex");
err = pthread_barrier_init (&bar, NULL, 2);
if (err != 0)
{
puts ("parent: cannot init barrier");
exit (1);
}
puts ("parent: create child");
err = pthread_create (&th, NULL, tf1, NULL);
if (err != 0)
{
puts ("parent: cannot create thread");
exit (1);
}
puts ("parent: wait for child to lock mutex");
err = pthread_barrier_wait (&bar);
if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
{
puts ("parent: cannot wait for barrier");
exit (1);
}
err = pthread_mutex_lock (&mut);
if (err != 0)
{
puts ("parent: mutex_lock failed");
exit (1);
}
err = pthread_mutex_unlock (&mut);
if (err != 0)
{
puts ("parent: mutex_unlock failed");
exit (1);
}
if (pthread_cancel (th) != 0)
{
puts ("cannot cancel thread");
exit (1);
}
void *r;
err = pthread_join (th, &r);
if (err != 0)
{
puts ("parent: failed to join");
exit (1);
}
if (r != PTHREAD_CANCELED)
{
puts ("child hasn't been canceled");
exit (1);
}
puts ("parent: create 2nd child");
err = pthread_create (&th, NULL, tf2, NULL);
if (err != 0)
{
puts ("parent: cannot create thread");
exit (1);
}
puts ("parent: wait for child to lock mutex");
err = pthread_barrier_wait (&bar);
if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
{
puts ("parent: cannot wait for barrier");
exit (1);
}
err = pthread_mutex_lock (&mut);
if (err != 0)
{
puts ("parent: mutex_lock failed");
exit (1);
}
err = pthread_mutex_unlock (&mut);
if (err != 0)
{
puts ("parent: mutex_unlock failed");
//.........这里部分代码省略.........
示例12: Init
rtems_task Init(
rtems_task_argument ignored
)
{
rtems_rbtree_control rbtree1;
rtems_rbtree_node *p;
test_node node1, node2;
test_node node_array[100];
test_node search_node;
int id;
int i;
puts( "\n\n*** TEST OF RTEMS RBTREE API ***" );
puts( "Init - Initialize rbtree empty" );
rtems_rbtree_initialize_empty( &rbtree1, &test_compare_function, true );
if ( !rtems_rbtree_is_unique( &rbtree1 ) )
puts( "INIT - FAILED IS UNIQUE CHECK" );
if ( rtems_rbtree_is_unique( NULL ) )
puts( "INIT - FAILED IS UNIQUE CHECK" );
/* verify that the rbtree insert work */
puts( "INIT - Verify rtems_rbtree_insert with two nodes" );
node1.id = 1;
node1.key = 1;
node2.id = 2;
node2.key = 2;
rtems_rbtree_insert( &rbtree1, &node1.Node );
rtems_rbtree_insert( &rbtree1, &node2.Node );
p = rtems_rbtree_insert( &rbtree1, NULL );
if (p != (void *)(-1))
puts( "INIT - FAILED NULL NODE INSERT" );
_RBTree_Rotate(NULL, RBT_LEFT);
i = (node1.Node.parent == &node2.Node);
_RBTree_Rotate( &node1.Node,
!node1.Node.child[RBT_LEFT] ? RBT_RIGHT : RBT_LEFT
);
if ( (node1.Node.parent == &node2.Node) != i )
puts( "INIT - FAILED FALSE ROTATION" );
if (!rb_assert(rbtree1.root) )
puts( "INIT - FAILED TREE CHECK" );
for ( p = rtems_rbtree_get_min(&rbtree1), id = 1 ; p ;
p = rtems_rbtree_get_min(&rbtree1) , id++ ) {
test_node *t = rtems_rbtree_container_of(p,test_node,Node);
if ( id > 2 ) {
puts( "INIT - TOO MANY NODES ON RBTREE" );
rtems_test_exit(0);
}
if ( t->id != id ) {
puts( "INIT - ERROR ON RBTREE ID MISMATCH" );
rtems_test_exit(0);
}
if (!rb_assert(rbtree1.root) )
puts( "INIT - FAILED TREE CHECK" );
}
if (id < 2) {
puts("INIT - NOT ENOUGH NODES ON RBTREE");
rtems_test_exit(0);
}
puts("INIT - Verify rtems_rbtree_insert with the same value twice");
node2.key = node1.key;
rtems_rbtree_insert(&rbtree1, &node1.Node);
p = rtems_rbtree_insert(&rbtree1, &node2.Node);
if (p != &node1.Node)
puts( "INIT - FAILED DUPLICATE INSERT" );
for ( p = rtems_rbtree_get_min(&rbtree1), id = 1 ; p ;
p = rtems_rbtree_get_min(&rbtree1) , id++ ) {
test_node *t = rtems_rbtree_container_of(p,test_node,Node);
if ( id > 1 ) {
puts( "INIT - TOO MANY NODES ON RBTREE" );
rtems_test_exit(0);
}
if ( t->id != id ) {
puts( "INIT - ERROR ON RBTREE ID MISMATCH" );
rtems_test_exit(0);
}
if (!rb_assert(rbtree1.root) )
puts( "INIT - FAILED TREE CHECK" );
}
if (id < 1) {
puts("INIT - NOT ENOUGH NODES ON RBTREE");
rtems_test_exit(0);
}
node2.key = 2;
/* verify that the rbtree is empty */
puts( "INIT - Verify rtems_rbtree_is_empty" );
if(!rtems_rbtree_is_empty(&rbtree1)) {
puts( "INIT - TREE NOT EMPTY" );
rtems_test_exit(0);
//.........这里部分代码省略.........
示例13: press_key
void press_key(int fd, int key, int value)
{
if (key>0)
{
static struct input_event event={
.type = EV_KEY
};
const static struct input_event syn_event={
.type = EV_SYN,
.code = SYN_REPORT,
.value = 0
};
event.code=key;
event.value=value;
write(fd, &event, sizeof(event));
write(fd, &syn_event, sizeof(event));
}
}
int main(int argc, char **argv)
{
int rfd=0, wfd=0, res=0;
unsigned char buf[16];
typedef unsigned long long keystate;
int opt;
unsigned char do_fork=1;
while ((opt = getopt(argc, argv, "f")) != -1)
{
switch (opt)
{
case 'f':
do_fork = 0;
break;
default:
fprintf (stderr, "Invalid option: -%c\n", opt);
return 1;
}
}
if (argc > optind+1)
{
fprintf (stderr, "Usage: %s [device]\n", *argv);
exit(1);
}
if (argc == optind+1)
{
if ((rfd = open (argv[optind], O_RDONLY)) <= 0)
{
perror (argv[optind]);
return 1;
}
}
else
{
rfd=open_kb();
}
wfd=open_uinput();
if (rfd <= 0)
{
puts ("failed to find keyboard");
return 1;
}
if (do_fork)
{
switch(fork())
{
case -1:
perror ("fork");
return 1;
case 0:
break;
default:
return 0;
}
}
keystate prev=0, cur=0;
while ((res = read(rfd, buf, 16)) > 0)
{
cur=*((keystate*)(buf+2));
if (cur && (cur != prev))
{
keystate cur_new = cur & (cur ^ prev);
keystate cur_old = prev & (cur ^ prev);
if (cur_new || cur_old)
{
int i;
keystate b;
for (i=0, b=1; positions[i]!=0; ++i, b<<=1)
{
if (cur_old & b)
//.........这里部分代码省略.........
示例14: run_single_buoy
MDA_TASK_RETURN_CODE MDA_TASK_BUOY:: run_single_buoy(int buoy_index, BUOY_COLOR color) {
puts("Press q to quit");
assert (buoy_index >= 0 && buoy_index <= 1);
MDA_VISION_MODULE_BUOY buoy_vision;
MDA_TASK_RETURN_CODE ret_code = TASK_MISSING;
/// Here we store the starting attitude vector, so we can return to this attitude later
int starting_yaw = attitude_input->yaw();
printf("Starting yaw: %d\n", starting_yaw);
//set (DEPTH, 400);
TASK_STATE state = STARTING;
bool done_buoy = false;
static TIMER timer;
static TIMER master_timer;
timer.restart();
master_timer.restart();
//###### hack code for competition
set (SPEED, 0);
int hack_depth, hack_time;
read_mv_setting ("hacks.csv", "BUOY_DEPTH", hack_depth);
read_mv_setting ("hacks.csv", "BUOY_TIME", hack_time);
printf ("Buoy: going to depth %d\n", hack_depth);
fflush(stdout);
if (hack_depth > 500)
set (DEPTH, 500);
if (hack_depth > 600)
set (DEPTH, 600);
set (DEPTH, hack_depth);
set (YAW, starting_yaw);
printf ("Buoy: moving forward for %d seconds\n", hack_time);
fflush(stdout);
timer.restart();
while (timer.get_time() < hack_time) {
set (SPEED, 8);
}
set(SPEED, 0);
if (hack_depth > 600)
set (DEPTH, 600);
if (hack_depth > 500)
set (DEPTH, 500);
set (YAW, starting_yaw);
return TASK_DONE;
//###### end hack code for competition
/**
* Basic Algorithm
* - Assume for now that we just want to hit the cylindrical buoys when they're red
* - We want to search for 1 or 2 buoys. If we find both:
* - Are both non-red and/or cycling? Go for the one indicated by buoy_index
* - Is one non-red and/or cycling? Go for that one
* - Do we only see one buoy? Go there if non-red and/or cycling
*
*/
while (1) {
IplImage* frame = image_input->get_image();
if (!frame) {
ret_code = TASK_ERROR;
break;
}
MDA_VISION_RETURN_CODE vision_code = buoy_vision.filter(frame);
(void) vision_code;
// clear dwn image - RZ: do we need this?
//int down_frame_ready = image_input->ready_image(DWN_IMG);
//(void) down_frame_ready;
bool valid[2] = {false, false};
int ang_x[2], ang_y[2];
int range[2];
int color[2];
static bool color_cycling[2] = {false, false};
static int curr_index = -1;
static int prev_time = -1;
static int prev_color = -1;
static int n_valid_color_find_frames = 0;
if (!done_buoy) {
// state machine
if (state == STARTING) {
// here we just move forward until we find something, and pan if we havent found anything for a while
printf ("Starting: Moving Foward for 1 meter\n");
move (FORWARD, 1);
if (timer.get_time() > 1) {
set (SPEED, 0);
timer.restart();
buoy_vision.clear_frames();
state = STOPPED;
}
}
else if (state == STOPPED) {
//.........这里部分代码省略.........
示例15: main
int main(int argc, char **argv) {
char buf[65536], *p;
int i;
while (fgets(buf, 65536, stdin)) {
line++;
p = buf + wsspan(buf);
while (*p) {
if (*p == '(') {
depth++;
p++;
if (!curproc && depth > 1)
errx(1, "depth > 1 without register-procedure on line %d\n", line);
if (depth > 3)
errx(1, "depth > 3 on line %d\n", line);
if (depth == 1) {
hnum = 0;
hmax = 7;
} else if (depth == 2) {
hmax = 3;
if (++iomode > 1)
curioargs = &curproc->oargs;
else
curioargs = &curproc->iargs;
} else if (depth == 3) {
struct ioarg *arg = calloc(1, sizeof(*arg));
*curioargs = curioarg = arg;
curioargs = &arg->next;
hnum = 0;
hlist = arg->hlist;
}
} else if (*p == ')') {
depth--;
p++;
if (depth == 0) {
hnum = hmax = inum = onum = iomode = 0;
curproc = NULL;
hlist = NULL;
} else if (depth == 1) {
curioargs = NULL;
} else if (depth == 2) {
curioarg = NULL;
}
} else if (*p == '\"') {
char *eq = ++p, *neq;
if ((depth != 1) && (depth != 3))
errx(1, "string with depth != 1 or 3 on line %d: %s\n", line, --p);
if (!curproc)
errx(1, "string without register-procedure on line %d\n", line);
if (hnum >= hmax)
errx(1, "too many strings on line %d\n", line);
while ((neq = strchr(eq, '\"')) && (neq > p) && (neq[-1] == '\\')) {
strcpy(neq - 1, neq);
eq = neq;
}
if (!neq) {
--p;
if (!fgets(buf + strlen(buf), 65536 - strlen(buf), stdin))
errx(1, "EOF in string at line %d\n", line);
continue;
}
hlist[hnum++] = strndup(p, neq - p);
p = ++neq;
} else if (!strncmp(p, "register-procedure", 18)) {
if (depth != 1)
errx(1, "register-procedure at depth %d, line %d\n", depth, line);
p += 18;
curproc = &procs[nprocs++];
hlist = curproc->hlist;
} else {
errx(1, "unknown token on line: %s\n", line, p);
}
p += wsspan(p);
}
}
qsort(procs, nprocs, sizeof(procs[0]), proccompar);
for (i = 0; i < nprocs; i++) {
struct ioarg *arg;
printf("@defun %s ", procs[i].hlist[0]);
for (arg = procs[i].iargs; arg; arg = arg->next) {
if (arg->next)
printf("%s, ", arg->hlist[0]);
else
printf("%s", arg->hlist[0]);
}
printf("\n%[email protected]{%s}", procs[i].hlist[2], procs[i].hlist[6]);
if (procs[i].iargs) {
puts("\n\[email protected]{Inputs}\[email protected] @bullet");
for (arg = procs[i].iargs; arg; arg = arg->next) {
arg->hlist[2][0] = toupper(arg->hlist[2][0]);
printf("@item @emph{%s} (%s)--%s\n", arg->hlist[0],
arg->hlist[1], arg->hlist[2]);
}
//.........这里部分代码省略.........