static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie) { FLOCK(COOKIEJAR(self)->lock, F_WRLCK); SoupDate *expire; if (new_cookie) { /* session-expire-time handling */ if (vb.config.cookie_expire_time == 0) { soup_cookie_set_expires(new_cookie, NULL); } else if (vb.config.cookie_expire_time > 0 && new_cookie->expires) { expire = soup_date_new_from_now(vb.config.cookie_expire_time); if (soup_date_to_time_t(expire) < soup_date_to_time_t(new_cookie->expires)) { soup_cookie_set_expires(new_cookie, expire); } soup_date_free(expire); } /* session-cookie handling */ if (!new_cookie->expires && vb.config.cookie_timeout) { expire = soup_date_new_from_now(vb.config.cookie_timeout); soup_cookie_set_expires(new_cookie, expire); soup_date_free(expire); } } SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie); FLOCK(COOKIEJAR(self)->lock, F_UNLCK); }
static void cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec) { FLOCK(COOKIEJAR(self)->lock, F_RDLCK); G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec); FLOCK(COOKIEJAR(self)->lock, F_UNLCK); }
/** * Prepend new data to file. * * @file: File to prepend the data * @format: Format string used to process va_list */ gboolean util_file_prepend(const char *file, const char *format, ...) { gboolean res = false; va_list args; char *content; FILE *f; content = util_get_file_contents(file, NULL); if ((f = fopen(file, "w"))) { FLOCK(fileno(f), F_WRLCK); va_start(args, format); /* write new content to the file */ vfprintf(f, format, args); va_end(args); /* append previous file content */ fputs(content, f); FLOCK(fileno(f), F_UNLCK); fclose(f); res = true; } g_free(content); return res; }
int fflush(FILE *f) { if (!f) { int r = __stdout_used ? fflush(__stdout_used) : 0; for (f=*__ofl_lock(); f; f=f->next) if (f->wpos > f->wbase) r |= fflush(f); __ofl_unlock(); return r; } FLOCK(f); /* If writing, flush output */ if (f->wpos > f->wbase) { f->write(f, 0, 0); if (!f->wpos) { FUNLOCK(f); return EOF; } } /* If reading, sync position, per POSIX */ if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR); /* Clear read and write modes */ f->wpos = f->wbase = f->wend = 0; f->rpos = f->rend = 0; FUNLOCK(f); return 0; }
static void close_file(FILE *f) { if (!f) return; FLOCK(f); if (f->wpos > f->wbase) f->write(f, 0, 0); if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR); }
size_t fread(void *destv, size_t size, size_t nmemb, FILE *f) { unsigned char *dest = destv; size_t len = size*nmemb, l = len, k; /* Never touch the file if length is zero.. */ if (!l) return 0; FLOCK(f); if (f->rend - f->rpos > 0) { /* First exhaust the buffer. */ k = MIN(f->rend - f->rpos, l); memcpy(dest, f->rpos, k); f->rpos += k; dest += k; l -= k; } /* Read the remainder directly */ for (; l; l-=k, dest+=k) { k = __toread(f) ? 0 : f->read(f, dest, l); if (k+1<=1) { FUNLOCK(f); return (len-l)/size; } } FUNLOCK(f); return nmemb; }
int feof(FILE *f) { FLOCK(f); int ret = !!(f->flags & F_EOF); FUNLOCK(f); return ret; }
wint_t fputwc(wchar_t c, FILE *f) { FLOCK(f); c = __fputwc_unlocked(c, f); FUNLOCK(f); return c; }
wint_t ungetwc(wint_t c, FILE* f) { unsigned char mbc[MB_LEN_MAX]; int l = 1; locale_t *ploc = &CURRENT_LOCALE, loc = *ploc; FLOCK(f); if (f->mode <= 0) fwide(f, 1); *ploc = f->locale; if (!f->rpos) __toread(f); if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF || (!isascii(c) && (l = wctomb((void*)mbc, c)) < 0)) { FUNLOCK(f); *ploc = loc; return WEOF; } if (isascii(c)) *--f->rpos = c; else memcpy(f->rpos -= l, mbc, l); f->flags &= ~F_EOF; FUNLOCK(f); *ploc = loc; return c; }
int fgetc(FILE *f) { int c; FLOCK(f); c = getc_unlocked(f); FUNLOCK(f); return c; }
off_t __ftello(FILE *f) { off_t pos; FLOCK(f); pos = __ftello_unlocked(f); FUNLOCK(f); return pos; }
int puts(const char *s) { int r; FLOCK(stdout); r = -(fputs(s, stdout) < 0 || putc_unlocked('\n', stdout) < 0); FUNLOCK(stdout); return r; }
int __fseeko(FILE *f, off_t off, int whence) { int result; FLOCK(f); result = __fseeko_unlocked(f, off, whence); FUNLOCK(f); return result; }
size_t fwrite(const void *src, size_t size, size_t nmemb, FILE *f) { size_t k, l = size*nmemb; if (!l) return l; FLOCK(f); k = __fwritex(src, l, f); FUNLOCK(f); return k==l ? nmemb : k/size; }
/** * Append new data to file. * * @file: File to append the data * @format: Format string used to process va_list */ gboolean util_file_append(const char *file, const char *format, ...) { va_list args; FILE *f; if ((f = fopen(file, "a+"))) { FLOCK(fileno(f), F_WRLCK); va_start(args, format); vfprintf(f, format, args); va_end(args); FLOCK(fileno(f), F_UNLCK); fclose(f); return true; } return false; }
/** * Saves all entries of given provider in given file. */ static void save_entries(HSTSProvider *provider, const char *file) { GHashTableIter iter; char *host, *date; HSTSEntry *entry; FILE *f; HSTSProviderPrivate *priv = HSTS_PROVIDER_GET_PRIVATE(provider); if ((f = fopen(file, "w"))) { FLOCK(fileno(f), F_WRLCK); g_hash_table_iter_init(&iter, priv->whitelist); while (g_hash_table_iter_next (&iter, (gpointer)&host, (gpointer)&entry)) { date = soup_date_to_string(entry->expires_at, SOUP_DATE_ISO8601_FULL); fprintf(f, HSTS_FILE_FORMAT, host, date, entry->include_sub_domains ? 'y' : 'n'); g_free(date); } FLOCK(fileno(f), F_UNLCK); fclose(f); } }
int fflush(FILE* f) { int r; if (f) { FLOCK(f); r = __fflush_unlocked(f); FUNLOCK(f); return r; } r = fflush(stdout); for (f = *__ofl_lock(); f; f = f->next) { FLOCK(f); if (f->wpos > f->wbase) r |= __fflush_unlocked(f); FUNLOCK(f); } __ofl_unlock(); return r; }
int fflush(FILE *f) { int r; if (f) { FLOCK(f); r = __fflush_unlocked(f); FUNLOCK(f); return r; } r = __stdout_used ? fflush(__stdout_used) : 0; OFLLOCK(); for (f=libc.ofl_head; f; f=f->next) { FLOCK(f); if (f->wpos > f->wbase) r |= __fflush_unlocked(f); FUNLOCK(f); } OFLUNLOCK(); return r; }
int main(int argc,char *argv[]) { int status; int rem; int offset; int len; char buffer[32]; char wgetbuf[1024]; int fd; const char *port = "/dev/ttyS0"; FILE *f; time_t t; t=time(NULL); // default to ttyS0 or invoke with 'linux_nmeap <other serial device>' if (argc == 2) { port = argv[1]; } readconf(argc, argv); /* --------------------------------------- */ /* open the serial port device */ /* using default 9600 baud for most GPS */ /* --------------------------------------- */ fd = openPort(getparam("gps_port"), atoi(getparam("baudrate"))); if (fd < 0) { /* open failed */ printf("openPort %d\n",fd); return fd; } /* ---------------------------------------*/ /*STEP 2 : initialize the nmea context */ /* ---------------------------------------*/ status = nmeap_init(&nmea,(void *)&user_data); if (status != 0) { printf("nmeap_init %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 3 : add standard GPGGA parser */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 4 : add standard GPRMC parser */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 5 : process input until done */ /* -------------------------------------- */ for(;;) { /* ---------------------------------------*/ /*STEP 6 : get a buffer of input */ /* -------------------------------------- */ len = rem = read(fd,buffer,sizeof(buffer)); if (len <= 0) { perror("read"); break; } /* ----------------------------------------------*/ /*STEP 7 : process input until buffer is used up */ /* --------------------------------------------- */ offset = 0; while(rem > 0) { /* --------------------------------------- */ /*STEP 8 : pass it to the parser */ /* status indicates whether a complete msg */ /* arrived for this byte */ /* NOTE : in addition to the return status */ /* the message callout will be fired when */ /* a complete message is processed */ /* --------------------------------------- */ status = nmeap_parseBuffer(&nmea,&buffer[offset],&rem); offset += (len - rem); /* ---------------------------------------*/ /*STEP 9 : process the return code */ /* DON"T NEED THIS IF USING CALLOUTS */ /* PICK ONE OR THE OTHER */ /* -------------------------------------- */ switch(status) { case NMEAP_GPGGA: printf("-------------switch\n"); print_gga(&gga); printf("-------------\n"); break; case NMEAP_GPRMC: printf("-------------switch\n"); print_rmc(&rmc); printf("-------------\n"); break; default: break; } if (!(f=fopen(getparam("kml_file"), "w"))) { perror("fopen"); exit(1); }; FLOCK(f); fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"\ "<kml xmlns=\"http://earth.google.com/kml/2.2\">\n"\ "<Document>\n"\ "<name>location.kml</name>\n"\ " <Style id=\"location\">\n"\ " <IconStyle>\n"\ " <color>%s</color>\n"\ " <scale>%s</scale>\n"\ " <Icon>\n"\ " <href>%s</href>\n"\ " </Icon>\n"\ " </IconStyle>\n"\ " <LabelStyle>\n"\ " <color>%s</color>\n"\ " <scale>%s</scale>\n"\ " </LabelStyle>\n"\ " <ListStyle>\n"\ " </ListStyle>\n"\ " </Style>\n"\ " <Placemark>\n"\ " <name>%s</name>\n"\ " <LookAt>\n"\ " <longitude>%.6f</longitude>\n"\ " <latitude>%.6f</latitude>\n"\ " <altitude>%.0f</altitude>\n"\ " <range>%s</range>\n"\ " <tilt>%s</tilt>\n"\ " <heading>%s</heading>\n"\ " </LookAt>\n"\ " <styleUrl>#location</styleUrl>\n"\ " <description>Date: %.6lu \n"\ " Time: %.6lu \n"\ " Speed: %.1f</description>\n"\ " <Point>\n"\ " <coordinates>%.6f,%.6f,%.0f</coordinates>\n"\ " </Point>\n"\ " </Placemark>\n"\ "</Document>\n"\ "</kml>\n" , getparam("icon_color"), getparam("icon_scale"), getparam("icon_normal"), getparam("label_color"), getparam("label_scale"), getparam("object_name"), mylon, mylat, myalt, getparam("look_range"), getparam("look_tilt"), getparam("look_heading"), mydat, mytim, myspd, mylon, mylat, myalt); if (myval && mylat != 0 && mylon != 0) { int interval= atoi(getparam("interval")); if (!interval) interval = 30; if ( t+interval < time(NULL) ) { snprintf(wgetbuf, sizeof(wgetbuf), "wget 'http://trackme.org.ua/gps/flygps?ver=1&id=%s&lat=%f&lng=%f&alt=%.0f&speed=%f' -q -O /dev/null && gpio set A1 0 > /dev/null ; usleep 200000 ; gpio set A1 1 > /dev/null", getparam("object_name"), mylat, mylon, myalt, myspd); system(wgetbuf); t = time(NULL); } } fflush(f); FUNLOCK(f); fclose(f); } } /* close the serial port */ close(fd); return 0; }