本文整理汇总了C++中scandir函数的典型用法代码示例。如果您正苦于以下问题:C++ scandir函数的具体用法?C++ scandir怎么用?C++ scandir使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了scandir函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
void UDirectory::update()
{
if(exists(path_))
{
std::string lastName;
bool endOfDir = false;
if(iFileName_ != fileNames_.end())
{
//Record the last file name
lastName = *iFileName_;
}
else if(fileNames_.size())
{
lastName = *fileNames_.rbegin();
endOfDir = true;
}
fileNames_.clear();
#ifdef WIN32
WIN32_FIND_DATA fileInformation;
#ifdef UNICODE
wchar_t * pathAll = createWCharFromChar((path_+"\\*").c_str());
HANDLE hFile = ::FindFirstFile(pathAll, &fileInformation);
delete [] pathAll;
#else
HANDLE hFile = ::FindFirstFile((path_+"\\*").c_str(), &fileInformation);
#endif
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
#ifdef UNICODE
char * fileName = createCharFromWChar(fileInformation.cFileName);
fileNames_.push_back(fileName);
delete [] fileName;
#else
fileNames_.push_back(fileInformation.cFileName);
#endif
} while(::FindNextFile(hFile, &fileInformation) == TRUE);
::FindClose(hFile);
std::vector<std::string> vFileNames = uListToVector(fileNames_);
std::sort(vFileNames.begin(), vFileNames.end(), sortCallback);
fileNames_ = uVectorToList(vFileNames);
}
#else
int nameListSize;
struct dirent ** nameList = 0;
nameListSize = scandir(path_.c_str(), &nameList, 0, sortCallback);
if(nameList && nameListSize>0)
{
for (int i=0;i<nameListSize;++i)
{
fileNames_.push_back(nameList[i]->d_name);
free(nameList[i]);
}
free(nameList);
}
#endif
//filter extensions...
std::list<std::string>::iterator iter = fileNames_.begin();
bool valid;
while(iter!=fileNames_.end())
{
valid = false;
if(extensions_.size() == 0 &&
iter->compare(".") != 0 &&
iter->compare("..") != 0)
{
valid = true;
}
for(unsigned int i=0; i<extensions_.size(); ++i)
{
if(UFile::getExtension(*iter).compare(extensions_[i]) == 0)
{
valid = true;
break;
}
}
if(!valid)
{
iter = fileNames_.erase(iter);
}
else
{
++iter;
}
}
iFileName_ = fileNames_.begin();
if(!lastName.empty())
{
bool found = false;
for(std::list<std::string>::iterator iter=fileNames_.begin(); iter!=fileNames_.end(); ++iter)
{
if(lastName.compare(*iter) == 0)
{
found = true;
iFileName_ = iter;
break;
}
}
//.........这里部分代码省略.........
示例2: scan_sys_cache_info
static int scan_sys_cache_info (void) {
long page_size = sysconf (_SC_PAGESIZE);
if (page_size <= 0) {
error (0, errno, "unable to get pagesize");
return -1;
}
/* versionsort sort by index number */
struct dirent ** list;
int nb_dir = scandir (SYSPATH, &list, scandir_filter, alphasort);
if (nb_dir < 0) {
error (0, errno, "scandir(%s)", SYSPATH);
return -1;
}
// alloc table (will set found to 0)
caches = calloc (nb_dir, sizeof (struct cache_info));
if (caches == NULL)
error (EXIT_FAILURE, errno, "calloc");
nb_cache_levels = nb_dir;
for (int i = 0; i < nb_dir; i++) {
const char * d = list[i]->d_name;
int ok;
char buf[BUF_SIZE];
struct cache_info cinfo;
// ensure type includes Data
if (read_sys_cache_file (d, "type", buf) == -1)
continue;
if (strcmp (buf, "Data") == 0)
cinfo.type = "Data";
else if (strcmp (buf, "Unified") == 0)
cinfo.type = "Unified";
else
continue;
// read size, assoc, level
if (read_sys_cache_file (d, "size", buf) == -1)
continue;
cinfo.size = str2size (buf, &ok);
if (!(ok && cinfo.size > 0))
continue;
if (read_sys_cache_file (d, "ways_of_associativity", buf) == -1)
continue;
cinfo.assoc = checked_strtoul (buf, &ok, NULL);
if (! (ok && cinfo.assoc > 0))
continue;
if (read_sys_cache_file (d, "level", buf) == -1)
continue;
int level = checked_strtoul (buf, &ok, NULL);
if (! (ok && level < nb_cache_levels))
continue;
cinfo.nb_colors = cinfo.size / (page_size * cinfo.assoc);
cinfo.found = 1;
caches[level] = cinfo;
}
return 0;
}
示例3: db_files_select
return -1;
}
int db_files_select(const struct dirent *file) {
int i;
for (i = 0; db_name[i]; i++) if (db_name[i] != file->d_name[i]) return 0;
while (1) switch (file->d_name[i]) {
case '\0': return 1;
case '0' ... '9': i++;
continue;
default: return 0;
}
}
int ret = scandir (dir, files, db_files_select, db_files_sort);
pthread_mutex_unlock(&mutex);
return ret;
}
/// Projde databázi a opraví atributy typu Pointer z id uzlů na pointery.
static void fix_pointers(Database *D, IdToNode *nodes) {
ndictFor(var, nodes) {
Node *node = var->value;
node_get_type(node)->init_pointers(nodes, node);
} ndictForEnd;
}
示例4: create_list_from_dir
static LIST* create_list_from_dir(char *dir) {
DIR *dir_p;
G_GAME_LIST *t;
struct dirent *dirent_p;
char filename[strlen(dir)+256];
LIST *l=NULL;
struct stat filestat;
struct DIRENT **namelist;
int nbf, i;
/*
if (dir[strlen(dir)-1]=='/')
dir[strlen(dir)-1]=0;
*/
if ((nbf = scandir(dir, &namelist, NULL/*game_selection*/, my_alphasort )) != -1) {
for (i = 0; i < nbf; i++) {
#ifdef __QNXNTO__
if (strcmp(namelist[i]->d_name, "..") == 0)
continue;
else if (strcmp(namelist[i]->d_name, ".") == 0)
continue;
else if (stricmp(namelist[i]->d_name, "romrc.d") == 0)
continue;
else if (stricmp(namelist[i]->d_name, "shots") == 0)
continue;
#endif
sprintf(filename, "%s/%s", dir, namelist[i]->d_name);
lstat(filename, &filestat);
if (S_ISDIR(filestat.st_mode))
{
t=malloc(sizeof(G_GAME_LIST));
t->dir=malloc(strlen(namelist[i]->d_name)*sizeof(char)+2);
sprintf(t->dir,"%s",namelist[i]->d_name);
t->dr=NULL;
t->file=NULL;
l=list_prepend(l,t);
}
else if (game_only==SDL_TRUE)
{
DRIVER *dr;
if ((dr=get_driver_for_zip(filename))!=NULL)
{
t=malloc(sizeof(G_GAME_LIST));
t->dir=NULL;
t->file=strdup(namelist[i]->d_name);
t->dr=dr;
l=list_prepend(l,t);
//printf("GAME %s\n",t->dr->longname);
}
}
else
{
t=malloc(sizeof(G_GAME_LIST));
t->file=strdup(namelist[i]->d_name);
t->dir=NULL;
t->dr=NULL;
l=list_prepend(l,t);
//printf("FILE %s\n",t->file);
}
}
}
if (nbf==-1) {
/* the dir is unreadable, we create pseudo dir . and .. */
t=malloc(sizeof(G_GAME_LIST));
t->dir=strdup("..");
t->dr=NULL;
t->file=NULL;
l=list_prepend(l,t);
t=malloc(sizeof(G_GAME_LIST));
t->dir=strdup(".");
t->dr=NULL;
t->file=NULL;
l=list_prepend(l,t);
}
return l;
}
示例5: GetiLOGen
int GetiLOGen() {
int count = 0, k = 0;
struct dirent ** devices;
char *d_dir = "/sys/module/hpilo/drivers/pci:hpilo/";
char buffer[256];
int ilogen = 0;
count = scandir(d_dir, &devices, pci_dev_filter, alphasort );
for (k = 0; k < count; k++) {
unsigned char config[256];
int config_fd;
strncpy(buffer, d_dir, sizeof(buffer) - 1);
strncat(buffer, devices[k]->d_name, 255 - strlen(buffer));
strncat(buffer, "/config", 255 - strlen(buffer));
if ((config_fd = open(buffer, O_RDONLY)) != -1) {
if (read(config_fd, config, 256) == 256 ) {
/* Check for iLO3 and later */
if ((config[45] == 0x10) && (config[44] == 0x3c)) {
/* This is a HP iLO */
if (config[47] == 0x33) {
switch (config[46]) {
case 0x04:
/* It's an iLO */
ilogen = 0;
break;
case 0x05:
/* It's an iLO */
ilogen = 1;
break;
case 0x09:
/* It's either an iLO2 or iLO3 */
switch (config[8]) {
case 3:
ilogen = 2;
break;
case 4:
ilogen = 3;
break;
default:
ilogen = 1;
}
break;
case 0x0e:
ilogen = 3;
break;
case 0x81:
ilogen = 4;
break;
default:
ilogen = config[8] - 1;
}
}
}
}
close(config_fd);
}
free(devices[k]);
}
free(devices);
return ilogen;
}
示例6: recursive_hardlink
int recursive_hardlink(const char *src, const char *dst, const char *client, struct cntr *p1cntr, struct cntr *cntr, struct config *conf)
{
int n=-1;
int ret=0;
struct dirent **dir;
char *tmp=NULL;
//logp("in rec hl: %s %s\n", src, dst);
if(!(tmp=prepend_s(dst, "dummy", strlen("dummy"))))
return -1;
if(mkpath(&tmp, dst))
{
logp("could not mkpath for %s\n", tmp);
free(tmp);
return -1;
}
free(tmp);
if((n=scandir(src, &dir, 0, 0))<0)
{
logp("recursive_hardlink scandir %s: %s\n",
src, strerror(errno));
return -1;
}
while(n--)
{
struct stat statp;
char *fullpatha=NULL;
char *fullpathb=NULL;
if(dir[n]->d_ino==0
|| !strcmp(dir[n]->d_name, ".")
|| !strcmp(dir[n]->d_name, ".."))
{ free(dir[n]); continue; }
if(!(fullpatha=prepend_s(src,
dir[n]->d_name, strlen(dir[n]->d_name)))
|| !(fullpathb=prepend_s(dst,
dir[n]->d_name, strlen(dir[n]->d_name))))
{
if(fullpatha) free(fullpatha);
if(fullpathb) free(fullpathb);
break;
}
if(lstat(fullpatha, &statp))
{
logp("could not lstat %s\n", fullpatha);
}
else if(S_ISDIR(statp.st_mode))
{
if(recursive_hardlink(fullpatha, fullpathb, client,
p1cntr, cntr, conf))
{
free(fullpatha);
free(fullpathb);
break;
}
}
else
{
//logp("hardlinking %s to %s\n", fullpathb, fullpatha);
write_status(client, STATUS_SHUFFLING, fullpathb,
p1cntr, cntr);
if(do_link(fullpatha, fullpathb, &statp, conf,
FALSE /* do not overwrite target */))
{
free(fullpatha);
free(fullpathb);
break;
}
}
free(fullpatha);
free(fullpathb);
free(dir[n]);
}
if(n>0)
{
ret=-1;
for(; n>0; n--) free(dir[n]);
}
free(dir);
return ret;
}
示例7: scandir
int SDLEngine::Run(void*)
{
std::cout << "input folder: " << input_folder << std::endl;
bool gray = false;
fcount = scandir(input_folder.c_str(), &filelist, ppm_select, alphasort);
if (fcount <= 0)
{
fcount = scandir(input_folder.c_str(), &filelist, pgm_select, alphasort);
gray = true;
}
if (fcount <= 0)
{
std::cout << "There are no .ppm or .pgm files in this folder! Maybe you have to convert the images first e.g. using" << std::endl
<< " mogrify -format ppm *.jpg" << std::endl;
return 0;
}
std::cout << "found " << fcount << " files" << std::endl;
char filename[255];
sprintf(filename, "%s/init.txt", input_folder.c_str());
std::ifstream aStream(filename);
if(!aStream || aStream.eof())
{
std::cout << "please create the file \"" << filename << "\" specifying the initial bounding box (x1,y1,x2,y2)" << std::endl;
return 0;
}
char line[255];
int x1,y1,x2,y2,imgid, width,height;
std::vector<ObjectBox> boxes;
while(aStream.getline(line,255))
{
x1 = y1 = x2 = y2 = imgid = 0;
int i = 0;
for(;line[i] >= '0' && line[i] <= '9'; i++)
x1 = x1*10 + (line[i] - '0');
for(i++;line[i] >= '0' && line[i] <= '9'; i++)
y1 = y1*10 + (line[i] - '0');
for(i++;line[i] >= '0' && line[i] <= '9'; i++)
x2 = x2*10 + (line[i] - '0');
for(i++;line[i] >= '0' && line[i] <= '9'; i++)
y2 = y2*10 + (line[i] - '0');
if(line[i] == ',')
for(i++;line[i] >= '0' && line[i] <= '9'; i++)
imgid = imgid*10 + (line[i] - '0');
ObjectBox b = {(float)x1,(float)y1,(float)(x2-x1),(float)(y2-y1),imgid};
boxes.push_back(b);
}
aStream.close();
std::cout << "output folder: " << output_folder << std::endl;
DIR * dir = opendir(output_folder.c_str());
if (dir == 0)
{
std::cout << "\tdoes not exist -> try to create it" << std::endl;
if(system(("mkdir "+output_folder).c_str()))
{
std::cout << "\t failed to create directory" << std::endl;
return 0;
}
}
closedir(dir);
sprintf(filename, "%s/%s", input_folder.c_str(), filelist[0]->d_name);
int z;
unsigned char* dummy = gray ? readFromPGM<unsigned char>(filename, width, height) :
readFromPPM<unsigned char>(filename, width, height, z);
delete[] dummy;
// Initialize MultiObjectTLD
MOTLDSettings s(gray ? COLOR_MODE_GRAY : COLOR_MODE_RGB);
// s.bbMin = 18;
MultiObjectTLD p(width, height, s);
std::vector<ObjectBox> addBoxes;
std::vector<ObjectBox>::iterator boxIt = boxes.begin();
if(ivScreen != NULL)
SDL_FreeSurface(ivScreen);
ivScreen = SDL_SetVideoMode( width, height, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); // | SDL_RESIZABLE
SDL_WM_SetCaption("MultiObjectTLD", 0 );
sprintf(filename, "%s/output.txt", output_folder.c_str());
std::ofstream outStream(filename);
for (int i=0; i < fcount && (!MAX_FILE_NUMBER || i<MAX_FILE_NUMBER); ++i)
{
// first load the image
sprintf(filename, "%s/%s", input_folder.c_str(), filelist[i]->d_name);
int xS, yS, z;
unsigned char* img = gray ? readFromPGM<unsigned char>(filename, xS, yS) :
readFromPPM<unsigned char>(filename, xS, yS, z);
// then process it with MultiObjectTLD
p.processFrame(img);
while(boxIt != boxes.end() && boxIt->objectId == i)
{
addBoxes.push_back(*boxIt);
boxIt++;
}
if(addBoxes.size() > 0){
p.addObjects(addBoxes);
addBoxes.clear();
//.........这里部分代码省略.........
示例8: load_plugin
int load_plugin(const char *dir, const char* plugname) {
struct stat statbuf;
struct dirent **namelist;
int n, ret;
char *full_path, full_name[264];
DIR *plugindir;
ret = 0;
full_path = NULL;
plugin_list = NULL;
if (strlen(plugname) > 265) {
fprintf(stderr, " Error - Plugin name exceeds maximum length of 256 charakters: %s\n", plugname);
return(-1);
}
/* plugin directory must be configured */
if (!dir) {
fprintf(stderr, " Error - Plugin directory not set while trying to load plugin %s.\n", plugname);
exit(EXIT_FAILURE);
}
if ((plugindir = opendir(dir)) == NULL) {
fprintf(stderr, " Error - Unable to open plugin directory: %m.\n");
exit(EXIT_FAILURE);
}
DEBUG_FPRINTF(stdout, " Looking for plugin %s in %s\n", plugname, dir);
if ((n = scandir(dir, &namelist, 0, alphasort)) < 0) {
fprintf(stderr, " Error - Unable to scan plugin directory: %m.\n");
return(-1);
} else while(n--) {
stat(namelist[n]->d_name, &statbuf);
/* assemble full name */
memset(full_name, 0, 264);
strncpy(full_name, "htm_", 4);
strncpy(&full_name[4], plugname, strlen(plugname));
strncpy(&full_name[4+strlen(plugname)], ".so", 3);
if ((ret = fnmatch(full_name, namelist[n]->d_name, 0)) == 0) {
/* found the plugin */
if ((full_path = (char *) malloc(strlen(dir) + strlen(namelist[n]->d_name) + 2)) == NULL) {
perror(" Error - Unable to allocate memory");
exit(EXIT_FAILURE);
}
snprintf(full_path, strlen(dir)+strlen(namelist[n]->d_name)+2, "%s/%s", dir, namelist[n]->d_name);
DEBUG_FPRINTF(stdout, " Plugin found: %s\n", full_path);
config_plugin(full_path);
free(full_path);
free(namelist[n]);
break;
}
free(namelist[n]);
}
closedir(plugindir);
if (ret != 0) {
fprintf(stderr, " Error - Unable to load plugin %s: %m.\n", full_name);
exit(EXIT_FAILURE);
}
free(namelist);
return(1);
}
示例9: throw
Node::Node(string name) throw (myExceptions){
int shmid, datagramport = -1, streamport, identifier, count;
char folder[100];
struct dirent **files;
struct data *d;
myExceptions e;
if ((shmid = shmget(KEY, 200, 0666)) == -1) {
shmid = shmget(KEY, 200, 0666 | IPC_CREAT);
if ((d = (struct data*)shmat(shmid, 0, 0)) == NULL) {
perror("attach");
e.setmsg("Error in shared memory attachment");
throw e;
}
d->portlist[0][0] = 10000; d->portlist[0][1] = 1;
d->portlist[1][0] = 10002; d->portlist[1][1] = 0;
d->portlist[2][0] = 10004; d->portlist[2][1] = 0;
d->portlist[3][0] = 10006; d->portlist[3][1] = 0;
d->portlist[4][0] = 10008; d->portlist[4][1] = 0;
d->portlist[5][0] = 10010; d->portlist[5][1] = 0;
d->portlist[6][0] = 10012; d->portlist[6][1] = 0;
d->portlist[7][0] = 10014; d->portlist[7][1] = 0;
d->portlist[8][0] = 10016; d->portlist[8][1] = 0;
d->portlist[9][0] = 10018; d->portlist[9][1] = 0;
d->port = datagramport = 10000;
strcpy(d->name, (char*)name.c_str());
}
else {
if ((d = (struct data*)shmat(shmid, 0, 0)) == NULL) {
perror("attach");
e.setmsg("Error in shared memory attachment");
throw e;
}
for (int i = 0; i < 10; ++i) {
if (d->portlist[i][1] == 0) {
datagramport = d->portlist[i][0];
d->portlist[i][1] = 1;
break;
}
}
}
streamport = datagramport + 1;
identifier = dohash(datagramport);
enode = address(dohash(d->port), d->port, d->name);
node = address(identifier, datagramport, name);
datagram = Connect(DATAGRAM, datagramport);
stream = Connect(STREAM, streamport);
sprintf(folder, "/Users/Sabyasachee/%d", datagramport);
strcat(folder, "shared");
if (access(folder, F_OK) != 0) {
if (mkdir(folder, 0777) == -1) {
perror("mkdir");
e.setmsg("Error in creating directory");
throw e;
}
}
count = scandir(folder, &files, file_select, alphasort);
for (int i = 0; i < count; ++i) {
filelist.push_back(files[i]->d_name);
}
state = NEW;
shmdt(d);
}
示例10: getComList
int getComList(char *ttylist)
{
int retval = 0;
struct dirent **namelist;
struct serial_struct serinfo;
char devicedir[1024];
char devfile[256];
char driver[64];
int n = 0, nn, fd;
ttylist[0] = '\0';
if ((nn = scandir(SYSDIR, &namelist, NULL, NULL)) > 0)
{
while (n < nn)
{
if (strcmp(namelist[n]->d_name,"..") && strcmp(namelist[n]->d_name,"."))
{
// Construct full absolute file path
sprintf(devicedir, "%s%s", SYSDIR, namelist[n]->d_name);
// Get the device driver
get_driver(devicedir, driver);
if (strlen(driver) > 0)
{
// Non empty drivers might be ok
//printf("Device: /dev/%s, Driver: %s\n", namelist[n]->d_name, driver);
sprintf(devfile, "/dev/%s", namelist[n]->d_name);
if (strstr(driver, "8250") != NULL)
{
// Check serial8250-devices separeately
if ((fd = open(devfile, O_RDWR | O_NONBLOCK | O_NOCTTY)) >= 0)
{
// If device open
if (ioctl(fd, TIOCGSERIAL, &serinfo) == 0)
{
// If can get get serial_info
if (serinfo.type != PORT_UNKNOWN)
{
// If device type is no PORT_UNKNOWN we accept the port
//printf("Device 8250 has port, accepted\n");
strcat(ttylist, "|");
strcat(ttylist, devfile);
}
}
close(fd);
}
}
else
{
// whatever has a driver and is not serial8250 is sure good
strcat(ttylist, "|");
strcat(ttylist, devfile);
}
}
}
free(namelist[n]);
n++;
}
free(namelist);
}
return (retval);
}
示例11: include_file
/* include a file or directory */
void include_file(YYLTYPE *yylloc, const char *name) {
struct stat st;
struct dirent **namelist;
char *fn;
int n;
struct sourceposition *pos;
pos = make_sourcepos(yylloc);
if (stat(name, &st)) {
if (errno == ENOENT &&
(index(name, '*') != NULL || index(name, '?') != NULL ||
index(name, '[') != NULL)) {
/* Globbing fiesta! */
glob_t glob_buf;
if (glob(name, 0, NULL, &glob_buf) != 0) {
filter_error(pos, "failed to glob \"%s\": %s", name, strerror(errno));
} else {
/* We go through the list of files backwards, because
* step_into_include_file() creates a stack of all the
* files processed and then works on them in a LIFO
* fashion -- which would make all of our rules files
* go backwards. Since I can't wrap my head around
* why that is, exactly, I'm hacking it up with
* this instead. Fixination appreciated.
*/
for (n = glob_buf.gl_pathc - 1; n >= 0; n--) {
if (stat(glob_buf.gl_pathv[n], &st)) {
filter_error(pos, "stat failed on globbed \"%s\": %s",
glob_buf.gl_pathv[n], strerror(errno));
} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
if (sourcefile_push(pos, glob_buf.gl_pathv[n])) {
filtergen_in = current_srcfile->f;
filtergen_lineno = current_srcfile->lineno;
// yycolumn = current_srcfile->column;
if (!filtergen_in) {
filter_error(pos, "failed to open %s", glob_buf.gl_pathv[n]);
} else {
filtergen_push_buffer_state(
filtergen__create_buffer(filtergen_in, YY_BUF_SIZE));
}
}
}
}
}
globfree(&glob_buf);
} else {
filter_error(pos, "stat failed on \"%s\": %s", name, strerror(errno));
}
} else {
if (S_ISDIR(st.st_mode)) {
char *b = strdup(name);
char *base = basename(b);
if (strcmp("/", base) == 0) {
filter_error(pos, "cannot include / path; skipping");
free(b);
return;
}
free(b);
if ((n = scandir(name, &namelist, NULL, alphasort)) < 0) {
filter_error(pos, "scandir failed on \"%s\": %s", name,
strerror(errno));
} else {
while (n--) {
/* FIXME: assumes d_name */
if (namelist[n]->d_name[0] == '.') {
free(namelist[n]);
continue;
}
if (asprintf(&fn, "%s/%s", name, namelist[n]->d_name) < 0) {
filter_error(
NULL, "internal error: asprintf failed constructing pathname "
"for included file \"%s\"",
namelist[n]->d_name);
free(fn);
} else {
include_file(yylloc, fn);
free(fn);
}
free(namelist[n]);
}
free(namelist);
}
} else {
if (sourcefile_push(pos, name)) {
filtergen_in = current_srcfile->f;
filtergen_lineno = current_srcfile->lineno;
// yycolumn = current_srcfile->column;
if (!filtergen_in) {
filter_error(pos, "failed to open %s", name);
} else {
filtergen_push_buffer_state(
filtergen__create_buffer(filtergen_in, YY_BUF_SIZE));
}
}
//.........这里部分代码省略.........
示例12: main
int main(
int argc,
char **argv
)
#endif
{
int fd;
int i;
int status;
off_t off;
struct dirent *d_not;
struct dirent **namelist;
struct stat s;
printf( "\n\n*** READDIR TEST ***\n" );
printf( "\nchdir to the root directory\n" );
status = chdir( "/" );
printf( "chdir() status : %d\n\n", status );
printf( "\nCreating a series of directories under /\n" );
i=0;
while ( strcmp(dnames[i], "END") != 0 )
{
status = mkdir( dnames[i], 0x1c0 );
printf("Creating directory: %s %d %d ", dnames[i], status, errno );
if ( errno == 0 )
printf(" Success\n");
else
printf(" Failure\n");
i++;
}
/*
* Create files under many and open the directory.
*/
printf("Create a lot of files\n");
status = mkdir( "/many", 0x1c0 );
status = chdir( "/many" );
for (i = 0; i<44; i++) {
printf("Create %s\n", many_files[i]);
fd = open (many_files[i], O_CREAT, S_IRWXU);
close (fd);
}
printf("Open /many and print the directory\n");
directory_not = opendir( "/many" );
printdir ( directory_not );
d_not = readdir( directory_not );
printf("open /b/myfile\n");
fd = open ("/b/my_file", O_CREAT, S_IRWXU);
rtems_test_assert( fd != -1 );
close (fd);
printf("scandir a file status: ");
status = scandir(
"/b/my_file",
&namelist,
select1,
NULL
);
printf("%d\n", status);
printf("Open /b/new_file\n");
fd = open( "/b/new_file", O_CREAT, S_IRWXU );
rtems_test_assert( fd != -1 );
printf("fcntl F_SETFD should return 0\n");
status = fcntl( fd, F_SETFD, 1 );
rtems_test_assert( status == 0 );
printf("fcntl F_SETFD should return 1\n");
status = fcntl( fd, F_GETFD, 1 );
rtems_test_assert( status == 1 );
#if 0
printf("fcntl F_DUPFD should return 0\n");
status = fcntl( fd, F_DUPFD, 0 );
rtems_test_assert ( status == 0 );
#else
printf("fcntl F_DUPFD should return 0 -- skip until implemented\n");
#endif
printf("fcntl F_GETFL returns current flags\n");
status = fcntl( fd, F_GETFL, 1 );
printf("fcntl F_GETFL returned 0x%x\n", status );
rtems_test_assert( status != -1 );
printf("fcntl F_SETFL to add O_APPEND and O_NONBLOCK\n");
status = fcntl( fd, F_SETFL, O_APPEND|O_NONBLOCK );
rtems_test_assert ( status != -1 );
printf("fcntl F_GETFL return current flags to see changes\n");
status = fcntl( fd, F_GETFL, 1 );
printf("fcntl F_GETFL returned 0x%x\n", status );
rtems_test_assert( status != -1 );
//.........这里部分代码省略.........
示例13: cgiMain
//.........这里部分代码省略.........
!= cgiFormSuccess )
int_error("Error retrieving CGI form enable end date.");
if ( cgiFormString("rev_endtime", rev_endtime, sizeof(rev_endtime))
!= cgiFormSuccess )
int_error("Error retrieving CGI form enable end time.");
strncat(rev_startstr, rev_startdate, sizeof(rev_startstr)-1);
strncat(rev_startstr, " ", 1); /* add a space between date and time */
strncat(rev_startstr, rev_starttime, sizeof(rev_startstr)-strlen(rev_startstr)-1);
strncat(rev_endstr, rev_enddate, sizeof(rev_endstr)-1);
strncat(rev_endstr, " ", 1); /* add a space between date and time */
strncat(rev_endstr, rev_endtime, sizeof(rev_endstr)-strlen(rev_endstr)-1);
snprintf(title, sizeof(title), "Search Revoked Certificates");
snprintf(subtitle, sizeof(subtitle), "Certificates revoked between %s and %s", rev_startstr, rev_endstr);
}
else if (strcmp(search, "ser") == 0) {
if ( cgiFormString("startserial", startserstr, sizeof(startserstr))
!= cgiFormSuccess )
int_error("Error retrieving CGI form start serial value.");
if ( cgiFormString("endserial", endserstr, sizeof(endserstr))
!= cgiFormSuccess )
int_error("Error retrieving CGI form end serial value.");
snprintf(title, sizeof(title), "Search Certs by Serial Number");
snprintf(subtitle, sizeof(subtitle), "Certificates with serial number between %s and %s", startserstr, endserstr);
}
else int_error("Error CGI form retrieving a valid search type.");
}
/* -------------------------------------------------------------------------- *
* We got CGI arguments, first we get a list of .pem files from the cert dir *
* ---------------------------------------------------------------------------*/
certcounter = scandir(CACERTSTORE, &certstore_files, file_select, hexsort);
// It can happen that our search does not return any certs. This is not an error.
//if(certcounter<=0) int_error("Error: No certificate files found.");
/* -------------------------------------------------------------------------- *
* calculate how many pages we get with MAXCERTDISPLAY *
* ---------------------------------------------------------------------------*/
if(certcounter<=MAXCERTDISPLAY) pagecounter = 1;
else {
disp_calc = div(certcounter, MAXCERTDISPLAY);
/* if the count of certs divided by MAXCERTDISPLAY has no remainder */
if(disp_calc.rem == 0) pagecounter = disp_calc.quot;
/* with a remainder, we must prepare an extra page for the rest */
else pagecounter = disp_calc.quot +1;
}
/* -------------------------------------------------------------------------- *
* Check if we have been subsequently called with a pagenumber & sort request *
* ---------------------------------------------------------------------------*/
if(cgiFormInteger("page", &pagenumber, 1) == cgiFormSuccess)
if(pagenumber > pagecounter || pagenumber <=0)
int_error("Error: Page does not exist.");
if(cgiFormString("sort", sorting, sizeof(sorting)) != cgiFormSuccess)
strncpy(sorting, "desc", sizeof(sorting));
/* -------------------------------------------------------------------------- *
* now we know how many certs we have in total and we can build the page(s). *
* For every MAXCERTDISPLAY certs we start a new page and cycle through by *
* calling ourself with the requested certs in range. *
* ---------------------------------------------------------------------------*/
示例14: destroy_instance_backing
int destroy_instance_backing (ncInstance * instance, int do_destroy_files)
{
int ret = OK;
int total_prereqs = 0;
char path [MAX_PATH];
virtualMachine * vm = &(instance->params);
// find and detach iSCSI targets, if any
for (int i=0; i<EUCA_MAX_VBRS && i<vm->virtualBootRecordLen; i++) {
virtualBootRecord * vbr = &(vm->virtualBootRecord[i]);
if (vbr->locationType==NC_LOCATION_IQN) {
if (disconnect_iscsi_target (vbr->resourceLocation)) {
logprintfl(EUCAERROR, "[%s] error: failed to disconnect iSCSI target attached to %s\n", instance->instanceId, vbr->backingPath);
}
}
}
// see if instance directory is there (sometimes startup fails before it is created)
set_path (path, sizeof (path), instance, NULL);
if (check_path (path))
return ret;
// to ensure that we are able to delete all blobs, we chown files back to 'eucalyptus'
// (e.g., libvirt on KVM on Maverick chowns them to libvirt-qemu while
// VM is running and then chowns them to root after termination)
set_path (path, sizeof (path), instance, "*");
if (diskutil_ch (path, EUCALYPTUS_ADMIN, NULL, BACKING_FILE_PERM)) {
logprintfl (EUCAWARN, "[%s] error: failed to chown files before cleanup\n", instance->instanceId);
}
if (do_destroy_files) {
char work_regex [1024]; // {userId}/{instanceId}/.*
set_id2 (instance, "/.*", work_regex, sizeof (work_regex));
if (blobstore_delete_regex (work_bs, work_regex) == -1) {
logprintfl (EUCAERROR, "[%s] error: failed to remove some artifacts in %s\n", instance->instanceId, path);
}
// remove the known leftover files
unlink (instance->xmlFilePath);
unlink (instance->libvirtFilePath);
unlink (instance->consoleFilePath);
if (strlen (instance->floppyFilePath)) {
unlink (instance->floppyFilePath);
}
set_path (path, sizeof (path), instance, "instance.checkpoint");
unlink (path);
for (int i=0; i < EUCA_MAX_VOLUMES; ++i) {
ncVolume * volume = &instance->volumes[i];
snprintf (path, sizeof (path), EUCALYPTUS_VOLUME_XML_PATH_FORMAT, instance->instancePath, volume->volumeId);
unlink (path);
}
// bundle instance will leave additional files
// let's delete every file in the directory
struct dirent **files;
int n = scandir(instance->instancePath, &files, 0, alphasort);
char toDelete[MAX_PATH];
if (n>0){
while (n--) {
struct dirent *entry = files[n];
if( entry !=NULL && strncmp(entry->d_name, ".",1)!=0 && strncmp(entry->d_name, "..", 2)!=0){
snprintf(toDelete, MAX_PATH, "%s/%s", instance->instancePath, entry->d_name);
unlink(toDelete);
free(entry);
}
}
free(files);
}
}
// Finally try to remove the directory.
// If either the user or our code introduced
// any new files, this last step will fail.
set_path (path, sizeof (path), instance, NULL);
if (rmdir (path) && do_destroy_files) {
logprintfl (EUCAWARN, "[%s] warning: failed to remove backing directory %s\n", instance->instanceId, path);
}
return ret;
}
示例15: get_number_of_log_files_hdl
/**
* Return number of log files in a dir and the name of the oldest file.
* @param indata, see gnolfh_in_t
* @param outdata, char *oldest_file
* @param max_outsize, Max size for oldest_file string
*
* @return int, number of logfiles or -1 if error
*/
int get_number_of_log_files_hdl(void *indata, void *outdata, size_t max_outsize)
{
struct dirent **namelist;
int n, old_date = -1, old_time = -1, old_ind = -1, files, i, failed = 0;
char path[PATH_MAX];
gnolfh_in_t *params_in;
char *oldest_file;
int rc = 0;
TRACE_ENTER();
params_in = (gnolfh_in_t *) indata;
oldest_file = (char *) outdata;
/* Initialize the filter */
n = snprintf(file_prefix, SA_MAX_NAME_LENGTH, "%s", params_in->file_name);
if (n >= SA_MAX_NAME_LENGTH) {
rc = -1;
LOG_WA("file_prefix > SA_MAX_NAME_LENGTH");
goto done_exit;
}
n = snprintf(path, PATH_MAX, "%s/%s",
params_in->logsv_root_dir, params_in->pathName);
if (n >= PATH_MAX) {
LOG_WA("path > PATH_MAX");
rc = -1;
goto done_exit;
}
files = n = scandir(path, &namelist, filter_func, alphasort);
if (n == -1 && errno == ENOENT) {
rc = 0;
goto done_exit;
}
if (n < 0) {
LOG_WA("scandir:%s - %s", strerror(errno), path);
rc = -1;
goto done_exit;
}
if (n == 0) {
rc = files;
goto done_exit;
}
while (n--) {
TRACE_3("%s", namelist[n]->d_name);
if (check_oldest(namelist[n]->d_name, params_in->file_name,
strlen(params_in->file_name), &old_date, &old_time)) {
old_ind = n;
} else {
failed++; /* wrong format */
}
}
if (old_ind != -1) {
TRACE_1("oldest: %s", namelist[old_ind]->d_name);
n = snprintf(oldest_file, max_outsize, "%s/%s",
path, namelist[old_ind]->d_name);
if (n >= max_outsize) {
LOG_WA("oldest_file > max_outsize");
rc = -1;
goto done_free;
} else {
rc = (files - failed);
}
} else {
TRACE("Only file/files with wrong format found");
}
done_free:
/* Free scandir allocated memory */
for (i = 0; i < files; i++)
free(namelist[i]);
free(namelist);
done_exit:
TRACE_LEAVE();
return rc;
}