File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / file.c
Revision 1.1.1.1.2.1: download - view: text, annotated - select for diffs - revision graph
Mon May 9 13:46:12 2011 UTC (13 years, 1 month ago) by misho
Branches: sync1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
under NetBSD mkstemps() function not exists!!!
added this feature for NetBSD systems

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: file.c,v 1.1.1.1.2.1 2011/05/09 13:46:12 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: #include "tool.h"
   11: #include "file.h"
   12: 
   13: 
   14: int syncOpen(const char *csFile, int mode)
   15: {
   16: 	int f;
   17: 
   18: 	if (!csFile || (*csFile == '-' && !csFile[1]))
   19: 		return mode;
   20: 
   21: 	f = open(csFile, !mode ? O_RDONLY : O_WRONLY | O_CREAT | O_TRUNC, 0644);
   22: 	if (f == -1)
   23: 		SETERR;
   24: 
   25: 	return f;
   26: }
   27: 
   28: inline int syncTemp(char * __restrict psName, int len)
   29: {
   30: 	char str[] = "/tmp/ansyncXXXXXX.dlt";
   31: 	int f;
   32: 
   33: 	f = mkstemps(str, 4);
   34: 	strlcpy(psName, str, len);
   35: 
   36: 	return f;
   37: }
   38: 
   39: void syncClose(int f)
   40: {
   41: 	if (f > 2)
   42: 		close(f);
   43: }
   44: 
   45: int syncEOF(int f)
   46: {
   47: 	off_t old;
   48: 	int ret = 0;
   49: 
   50: 	old = lseek(f, 0, SEEK_CUR);
   51: 	ret = lseek(f, 0, SEEK_END) - old;
   52: 	if (lseek(f, old, SEEK_SET) == -1) {
   53: 		SETERR;
   54: 		return -1;
   55: 	}
   56: 
   57: 	return !ret;
   58: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>