kfinfo.c

最終更新:2009/12/16

kfinfo.c

001: /* kfinfo.c
002:  * ファイル情報のサンプル
003:  * apr_stat の挙動を確認するプログラム
004:  */
005: 
006: #define USAGE "Usage: kfinfo <path>"
007: 
008: #include "apr_general.h"
009: #include "apr_errno.h"
010: #include "apr_strings.h"
011: #include "apr_file_info.h"
012: 
013: #include "mystab.h"
014: 
015: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) {
016: 
017:   int error_flag = 0;
018:   apr_status_t rv = APR_SUCCESS;
019:   
020:   char *path = NULL;
021:   apr_finfo_t finfo;
022:   
023:   if (ac < 2) {
024:     apr_file_printf(astderr, "%s\n", USAGE);
025:     error_flag = 1;
026:     goto _FINALLY_;
027:   }
028: 
029:   path = av[1];
030:   
031:   rv = apr_stat(&finfo, path, APR_FINFO_MIN, pool);
032:   if (rv != APR_SUCCESS) {
033:     error_flag = 1;
034:     goto _FINALLY_;
035:   }
036: 
037:   {
038:     char *filetype_str=NULL;
039:     switch(finfo.filetype) {
040:       case APR_NOFILE:
041:         filetype_str="no file type determined";
042:         break;
043:       case APR_REG:
044:         filetype_str="APR_REG / a regular file";
045:         break;
046:       case APR_DIR:
047:         filetype_str="APR_DIR / a directory";
048:         break;
049:       case APR_CHR:
050:         filetype_str="APR_CHR / a character device";
051:         break;
052:       case APR_BLK:
053:         filetype_str="APR_BLK / a block device";
054:         break;
055:       case APR_PIPE:
056:         filetype_str="APR_PIPE / a FIFO / pipe";
057:         break;
058:       case APR_LNK:
059:         filetype_str="APR_LNK / a symbolic link";
060:         break;
061:       case APR_SOCK:
062:         filetype_str="APR_SOCK / a [unix domain] socket";
063:         break;
064:       case APR_UNKFILE:
065:         filetype_str="APR_UNKFILE / a file of some other unknown type";
066:     }
067:     
068:     apr_file_printf(astdout, "filetype=[%s]\n", filetype_str);
069:   }
070: 
071:   apr_file_printf(astdout, "size=[%d]bytes\n", (int)finfo.size);
072: 
073:   #define XPRINTTIME(XSTR,X) {char tbuf[30]; apr_rfc822_date(tbuf, finfo.X); apr_file_printf(astdout, "%s=[%s]\n", XSTR, tbuf);}
074:   
075:   XPRINTTIME("atime", atime);
076:   XPRINTTIME("mtime", mtime);
077:   XPRINTTIME("ctime", ctime);
078: 
079: 
080:   {
081:     const char *rootpath=NULL;
082:     const char *filepath = apr_pstrdup(pool, path);
083:     rv = apr_filepath_root(&rootpath, &filepath, APR_FILEPATH_TRUENAME /*APR_FILEPATH_NATIVE*/, pool);
084:     if (rv != APR_SUCCESS) {
085:       error_flag = 1;
086:       goto _FINALLY_;
087:     }
088:     apr_file_printf(astdout, "rootpath=[%s], filepath=[%s]\n", rootpath, filepath);
089:   }
090: 
091:   _FINALLY_:
092:   
093:   if (error_flag) {
094:     if (rv != APR_SUCCESS) {
095:       char error_buf[256];
096:       apr_file_printf(astderr, "%s\n", apr_strerror(rv, error_buf, sizeof(error_buf)));
097:     }
098:     apr_file_printf(astderr, "failed!\n");
099:     return 1; /* 異常終了 */
100:   }
101:   
102:   apr_file_printf(astdout, "\ndone.\n");
103:   return 0; /* 正常終了 */
104:   
105: } /* end of main */
Copyright (C) KAKU PROJECT (2009)KAKU PROJECT (2009)