implementing "ls -l" ....

Started by
2 comments, last by pdstatha 22 years, 1 month ago
I am trying to implement the unix command "ls -l", to do this I have the following code.
    
/*Simple use of the stat method*/
/*except this it lists the permissions*/
/*of the files*/

#include <stdio.h>

#include <sys/types.h>

#include <dirent.h>

#include <stdlib.h>

#include <time.h>

#include <sys/stat.h>

#include <pwd.h>

#include <grp.h>

#include <unistd.h>

#define SIZE 256

void main(int argc, char **argv) {
  /* used for listing the whole directory */
  /*   DIR *dirp;*/
  /*    struct dirent *direntp;*/
  struct stat sbuf;
  struct passwd *my_passwd;
  struct group *my_group;
  struct tm *strtime;

  char buffer[SIZE];
  mode_t mode;
  time_t modtime;

  uid_t me1;
  uid_t me2;
  gid_t gr1;
  gid_t gr2;
  int result;

  if(argv[1] != '\0'){
    /* used for listing the whole directory*/
    /* if((dirp = opendir(argv[1])) != NULL) {*/
    if(stat(argv[1], &sbuf) != -1){
      if(lstat(argv[1], &sbuf) != -1){
        mode = sbuf.st_mode;

        modtime = sbuf.st_mtime;
        strtime = localtime(&modtime);

        me1 = sbuf.st_uid;
        me2 = me1;

        gr1 = sbuf.st_gid;
        gr2 = gr1;

        my_passwd = getpwuid(me2);
        if(!my_passwd){
          printf("no user: %d\n", (int) me2);
          exit(EXIT_FAILURE);
        }

        my_group = getgrgid(gr2);
        if(!my_group){
          printf("no group: %d\n", (int) gr2);
          exit(EXIT_FAILURE);
	}

	/*used for listing the whole directory*/
        /* while((direntp = readdir(dirp)) != NULL) {*/
          if((result = S_ISDIR(mode)) != 0)  printf("d");
          else                               printf("-");
          if(mode & S_IRUSR)  printf("r");
          else                printf("-");
          if(mode & S_IWUSR)  printf("w");
          else                printf("-");
          if(mode & S_IXUSR)  printf("x");
          else                printf("-");
          if(mode & S_IRGRP)  printf("r");
          else                printf("-");
          if(mode & S_IWGRP)  printf("w");
          else                printf("-");
          if(mode & S_IXGRP)  printf("x");
          else                printf("-");
          if(mode & S_IROTH)  printf("r");
          else                printf("-");
          if(mode & S_IWOTH)  printf("w");
          else                printf("-");
          if(mode & S_IXOTH)  printf("x");
          else                printf("-");
          printf("    %d", sbuf.st_nlink);
          printf(" %s", my_passwd->pw_name);
          printf("     %s", my_group->gr_name);
          printf("\t       %d",(int) sbuf.st_size);
          strftime(buffer, SIZE," %b %d %H:%M", strtime);
          fputs(buffer,stdout);
	  /*used for listing the whole directory
          /*printf("\t %s", direntp->d_name);*/
          printf("\n");
	  /*used for listing the whole directory*/
	  /*}*/
       /*(void)closedir(dirp);*/
      }else
        printf("Can't stat %s\n", argv[1]);
    }else
      printf("Can't stat %s\n", argv[1]);
  /*used for listing the whole directory*/
   /*  }else*/
     /* printf("%s: can't open %s\n", argv[0], argv[1]);*/
  }else
    printf("Usage: %s file_name\n", argv[0]);
}    
Excusing the fact that this code is very messy at the moment, I have one problem with this code where it says "used for listing the whole directory" there loops through each file in the directory and prints out it's information. If were to unquote these lines it would compile fine, except it would list every file within the directory as being of type directory! I was wondering if anyone had any solutions. Edited by - pdstatha on February 24, 2002 9:47:28 AM
Advertisement
I''ll try and explain myself a little more clearly, if I were to do an "ls -al" command in unix then I would expect an output like this.

drwxr-xr-x 6 root root 4096 Feb 23 21:54 .
drwxr-x--- 12 root root 4096 Feb 24 14:00 ..
drwxr-xr-x 2 root root 4096 Feb 23 21:42 cd
drwxr-xr-x 2 root root 4096 Feb 23 19:29 hello
drwxr-xr-x 2 root root 4096 Feb 24 15:40 ls
drwxr-xr-x 2 root root 4096 Feb 23 21:52 pwd

Where from my code I am getting this

drwxr-xr-x 6 root root 4096 Feb 23 21:54 .
drwxr-xr-x 6 root root 4096 Feb 23 21:54 ..
drwxr-xr-x 6 root root 4096 Feb 23 21:54 hello
drwxr-xr-x 6 root root 4096 Feb 23 21:54 cd
drwxr-xr-x 6 root root 4096 Feb 23 21:54 pwd
drwxr-xr-x 6 root root 4096 Feb 23 21:54 ls

From my powers of observation I have noticed that all the details apart from the name of the file/directory are the same as the first one in the list!!!
If your other code is the same without comments i guess that your problem lies on the variable mode. You are using it to list the information of the files, but mode was taken from sbuf who was taken from argv[1] and never updated again
humanity will always be slaved by its ignorance
Are you doing this for learning purposes or do you just need the info? If the latter, I suggest calling fork() and exec() to actually call ls. Then just redirect the output to your original process.

This topic is closed to new replies.

Advertisement