Initial revision
[geeqie.git] / src / fileops.c
1 /*
2  * GQview image viewer
3  * (C)1999 John Ellis
4  *
5  * Author: John Ellis
6  *
7  */
8
9 #include "gqview.h"
10
11 /*
12  *-----------------------------------------------------------------------------
13  * generic file information and manipulation routines (public)
14  *-----------------------------------------------------------------------------
15  */ 
16
17 /* first we try the HOME environment var, if that doesn't work, we try getpwuid(). */
18 gchar *homedir()
19 {
20         gchar *home = getenv("HOME");
21         if(home)
22                 return home;
23         else
24                 {
25                 struct passwd *pw = getpwuid(getuid());
26                 if (pw)
27                         return pw->pw_dir;
28                 else
29                         return NULL ; /* now we've got a problem */
30                 }
31 }
32
33 int isfile(char *s)
34 {
35    struct stat st;
36    
37    if ((!s)||(!*s)) return 0;
38    if (stat(s,&st)<0) return 0;
39    if (S_ISREG(st.st_mode)) return 1;
40    return 0;
41 }
42
43 int isdir(char *s)
44 {
45    struct stat st;
46    
47    if ((!s)||(!*s)) return 0;
48    if (stat(s,&st)<0) return 0;
49    if (S_ISDIR(st.st_mode)) return 1;
50    return 0;
51 }
52
53 int filesize(char *s)
54 {
55    struct stat st;
56    
57    if ((!s)||(!*s)) return 0;
58    if (stat(s,&st)<0) return 0;
59    return (int)st.st_size;
60 }
61
62 time_t filetime(gchar *s)
63 {
64         struct stat st;
65
66         if ((!s)||(!*s)) return 0;
67         if (stat(s,&st)<0) return 0;
68         return st.st_mtime;
69 }
70
71 int copy_file(char *s, char *t)
72 {
73         FILE *fi, *fo;
74         char buf[4096];
75         int b;
76
77         fi = fopen(s, "rb");
78         if (!fi)
79                 {
80                 return FALSE;
81                 }
82
83         fo = fopen(t, "wb");
84         if (!fo)
85                 {
86                 fclose(fi);
87                 return FALSE;
88                 }
89
90         while((b = fread(buf, sizeof(char), 4096, fi)) && b != 0)
91                 {
92                 if (fwrite(buf, sizeof(char), b, fo) != b)
93                         {
94                         fclose(fi);
95                         fclose(fo);
96                         return FALSE;
97                         }
98                 }
99
100         fclose(fi);
101         fclose(fo);
102         return TRUE;
103 }
104
105 int move_file(char *s, char *t)
106 {
107         if (rename (s, t) < 0)
108                 {
109                 /* this may have failed because moving a file across filesystems
110                 was attempted, so try copy and delete instead */
111                 if (copy_file(s, t))
112                         {
113                         if (unlink (s) < 0)
114                                 {
115                                 /* err, now we can't delete the source file so return FALSE */
116                                 return FALSE;
117                                 }
118                         }
119                 else
120                         return FALSE;
121                 }
122
123         return TRUE;
124 }
125
126 gchar *get_current_dir()
127 {
128         char buf[512];
129         getcwd(buf, 510);
130         return g_strdup(buf);
131 }
132
133