#include #include #include #include "tlmplib.h" #include "tlmplib.m" #include struct _F_walkfs_private{ bool mustend; _F_walkfs_private(){ mustend = false; } }; bool _F_walkfs::recurse(const char *, const char *, const char *, int) { return true; } void _F_walkfs::end() { priv->mustend = true; } static int walkfs_priv (_F_walkfs &c, int baselen, const char *dirpath, int depth) { int ret = -1; DIR *dir = opendir (dirpath); if (dir != NULL){ ret = 0; struct dirent *ent; int next_depth = depth+1; while (!c.priv->mustend && (ent = readdir (dir)) != NULL){ if (strcmp(ent->d_name,".")!=0 && strcmp(ent->d_name,"..")!=0){ char path[PATH_MAX],basename[PATH_MAX]; // We grab a copy so sub-function may use readdir as well strcpy (basename,ent->d_name); snprintf (path,sizeof(path)-1,"%s/%s",dirpath,basename); const char *relpath = path + baselen; c.onefile (path,basename,relpath,depth); if (file_type(path)==1 && c.recurse(path,basename,relpath,depth)){ // This is a directory and we are allowed to recurse ret += walkfs_priv (c,baselen,path,next_depth); } ret ++; } } closedir (dir); } return ret; } int walkfs (_F_walkfs &c, const char *dirpath) { _F_walkfs_private priv; c.priv = &priv; return walkfs_priv (c,strlen(dirpath)+1,dirpath,0); } class _F_walkpopen__v1_private { public: POPEN *pop; FILE *fout; }; int _F_walkpopen__v1::wait (POPEN &pop, int timeout, bool &end) { return pop.wait (timeout); } int _F_walkpopen__v1::oneerr(const char *, const char *, FILE *) { return 0; } void _F_walkpopen__v1::start(FILE *, bool &) { } void _F_walkpopen__v1::end() { } void _F_walkpopen__v1::empty(const char *) { } void _F_walkpopen__v1::fail(const char *cmd) { tlmp_error (MSG_U(E_CANTEXEC,"Can't execute command %s\n"),cmd); } void _F_walkpopen__v1::close_input() { priv->pop->closepipe(); priv->fout = NULL; } void _F_walkpopen__v1::init(FILE *, bool &) { } int walkpopen__v1 ( _F_walkpopen__v1 &c, POPEN &pop, int timeout, const char *command) { _F_walkpopen__v1_private priv; priv.pop = &pop; c.priv = &priv; int ret = -1; if (pop.isok()){ ret = 0; bool end = false; priv.fout = pop.getfout(); c.init (priv.fout,end); bool start_called = false; while (!end){ char buf[PATH_MAX]; while (pop.readout(buf,sizeof(buf))!=-1){ int last = strlen(buf) - 1; if (last >= 0 && buf[last] == '\n') buf[last] = '\0'; if (ret == 0){ c.start(priv.fout,end); start_called = true; } if (c.oneline (buf,ret,priv.fout)==-1){ ret = -1; end = true; break; } ret ++; } while (pop.readerr(buf,sizeof(buf))!=-1){ int last = strlen(buf) - 1; if (last >= 0 && buf[last] == '\n') buf[last] = '\0'; if (c.oneerr (buf,command,priv.fout)==-1){ ret = -1; end = true; break; } } if (end || c.wait(pop,timeout,end)< 0) break; } if (ret == 0){ c.empty(command); }else if (start_called){ c.end(); } int tmp = pop.getstatus(); if (tmp != 0) ret = -tmp; }else{ c.fail(command); } return ret; } int walkpopen__v1 ( _F_walkpopen__v1 &c, const char *command, int timeout) { POPEN pop (command); return walkpopen__v1 (c,pop,timeout,command); } int walkpopen__v1 ( _F_walkpopen__v1 &c, const char *command, const char *arg, int timeout) { POPEN pop (command,arg); SSTRING full; full.setfromf ("%s %s",command,arg); return walkpopen__v1 (c,pop,timeout,full.get()); } int walkpopen__v1 ( _F_walkpopen__v1 &c, const char *command, int timeout, bool user) { if (user){ POPENUSER pop (command); return walkpopen__v1 (c,pop,timeout,command); }else{ POPEN pop (command); return walkpopen__v1 (c,pop,timeout,command); } }