Example #1
0
main()
{
      int i, j, base;
      int search[150], array[12][12];
      for(i=1 ; i<11 ; ++i)
      {
            array[i][0] = -1;
            array[i][11] = -1;
            array[0][i] = -1;
            array[11][i] = -1;
            for(j=1 ; j<11 ; ++j)
                  array[i][j] = 0;
      }
      srand((int)time(NULL));
      i = rand() % 10 + 1;
      j = rand() % 10 + 1;
      base = addelem(0, array, search, i, j);
      array[i][j] = RT + RT;      /* Not a valid value */
      while(0 < base)
      {
            i = rand() % base;
            j = search[i];
            search[i] = search[--base];
            i = j % 100;
            j /= 100;
            openwall(array, i, j);
            base = addelem(base, array, search, i, j);
      }
      writemaze(array);
      return 0;
}
Example #2
0
File: devfs.c Project: 8l/inferno
void
fscreate(Chan *c, char *name, int mode, ulong perm)
{
	Dir *d;
	Cname *n;

	if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
		error(Efilename);
	n = addelem(newcname(FS(c)->name->s), name);
	osenter();
	FS(c)->fd = create(n->s, mode, perm);
	osleave();
	if(FS(c)->fd < 0) {
		cnameclose(n);
		fserr(FS(c));
	}
	d = dirfstat(FS(c)->fd);
	if(d == nil) {
		cnameclose(n);
		close(FS(c)->fd);
		FS(c)->fd = -1;
		fserr(FS(c));
	}
	c->qid = d->qid;
	free(d);

	cnameclose(FS(c)->name);
	FS(c)->name = n;

	c->mode = openmode(mode);
	c->offset = 0;
	FS(c)->offset = 0;
	c->flag |= COPEN;
}
Example #3
0
/*
 * Walks elems starting at f.
 * Ok if nelems is 0.
 */
static Path*
walkpath(Memblk *f, char *elems[], int nelems)
{
	int i;
	Memblk *nf;
	Path *p;

	p = newpath(f);
	if(catcherror()){
		putpath(p);
		error(nil);
	}
	isfile(f);
	for(i = 0; i < nelems; i++){
		if((f->d.mode&DMDIR) == 0)
			error("not a directory");
		rwlock(f, Rd);
		if(catcherror()){
			rwunlock(f, Rd);
			error("walk: %r");
		}
		nf = dfwalk(f, elems[i], Rd);
		rwunlock(f, Rd);
		addelem(&p, nf);
		mbput(nf);
		f = nf;
		USED(&f);	/* in case of error() */
		noerror();
	}
	noerror();
	return p;
}
Example #4
0
t_ls	*filla(int *state, t_ls *a, char *path, struct dirent *dir)
{
	t_osef				*osef;

	if (((state[A] == 0 && dir->d_name[0] != '.' &&\
			dir->d_name[0] != 0 &&
				state[F] == 0)) || (((state[A] == 1) ||
					(state[F] == 1)) && dir->d_name[0] != 0))
	{
		if (!(osef = malloc(sizeof(*osef))))
			return (NULL);
		osef->name = ft_strdup(dir->d_name);
		lstat(ft_strjoinslash(path, dir->d_name), &osef->my_stat);
		a = addelem(&a, osef);
	}
	return (a);
}
Example #5
0
static Walkqid*
fswalk(Chan *c, Chan *nc, char **name, int nname)
{
	int i;
	Path *path;
	Walkqid *wq;
	UnixFd *ufd;

	if(nc != nil)
		panic("fswalk: nc != nil");
	wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
	nc = devclone(c);
	fsclone(c, nc);
	ufd = c->aux;
	path = ufd->path;
	incref(&path->ref);

	wq->clone = nc;
	for(i=0; i<nname; i++){
		ufd = nc->aux;
		replacepath(nc, path);
		if(fswalk1(nc, name[i]) < 0){
			if(i == 0){
				pathclose(path);
				cclose(nc);
				free(wq);
				error(Enonexist);
			}
			break;
		}
		path = addelem(path, name[i], nil);
		wq->qid[i] = nc->qid;
	}
	replacepath(nc, path);
	pathclose(path);
	if(i != nname){
		cclose(nc);
		wq->clone = nil;
	}
	wq->nqid = i;
	return wq;
}
Example #6
0
static void
fscreate(Chan *c, char *name, int mode, ulong perm)
{
	char *path, *path0;
	int fd, mm;
	UnixFd *ufd;
	struct stat st;

	ufd = c->aux;
	if(Trace)
		print("fscreate %s %#x %#o\n", ufd->path->s, mode, perm);

	if(!(c->qid.type & QTDIR))
		error(Enotdir);

	if(mode & ~(OTRUNC|ORCLOSE|3))
		error(Ebadarg);

	if(perm & ~(DMDIR|0777))
		error(Ebadarg);

	path0 = fspath(c, nil);
	path = fspath(c, name);
	if(waserror()){
		free(path);
		free(path0);
		nexterror();
	}
	
	if(stat(path0, &st) < 0)
		oserror();

	if(perm & DMDIR){
		if(mode != OREAD)
			error(Eperm);
		/* have to do the minimum 0400 so we can open it */
		if(mkdir(path, (0400 | perm) & 0777) < 0)
			oserror();
		if((fd = open(path, 0)) < 0)
			oserror();
		fchown(fd, -1, st.st_gid);
		if(fstat(fd, &st) < 0){
			close(fd);
			oserror();
		}
		if((ufd->dir = opendir(path)) == nil) {
			/* arguably we should set the mode here too 
 			 * but it's hard to see that this case 
 			 * will ever happen 
 			 */
			close(fd);
			oserror();
		}
		// Be like Plan 9 file servers: inherit mode bits 
		// and group from parent.
		fchmod(fd, perm & st.st_mode & 0777);
		close(fd);
		ufd->diroffset = 0;
		ufd->nextde = nil;
	}else{
		mm = mode & 3;
		if(mode & OTRUNC)
			mm |= O_TRUNC;
		if((fd = open(path, mm|O_CREAT|O_EXCL, 0666)) < 0)
			oserror();
		// Be like Plan 9 file servers: inherit mode bits 
		// and group from parent.
		fchmod(fd, perm & st.st_mode & 0777);
		fchown(fd, -1, st.st_gid);
		if(fstat(fd, &st) < 0){
			close(fd);
			oserror();
		}
		ufd->fd = fd;
	}
	free(path);
	free(path0);
	poperror();
	
	ufd->path = addelem(ufd->path, name, nil);
	c->qid = fsqid(&st);
	c->offset = 0;
	c->flag |= COPEN;
	c->mode = openmode(mode);
}
Example #7
0
/*
 * This is unrealistic in that it keeps the file locked
 * during the entire put. This means that we can only give
 * fslru() a chance before each put, and not before each
 * write, because everything is going to be in use and dirty if
 * we run out of memory.
 */
static void
fsput(int, char *argv[])
{
	int fd;
	char *fn;
	Memblk *m, *f;
	Dir *d;
	char buf[4096];
	uvlong off;
	long nw, nr;
	Path *p;
	char *nm;

	fd = open(argv[1], OREAD);
	if(fd < 0)
		error("open: %r\n");
	d = dirfstat(fd);
	if(d == nil){
		error("dirfstat: %r\n");
	}
	nm = fsname(argv[2]);
	if(catcherror()){
		free(nm);
		close(fd);
		free(d);
		error(nil);
	}
	p = walkto(nm, &fn);
	if(catcherror()){
		putpath(p);
		error(nil);
	}
	meltedpath(&p, p->nf, 1);
	m = p->f[p->nf-1];
	if(catcherror()){
		rwunlock(m, Wr);
		error(nil);
	}
	f = dfcreate(m, fn, usrid(d->uid), d->mode&(DMDIR|0777));
	noerror();
	addelem(&p, f);
	decref(f);	/* kept now in p */
	rwlock(f, Wr);
	rwunlock(m, Wr);
	if(catcherror()){
		rwunlock(f, Wr);
		error(nil);
	}
	if((d->mode&DMDIR) == 0){
		off = 0;
		for(;;){
			if(fsmemfree() < Mminfree)
				fslru();
			nr = read(fd, buf, sizeof buf);
			if(nr <= 0)
				break;
			nw = dfpwrite(f, buf, nr, &off);
			dprint("wrote %ld of %ld bytes\n", nw, nr);
			off += nr;
		}
	}
	noerror();
	noerror();
	noerror();
	if(verb)
		print("created %H\nat %H\n", f, m);
	rwunlock(f, Wr);
	free(nm);
	putpath(p);
	close(fd);
	free(d);
}
Example #8
0
File: devfs.c Project: 8l/inferno
Walkqid*
fswalk(Chan *c, Chan *nc, char **name, int nname)
{
	int j, alloc;
	Walkqid *wq;
	Dir *dir;
	char *n;
	Cname *current, *next;
	Qid rootqid;

	if(nname > 0)
		isdir(c);	/* do we need this? */

	alloc = 0;
	current = nil;
	wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
	if(waserror()){
		if(alloc && wq->clone!=nil)
			cclose(wq->clone);
		cnameclose(current);
		free(wq);
		return nil;
	}
	if(nc == nil){
		nc = devclone(c);
		nc->type = 0;
		alloc = 1;
	}
	wq->clone = nc;

	rootqid = FS(c)->rootqid;
	current = FS(c)->name;
	if(current != nil)
		incref(&current->r);
	for(j=0; j<nname; j++){
		if(!(nc->qid.type&QTDIR)){
			if(j==0)
				error(Enotdir);
			break;
		}
		n = name[j];
		if(strcmp(n, ".") != 0 && !(isdotdot(n) && nc->qid.path == rootqid.path)){	/* TO DO: underlying qids aliased */
			//print("** ufs walk '%s' -> %s\n", current->s, n);
			next = current;
			incref(&next->r);
			next = addelem(current, n);
			dir = dirstat(next->s);
			if(dir == nil){
				cnameclose(next);
				if(j == 0)
					error(Enonexist);
				strcpy(up->env->errstr, Enonexist);
				break;
			}
			nc->qid = dir->qid;
			free(dir);
			cnameclose(current);
			current = next;
		}
		wq->qid[wq->nqid++] = nc->qid;
	}
//	print("** ufs walk '%s'\n", current->s);

	poperror();
	if(wq->nqid < nname){
		cnameclose(current);
		if(alloc)
			cclose(wq->clone);
		wq->clone = nil;
	}else if(wq->clone){
		/* now attach to our device */
		nc->aux = smalloc(sizeof(Fsinfo));
		nc->type = c->type;
		FS(nc)->rootqid = FS(c)->rootqid;
		FS(nc)->name = current;
		FS(nc)->fd = -1;
		FS(nc)->root = FS(c)->root;
	}else
		panic("fswalk: can't happen");
	return wq;
}
Example #9
0
static PyObject*
node2tuple(node *n,                     /* node to convert               */
           SeqMaker mkseq,              /* create sequence               */
           SeqInserter addelem,         /* func. to add elem. in seq.    */
           int lineno,                  /* include line numbers?         */
           int col_offset)              /* include column offsets?       */
{
    PyObject *result = NULL, *w;

    if (n == NULL) {
        Py_RETURN_NONE;
    }

    if (ISNONTERMINAL(TYPE(n))) {
        int i;

        result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
        if (result == NULL)
            goto error;

        w = PyLong_FromLong(TYPE(n));
        if (w == NULL)
            goto error;
        (void) addelem(result, 0, w);

        for (i = 0; i < NCH(n); i++) {
            w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
            if (w == NULL)
                goto error;
            (void) addelem(result, i+1, w);
        }

        if (TYPE(n) == encoding_decl) {
            w = PyUnicode_FromString(STR(n));
            if (w == NULL)
                goto error;
            (void) addelem(result, i+1, w);
        }
    }
    else if (ISTERMINAL(TYPE(n))) {
        result = mkseq(2 + lineno + col_offset);
        if (result == NULL)
            goto error;

        w = PyLong_FromLong(TYPE(n));
        if (w == NULL)
            goto error;
        (void) addelem(result, 0, w);

        w = PyUnicode_FromString(STR(n));
        if (w == NULL)
            goto error;
        (void) addelem(result, 1, w);

        if (lineno) {
            w = PyLong_FromLong(n->n_lineno);
            if (w == NULL)
                goto error;
            (void) addelem(result, 2, w);
        }

        if (col_offset) {
            w = PyLong_FromLong(n->n_col_offset);
            if (w == NULL)
                goto error;
            (void) addelem(result, 2 + lineno, w);
        }
    }
    else {
        PyErr_SetString(PyExc_SystemError,
                        "unrecognized parse tree node type");
        return ((PyObject*) NULL);
    }
    return result;

error:
    Py_XDECREF(result);
    return NULL;
}