00001
00002 #include <errno.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <stdlib.h>
00006 #include <fcntl.h>
00007 #include <getopt.h>
00008 #include <dirent.h>
00009 #include <stddef.h>
00010 #include <sys/types.h>
00011 #include <sys/stat.h>
00012 #if defined(_WIN32)
00013 #include <io.h>
00014 #else
00015 #include <unistd.h>
00016 #endif
00017 #include "../src/securestorage.h"
00018
00019 extern int errno;
00020 extern int serrno;
00021
00022
00023 int main (int argc, char *argv[]) {
00024
00025 int c;
00026 char *path=NULL;
00027 char *new_dir=NULL;
00028 char *stat_path;
00029 static struct option longopts[] = {
00030 {"help", no_argument, 0, 'H'},
00031 {0, 0, 0, 0}
00032 };
00033
00034 opterr = 0;
00035 while ((c = getopt_long (argc, argv, "h:", longopts, NULL)) != EOF) {
00036 switch (c) {
00037 case 'h':
00038 fprintf(stdout,"Usage: %s <path>\n",argv[0]);
00039 break;
00040 case 'H':
00041 fprintf(stdout,"Usage: %s <path>\n",argv[0]);
00042 break;
00043 case '?':
00044 fprintf(stdout,"Usage: %s <path>\n",argv[0]);
00045 break;
00046 default:
00047 break;
00048 }
00049 }
00050 path = argv[optind];
00051 new_dir = argv[optind+1];
00052 if (path==NULL) {
00053 fprintf(stdout,"Usage: %s <path> [<new_dir>]\n",argv[0]);
00054 exit(1);
00055 }
00056
00057 char new_path[LCG_MAXPATHLEN+5];
00058 if (new_dir != NULL) {
00059 sprintf (new_path, "%s/%s", path, new_dir);
00060 if ( securestorage_mkdir(new_path, 777) < 0 ) {
00061 int errno_save = errno;
00062 fprintf(stderr, "securestorage_mkdir: %s\n", securestorage_error(errno_save));
00063 }
00064 }
00065 struct dirent *ep;
00066 ss_DIR *dp;
00067 struct ss_filestatg info;
00068 int ret = 0;
00069
00070 dp = securestorage_opendir(path);
00071
00072 if (dp == NULL) {
00073 int errno_save = errno;
00074 fprintf(stderr, "securestorage_opendir: %s\n", securestorage_error(errno_save));
00075 exit(1);
00076 } else if (dp !=NULL) {
00077 while ( (ep = securestorage_readdir(dp)) ) {
00078 printf("%s --> ", ep->d_name);
00079 stat_path = calloc(1, (strlen(path) + strlen(ep->d_name) +1));
00080 sprintf(stat_path, "%s/%s", path, ep->d_name);
00081 ret = securestorage_statg(stat_path, NULL, &info);
00082 int errno_save = serrno;
00083 free(stat_path);
00084 if(ret == -1) {
00085 errno = errno_save;
00086 fprintf(stderr, "%s\n", securestorage_error(errno_save));
00087 }
00088 if (info.filemode & S_IFDIR)
00089 printf("is a Dir\n");
00090 else
00091 printf("is a File\n");
00092 }
00093 securestorage_closedir(dp);
00094 } else {
00095 int errno_save = errno;
00096 fprintf(stderr, "couldn'r open the directory : %s\n", securestorage_error(errno_save));
00097 }
00098 if (new_dir != NULL) {
00099 if ( securestorage_rmdir(new_path) < 0 ) {
00100 int errno_save = errno;
00101 fprintf(stderr, "securestorage_rmdir: %s\n", securestorage_error(errno_save));
00102 }
00103 }
00104
00105 exit (0);
00106 }