Annotation of researchv10no/cmd/mk/src/dir.c, revision 1.1.1.1

1.1       root        1: #include       <sys/types.h>
                      2: #include       <sys/stat.h>
                      3: #ifndef SEQUENT
                      4: #include       <ndir.h>
                      5: #else
                      6: #include       <dir.h>
                      7: #endif
                      8: 
                      9: #ifdef SEQUENT
                     10: #define                DIRSIZE MAXNAMELEN
                     11: #else
                     12: #define                DIRSIZE 14
                     13: #endif
                     14: #ifndef        MAXNAMELEN
                     15: #define        MAXNAMELEN      255
                     16: #endif
                     17: #ifndef DIRSIZ
                     18: #define DIRSIZ(dp) \
                     19:     ((sizeof(struct direct) - MAXNAMLEN + (dp)->d_namlen + sizeof(ino_t) - 1) &\
                     20:     ~(sizeof(ino_t) - 1))
                     21: #endif
                     22: 
                     23: extern char *strncpy(), *malloc();
                     24: 
                     25: DIR *
                     26: opendir(name)
                     27: register char *name;
                     28: {
                     29:        DIR dirbuf, *dirp;
                     30:        struct stat statb;
                     31:        char buf[MAXNAMELEN+1];
                     32:        register char *s;
                     33: 
                     34:        strncpy(buf, name, MAXNAMELEN);
                     35:        buf[MAXNAMELEN-1] = 0;
                     36:        if ((dirbuf.dd_fd = open(buf, 0)) < 0)
                     37:                return((DIR *)0);
                     38:        if (fstat(dirbuf.dd_fd, &statb)!=0 || (statb.st_mode & S_IFMT)!=S_IFDIR){
                     39:                close(dirbuf.dd_fd);
                     40:                return((DIR *)0);
                     41:        }
                     42:        dirbuf.dd_loc = 0;
                     43:        dirp = (DIR *)malloc(sizeof(DIR));
                     44:        *dirp = dirbuf;
                     45:        return(dirp);
                     46: }
                     47: 
                     48: void
                     49: closedir(dirp)
                     50: DIR *dirp;
                     51: {
                     52:        close(dirp->dd_fd);
                     53:        free((char *)dirp);
                     54: }
                     55: 
                     56: /*
                     57:  * read an old stlye directory entry and present it as a new one
                     58:  */
                     59: #define        ODIRSIZ 14
                     60: 
                     61: struct olddirect {
                     62:        ino_t   d_ino;
                     63:        char    d_name[ODIRSIZ];
                     64: #ifdef CRAY
                     65:        char    d_pad[10];
                     66: #endif
                     67: };
                     68: 
                     69: /*
                     70:  * get next entry in a directory.
                     71:  */
                     72: struct direct *
                     73: readdir(dirp)
                     74:        register DIR *dirp;
                     75: {
                     76:        register struct olddirect *dp;
                     77:        static struct direct dir;
                     78: 
                     79:        for (;;) {
                     80:                if (dirp->dd_loc == 0) {
                     81:                        dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 
                     82:                            DIRBLKSIZ);
                     83:                        if (dirp->dd_size <= 0)
                     84:                                return((struct direct *)0);
                     85:                }
                     86:                if (dirp->dd_loc >= dirp->dd_size) {
                     87:                        dirp->dd_loc = 0;
                     88:                        continue;
                     89:                }
                     90:                dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
                     91:                dirp->dd_loc += sizeof(struct olddirect);
                     92:                if (dp->d_ino == 0)
                     93:                        continue;
                     94:                dir.d_ino = dp->d_ino;
                     95:                strncpy(dir.d_name, dp->d_name, ODIRSIZ);
                     96:                dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
                     97:                dir.d_namlen = strlen(dir.d_name);
                     98:                dir.d_reclen = DIRSIZ(&dir);
                     99:                return (&dir);
                    100:        }
                    101: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.