libi2ncommon: (gerd) initial submission, not in use yet
[libi2ncommon] / src / filefunc.cpp
CommitLineData
e93545dd
GE
1/***************************************************************************
2 escape.cpp - escaping of strings
3 -------------------
4 begin : Sun Nov 14 1999
5 copyright : (C) 1999 by Intra2net AG
6 email : info@intra2net.com
7 ***************************************************************************/
8
9#include <string>
10#include <iostream>
11
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <pwd.h>
15#include <grp.h>
16#include <unistd.h>
17
18#include <filefunc.hxx>
19
20using namespace std;
21
22long fsize (const string &name)
23{
24 long iReturn = -1;
25
26 struct stat statbuff;
27
28 if (lstat(name.c_str(), &statbuff) < 0)
29 return -1;
30
31 if (!S_ISREG(statbuff.st_mode))
32 return -1;
33
34 iReturn=statbuff.st_size;
35
36 return iReturn;
37}
38
39string load_file(const string &name)
40{
41 FILE *f;
42 string s;
43
44 if (name.find ("..") != string::npos)
45 return ("can't load file (..): " + name);
46
47 f=::fopen(name.c_str(),"rb");
48 if (!f)
49 {
50 return ("");
51 }
52
53 ::fseek(f,0,SEEK_END);
54 int size=::ftell(f);
55 ::fseek(f,0,SEEK_SET);
56 char *c=new char[size+1];
57 ::fread(c,1,size,f);
58 s.assign(c,size);
59 delete[] c;
60 ::fclose (f);
61
62 return s;
63}
64
65bool chown(const char* file,const char* owner, const char* group)
66{
67 struct passwd *p;
68 struct group *g;
69 uid_t uid;
70 gid_t gid;
71
72 p = getpwnam(owner);
73 if (p == NULL)
74 return false;
75 uid=p->pw_uid;
76
77 g = getgrnam(group);
78 if (g == NULL)
79 return false;
80 gid=g->gr_gid;
81
82 if (!::chown(file,uid,gid))
83 return true;
84 else
85 return false;
86}
87
88