9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

convM2D.c (1990B)


      1 #include	<u.h>
      2 #include	<libc.h>
      3 #include	<fcall.h>
      4 
      5 int
      6 statchecku(uchar *buf, uint nbuf, int dotu)
      7 {
      8 	uchar *ebuf;
      9 	int i, nstr;
     10 
     11 	ebuf = buf + nbuf;
     12 
     13 	if(nbuf < STATFIXLEN || nbuf != BIT16SZ + GBIT16(buf))
     14 		return -1;
     15 
     16 	buf += STATFIXLEN - 4 * BIT16SZ;
     17 
     18 	nstr = 4;
     19 	if(dotu)
     20 		nstr = 5;
     21 	for(i = 0; i < nstr; i++){
     22 		if(buf + BIT16SZ > ebuf)
     23 			return -1;
     24 		buf += BIT16SZ + GBIT16(buf);
     25 	}
     26 
     27 	if(dotu)
     28 		buf += 3*BIT32SZ;
     29 
     30 	if(buf != ebuf)
     31 		return -1;
     32 
     33 	return 0;
     34 }
     35 
     36 int
     37 statcheck(uchar *buf, uint nbuf)
     38 {
     39 	return statchecku(buf, nbuf, 0);
     40 }
     41 
     42 static char nullstring[] = "";
     43 
     44 uint
     45 convM2Du(uchar *buf, uint nbuf, Dir *d, char *strs, int dotu)
     46 {
     47 	uchar *p, *ebuf;
     48 	char *sv[5];
     49 	int i, ns, nstr;
     50 
     51 	if(nbuf < STATFIXLEN)
     52 		return 0; 
     53 
     54 	p = buf;
     55 	ebuf = buf + nbuf;
     56 
     57 	p += BIT16SZ;	/* ignore size */
     58 	d->type = GBIT16(p);
     59 	p += BIT16SZ;
     60 	d->dev = GBIT32(p);
     61 	p += BIT32SZ;
     62 	d->qid.type = GBIT8(p);
     63 	p += BIT8SZ;
     64 	d->qid.vers = GBIT32(p);
     65 	p += BIT32SZ;
     66 	d->qid.path = GBIT64(p);
     67 	p += BIT64SZ;
     68 	d->mode = GBIT32(p);
     69 	p += BIT32SZ;
     70 	d->atime = GBIT32(p);
     71 	p += BIT32SZ;
     72 	d->mtime = GBIT32(p);
     73 	p += BIT32SZ;
     74 	d->length = GBIT64(p);
     75 	p += BIT64SZ;
     76 
     77 	nstr = 4;
     78 	if(dotu)
     79 		nstr = 5;
     80 	for(i = 0; i < nstr; i++){
     81 		if(p + BIT16SZ > ebuf)
     82 			return 0;
     83 		ns = GBIT16(p);
     84 		p += BIT16SZ;
     85 		if(p + ns > ebuf)
     86 			return 0;
     87 		if(strs){
     88 			sv[i] = strs;
     89 			memmove(strs, p, ns);
     90 			strs += ns;
     91 			*strs++ = '\0';
     92 		}
     93 		p += ns;
     94 	}
     95 
     96 	if(dotu){
     97 		if(p + BIT32SZ*3 > ebuf)
     98 			return 0;
     99 		d->uidnum = GBIT32(p);
    100 		p += BIT32SZ;
    101 		d->gidnum = GBIT32(p);
    102 		p += BIT32SZ;
    103 		d->muidnum = GBIT32(p);
    104 		p += BIT32SZ;
    105 	}
    106 	
    107 	if(strs){
    108 		d->name = sv[0];
    109 		d->uid = sv[1];
    110 		d->gid = sv[2];
    111 		d->muid = sv[3];
    112 		d->ext = nullstring;
    113 		if(dotu)
    114 			d->ext = sv[4];
    115 	}else{
    116 		d->name = nullstring;
    117 		d->uid = nullstring;
    118 		d->gid = nullstring;
    119 		d->muid = nullstring;
    120 		d->ext = nullstring;
    121 	}
    122 	
    123 	return p - buf;
    124 }
    125 
    126 uint
    127 convM2D(uchar *buf, uint nbuf, Dir *d, char *strs)
    128 {
    129 	return convM2Du(buf, nbuf, d, strs, 0);
    130 }