cleaned up a little
[tridge/junkcode.git] / lls.c
1 #include <stdio.h>
2 #include <time.h>
3 #include <sys/stat.h>
4 #include <stdlib.h>
5 #include <errno.h>
6
7 static char *timestring(time_t t)
8 {
9         static char TimeBuf[200];
10         struct tm *tm = localtime(&t);
11
12         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
13
14         if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
15                 TimeBuf[strlen(TimeBuf)-1] = 0;
16         }
17
18         return(TimeBuf);
19 }
20
21 static void ls_one(char *fname)
22 {
23         char perms[11] = "----------";
24         char *perm_map = "rwxrwxrwx";
25         struct stat st;
26         int i;
27
28         if (lstat(fname, &st) != 0) {
29                 perror(fname);
30                 return;
31         }
32
33         for (i=0;i<9;i++) {
34                 if (st.st_mode & (1<<i)) perms[9-i] = perm_map[8-i];
35         }
36         if (S_ISLNK(st.st_mode)) perms[0] = 'l';
37         if (S_ISDIR(st.st_mode)) perms[0] = 'd';
38         if (S_ISBLK(st.st_mode)) perms[0] = 'b';
39         if (S_ISCHR(st.st_mode)) perms[0] = 'c';
40         if (S_ISSOCK(st.st_mode)) perms[0] = 's';
41         if (S_ISFIFO(st.st_mode)) perms[0] = 'p';
42
43
44         printf("%s %11.0f %s %s\n", 
45                perms, 
46                (double)st.st_size, timestring(st.st_mtime), fname);
47 }
48
49 int main(int argc, char *argv[])
50 {
51         int i;
52
53         for (i=1; i<argc;i++) {
54                 ls_one(argv[i]);
55         }
56         return 0;
57 }