int main() { char file[MAXPATH]; if(searchfile(file, "autoexec", "bat", "D:\\test;D:\\tmp;.;..;C:\\")) { fprintf(stderr, "file found: %s\n", file); } getchar(); char *path = getenv("PATH"); if(searchfile(file, "autoexec", "bat", path)) { fprintf(stderr, "file found: %s\n", file); } getchar(); return 0; }
void recurse(const char *path, const unsigned char *value, const unsigned char *mask, int len) { struct stat s; if(stat(path, &s)) { perror("stat"); return; } if(!S_ISDIR(s.st_mode)) { int fd = open(path, O_RDONLY); if(fd >= 0) { searchfile(path, fd, value, mask, len); close(fd); } else perror(path); return; } DIR *dir = opendir(path); if(!dir) { perror(path); exit(3); } struct dirent *d; while ((d = readdir(dir))) { if (!(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))) continue; char newpath[strlen(path) + strlen(d->d_name) + 1]; strcpy(newpath, path); strcat(newpath, "/"); strcat(newpath, d->d_name); recurse(newpath, value, mask, len); } closedir(dir); }
void B9Edit::importSlicesFromSvg(QString file, double pixelsizemicrons) { int layers = 0; double xsizemm = 0.0; double ysizemm = 0.0; double x = 0; double y = 0; bool inverted = false; QSvgRenderer SvgRender; if(!SvgRender.load(file)) { QMessageBox msgBox; msgBox.setText("Unable to import SVG file."); msgBox.exec(); return; } if(!SvgRender.elementExists("layer0")) { QMessageBox msgBox; msgBox.setText("SVG file does not contain compatible layer information\nUnable to import."); msgBox.exec(); return; } //do a quick search for the word "slic3r:type="contour"" and then the following "style="fill:" //to figure out whether the image is inverted or not. QString buff = ""; QFile searchfile(file); searchfile.open(QIODevice::ReadOnly); QTextStream searchstream(&searchfile); while(buff != "slic3r:type=\"contour\"" && !searchstream.atEnd()) { searchstream >> buff; } if(!searchstream.atEnd())//we must have found it. { while(buff != "style=\"fill:" && !searchstream.atEnd()) { searchstream >> buff; } if(!searchstream.atEnd()) { searchstream >> buff; if(buff == "white\"") { inverted = false; } else { inverted = true; } } else {
int main(int argc, char **argv) { unsigned char value[0x100], mask[0x100]; short len = 0; short e = 0; char *h = NULL; if(argc < 2) { fprintf(stderr, "usage: %s <hex> [<path> [...]]\n", *argv); e = 1; } else { h = argv[1]; int v0, v1; while(e == 0 && *h && len < 0x100) { if(*h != '.') { while(*h == ' ') h++; if((v0 = ascii2hex(h++)) > -1) { while(*h == ' ') h++; if((v1 = ascii2hex(h++)) > -1) { value[len] = (v0 << 4) | v1; mask[len++] = 0xFF; } else e = 2; } else e = 2; } else { value[len] = mask[len] = 0; len++; h += 1; } } } if(e == 0) { if (argc < 3) searchfile("stdin", 0, value, mask, len); else { int c = 2; while(c < argc) recurse(argv[c++], value, mask, len); } } else { if(!len) e = 2; else if(len % 2 == 1) e = 3; else if(*h) e = 4; fprintf(stderr, "ERROR: %s\n", errors[e-1]); } return e; }
void processcommand( char *command, char *P1, char *P2, char *P3 ) { if( strcmp(command, "createvfs") == 0 ) { int size = atoi(P2); if( (0 == strcmp(P1,"")) || 0 == P2 ) { printf("createvfs_FAILURE %s \n",ERR_VFS_CREATE_00); } else { createvfs (P1,size); } } else if( strcmp(command, "mountvfs") == 0 ) { if( (0 == strcmp(P1,"")) ) { printf("mountvfs_FAILURE %s \n",ERR_VFS_MOUNT_05); } else { if( 1 == ui_mountFlag ) { printf("mountvfs_FAILURE %s \n",ERR_VFS_MOUNT_04); } else { mountvfs (P1); } } } else if( strcmp(command, "unmountvfs") == 0 ) { if( (0 == strcmp(P1,"")) ) { printf("unmountvfs_FAILURE %s \n",ERR_VFS_UNMOUNT_00); } else { if( 0 == ui_mountFlag ) { printf("unmountvfs_FAILURE %s \n",ERR_VFS_UNMOUNT_04); } else { unmountvfs (P1); } } } else if( strcmp(command, "makedir") == 0 ) { if( (0 == strcmp(P1,"")) || (0 == strcmp(P2,"")) ) { printf("makedir_FAILURE %s \n",ERR_VFS_MAKEDIR_00); } else { if( 0 == ui_mountFlag ) { printf("makedir_FAILURE %s \n",ERR_VFS_MAKEDIR_05); } else { makedir (P1,P2); } } } else if( strcmp(command, "deletedir") == 0 ) deletedir (P1); else if( strcmp(command, "movedir") == 0 ) movedir (P1,P2); else if( strcmp(command, "listdir") == 0 ) { int flag = atoi(P2); listdir (P1,flag,P3); } else if( strcmp(command, "addfile") == 0 ) addfile (P1,P2,P3); else if( strcmp(command, "listfile") == 0 ) listfile (P1,P2); else if( strcmp(command, "updatefile") == 0 ) updatefile (P1,P2); else if( strcmp(command, "removefile") == 0 ) removefile (P1); else if( strcmp(command, "movefile") == 0 ) movefile (P1,P2); else if( strcmp(command, "copyfile") == 0 ) copyfile (P1,P2); else if( strcmp(command, "exportfile") == 0 ) exportfile (P1,P2); else if( strcmp(command, "searchfile") == 0 ) searchfile (P1,P2); else printf("Ignoring invalid command %s\n", command); }
// Execute cmd. Never returns. void runcmd(struct cmd *cmd) { int p[2], r; struct execcmd *ecmd; struct pipecmd *pcmd; struct redircmd *rcmd; if(cmd == 0) exit(0); switch(cmd->type) { default: fprintf(stderr, "unknown runcmd\n"); exit(-1); case ' ': ecmd = (struct execcmd*)cmd; if(ecmd->argv[0] == 0) exit(0); char abscmd[CMD_MAXLEN]; if(NULL == searchfile(abscmd, CMD_MAXLEN, ecmd->argv[0], F_OK|R_OK|X_OK)) { fprintf(stderr, "file not exist or permission denied\n"); exit(-1); } if(-1 == execv(abscmd, ecmd->argv) ) { fprintf(stderr, "exec failed\n"); } break; case '>': case '<': rcmd = (struct redircmd*)cmd; char absfile[CMD_MAXLEN]; memset(absfile, 0, sizeof(absfile)); int newfd; if(rcmd->fd == 0) { if(-1 == access(rcmd->file, F_OK|R_OK)) { newfd= open(rcmd->file, rcmd->mode); } else { if(NULL == searchfile(absfile, sizeof(absfile), rcmd->file, F_OK|R_OK)) { die("redirction file not exist or permission denied"); } else { newfd= open(absfile, rcmd->mode); } } } else { if('/' == (rcmd->file)[0]) { newfd= open(rcmd->file, rcmd->mode); } else { char* pwd= getenv("PWD"); strcat(absfile, pwd); strncat(absfile, "/", 1); strncat(absfile, rcmd->file, sizeof(rcmd->file)); newfd= open(absfile, rcmd->mode, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); } } int bakfd= dup(rcmd->fd); dup2(newfd, rcmd->fd); close(newfd); runcmd(rcmd->cmd); dup2(bakfd, rcmd->fd); break; case '|': pcmd = (struct pipecmd*)cmd; int pipes[2]; if(-1 == pipe(pipes)) { die("pipe"); } int pid= fork(); if(-1 == pid) { die("fork"); } if(0 == pid) { // left cmd dup2(pipes[1], STDOUT_FILENO); close(pipes[0]); runcmd(pcmd->left); } else { // right cmd dup2(pipes[0], STDIN_FILENO); close(pipes[1]); waitpid(pid); runcmd(pcmd->right); } break; } exit(0); }