static int read_csv_row(FILE * f, char ** out) { csv_row_parser * p = csv_row_parser_alloc(); flockfile(f); while (!csv_row_parser_done(p) && !csv_row_parser_error(p)) { int nextChar = getc_unlocked(f); if (nextChar == EOF) { funlockfile(f); if (ferror(f)) { csv_row_parser_free_all(p); return -1; } flockfile(f); } csv_row_parser_next(p, nextChar); } funlockfile(f); if (csv_row_parser_error(p)) { csv_row_parser_free_all(p); return -1; } else if (p->fieldCount == 1 && !p->joinedFields[0]) { csv_row_parser_free_all(p); return 0; } (*out) = p->joinedFields; int count = p->fieldCount; csv_row_parser_free_struct(p); return count; }
rtems_task Init( rtems_task_argument argument ) { FILE *fd; int sc; TEST_BEGIN(); puts( "Open /testfile" ); fd = fopen( "/testfile", "w+" ); rtems_test_assert( fd ); puts( "flockfile /testfile" ); flockfile( fd ); puts( "ftrylockfile /testfile" ); sc = ftrylockfile( fd ); rtems_test_assert( sc == -1 ); rtems_test_assert( errno == ENOTSUP ); puts( "flockfile /testfile" ); flockfile( fd ); puts( "funlockfile /testfile" ); funlockfile( fd ); TEST_END(); rtems_test_exit(0); }
int fseeko(FILE *stream, off_t offset, int whence) { off_t floc; struct stat filestat; switch (whence) { case SEEK_CUR: #ifdef __bsdi__ flockfile(stream); #endif if (fgetpos(stream, &floc) != 0) goto failure; floc += offset; if (fsetpos(stream, &floc) != 0) goto failure; #ifdef __bsdi__ funlockfile(stream); #endif return 0; break; case SEEK_SET: if (fsetpos(stream, &offset) != 0) return -1; return 0; break; case SEEK_END: #ifdef __bsdi__ flockfile(stream); #endif fflush(stream); /* force writes to fd for stat() */ if (fstat(fileno(stream), &filestat) != 0) goto failure; floc = filestat.st_size; floc += offset; if (fsetpos(stream, &floc) != 0) goto failure; #ifdef __bsdi__ funlockfile(stream); #endif return 0; break; default: errno = EINVAL; return -1; } failure: #ifdef __bsdi__ funlockfile(stream); #endif return -1; }
int LogStderrV(int level, const char *msg, va_list args) { if (logFile == NULL || msg == NULL || !(logMask & LOG_MASK(level))) return -1; (void) LogErrorV(msg, args); #if defined(HAVE_FLOCKFILE) flockfile(stderr); #endif (void) fprintf(stderr, "%s: ", programName); (void) vfprintf(stderr, msg, args); if (errno != 0) (void) fprintf(stderr, ": (%d) %s", errno, strerror(errno)); (void) fputs("\r\n", stderr); (void) fflush(stderr); #if defined(HAVE_FLOCKFILE) funlockfile(stderr); #endif return 0; }
/** * exports an IDMEF message * to do this, the internally stored IDMEF template is processed with all keys of the internal * parameterMap and then the file is saved in the destination directory * the external process which sends the IDMEF messages uses the URL given in the first line of * the saved message */ void IDMEFExporter::exportMessage(const string idmefmsg) { DPRINTF("sending IDMEF message"); string filename = getFilename(); // save message to destination directory FILE* f = fopen(filename.c_str(), "w+"); if (f == NULL) goto error; flockfile(f); // first line is URL where processing script should send event to if (fwrite(sendURL.c_str(), 1, sendURL.size(), f) != sendURL.size()) goto error; if (fwrite("\n", 1, 1, f) != 1) goto error; if (fwrite(idmefmsg.c_str(), 1, idmefmsg.size(), f) != idmefmsg.size()) goto error; if (fwrite("\n", 1, 1, f) != 1) goto error; funlockfile(f); if (fclose(f) != 0) goto error; mutex.unlock(); return; error: THROWEXCEPTION("failed to write to file %s, error: %s", filename.c_str(), strerror(errno)); }
//------------------------------------------------------------------------------ int CPosumStream::_close( int fh ) { int r = 0; //_CHECK_FH_CLEAR_OSSERR_RETURN( fh, EBADF, -1 ); //_VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1); //_VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1); flockfile(); __try { if ( m_chFlags & FOPEN ) { r = _close_nolock( fh ); } else { errno = EBADF; r = -1; //assert(("Invalid file descriptor. File possibly closed by a different thread",0)); } } __finally { funlockfile(); } return r; }
/* Log a user supplied string. Escapes non-printable before printing. */ void _assuan_log_sanitized_string(const char *string) { const unsigned char *s = (const unsigned char *) string; FILE *fp = assuan_get_assuan_log_stream(); if(! *s) return; #ifdef HAVE_FLOCKFILE flockfile(fp); #endif for(; *s; s++) { int c = 0; switch(*s) { case '\r': c = 'r'; break; case '\n': c = 'n'; break; case '\f': c = 'f'; break; case '\v': c = 'v'; break; case '\b': c = 'b'; break; default: if((isascii(*s) && isprint(*s)) || (*s >= 0x80)) putc_unlocked(*s, fp); else { putc_unlocked('\\', fp); fprintf(fp, "x%02x", *s); } } if(c) { putc_unlocked('\\', fp); putc_unlocked(c, fp); } } #ifdef HAVE_FUNLOCKFILE funlockfile(fp); #endif }
off64_t BufferedFile::size() { #ifdef _MSC_VER return _filelengthi64(fileno(m_fp)); // Interestingly, implemented as fseek/ftell in the windows crt #else // going to calculate size 2 ways - first, via seek off64_t length = -1; off64_t here; flockfile(m_fp); try { if ((here = ftell(m_fp)) > -1) { if (fseek(m_fp, 0, SEEK_END) > -1) { length = ftell(m_fp); fseek(m_fp, here, SEEK_SET); } } funlockfile(m_fp); } catch(...) { funlockfile(m_fp); } return length; #endif }
static void smb_svc_log(int pri, const char *fmt, va_list ap) { static time_t prev_ts; char fbuf[SMBD_LOG_MSGSIZE]; char cbuf[CBUFSIZ]; char *newfmt; time_t ts; int save_errno = errno; pri &= LOG_PRIMASK; if (smbd.s_debug == 0 && pri == LOG_DEBUG) return; ts = time(NULL); if (prev_ts != ts) { prev_ts = ts; /* NB: cbuf has \n */ (void) fprintf(stdout, "@ %s", ctime_r(&ts, cbuf, sizeof (cbuf))); } newfmt = smb_syslog_fmt_m(fbuf, sizeof (fbuf), fmt, save_errno); flockfile(stdout); (void) fprintf(stdout, "smbd.%s: ", pri_name[pri]); /* LINTED E_SEC_PRINTF_VAR_FMT */ (void) vfprintf(stdout, newfmt, ap); (void) fprintf(stdout, "\n"); funlockfile(stdout); (void) fflush(stdout); }
int __LLBC_FilePrint(bool newline, FILE *file, const char *fmt, ...) { const int fileNo = LLBC_File::GetFileNo(file); if (UNLIKELY(fileNo == -1)) { return LLBC_FAILED; } char *buf; int len; LLBC_FormatArg(fmt, buf, len); #if LLBC_TARGET_PLATFORM_NON_WIN32 flockfile(file); fprintf(file, (newline?"%s\n":"%s"), buf); fflush(file); funlockfile(file); #else LLBC_INTERNAL_NS __g_consoleLock[(fileNo == 1 || fileNo == 2 ? 0 : 1)].Lock(); fprintf(file, newline?"%s\n":"%s", buf); fflush(file); LLBC_INTERNAL_NS __g_consoleLock[(fileNo == 1 || fileNo == 2 ? 0 : 1)].Unlock(); #endif LLBC_Free(buf); return LLBC_OK; }
int LLBC_FlushFile(FILE *file) { if (UNLIKELY(!file)) { LLBC_SetLastError(LLBC_ERROR_INVALID); return LLBC_FAILED; } #if LLBC_TARGET_PLATFORM_NON_WIN32 flockfile(file); if (UNLIKELY(::fflush(file) != 0)) { funlockfile(file); LLBC_SetLastError(LLBC_ERROR_CLIB); return LLBC_FAILED; } funlockfile(file); return LLBC_OK; #else // Win32 const int fileNo = LLBC_File::GetFileNo(file); LLBC_INTERNAL_NS __g_consoleLock[(fileNo == 1 || fileNo == 2 ? 0 : 1)].Lock(); if (UNLIKELY(::fflush(file) != 0)) { LLBC_INTERNAL_NS __g_consoleLock[(fileNo == 1 || fileNo == 2 ? 0 : 1)].Unlock(); LLBC_SetLastError(LLBC_ERROR_CLIB); return LLBC_FAILED; } LLBC_INTERNAL_NS __g_consoleLock[(fileNo == 1 || fileNo == 2 ? 0 : 1)].Unlock(); return LLBC_OK; #endif }
int writeSelectedDataFile(struct crealNodeStruct *node, uint nodeIndex, uint propIndex, long double *currentTime) { /* In this function we append to a file handle and we take care of thing in terms of * writing out the stuff for the graph information */ FILE *fp = NULL; //the main file pointer char path[1024], tempString[1024]; // Good.... maybe the size should be increased eventually memset(path , 0 , sizeof(char) * 1024); // this is just setting the memory to zero sprintf(path,"%d_%d.dat\0", nodeIndex, propIndex); fp = fopen(path, "a"); //MAKE SURE TO OPEN THIS THING UP FOR APPENDING //~ if (fp == NULL) { return -1; //bad } flockfile(fp); //lock the file handle.. like a semaphore fprintf(fp,"%d %f\n",(uint) *currentTime, *node->properties[propIndex] ); //use fprintf to write directly to the file so we can write everything out to a file funlockfile(fp); fclose(fp); //close the file pointer //~ return 1; //everything went good }
void rewind(FILE* fp) { flockfile(fp); fseeko_unlocked(fp, 0, SEEK_SET); clearerr_unlocked(fp); funlockfile(fp); }
void vwarn (const char *format, __gnuc_va_list ap) { int error = errno; flockfile (stderr); if (_IO_fwide (stderr, 0) > 0) { __fwprintf (stderr, L"%s: ", __progname); if (format) { convert_and_print (format, ap); fputws_unlocked (L": ", stderr); } __set_errno (error); __fwprintf (stderr, L"%m\n"); } else { fprintf (stderr, "%s: ", __progname); if (format) { vfprintf (stderr, format, ap); fputs_unlocked (": ", stderr); } __set_errno (error); fprintf (stderr, "%m\n"); } funlockfile (stderr); }
static void PrintMsg (void *d, int type, const vlc_log_t *p_item, const char *format, va_list ap) { FILE *stream = stderr; int verbose = (intptr_t)d; if (verbose < 0 || verbose < (type - VLC_MSG_ERR)) return; int canc = vlc_savecancel (); flockfile (stream); utf8_fprintf (stream, "[%0*"PRIxPTR"] ", ptr_width, p_item->i_object_id); if (p_item->psz_header != NULL) utf8_fprintf (stream, "[%s] ", p_item->psz_header); utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module, p_item->psz_object_type, msg_type[type]); utf8_vfprintf (stream, format, ap); putc_unlocked ('\n', stream); #if defined (_WIN32) || defined (__OS2__) fflush (stream); #endif funlockfile (stream); vlc_restorecancel (canc); }
static void _Data_writeToTextFile (Daata me, MelderFile file, bool verbose) { try { if (! Data_canWriteText (me)) Melder_throw (U"Objects of class ", my classInfo -> className, U" cannot be written to a text file."); autoMelderFile mfile = Data_createTextFile (me, file, verbose); #ifndef _WIN32 flockfile (file -> filePointer); // BUG #endif MelderFile_write (file, U"File type = \"ooTextFile\"\nObject class = \"", my classInfo -> className); if (my classInfo -> version > 0) MelderFile_write (file, U" ", my classInfo -> version); MelderFile_write (file, U"\"\n"); Data_writeText (me, file); MelderFile_writeCharacter (file, U'\n'); #ifndef _WIN32 if (file -> filePointer) funlockfile (file -> filePointer); #endif mfile.close (); } catch (MelderError) { #ifndef _WIN32 if (file -> filePointer) funlockfile (file -> filePointer); // the file pointer is null before Data_createTextFile() and after mfile.close() #endif throw; } }
static void file_port_batch_read_start(file_port_t* port){ if (!port->open){ dfsch_error("Port is already closed", (dfsch_object_t*)port); } flockfile(port->file); }
/* * logthing - output a log entry * @loglevel: The level of the log. * @format: A format string, followed by any parameters required. * * This function outputs a log entry. A leading time/date stamp and a * trailing newline are automatically added. The loglevel is compared to * the current log threshold and if equal or above the log entry is * output. The format parameter is of the same nature as that used in * printf. */ int logthing(loglevels loglevel, const char *format, ...) { FILE *logfile = NULL; va_list ap; if (loglevel >= logthres) { if (logfilename != NULL) { logfile = fopen(logfilename, "a"); if (logfile != NULL) { flockfile(logfile); } else { logfile = stderr; flog(logfile, "Couldn't open logfile: %s", logfilename); } } else { logfile = stderr; } va_start(ap, format); vflog(logfile, format, ap); va_end(ap); if (logfile != stderr) { funlockfile(logfile); fclose(logfile); logfile = NULL; } } return 0; }
char *fgetln(FILE *fp, size_t *lenp) { char *buf = NULL; int c, used = 0, len = 0; flockfile(fp); while ((c = getc_unlocked(fp)) != EOF) { if (!buf || len >= used) { size_t nsize; char *newbuf; nsize = used + BUFSIZ; if (!(newbuf = (char*)realloc(buf, nsize))) { funlockfile(fp); if (buf) free(buf); return NULL; } buf = newbuf; used = nsize; } buf[len++] = c; if (c == '\n') { break; } } funlockfile(fp); *lenp = len; return buf; }
/** * @brief Write message to file * @copydetails ll_pr_cb_t */ static int file_pr(void *priv, const char *name, enum ll_level level, const char *format, va_list args) { assert(priv); assert(name); assert(format); FILE *f = priv; int rc = -1; flockfile(f); do { if (fprintf(f, "%" PRIi64 ";%s;%s;", (int64_t)time(NULL), name, ll_level_str(level)) < 0) { break; } if (vfprintf(f, format, args) < 0) { break; } if (fputc('\n', f) < 0) { break; } rc = 0; } while (0); funlockfile(f); return (rc); }
output (struct handle *h, const char *act, uint64_t id, const char *fmt, ...) { va_list args; struct timeval tv; struct tm tm; char timestamp[27] = "Time unknown"; /* Logging is best effort, so ignore failure to get timestamp */ if (!gettimeofday (&tv, NULL)) { size_t s; gmtime_r (&tv.tv_sec, &tm); s = strftime (timestamp, sizeof timestamp - sizeof ".000000" + 1, "%F %T", &tm); assert (s); snprintf (timestamp + s, sizeof timestamp - s, ".%06ld", 0L + tv.tv_usec); } flockfile (logfile); fprintf (logfile, "%s connection=%" PRIu64 " %s ", timestamp, h->connection, act); if (id) fprintf (logfile, "id=%" PRIu64 " ", id); va_start (args, fmt); vfprintf (logfile, fmt, args); va_end (args); fputc ('\n', logfile); fflush (logfile); funlockfile (logfile); }
static void PrintColorMsg (void *d, int type, const msg_item_t *p_item, const char *format, va_list ap) { const int *pverbose = d; FILE *stream = stderr; if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)) return; int canc = vlc_savecancel (); flockfile (stream); fprintf (stream, "["GREEN"%p"GRAY"] ", (void *)p_item->i_object_id); if (p_item->psz_header != NULL) utf8_fprintf (stream, "[%s] ", p_item->psz_header); utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module, p_item->psz_object_type, msg_type[type], msg_color[type]); utf8_vfprintf (stream, format, ap); fputs (GRAY"\n", stream); #if defined (WIN32) || defined (__OS2__) fflush (stream); #endif funlockfile (stream); vlc_restorecancel (canc); }
// xmalloc, xrealloc char *readLine(FILE *stream) { register unsigned i = 0; unsigned currentSize = 32; register char c; char *str = (char*) xmalloc(32); #ifdef _POSIX_THREAD_SAFE_FUNCTIONS // locking and getc_unlocked functions flockfile(stream); while((c = getc_unlocked(stream)) != EOF && c != '\n') { if(i == currentSize) str = (char*) xrealloc(str, currentSize <<= 1); str[i++] = c; } funlockfile(stream); #else while((c = getc(stream)) != EOF && c != '\n') { if(i == currentSize) str = (char*) xrealloc(str, currentSize <<= 1); str[i++] = c; } #endif if(i == currentSize) str = (char*) xrealloc(str, currentSize += 1); else if(c == EOF && i == 0) { free(str); return (char*) NULL; } else str = realloc(str, i+1); // freeing space -> no need to check for null str[i] = '\0'; return str; }
int ungetc(int c, FILE* fp) { flockfile(fp); int ret = ungetc_unlocked(c, fp); funlockfile(fp); return ret; }
void openfile(FILE* &file,const char* filename,const char* openmode) { file = fopen(filename, openmode); except(NULL==file, "Cannot open file!\n"); flockfile(file); }
int putc (int ch, FILE * stream) { flockfile (stream); int x = putc_unlocked (ch, stream); funlockfile (stream); return x; }
getchar() { int ret; flockfile(stdin); ret = getc(stdin); funlockfile(stdin); return(ret); }
/* Check if an ecryptfs private device or mount point is mounted. * Return 1 if a filesystem in mtab matches dev && mnt && sig. * Return 0 otherwise. */ int ecryptfs_private_is_mounted(char *dev, char *mnt, char *sig, int mounting) { FILE *fh = NULL; struct mntent *m = NULL; char *opt = NULL; int mounted; #if 0 //sw.yoo_20120111 - do not use setmntent, hasmntopt, endmntent function if (asprintf(&opt, "ecryptfs_sig=%s", sig) < 0) { perror("asprintf"); return 0; } fh = setmntent("/proc/mounts", "r"); if (fh == NULL) { perror("setmntent"); return 0; } mounted = 0; flockfile(fh); while ((m = getmntent(fh)) != NULL) { if (strcmp(m->mnt_type, "ecryptfs") != 0) /* Skip if this entry is not an ecryptfs mount */ continue; if (mounting == 1) { /* If mounting, return "already mounted" if EITHER the * dev or the mnt dir shows up in mtab/mounts; * regardless of the signature of such mounts; */ if (dev != NULL && strcmp(m->mnt_fsname, dev) == 0) { mounted = 1; break; } if (mnt != NULL && strcmp(m->mnt_dir, mnt) == 0) { mounted = 1; break; } } else { /* Otherwise, we're unmounting, and we need to be * very conservative in finding a perfect match * to unmount. The device, mountpoint, and signature * must *all* match perfectly. */ if ( strcmp(m->mnt_fsname, dev) == 0 && strcmp(m->mnt_dir, mnt) == 0 && hasmntopt(m, opt) != NULL ) { mounted = 1; break; } } } endmntent(fh); if (opt != NULL) free(opt); return mounted; #else return 0; #endif }
gboolean read_line (FILE *stream, GString *str) { int n_read = 0; #ifdef HAVE_FLOCKFILE flockfile (stream); #endif g_string_truncate (str, 0); while (1) { int c; #ifdef HAVE_FLOCKFILE c = getc_unlocked (stream); #else c = getc (stream); #endif if (c == EOF) goto done; else n_read++; switch (c) { case '\r': case '\n': { #ifdef HAVE_FLOCKFILE int next_c = getc_unlocked (stream); #else int next_c = getc (stream); #endif if (!(next_c == EOF || (c == '\r' && next_c == '\n') || (c == '\n' && next_c == '\r'))) ungetc (next_c, stream); goto done; } default: g_string_append_c (str, c); } } done: #ifdef HAVE_FLOCKFILE funlockfile (stream); #endif return n_read > 0; }
void* doit( void* x ) { long id = ((long) x); flockfile(stdout); printf( "I am thread %ld\n", id ); funlockfile(stdout); return NULL; }