Fixed message typo.
[captive.git] / src / client / cmdline / file_info.c
1 /* $Id$
2  * client cmdline interface GnomeVFSFileInfo utils for libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <glib/gerror.h>
24 #include <libgnomevfs/gnome-vfs-file-info.h>
25 #include <glib/gstrfuncs.h>
26 #include <stdio.h>
27 #include <time.h>
28
29
30 #include "file_info.h"  /* self */
31 #include "utf8.h"
32
33
34 GQuark cmdline_file_info_error_quark(void)
35 {
36 GQuark r=0;
37
38         if (!r)
39                 r=g_quark_from_static_string("cmdline-file_info");
40
41         return r;
42 }
43
44
45 void cmdline_captive_file_info_object_dump_line(CaptiveFileInfoObject *captive_file_info_object,GError **errp)
46 {
47 const gchar *file_type,*file_perms;
48 gchar *file_size;
49
50         g_return_if_fail(CAPTIVE_FILE_INFO_IS_OBJECT(captive_file_info_object));
51         g_return_if_fail(!errp || !*errp);
52
53         switch (!(captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) ? GNOME_VFS_FILE_TYPE_UNKNOWN : captive_file_info_object->p.type) {
54                 case GNOME_VFS_FILE_TYPE_REGULAR:   file_type="FILE"; break;
55                 case GNOME_VFS_FILE_TYPE_DIRECTORY: file_type="DIR ";  break;
56                 case GNOME_VFS_FILE_TYPE_SOCKET:    file_type="DEV ";  break;
57                 default:                            file_type="??? ";  break;
58                 }
59
60         if (!(captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS))
61                 file_perms="???";
62         else if (captive_file_info_object->p.permissions & GNOME_VFS_PERM_USER_WRITE)
63                 file_perms="r/w";
64         else
65                 file_perms="r/o";
66
67         if (captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
68                 file_size=g_strdup_printf("%8" GNOME_VFS_SIZE_FORMAT_STR,captive_file_info_object->p.size);
69         else
70                 file_size=g_strdup_printf("%8s","???");
71
72         /*      type pm sz nm */
73         printf("[%s] %s %s %s\n",file_type,file_perms,file_size,CMD_LOCALE_FROM_UTF8_ALLOCA(captive_file_info_object->p.name));
74         
75         g_free(file_size);
76 }
77
78 static void timespec_split_print(time_t sec,guint nsec)
79 {
80 char *ctime_buf,*s;
81
82         g_return_if_fail(sec!=0);
83         g_return_if_fail(nsec<1000000000);
84
85         ctime_buf=ctime(&sec);
86         if ((s=strchr(ctime_buf,'\n')))
87                 *s='\0';
88         printf("%s +%09u nsec\n",ctime_buf,(unsigned)nsec);
89 }
90
91 void cmdline_captive_file_info_object_dump_full(CaptiveFileInfoObject *captive_file_info_object,GError **errp)
92 {
93         g_return_if_fail(CAPTIVE_FILE_INFO_IS_OBJECT(captive_file_info_object));
94         g_return_if_fail(!errp || !*errp);
95
96         printf("Filename: %s\n",CMD_LOCALE_FROM_UTF8_ALLOCA(captive_file_info_object->p.name));
97
98         fputs("File type: ",stdout);
99         switch (!(captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) ? GNOME_VFS_FILE_TYPE_UNKNOWN : captive_file_info_object->p.type) {
100                 case GNOME_VFS_FILE_TYPE_REGULAR:   puts("REGULAR");   break;
101                 case GNOME_VFS_FILE_TYPE_DIRECTORY: puts("DIRECTORY"); break;
102                 case GNOME_VFS_FILE_TYPE_SOCKET:    puts("SOCKET");    break;
103                 default:                            puts("???");       break;
104                 }
105
106         fputs("File size: ",stdout);
107         if (captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
108                 printf("%" GNOME_VFS_SIZE_FORMAT_STR "\n",captive_file_info_object->p.size);
109         else
110                 puts("???");
111
112         fputs("Block count: ",stdout);
113         if (captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT)
114                 printf("%" GNOME_VFS_SIZE_FORMAT_STR "\n",captive_file_info_object->p.block_count);
115         else
116                 puts("???");
117
118         fputs("Writable?: ",stdout);
119         if (!(captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS))
120                 puts("???");
121         else if (captive_file_info_object->p.permissions & GNOME_VFS_PERM_USER_WRITE)
122                 puts("read/write");
123         else
124                 puts("read/only");
125
126         fputs("Access-time (atime): ",stdout);
127         if (!(captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_ATIME))
128                 puts("???");
129         else
130                 timespec_split_print(captive_file_info_object->p.atime,captive_file_info_object->atime_nsec);
131
132         fputs("Modification-time (mtime): ",stdout);
133         if (!(captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME))
134                 puts("???");
135         else
136                 timespec_split_print(captive_file_info_object->p.mtime,captive_file_info_object->mtime_nsec);
137
138         fputs("Change-time (ctime): ",stdout);
139         if (!(captive_file_info_object->p.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_CTIME))
140                 puts("???");
141         else
142                 timespec_split_print(captive_file_info_object->p.ctime,captive_file_info_object->ctime_nsec);
143 }