12 #include "lixp_util.h"
16 * Copy src into destination escaping %'s into %%'s;
18 * NOTE: destination buffer must be twice as large as the source.
20 static inline void copy_escaping_fmt(char *dest
, const char *src
)
25 while ((pc
= strchr (s
, '%'))) {
36 /* ------------------------------------------------------------------------
39 int lixp_pusherrorf(lua_State
*L
, const char *fmt
, ...)
49 estr
= strerror(errno
);
52 _fmt
= malloc(strlen(fmt
) + 2 + (elen
*2) + 1);
56 copy_escaping_fmt(_fmt
, estr
);
61 lua_pushvfstring(L
, _fmt
, ap
);
66 lua_pushnumber(L
, errno
);
72 lua_pushvfstring(L
, "%s", ap
);
77 int lixp_pusherror(lua_State
*L
, const char *info
)
81 lua_pushstring(L
, strerror(errno
));
82 lua_pushnumber(L
, errno
);
86 return lixp_pusherrorf(L
, "%s", info
);
89 /* ------------------------------------------------------------------------
90 * write a buffer to an IXP file
92 int lixp_write_data (IxpCFid
*fid
, const char *data
, size_t data_len
)
99 int rc
= ixp_write(fid
, (char*)data
+ ofs
, left
);
113 /* ------------------------------------------------------------------------
114 * dump IXP status structure to lua table
116 static void setrwx(long m
, char *s
)
118 static char *modes
[] = {
123 strncpy(s
, modes
[m
], 3);
126 static void build_modestr(char *buf
, const struct IxpStat
*stat
)
129 if(stat
->mode
& P9_DMDIR
)
132 setrwx((stat
->mode
>> 6) & 7, &buf
[2]);
133 setrwx((stat
->mode
>> 3) & 7, &buf
[5]);
134 setrwx((stat
->mode
>> 0) & 7, &buf
[8]);
138 static void build_timestr(char *buf
, const struct IxpStat
*stat
)
140 ctime_r((time_t*)&stat
->mtime
, buf
);
141 buf
[strlen(buf
) - 1] = '\0';
145 #define setfield(type,name,value) \
146 lua_pushstring (L, name); \
147 lua_push##type (L, value); \
148 lua_settable (L, -3);
149 int lixp_pushstat (lua_State
*L
, const struct IxpStat
*stat
)
154 setfield(number
, "type", stat
->type
);
155 setfield(number
, "dev", stat
->dev
);
156 //setfield(Qid, "qid", stat->qid); // TODO: what is this?
157 setfield(number
, "mode", stat
->mode
);
158 setfield(number
, "atime", stat
->atime
);
159 setfield(number
, "mtime", stat
->mtime
);
160 setfield(number
, "length", stat
->length
);
161 setfield(string
, "name", stat
->name
);
162 setfield(string
, "uid", stat
->uid
);
163 setfield(string
, "gid", stat
->gid
);
164 setfield(string
, "muid", stat
->muid
);
166 build_modestr(buf
, stat
);
167 setfield(string
, "modestr", buf
);
169 build_timestr(buf
, stat
);
170 setfield(string
, "timestr", buf
);