Blob
- Date:
- Message:
- Various fixes. B - fixed usage, DISPLAY :0 vs :0.0 9term - fixed various terminal things rc - notice traps in Read _p9dir - only run disk code for disks dirread - getdirentries on FreeBSD and Linux are different w.r.t. meaning of off. notify - set up so signals interrupt system calls bprint - use bfmt.
- Actions:
- History | Blame | Raw File
1 #include <u.h>2 #define NOPLAN9DEFINES3 #include <libc.h>5 #include <sys/types.h>6 #include <sys/stat.h>7 #include <dirent.h>8 #include <pwd.h>9 #include <grp.h>11 #if defined(__FreeBSD__)12 #include <sys/disklabel.h>13 static int diskdev[] = {14 151, /* aacd */15 116, /* ad */16 157, /* ar */17 118, /* afd */18 133, /* amrd */19 13, /* da */20 102, /* fla */21 109, /* idad */22 95, /* md */23 131, /* mlxd */24 168, /* pst */25 147, /* twed */26 43, /* vn */27 3, /* wd */28 87, /* wfd */29 };30 static int31 isdisk(struct stat *st)32 {33 int i, dev;35 if(!S_ISCHR(st->st_mode))36 return 0;37 dev = major(st->st_rdev);38 for(i=0; i<nelem(diskdev); i++)39 if(diskdev[i] == dev)40 return 1;41 return 0;42 }43 #define _HAVEDISKLABEL44 #endif46 #if !defined(__linux__) && !defined(__sun__)47 #define _HAVESTGEN48 #endif50 int51 _p9dir(struct stat *st, char *name, Dir *d, char **str, char *estr)52 {53 char *s;54 char tmp[20];55 struct group *g;56 struct passwd *p;57 int sz;59 sz = 0;60 if(d)61 memset(d, 0, sizeof *d);63 /* name */64 s = strrchr(name, '/');65 if(s)66 s++;67 if(!s || !*s)68 s = name;69 if(*s == '/')70 s++;71 if(*s == 0)72 s = "/";73 if(d){74 if(*str + strlen(s)+1 > estr)75 d->name = "oops";76 else{77 strcpy(*str, s);78 d->name = *str;79 *str += strlen(*str)+1;80 }81 }82 sz += strlen(s)+1;84 /* user */85 p = getpwuid(st->st_uid);86 if(p == nil){87 snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);88 s = tmp;89 }else90 s = p->pw_name;91 sz += strlen(s)+1;92 if(d){93 if(*str+strlen(s)+1 > estr)94 d->uid = "oops";95 else{96 strcpy(*str, s);97 d->uid = *str;98 *str += strlen(*str)+1;99 }100 }102 /* group */103 g = getgrgid(st->st_gid);104 if(g == nil){105 snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);106 s = tmp;107 }else108 s = g->gr_name;109 sz += strlen(s)+1;110 if(d){111 if(*str + strlen(s)+1 > estr)112 d->gid = "oops";113 else{114 strcpy(*str, s);115 d->gid = *str;116 *str += strlen(*str)+1;117 }118 }120 if(d){121 d->type = 'M';123 d->muid = "";124 d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;125 #ifdef _HAVESTGEN126 d->qid.vers = st->st_gen;127 #endif128 d->mode = st->st_mode&0777;129 d->atime = st->st_atime;130 d->mtime = st->st_mtime;131 d->length = st->st_size;133 if(S_ISDIR(st->st_mode)){134 d->length = 0;135 d->mode |= DMDIR;136 d->qid.type = QTDIR;137 }139 /* fetch real size for disks */140 #ifdef _HAVEDISKLABEL141 if(isdisk(st)){142 int fd, n;143 struct disklabel lab;145 if((fd = open(name, O_RDONLY)) < 0)146 goto nosize;147 if(ioctl(fd, DIOCGDINFO, &lab) < 0)148 goto nosize;149 n = minor(st->st_rdev)&7;150 if(n >= lab.d_npartitions)151 goto nosize;153 d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;155 nosize:156 if(fd >= 0)157 close(fd);158 }159 #endif160 }162 return sz;163 }