All pastes #155750 Raw Edit

Unnamed

public text v1 · immutable
#155750 ·published 2006-08-31 09:43 UTC
rendered paste body
/*
 * Simple implementation of ls,
 * exercise found in the book unix filesystems: evolution, design and
 * implementation.
 * By they way, it's not complete at all :- )
*/

#include <linux/types.h>
#include <sys/stat.h>
#include <linux/dirent.h>
#include <linux/unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define BUFSZ 2048

_syscall3(int, getdents, uint, fd, struct dirent *, dirp, uint, count); // nodig volgens manpage...
char *perms(struct stat *st);

int main(int argc, char *argv[])
{
     struct  dirent        *dir;
     struct  stat          st;
     struct  passwd        *pw;
     struct  group         *grp;
     char                  buf[BUFSZ], *bp, *ftime;
     int                   dfd, fd, nread;

     dfd = open("/etc/", O_RDONLY);
     bzero(buf, BUFSZ);
     while (nread = getdents(dfd, (struct dirent *)&buf,
                                 BUFSZ) != 0) {
bp = buf;
          dir = (struct dirent *)buf;
          do {
               if (dir->d_reclen != 0) {
                   stat(dir->d_name, &st);
                   ftime = ctime(&st.st_mtime);
                   ftime[16] = '\0'; ftime += 4;
                   pw = getpwuid(st.st_uid);
                   grp = getgrgid(st.st_gid);
                   char *permissions = perms(&st);

                   printf("%-8s %3d %-8s %-7s %9d %s %s\n",
                           permissions, st.st_nlink, pw->pw_name, grp->gr_name,
                           st.st_size, ftime, dir->d_name);
               }
               bp = bp + dir->d_reclen;
               dir = (struct dirent *)(bp);
          } while (dir->d_ino != 0);
          bzero(buf, BUFSZ);
     }
}

char *perms(struct stat *st) {
   char *permissions = malloc(sizeof(char) * 11);

   if (st->st_mode & S_IFDIR) {
      permissions[0] = 'd';
   } else if (st->st_mode & S_IFIFO) {
      permissions[0] = 'p';
   } else if (st->st_mode & S_IFCHR) {
      permissions[0] = 'c';
   } else if (st->st_mode & S_IFBLK) {
      permissions[0] = 'b';
   } else {
      permissions[0] = '-';
   }

   permissions[1] = ((st->st_mode & S_IRUSR) ? 'r' : '-'); // owner read perm
   permissions[2] = ((st->st_mode & S_IWUSR) ? 'w' : '-'); // owner write perm
   permissions[3] = ((st->st_mode & S_IXUSR) ? 'x' : '-'); // owner exec perm
   permissions[4] = ((st->st_mode & S_IRGRP) ? 'r' : '-'); // group read perm
   permissions[5] = ((st->st_mode & S_IWGRP) ? 'w' : '-'); // group write perm
   permissions[6] = ((st->st_mode & S_IXGRP) ? 'x' : '-'); // group exec perm
   permissions[7] = ((st->st_mode & S_IROTH) ? 'r' : '-'); // others read perm
   permissions[8] = ((st->st_mode & S_IWOTH) ? 'w' : '-'); // others write perm
   permissions[9] = ((st->st_mode & S_IXOTH) ? 'x' : '-'); // others exec perm
   permissions[10] = '\0';

   return permissions;
}