/* ---------------- get_n_att --------------------------------- * Keep reading the file and find the number of attributes in the * classification. * There is a call to find_regex() below. This used to print an * error when it did not find anything. Unfortunately, it the * unified code, this may not be an error. We may be reading a * file without sequence information. */ static size_t get_n_att (FILE *fp, char *buf, const int bufsiz) { size_t n_att = 1; const char *this_sub = "get_n_att"; const char *parse_fail = "Failed at %s:%d\n"; const char *look_for = "Looking for regex \"%s\"\n"; const char *heading = "num description "; const char *magic_line = "[0-9]+ aa *[0-9] +[0-9].[0-9]+"; if ( ! find_line (buf, bufsiz, fp, heading)) { err_printf (this_sub, parse_fail, __FILE__, __LINE__); err_printf (this_sub, look_for, heading); return 0; } if ( ! find_regex (buf, bufsiz, fp, magic_line, 20)) { # ifdef want_parse_error err_printf (this_sub, parse_fail, __FILE__, __LINE__); err_printf (this_sub, look_for, magic_line); # endif /* want_parse_error */ return 0; } while ( find_regex (buf, bufsiz, fp, magic_line, 1)) n_att++; return n_att; }
/* ---------------- get_n_class ------------------------------- * Read the classification output and return the number of * classes. Returning zero means we found an error. */ static size_t get_n_class (FILE *fp, char *buf, const int bufsiz) { char *s; long int l; const char *this_sub = "get_n_class"; const char *class_str = "[0-9]* POPULATED CLASSE"; if ((s = find_regex ( buf, bufsiz, fp, class_str, 0)) == NULL) { err_printf (this_sub, "Failed to find number of classes\n"); return 0; } l = strtol (s, NULL, 0); return (size_t) l; }
// // name: inet_prio // @param // @return prio int iet_prio(const char *dev, char * args) { char preferredip_buff[255] = ""; char *preferredip = &preferredip_buff[0]; // Phase 1 : checks. If anyone fails, return prio 0. // check if args exists if (!args) { dc_log(0, "need prio_args with preferredip set"); return 0; } // check if args format is OK if (sscanf(args, "preferredip=%s", preferredip) ==1) {} else { dc_log(0, "unexpected prio_args format"); return 0; } // check if ip is not too short if (strlen(preferredip) <= 7) { dc_log(0, "prio args: preferredip too short"); return 0; } // Phase 2 : find device in /dev/disk/by-path to match device/ip DIR *dir_p; struct dirent *dir_entry_p; enum { BUFFERSIZE = 1024 }; char buffer[BUFFERSIZE]; char fullpath[BUFFERSIZE] = "/dev/disk/by-path/"; dir_p = opendir(fullpath); // loop to find device in /dev/disk/by-path while( NULL != (dir_entry_p = readdir(dir_p))) { if (dir_entry_p->d_name[0] != '.') { char path[BUFFERSIZE] = "/dev/disk/by-path/"; strcat(path,dir_entry_p->d_name); ssize_t nchars = readlink(path, buffer, sizeof(buffer)-1); if (nchars != -1) { char *device; buffer[nchars] = '\0'; device = find_regex(buffer,"(sd[a-z]+)"); // if device parsed is the right one if (device!=NULL && strncmp(device, dev, strlen(device)) == 0) { char *ip; ip = find_regex(dir_entry_p->d_name,"([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})"); // if prefferedip and ip fetched matches if (ip!=NULL && strncmp(ip, preferredip, strlen(ip)) == 0) { // high prio free(ip); free(device); closedir(dir_p); return 20; } free(ip); } free(device); } else { printf("error\n"); } } } // nothing found, low prio closedir(dir_p); return 10; }