source: trunk/third/scrollkeeper/libs/file.c @ 20861

Revision 20861, 1.8 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20860, which included commits to RCS files with non-trunk default branches.
Line 
1/* copyright (C) 2001 Sun Microsystems, Inc.*/
2
3/*   
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#include <config.h>
20#include <sys/stat.h>
21#include <scrollkeeper.h>
22#include <stdio.h>
23
24int is_file(char *filename)
25{
26    struct stat buf;
27
28    if (!stat(filename, &buf) && S_ISREG(buf.st_mode))
29        return 1;
30
31    return 0;
32}
33
34int is_dir(char *path)
35{
36    struct stat buf;
37
38    if (!stat(path, &buf) && S_ISDIR(buf.st_mode))
39        return 1;
40
41    return 0;
42}
43
44int copy_file(char *old, char *new)
45{
46        FILE *old_fid, *new_fid;
47        unsigned char buf[1024];
48        int nitems;
49       
50        old_fid = fopen(old, "r");
51        if (old_fid == NULL) {
52                return 0;
53        }
54       
55        new_fid = fopen(new, "w");
56        if (new_fid == NULL) {
57                fclose (old_fid);
58                return 0;
59        }
60       
61       
62        while (!feof(old_fid)) {
63                nitems = fread((void *)buf, sizeof(unsigned char), 1024, old_fid);
64                if (nitems == 0) {
65                        if (ferror(old_fid)) {
66                                fclose (old_fid);
67                                fclose (new_fid);
68                                return 1;
69                        }
70                }
71               
72                if (fwrite((void *)buf, sizeof(unsigned char), nitems, new_fid) == 0) {
73                        fclose (old_fid);
74                        fclose (new_fid);
75                        return 1;
76                }
77        }
78       
79        fclose(old_fid);
80        fclose(new_fid);
81               
82        return 1;
83}
84
Note: See TracBrowser for help on using the repository browser.