altri pezzi di codice da aggiungere
authorSimone Piccardi <piccardi@gnulinux.it>
Fri, 4 Apr 2003 09:55:30 +0000 (09:55 +0000)
committerSimone Piccardi <piccardi@gnulinux.it>
Fri, 4 Apr 2003 09:55:30 +0000 (09:55 +0000)
listati/DirScan.c [new file with mode: 0644]
listati/dirent.c [new file with mode: 0644]
listati/is_file_dir.h [new file with mode: 0644]
listati/my_ls.c [new file with mode: 0644]
listati/sched_param.c [new file with mode: 0644]
listati/stat.h [new file with mode: 0644]
listati/utimbuf.h [new file with mode: 0644]

diff --git a/listati/DirScan.c b/listati/DirScan.c
new file mode 100644 (file)
index 0000000..725b524
--- /dev/null
@@ -0,0 +1,33 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>        /* directory */
+#include <stdlib.h>        /* C standard library */
+#include <unistd.h>
+
+/*
+ * Function DirScan: 
+ * 
+ * Input:  the directory name and a computation function
+ * Return: 0 if OK, -1 on errors
+ */
+int DirScan(char * dirname, int(*compute)(struct dirent *)) 
+{
+    DIR * dir;
+    struct dirent *direntry;
+
+    if ( (dir = opendir(dirname)) == NULL) {               /* oper directory */
+        printf("Opening %s\n", dirname);          /* on error print messages */
+        perror("Cannot open directory");                  /* and then return */
+        return -1;
+    }
+    fd = dirfd(dir);                                  /* get file descriptor */
+    fchdir(fd);                                          /* change directory */
+    /* loop on directory entries */
+    while ( (direntry = readdir(dir)) != NULL) {               /* read entry */
+        if (compute(direntry)) {                   /* execute function on it */
+            return -1;                                    /* on error return */
+        }
+    }
+    closedir(dir);
+    return 0;
+}
diff --git a/listati/dirent.c b/listati/dirent.c
new file mode 100644 (file)
index 0000000..f8e4c25
--- /dev/null
@@ -0,0 +1,7 @@
+struct dirent {
+    ino_t d_ino;                    /* inode number */
+    off_t d_off;                    /* offset to the next dirent */
+    unsigned short int d_reclen;    /* length of this record */
+    unsigned char d_type;           /* type of file */
+    char d_name[256];               /* We must not include limits.h! */
+};
diff --git a/listati/is_file_dir.h b/listati/is_file_dir.h
new file mode 100644 (file)
index 0000000..fac2e21
--- /dev/null
@@ -0,0 +1 @@
+#define IS_FILE_DIR(x) (((x) & S_IFMT) & (S_IFDIR | S_IFREG))
diff --git a/listati/my_ls.c b/listati/my_ls.c
new file mode 100644 (file)
index 0000000..1ff634a
--- /dev/null
@@ -0,0 +1,30 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>        /* directory */
+#include <stdlib.h>        /* C standard library */
+#include <unistd.h>
+
+/* computation function for DirScan */
+int do_ls(struct dirent * direntry);
+/* main body */
+int main(int argc, char *argv[]) 
+{
+    ...
+    if ((argc - optind) != 1) {          /* There must be remaing parameters */
+        printf("Wrong number of arguments %d\n", argc - optind);
+        usage();
+    }
+    DirScan(argv[1], do_ls);
+    exit(0);
+}
+/*
+ * Routine to print file name and size inside DirScan
+ */
+int do_ls(struct dirent * direntry) 
+{
+    struct stat data;
+
+    stat(direntry->d_name, &data);                          /* get stat data */
+    printf("File: %s \t size: %d\n", direntry->d_name, data.st_size);
+    return 0;
+}
diff --git a/listati/sched_param.c b/listati/sched_param.c
new file mode 100644 (file)
index 0000000..72fd1e5
--- /dev/null
@@ -0,0 +1,3 @@
+struct sched_param {
+    int sched_priority;
+};
diff --git a/listati/stat.h b/listati/stat.h
new file mode 100644 (file)
index 0000000..9f1fc76
--- /dev/null
@@ -0,0 +1,15 @@
+struct stat {
+    dev_t         st_dev;      /* device */
+    ino_t         st_ino;      /* inode */
+    mode_t        st_mode;     /* protection */
+    nlink_t       st_nlink;    /* number of hard links */
+    uid_t         st_uid;      /* user ID of owner */
+    gid_t         st_gid;      /* group ID of owner */
+    dev_t         st_rdev;     /* device type (if inode device) */
+    off_t         st_size;     /* total size, in bytes */
+    unsigned long st_blksize;  /* blocksize for filesystem I/O */
+    unsigned long st_blocks;   /* number of blocks allocated */
+    time_t        st_atime;    /* time of last access */
+    time_t        st_mtime;    /* time of last modification */
+    time_t        st_ctime;    /* time of last change */
+};
diff --git a/listati/utimbuf.h b/listati/utimbuf.h
new file mode 100644 (file)
index 0000000..4db8658
--- /dev/null
@@ -0,0 +1,4 @@
+struct utimbuf {
+        time_t actime;  /* access time */
+        time_t modtime; /* modification time */
+};