plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

convD2M.c (1495B)


      1 #include	<u.h>
      2 #include	<libc.h>
      3 #include	<fcall.h>
      4 
      5 uint
      6 sizeD2M(Dir *d)
      7 {
      8 	char *sv[5];
      9 	int i, ns, nstr, fixlen;
     10 
     11 	sv[0] = d->name;
     12 	sv[1] = d->uid;
     13 	sv[2] = d->gid;
     14 	sv[3] = d->muid;
     15 
     16 	fixlen = STATFIXLEN;
     17 	nstr = 4;
     18 
     19 	ns = 0;
     20 	for(i = 0; i < nstr; i++)
     21 		if(sv[i])
     22 			ns += strlen(sv[i]);
     23 
     24 	return fixlen + ns;
     25 }
     26 
     27 uint
     28 convD2M(Dir *d, uchar *buf, uint nbuf)
     29 {
     30 	uchar *p, *ebuf;
     31 	char *sv[5];
     32 	int i, ns, nsv[5], ss, nstr, fixlen;
     33 
     34 	if(nbuf < BIT16SZ)
     35 		return 0;
     36 
     37 	p = buf;
     38 	ebuf = buf + nbuf;
     39 
     40 	sv[0] = d->name;
     41 	sv[1] = d->uid;
     42 	sv[2] = d->gid;
     43 	sv[3] = d->muid;
     44 
     45 	fixlen = STATFIXLEN;
     46 	nstr = 4;
     47 
     48 	ns = 0;
     49 	for(i = 0; i < nstr; i++){
     50 		if(sv[i])
     51 			nsv[i] = strlen(sv[i]);
     52 		else
     53 			nsv[i] = 0;
     54 		ns += nsv[i];
     55 	}
     56 
     57 	ss = fixlen + ns;
     58 
     59 	/* set size befor erroring, so user can know how much is needed */
     60 	/* note that length excludes count field itself */
     61 	PBIT16(p, ss-BIT16SZ);
     62 	p += BIT16SZ;
     63 
     64 	if(ss > nbuf)
     65 		return BIT16SZ;
     66 
     67 	PBIT16(p, d->type);
     68 	p += BIT16SZ;
     69 	PBIT32(p, d->dev);
     70 	p += BIT32SZ;
     71 	PBIT8(p, d->qid.type);
     72 	p += BIT8SZ;
     73 	PBIT32(p, d->qid.vers);
     74 	p += BIT32SZ;
     75 	PBIT64(p, d->qid.path);
     76 	p += BIT64SZ;
     77 	PBIT32(p, d->mode);
     78 	p += BIT32SZ;
     79 	PBIT32(p, d->atime);
     80 	p += BIT32SZ;
     81 	PBIT32(p, d->mtime);
     82 	p += BIT32SZ;
     83 	PBIT64(p, d->length);
     84 	p += BIT64SZ;
     85 
     86 	for(i = 0; i < nstr; i++){
     87 		ns = nsv[i];
     88 		if(p + ns + BIT16SZ > ebuf)
     89 			return 0;
     90 		PBIT16(p, ns);
     91 		p += BIT16SZ;
     92 		if(ns)
     93 			memmove(p, sv[i], ns);
     94 		p += ns;
     95 	}
     96 
     97 	if(ss != p - buf)
     98 		return 0;
     99 
    100 	return p - buf;
    101 }