Example #1
0
MAIN()
{
  Sfio_t* f;
  Sfio_t  sf;

  if(sfopen(sfstdout,"abc","s") != sfstdout)
    terror("Bad reopening of sfstdout\n");
  if(sfopen(sfstdin,"123","s") != sfstdin)
    terror("Bad reopening of sfstdin\n");
  sfclose(sfstdin);

  if(!(f = sfopen(NIL(Sfio_t*),"123","s")) )
    terror("Opening a stream\n");
  sfclose(f);
  if(sfopen(f,"123","s") != NIL(Sfio_t*))
    terror("can't reopen a closed stream!\n");

  if(sfnew(&sf,NIL(char*),(size_t)SF_UNBOUND,0,SF_EOF|SF_READ) != &sf)
    terror("Did not open sf\n");
  sfset(&sf,SF_STATIC,1);
  if(sfclose(&sf) < 0 || !(sfset(&sf,0,0)&SF_STATIC))
    terror("Did not close sf\n");

  /* test for exclusive opens */
  unlink(tstfile(0));
  if(!(f = sfopen(NIL(Sfio_t*),tstfile(0),"wx") ) )
    terror("sfopen failed\n");
  if((f = sfopen(f,tstfile(0),"wx") ) )
    terror("sfopen should not succeed here\n");

  TSTEXIT(0);
}
Example #2
0
tmain()
{
	Sfio_t*	f;
	Sfio_t	sf;

	if(sfopen(sfstdout,"abc","s") != sfstdout)
		terror("Bad reopening of sfstdout");
	if(sfopen(sfstdin,"123","s") != sfstdin)
		terror("Bad reopening of sfstdin");
	sfclose(sfstdin);

	if(!(f = sfopen(NIL(Sfio_t*),"123","s")) )
		terror("Opening a stream");
	sfclose(f);
	if(sfopen(f,"123","s") != NIL(Sfio_t*))
		terror("can't reopen a closed stream!");

	if(sfnew(&sf,NIL(char*),(size_t)SF_UNBOUND,0,SF_EOF|SF_READ) != &sf)
		terror("Did not open sf");
	sfset(&sf,SF_STATIC,1);
	if(!sfclose(&sf) || errno != EBADF)
		terror("sfclose(sf) should fail with EBADF");
	if(!(sfset(&sf,0,0)&SF_STATIC))
		terror("Did not close sf");

	/* test for exclusive opens */
	unlink(tstfile("sf", 0));
	if(!(f = sfopen(NIL(Sfio_t*),tstfile("sf", 0),"wx") ) )
		terror("sfopen failed");
	if((f = sfopen(f,tstfile("sf", 0),"wx") ) )
		terror("sfopen should not succeed here");

	texit(0);
}
Example #3
0
int
b_comm(int argc, char *argv[], Shbltin_t* context)
{
	register int mode = C_FILE1|C_FILE2|C_COMMON;
	register char *cp;
	Sfio_t *f1, *f2;

	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
	for (;;)
	{
		switch (optget(argv, usage))
		{
 		case '1':
			mode &= ~C_FILE1;
			continue;
		case '2':
			mode &= ~C_FILE2;
			continue;
		case '3':
			mode &= ~C_COMMON;
			continue;
		case ':':
			error(2, "%s",opt_info.arg);
			break;
		case '?':
			error(ERROR_usage(2), "%s",opt_info.arg);
			break;
		}
		break;
	}
	argv += opt_info.index;
	argc -= opt_info.index;
	if(error_info.errors || argc!=2)
		error(ERROR_usage(2),"%s",optusage(NiL));
	cp = *argv++;
	if(streq(cp,"-"))
		f1 = sfstdin;
	else if(!(f1 = sfopen(NiL, cp,"r")))
		error(ERROR_system(1),"%s: cannot open",cp);
	cp = *argv;
	if(streq(cp,"-"))
		f2 = sfstdin;
	else if(!(f2 = sfopen(NiL, cp,"r")))
		error(ERROR_system(1),"%s: cannot open",cp);
	if(mode)
	{
		if(comm(f1,f2,sfstdout,mode) < 0)
			error(ERROR_system(1)," write error");
	}
	else if(f1==sfstdin || f2==sfstdin)
		sfseek(sfstdin,(Sfoff_t)0,SEEK_END);
	if(f1!=sfstdin)
		sfclose(f1);
	if(f2!=sfstdin)
		sfclose(f2);
	return error_info.errors;
}
Example #4
0
MAIN()
{
	char	*s = "1234567890\n";
	Sfoff_t	n, i;
	Sfio_t	*f;
	char	buf[1024];
	char*	addr;

	if(sfopen(sfstdout,tstfile(0),"w+") != sfstdout)
		terror("Opening output file\n");
	for(i = 0; i < 10000; ++i)
		if(sfputr(sfstdout,s,-1) < 0)
			terror("Writing data\n");

	if(!(f = sfopen((Sfio_t*)0,tstfile(1),"w")))
		terror("Opening output file \n");

	sfseek(sfstdout,(Sfoff_t)0,0);
	if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i)
		terror("Move %d lines, Expect %d\n",n,i);

	sfseek(sfstdout,(Sfoff_t)0,0);
	sfseek(f,(Sfoff_t)0,0);
	sfsetbuf(sfstdout,buf,sizeof(buf));
	if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i)
		terror("Move %d lines, Expect %d\n",n,i);

	sfopen(sfstdin,tstfile(0),"r");
	sfopen(sfstdout,tstfile(1),"w");
	sfmove(sfstdin,sfstdout,(Sfoff_t)SF_UNBOUND,-1);
	if(!sfeof(sfstdin))
		terror("Sfstdin is not eof\n");
	if(sferror(sfstdin))
		terror("Sfstdin is in error\n");
	if(sferror(sfstdout))
		terror("Sfstdout is in error\n");

	sfseek(sfstdin,(Sfoff_t)0,0);
	sfseek(sfstdout,(Sfoff_t)0,0);
	sfsetbuf(sfstdin,buf,sizeof(buf));

	addr = (char*)sbrk(0);
	sfmove(sfstdin,sfstdout,(Sfoff_t)((unsigned long)(~0L)>>1),-1);
	if((ssize_t)((char*)sbrk(0)-addr) > 256*1024)
		terror("Too much space allocated in sfmove\n");

	if(!sfeof(sfstdin))
		terror("Sfstdin is not eof2\n");
	if(sferror(sfstdin))
		terror("Sfstdin is in error2\n");
	if(sferror(sfstdout))
		terror("Sfstdout is in error2\n");

	TSTEXIT(0);
}
Example #5
0
File: tungetc.c Project: att/ast
tmain() {
    UNUSED(argc);
    UNUSED(argv);
    Sfio_t *f;
    char *str, *alpha, *s;
    char buf[128];
    int n;

    str = "0123456789";
    alpha = "abcdefghijklmnop";

    if (!(f = sfopen(NULL, alpha, "s"))) terror("Opening stream");

    for (n = 9; n >= 0; --n) {
        if (sfungetc(f, n + '0') != n + '0') terror("Ungetc");
    }

    if (!(s = sfreserve(f, SF_UNBOUND, 0)) || sfvalue(f) != 10) terror("Peek stream1");
    if (strncmp(s, str, 10) != 0) terror("Bad data1");

    if (!(s = sfreserve(f, SF_UNBOUND, 0)) || sfvalue(f) != (ssize_t)strlen(alpha)) {
        terror("Peek stream2");
    }
    if (strncmp(s, alpha, strlen(alpha)) != 0) terror("Bad data2");

    sfseek(f, (Sfoff_t)0, 0);
    for (n = 9; n >= 0; --n) {
        if (sfungetc(f, n + '0') != n + '0') terror("Ungetc2");
    }
    if (sfgetc(f) != '0') terror("Sfgetc");
    sfseek(f, (Sfoff_t)0, 0);
    if (!(s = sfreserve(f, SF_UNBOUND, 0)) || sfvalue(f) != (ssize_t)strlen(alpha)) {
        terror("Peek stream3");
    }
    if (strncmp(s, alpha, strlen(alpha)) != 0) terror("Bad data2");

    sfseek(f, (Sfoff_t)0, 0);
    if (sfungetc(f, '0') != '0') terror("Ungetc3");

    strcpy(buf, "0123456789\n");
    if (!(f = sfopen(f, buf, "s+"))) terror("Reopening  string");
    if (sfungetc(f, '\n') != '\n') terror("Can't unget new-line2");
    if (sfungetc(f, 'd') != 'd') terror("Can't unget d");
    if (sfungetc(f, 'c') != 'c') terror("Can't unget c");
    if (sfungetc(f, '\n') != '\n') terror("Can't unget new-line");
    if (sfungetc(f, 'b') != 'b') terror("Can't unget b");
    if (sfungetc(f, 'a') != 'a') terror("Can't unget a");

    if (!(s = sfgetr(f, '\n', 1)) || strcmp(s, "ab") != 0) terror("Did not get ab");
    if (!(s = sfgetr(f, '\n', 1)) || strcmp(s, "cd") != 0) terror("Did not get cd");
    if (!(s = sfgetr(f, '\n', 1)) || strcmp(s, "0123456789") != 0) terror("Did not get 0123456789");

    texit(0);
}
Example #6
0
static int
verify(char* path, char* old, char* processor, int must)
{
	char*	ns;
	char*	os;
	int	nz;
	int	oz;
	int	r;
	Sfio_t*	nf;
	Sfio_t*	of;

	r = 0;
	if (nf = sfopen(NiL, path, "r"))
	{
		if ((ns = sfgetr(nf, '\n', 1)) && (nz = sfvalue(nf) - 1) > 0)
		{
			ns += nz;
			if ((oz = strlen(processor)) <= nz && !strcmp(processor, ns - oz))
				r = 1;
			else
				error(2, "%s: %s clashes with %s", path, processor, ns - nz);
		}
		if (r && old && sfseek(nf, 0L, 0) == 0 && (of = sfopen(NiL, old, "r")))
		{
			for (;;)
			{
				ns = sfreserve(nf, 0, 0);
				nz = sfvalue(nf);
				os = sfreserve(of, 0, 0);
				oz = sfvalue(nf);
				if (nz <= 0 || oz <= 0)
					break;
				if (nz > oz)
					nz = oz;
				if (memcmp(ns, os, nz))
					break;
				nz = sfread(nf, ns, nz);
				oz = sfread(of, os, nz);
				if (!nz || !oz)
					break;
			}
			sfclose(of);
			if (!nz && !oz && !touch(old, (time_t)-1, (time_t)-1, 0))
				r = 0;
		}
		sfclose(nf);
	}
	else if (must)
		error(ERROR_SYSTEM|2, "%s: cannot read", path);
	return r;
}
Example #7
0
File: tleak.c Project: gitpan/sfio
main()
{
	if(sfopen(sfstdout,"xxx","w") != sfstdout)
		terror("Can't open xxx to write\n");
	if(sfputr(sfstdout,"012345678\n",-1) != 10)
		terror("Can't write to xxx\n");
	sfsync(sfstdout);

	if(sfopen(sfstdout,"yyy","w") != sfstdout)
		terror("Can't open yyy to write\n");
	sfclose(sfstdout);

	return 0;
}
Example #8
0
static int loadnpanxx (char *file) {
    Sfio_t *fp;
    char *s, *line, *avs[10];

    SUmessage (1, "loadnpanxx", "loading file %s", file);
    if (!(fp = sfopen (NULL, file, "r"))) {
        SUwarning (1, "loadnpanxx", "open failed for file %s", file);
        return -1;
    }
    while ((line = sfgetr (fp, '\n', 1))) {
        if (line[strlen (line) - 1] == '\r')
            line[strlen (line) - 1] = 0;
        if (tokscan (
            line, &s, "%s:%s:%s:%s\n", &avs[0], &avs[1], &avs[2], &avs[3]
        ) != 4) {
            SUwarning (1, "loadnpanxx", "bad line in file %s, %s", file, line);
            continue;
        }
        if (additem (avs[0], avs[1], avs[2], avs[3]) == -1) {
            SUwarning (1, "loadnpanxx", "additem failed");
            return -1;
        }
    }
    sfclose (fp);
    return 0;
}
Example #9
0
int timerangesload (char *file, int size) {
    Sfio_t *fp;
    char *line, *s1, *s2;
    int trm;

    trm = 0, trn = atoi (getenv ("TIMERANGESSIZE"));
    if (trn > 0) {
        if (!(trs = vmalloc (Vmheap, sizeof (timerange_t) * trn)))
            SUerror ("vg_timeranges", "cannot allocate trs");
        memset (trs, 0, sizeof (timerange_t) * trn);
        if (!(fp = sfopen (NULL, getenv ("TIMERANGESFILE"), "r")))
            SUerror ("vg_timeranges", "cannot open inv filter file");
        while ((line = sfgetr (fp, '\n', 1))) {
            if (!(s1 = strchr (line, '|'))) {
                SUwarning (0, "vg_timeranges", "bad line: %s", line);
                break;
            }
            *s1++ = 0;
            trs[trm].ft = strtoll (line, &s2, 10);
            trs[trm].lt = strtoll (s1, &s2, 10);
            trm++;
        }
        sfclose (fp);
    }
    if (trm != trn)
        trn = -1;

    return 0;
}
Example #10
0
int LogOpen(void)
{
#ifdef MAXSNOOP
  if(!pipe)
    SnSetPipeName("\\pipe\\maxsnoop");

  SnoopOpen(pipe, &hsnoop, xfer_id, NullNotify);
#endif

  if (!logfile && log_name && log_name[0])
  {
    log_written=TRUE;

    Convert_Star_To_Task(log_name);

    if ((logfile=sfopen(log_name, fopen_append, O_APPEND | O_TEXT | O_WRONLY |
                        O_CREAT | O_NOINHERIT, SH_DENYWR))==NULL)
    {
      Lprintf(copen_log, log_name);
      vbuf_flush();
    }

    return (logfile != NULL);
  }
  else return TRUE;
}
Example #11
0
int EMinit (char *file) {
    Sfio_t *fp;
    char *line, *s1;

    EMops = NULL;
    EMopn = EMopm = 0;
    if (!(imagedict = dtopen (&imagedisc, Dtset))) {
        SUwarning (0, "EMinit", "cannot create imagedict");
        return -1;
    }
    if (!(nodedict = dtopen (&nodedisc, Dtset))) {
        SUwarning (0, "EMinit", "cannot create nodedict");
        return -1;
    }
    if (!(edgedict = dtopen (&edgedisc, Dtset))) {
        SUwarning (0, "EMinit", "cannot create edgedict");
        return -1;
    }

    if (!(fp = sfopen (NULL, file, "r")))
        SUerror ("EMinit", "cannot open file %s", file);
    while ((line = sfgetr (fp, '\n', 1))) {
        if (!(s1 = strchr (line, '|'))) {
            SUwarning (0, "EMinit", "bad line: %s", line);
            continue;
        }
        *s1++ = 0;
        if (load (line, s1) == -1)
            SUerror ("EMinit", "cannot load embed file %s", line);
    }
    sfclose (fp);

    return 0;
}
Example #12
0
/*
  read_gmsh: read the gmsh II file and output a pointer to mesh structure 
  ckecked: ok
*/
mesh *read_gmsh(const char *fname)
{

    id i; // index cell 
    id j; // index for face in all faces 
    id k; // index for faces in one cell 
    gmsh_raw *gmsh; 
    mesh *m; 

    // open mesh file and allocate gmsh strucut
    fp = sfopen(fname, "in read_gmsh() for fp");     
    gmsh = smalloc(sizeof(gmsh_raw)*1, "in read_gmsh, for gmsh");
    
    /*find the opening mark, and read the text blocks*/
    while(fgets(line, MAXLINE, fp) != NULL){ // read line by line 
        if (sscanf(line, "%s", w) == 1){
            if (strcmp(w, "$PhysicalNames") == 0) read_physicalNames(fp, gmsh);  
            if (strcmp(w, "$Nodes") == 0)         read_nodes(fp, gmsh);  
            if (strcmp(w, "$Elements") == 0)      read_elems(fp, gmsh);  
        }
    }

    /* construct mesh from elements*/
    m = elems_to_mesh(gmsh);

    /* sort the faces according to its 3 types: boundary, salve, nb*/ 
    sort_faces();

    /*clean up*/
    fclose(fp);
    free(facetypes);

    return m;
}
Example #13
0
static int savenpacoords (char *dir) {
    Sfio_t *fp;
    loc_t *locp;
    int loci;
    int npanxxi;
    npa_t *npap;
    int npai, npa;

    SUmessage (1, "savenpacoords", "saving file %s/npa.coords", dir);
    if (!(fp = sfopen (NULL, sfprints ("%s/npa.coords", dir), "w"))) {
        SUwarning (1, "savenpacoords", "open failed for %s/npa.coords", dir);
        return -1;
    }
    sfsetbuf (fp, NULL, 1048576);
    for (loci = 0; loci < locn; loci++) {
        locp = locs[loci];
        for (npanxxi = 0; npanxxi < locp->npanxxn; npanxxi++) {
            npa = locp->npanxxs[npanxxi]->npanxx / 1000;
            xs[npa] += locp->x, ys[npa] += locp->y, ns[npa]++;
        }
    }
    for (npai = 0; npai < npan; npai++) {
        npap = npas[npai];
        if (ns[npap->npa] > 0)
            sfprintf (
                fp, "%03d %d %d\n", npap->npa,
                (int) (xs[npap->npa] / (double) ns[npap->npa]),
                (int) (ys[npap->npa] / (double) ns[npap->npa])
            );
    }
    sfclose (fp);
    return 0;
}
Example #14
0
extern char*	getpass(const char *prompt)
{
	struct termios told,tnew;
	Sfio_t *iop;
	static char *cp, passwd[32];
	void (*savesig)(int);
	if(!(iop = sfopen((Sfio_t*)0, "/dev/tty", "r")))
		return(0);
	if(tcgetattr(sffileno(iop),&told) < 0)
		return(0);
	interrupt = 0;
	tnew = told;
	tnew.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
	if(tcsetattr(sffileno(iop),TCSANOW,&tnew) < 0)
		return(0);
	savesig = signal(SIGINT, handler);
	sfputr(sfstderr,prompt,-1);
	if(cp = sfgetr(iop,'\n',1))
		strncpy(passwd,cp,sizeof(passwd)-1);
	tcsetattr(sffileno(iop),TCSANOW,&told);
	sfputc(sfstderr,'\n');
	sfclose(iop);
	signal(SIGINT, savesig);
	if(interrupt)
		kill(getpid(),SIGINT);
	return(cp?passwd:0);
}
Example #15
0
MAIN()
{
	Sfio_t*	f;
	char	buf[1024], *s;
	int	n;
#ifdef DEBUG
	Sfio_t*	logf = sfopen(0,"LOG","a"); sfsetbuf(logf,NIL(Void_t*),0);
#endif

	alarm(10);
	if(argc > 1)
	{	/* coprocess only */
		while((s = sfreserve(sfstdin,-1,0)) )
		{
#ifdef DEBUG
			sfwrite(logf, s, sfvalue(sfstdin));
#endif
			sfwrite(sfstdout, s, sfvalue(sfstdin));
		}
		return 0;
	}

	/* make coprocess */
	if(!(f = sfpopen(NIL(Sfio_t*), sfprints("%s -p",argv[0]), "r+")))
		terror("Opening for read/write\n");
	for(n = 0; n < 10; ++n)
	{	sfsprintf(buf,sizeof(buf),"Line %d",n);
		sfputr(f,buf,'\n');
		if(!(s = sfgetr(f,'\n',1)))
			terror("Did not read back line\n");
		if(strcmp(s,buf) != 0)
			terror("Input=%s, Expect=%s\n",s,buf);
	}

	if(sfputr(f,"123456789",'\n') != 10)
		terror("Bad write");

	if(sfread(f,buf,3) != 3)
		terror("Did not get data back\n");
	if(strncmp(s,"123",3) != 0)
		terror("Wrong data\n");

	if(sfwrite(f,"aaa",3) != 3 || sfputc(f,'\n') != '\n')
		terror("Fail on write\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten 456789\n"); 
	if(strcmp(s,"456789") != 0)
		terror("Wrong data2\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten aaa\n"); 
	if(strcmp(s,"aaa") != 0)
		terror("Wrong data3\n");

	sfclose(f);
	
	TSTEXIT(0);
}
Example #16
0
File: tappend.c Project: att/ast
tmain() {
    UNUSED(argc);
    UNUSED(argv);
    Sfio_t *f1, *f2;
    char *s;
    Sfoff_t p;
    char buf[1024];
    int r, w;

    if (!(f1 = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Can't open f1");
    if (!(f1 = sfopen(f1, tstfile("sf", 0), "a+"))) terror("Can't open f1");

    if (!(f2 = sfopen(NULL, tstfile("sf", 0), "a+"))) terror("Can't open f2");

    if (sfwrite(f1, "012345678\n", 10) != 10 || sfsync(f1) < 0) terror("Writing to f1");
    if ((p = sftell(f1)) != 10) terror("Bad sftell1 %ld", p);

    if (sfwrite(f2, "abcdefghi\n", 10) != 10 || sfsync(f2) < 0) terror("Writing to f2");
    if ((p = sftell(f2)) != 20) terror("Bad sftell2");

    if ((p = sfseek(f1, (Sfoff_t)0, 0)) != 0) terror("Bad seek");
    if (!(s = sfgetr(f1, '\n', 1))) terror("Bad getr1");
    if (strcmp(s, "012345678") != 0) terror("Bad input1");

    if ((p = sftell(f1)) != 10) terror("Bad sftell3");

    if (sfwrite(f1, "012345678\n", 10) != 10 || sfsync(f1) < 0) terror("Writing to f1");
    if ((p = sftell(f1)) != 30) terror("Bad sftell4");

    if ((p = sfseek(f2, (Sfoff_t)10, 0)) != 10) terror("Bad seek");
    if (!(s = sfgetr(f2, '\n', 1))) terror("Bad getr2");
    if (strcmp(s, "abcdefghi") != 0) terror("Bad input2");

    if (!(s = sfgetr(f2, '\n', 1))) terror("Bad getr3");
    if (strcmp(s, "012345678") != 0) terror("Bad input3");

    if (!(f1 = sfopen(f1, tstfile("sf", 0), "w"))) terror("Can't open file to write");
    for (r = 0; r < 1024; ++r) buf[r] = 'a';
    if ((w = sfwrite(f1, buf, 1024)) != 1024) terror("writing w=%d", w);
    if (!(f1 = sfopen(f1, tstfile("sf", 0), "a"))) terror("Can't open file to append");
    sfseek(f1, (Sfoff_t)0, 0);
    if ((w = sfwrite(f1, buf, 64)) != 64) terror("writing w=%d", w);
    if ((r = (int)sftell(f1)) != (1024 + 64)) terror("seek position wrong s=%d", r);

    texit(0);
}
Example #17
0
int
main(int argc, char** argv)
{
	register char*		s;
	register Sfio_t*	sp;

	unsigned long		n;
	int			summary = 0;
	unsigned long		total = 0;
	int			trailer;

	NoP(argc);
	error_info.id = "ncsl";
	for (;;)
	{
		switch (optget(argv, usage))
		{
		case 0:
			break;
		case 's':
			summary = 1;
			continue;
		case '?':
			error(ERROR_USAGE|4, "%s", opt_info.arg);
			continue;
		case ':':
			error(2, "%s", opt_info.arg);
			continue;
		}
		break;
	}
	argv += opt_info.index;
	if (error_info.errors)
		error(ERROR_USAGE|4, "%s", optusage(NiL));
	if (*argv)
	{
		trailer = *(argv + 1) != 0;
		while (s = *argv++)
		{
			if (!(sp = sfopen(NiL, s, "r")))
				error(2, "%s: cannot open for reading", s);
			else
			{
				n = ncsl(sp);
				sfclose(sp);
				if (!summary)
					sfprintf(sfstdout, "%s: %lu\n", s, n);
				total += n;
			}
		}
		if (summary || trailer)
			sfprintf(sfstdout, "%lu\n", total);
	}
	else
		sfprintf(sfstdout, "%d\n", ncsl(sfstdin));
	return error_info.errors != 0;
}
Example #18
0
void xfer_del(void)
{
	char new_log[PATHLEN];
	char linebuf[LINELEN + 1];
	char bad_real[NAMELEN], bad_temp[NAMELEN];
	long bad_fsize, bad_ftime;
	FILE *fp, *new_fp;
	boolean left;

	if (fexist(xfer_pathname))
		unlink(xfer_pathname);

	if ((fp = sfopen(xfer_log, "rt", DENY_WRITE)) != NULL) {
		mergepath(new_log, download, "BAD-XFER.$$$");
		if ((new_fp = sfopen(new_log, "wt", DENY_ALL)) != NULL) {
			left = false;
			while (fgets(linebuf, LINELEN, fp)) {
				sscanf(linebuf, "%s %s %ld %lo",
				       bad_real, bad_temp, &bad_fsize,
				       &bad_ftime);
				if (strcmp(xfer_real, bad_real)
				    || strcmp(xfer_temp, bad_temp)
				    || xfer_fsize != bad_fsize
				    || xfer_ftime != bad_ftime) {
					fputs(linebuf, new_fp);
					left = true;
				}
			}
			fclose(fp);
			fclose(new_fp);
			unlink(xfer_log);
			if (left)
				rename(new_log, xfer_log);
			else
				unlink(new_log);
		} else
			fclose(fp);
	}
}				/*xfer_del() */
Example #19
0
static void
handle(void)
{
	register int	c;
	char*		s;
	char*		b;
	mode_t		mask;

	if (ed.caught == SIGINT) {
		ed.caught = 0;
		ed.lastc = '\n';
		sfputc(ed.msg, '\n');
		error(2, "interrupt");
	}
	for (c = 0; c < elementsof(signals); c++)
		signal(signals[c], SIG_IGN);
	if (ed.dol > ed.zero) {
		ed.addr1 = ed.zero + 1;
		ed.addr2 = ed.dol;
		mask = umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
		b = "ed.hup";
		if (!(ed.iop = sfopen(NiL, b, "w")) && !ed.restricted && (s = getenv("HOME"))) {
			sfstrseek(ed.buffer.line, 0, SEEK_SET);
			sfprintf(ed.buffer.line, "%s/%s", s, b);
			if (!(b = sfstruse(ed.buffer.line)))
				error(ERROR_SYSTEM|3, "out of space");
			ed.iop = sfopen(NiL, b, "w");
		}
		umask(mask);
		if (!ed.iop)
			error(ERROR_SYSTEM|1, "%s: cannot save changes", b);
		else {
			error_info.file = b;
			putfile();
		}
	}
	ed.modified = 0;
	quit(0);
}
Example #20
0
static int
note(Css_t* css, register Connection_t* to, int log, char* s, size_t n, int force, Cssdisc_t* disc)
{
	register State_t*	state = (State_t*)disc;
	ssize_t			z;

	if ((force || to->blocked[log] < 0) && (z = data(state, to, s, n, force)) != n)
	{
		if (!force && !state->logged)
		{
			state->logged = 1;
			if (!state->logs[log].sp)
			{
				state->logs[log].name[0] = '0' + log;
				remove(state->logs[log].name);
				if (!(state->logs[log].sp = sfopen(NiL, state->logs[log].name, "r+")))
					error(ERROR_SYSTEM|3, "%s: cannot create message log", state->logs[log].name);
				message((-1, "[%d] %s: create log", __LINE__, state->logs[log].name));
			}
			message((-1, "[%d] %s: %d log", __LINE__, state->logs[log].name, to->fp->fd));
			if (sfwrite(state->logs[log].sp, s, n) != n)
				error(ERROR_SYSTEM|3, "%s: log file write error", state->logs[log].name);
			if ((state->logs[log].offset += n) >= HOG && !state->logs[!log].sp)
				state->log = !log;
		}
		if (to->blocked[log] < 0)
		{
			message((-1, "[%d] %s: block", __LINE__, state->logs[log].name));
			state->logs[log].blocked++;
		}
		to->blocked[log] = state->logs[log].offset - n + z;
		message((-1, "[%d] %s: %d offset %I*d", __LINE__, state->logs[log].name, to->fp->fd, sizeof(to->blocked[log]), to->blocked[log]));
		cssfd(css, to->fp->fd, CS_POLL_READ|CS_POLL_WRITE);
		return 0;
	}
	if (to->blocked[log] >= 0)
	{
		message((-1, "[%d] %s: %d unblock", __LINE__, state->logs[log].name, to->fp->fd));
		to->blocked[log] = -1;
		if (!--state->logs[log].blocked)
		{
			sfclose(state->logs[log].sp);
			state->logs[log].sp = 0;
			state->logs[log].offset = 0;
			remove(state->logs[log].name);
			message((-1, "[%d] %s: clear", __LINE__, state->logs[log].name));
		}
	}
	return 1;
}
Example #21
0
Sfio_t*
tokline(const char* arg, int flags, int* line)
{
	Sfio_t*		f;
	Sfio_t*		s;
	Splice_t*	d;
	char*		p;
	char*		e;

	static int	hidden;

	if (!(d = newof(0, Splice_t, 1, 0)))
		return 0;
	if (!(s = sfopen(NiL, NiL, "s")))
	{
		free(d);
		return 0;
	}
	if (!(flags & (SF_STRING|SF_READ)))
		f = (Sfio_t*)arg;
	else if (!(f = sfopen(NiL, arg, (flags & SF_STRING) ? "s" : "r")))
	{
		free(d);
		sfclose(s);
		return 0;
	}
	else if ((p = sfreserve(f, 0, 0)) && sfvalue(f) > 11 && strmatch(p, "#!!! +([-0-9]) *([!\n]) !!!\n*") && (e = strchr(p, '\n')))
	{
		flags = strtol(p + 5, &p, 10);
		error(flags, "%s:%-.*s", arg, e - p - 4, p);
	}
	d->disc.exceptf = spliceline;
	d->sp = f;
	*(d->line = line ? line : &hidden) = 0;
	sfdisc(s, (Sfdisc_t*)d);
	return s;
}
Example #22
0
extern void
error_break(void)
{
	char*	s;

	if (error_state.tty || (error_state.tty = sfopen(NiL, "/dev/tty", "r+")))
	{
		sfprintf(error_state.tty, "error breakpoint: ");
		if (s = sfgetr(error_state.tty, '\n', 1))
		{
			if (streq(s, "q") || streq(s, "quit"))
				exit(0);
			stropt(s, options, sizeof(*options), setopt, NiL);
		}
	}
}
Example #23
0
Sfio_t*
fapply(Rule_t* r, char* lhs, char* rhs, char* act, Flags_t flags)
{
	Sfio_t*	fp;

	fp = 0;
	if (!apply(r, lhs, rhs, act, flags|CO_DATAFILE))
	{
		if (fp = sfopen(NiL, state.tmpfile, "re"))
			remove(state.tmpfile);
		else
			error(2, "%s: cannot read temporary data output file %s", r->name, state.tmpfile);
	}
	state.tmpfile = 0;
	return fp;
}
Example #24
0
static int read_tree(Namval_t* np, Sfio_t *iop, int n, Namfun_t *dp)
{
	Sfio_t	*sp;
	char	*cp;
	int	c;
	if(n>=0)
		return(-1);
	while((c = sfgetc(iop)) &&  isblank(c));
	sfungetc(iop,c);
	sfprintf(sh.strbuf,"%s=%c",nv_name(np),0);
	cp = sfstruse(sh.strbuf);
	sp = sfopen((Sfio_t*)0,cp,"s");
	sfstack(iop,sp);
	c=sh_eval(iop,SH_READEVAL);
	return(c);
}
Example #25
0
static int savepolys (char *dir, char *prefix) {
    char *fname;
    Sfio_t *fp;
    item_t *itemp;
    int itempi;
    int indi;
    int pointi, pointj;

    fname = strdup (sfprints ("%s/%s_polys.ps", dir, prefix));
    SUmessage (1, "savepolys", "saving file %s", fname);
    if (!(fp = sfopen (NULL, fname, "w"))) {
        SUwarning (1, "savepolys", "open failed for %s", fname);
        return -1;
    }
    sfprintf (fp, "%%!PS\n");
    sfprintf (fp, "8 72 mul 10 72 mul scale 0 setlinewidth\n");
    sfprintf (fp, "/n { newpath } def\n");
    sfprintf (fp, "/m { moveto } def\n");
    sfprintf (fp, "/l { lineto } def\n");
    sfprintf (fp, "/p { /y exch def /x exch def x y moveto x y lineto } def\n");
    sfprintf (fp, "/s { closepath stroke } def\n");
    sfprintf (fp, "/f { closepath fill } def\n");
    /* (x, y, z) of each point */
    for (itempi = 0; itempi < itempn; itempi++) {
        itemp = itemps[itempi];
        if (itemp->pointn == 0)
            continue;
        for (pointi = 0, indi = 0; indi < itemp->indn; indi++) {
            sfprintf (
                fp, "n %f %f m\n", X (itemp->points[pointi].x),
                Y (itemp->points[pointi].y)
            );
            pointi++;
            for (pointj = 1; pointj < itemp->inds[indi]; pointi++, pointj++)
                sfprintf (
                    fp, "%f %f l\n", X (itemp->points[pointi].x),
                    Y (itemp->points[pointi].y)
                );
            sfprintf (fp, "s\n");
        }
    }
    sfprintf (fp, "showpage\n");
    sfclose (fp);
    return 0;
}
Example #26
0
/* writeFile:
 * Write graph into file f.
 * Return 0 on success
 */
int writeFile(Agraph_t * g, char *f)
{
    int rv;
    Sfio_t *fp;

    if (!f) {
	error(1, "NULL string passed to writeG");
	return 1;
    }
    fp = sfopen(0, f, "w");
    if (!fp) {
	error(1, "Could not open %s for writing in writeG", f);
	return 1;
    }
    rv = agwrite(g, fp);
    sfclose(fp);
    return rv;
}
Example #27
0
/* writeFile:
 * Write graph into file f.
 * Return 0 on success
 */
int writeFile(Agraph_t * g, char *f, Agiodisc_t* io)
{
    int rv;
    Sfio_t *fp;

    if (!f) {
	exerror("NULL string passed to writeG");
	return 1;
    }
    fp = sfopen(0, f, "w");
    if (!fp) {
	exwarn("Could not open %s for writing in writeG", f);
	return 1;
    }
    rv = sfioWrite(g, fp, io);
    sfclose(fp);
    return rv;
}
Example #28
0
int openFile(Expr_t * ex, char *fname, char *mode)
{
    int idx;

    /* find open index */
    for (idx = 3; idx < elementsof(ex->file); idx++)
	if (!ex->file[idx])
	    break;
    if (idx == elementsof(ex->file)) {
	exerror("openF: no available descriptors");
	return -1;
    }
    ex->file[idx] = sfopen(0, fname, mode);
    if (ex->file[idx])
	return idx;
    else
	return -1;
}
Example #29
0
File: vfs.c Project: abgood/vfs
VFS_INT32 vfs_file_exists( const VFS_CHAR* file  )
{
    int i,count;
    vfs_archive_obj* _archive;
    VFS_UINT64 size;

    const VFS_CHAR*filefullpath;
    VFS_CHAR filepath[VFS_MAX_FILENAME+1];

    FILE* fp;

    /************************************************************************/

    if( !file)
        return VFS_FILE_NOT_EXISTS;

    /* 先尝试在包里查找 */
    count = vfs_get_archive_count();
    for( i = 0; i<count; ++i )
    {
        _archive = vfs_get_archive_index(i);
        if( _archive && VFS_TRUE == _archive->plugin->plugin.archive.archive_locate_item(_archive->archive,file,&size) )
            return VFS_FILE_EXISTS_IN_ARCHIVE;
    }


    /* 判断是否在本地 */
    memset(filepath,0,sizeof(filepath));
    filefullpath = file;
    if( vfs_util_path_combine(filepath,g_vfs->_M_workpath,file) )
    {
        filefullpath = filepath;
    }

    fp = sfopen(filefullpath,"rb");
    if( fp )
    {
        VFS_SAFE_FCLOSE(fp);
        return VFS_FILE_EXISTS_IN_DIR;
    }

    /* 不存在文件 */
    return VFS_FILE_NOT_EXISTS;
}
Example #30
0
/* readFile:
 * Read graph from file f.
 * Return 0 on failure
 */
Agraph_t *readFile(char *f)
{
    Agraph_t *gp;
    Sfio_t *fp;

    if (!f) {
	exerror("NULL string passed to readG");
	return 0;
    }
    fp = sfopen(0, f, "r");
    if (!fp) {
	exwarn("Could not open %s for reading in readG", f);
	return 0;
    }
    gp = readG(fp);
    sfclose(fp);

    return gp;
}