int is_dir(char *s) { int i; i = 0; if (s[i] != '%') return (0); i++; if (s[i] != ':' && is_all_num(&s[i]) != 1) return (0); else if (is_all_num(&s[i]) == 1) return (1); if (s[i] == ':') while (s[++i] != '\0') { if (in_str(s[i], LABEL_CHARS) != 1) { while (s[i] == ' ' || s[i] == '\t') i++; if (s[i] == '\0' || in_str(s[i], COMMENT_CHAR)) return (1); return (0); } } else return (0); return (1); }
int is_ind(char *s) { int i; i = 0; if (is_all_num(&s[0]) == 1) return (1); if (s[i] == ':') { while (s[++i] != '\0') { if (in_str(s[i], LABEL_CHARS) != 1) { while (s[i] == ' ' || s[i] == '\t') i++; if (s[i] == '\0' || in_str(s[i], COMMENT_CHAR)) return (1); return (0); } if (s[i + 1] == '\0') return (1); } } return (0); }
JNIEXPORT jint JNICALL Java_com_baidu_video_download_JNIP2P_netStatReport( JNIEnv* env, jobject obj, jstring strProduct, jstring strVersion, jstring strSubstat, jstring strChannel, jstring strBody) { char* szProduct = (char*)env->GetStringUTFChars(strProduct, false); char* szVersion = (char*)env->GetStringUTFChars(strVersion, false); char* szSubstat = (char*)env->GetStringUTFChars(strSubstat, false); char* szChannel = (char*)env->GetStringUTFChars(strChannel, false); if(szProduct==NULL || szVersion==NULL || szSubstat==NULL || szChannel==NULL) return ERROR_PARAM; char* szBody = (char*)env->GetStringUTFChars(strBody, false); std::string strTemp = szBody; std::vector<std::string> vecOut; SplitParams(strTemp, ",", vecOut); if(vecOut.size()==0) return ERROR_PARAM; CP2PStatReport rp(szProduct, szVersion, szChannel, szSubstat); for(size_t i=0; i<vecOut.size(); i++) { std::vector<std::string> vecItem; SplitParams(vecOut[i], "=", vecItem); if(vecItem.size()!=2) return ERROR_PARAM; if(is_all_num(vecItem[0])) { uint64_t nKey = _atoi64(vecItem[0].c_str()); if(is_all_num(vecItem[1])) { rp.StatAdd(nKey, _atoi64(vecItem[1].c_str())); } else { rp.StatAdd(nKey, (unsigned char*)vecItem[1].c_str(), vecItem[1].length()); } } else { continue; } } return ERROR_SUCCESS; }
int is_reg(char *s) { if (s[0] == 'r' && is_all_num(&s[1]) && ft_atoi(&s[1]) <= REG_NUMBER) return (1); return (0); }
int main(void) { DIR *dir; struct dirent *diritem; FILE *stream; struct utmp *user; int count = 0; int c; int n; proc_t **proc_stat_table = NULL; if (!(dir = opendir(PROC_PATH))) { perror("can not open /proc"); exit(1); } while (diritem = readdir(dir)) { char *dirname = malloc(PATH_MAX); struct stat buffer; proc_t *proc_stat = malloc(sizeof (proc_t)); strcpy(dirname, PROC_PATH); strcat(dirname, diritem->d_name); if (stat(dirname, &buffer)) { perror("can not stat"); exit(2); } if (!S_ISDIR(buffer.st_mode)) { // wether it is a dir : stat.h ctrl + ] goto final; } if (!(is_all_num(diritem->d_name))) { goto final; } strcat(dirname, "/stat"); if (!(stream = fopen(dirname, "r"))) { perror("can not fopen"); goto final; } fscanf(stream, "%ld %s %s " "%ld %ld %ld" "%ld %ld " "%lu %lu %lu %lu " "%lu %Lu %Lu %Lu " "%Lu %ld " "%ld %d %lu " "%Lu", &(proc_stat->pid), &(proc_stat->command), &(proc_stat->status), &(proc_stat->ppid), &(proc_stat->pgrp), &(proc_stat->sid), &(proc_stat->tty), &(proc_stat->tpgid), &(proc_stat->flags), &(proc_stat->min_flt), &(proc_stat->cmin_flt), &(proc_stat->maj_flt), &(proc_stat->cmaj_flt), &(proc_stat->utime), &(proc_stat->stime), &(proc_stat->cutime), &(proc_stat->cstime), &(proc_stat->priority), &(proc_stat->nice), &(proc_stat->nlwp), &(proc_stat->alarm), &(proc_stat->start_time)); fclose(stream); if (proc_stat->tty <= 0) { goto final; } char *temp = strrchr(dirname, '/'); *temp = 0; strcat(dirname, "/cmdline"); int fd = open(dirname, O_RDONLY); if (fd < 0) goto final; char buf[64]; n = read(fd, buf, 64); for (c = 0; c < n - 1; c++) { if (buf[c] == 0) buf[c] = ' '; } buf[n-1] = 0; strcpy(proc_stat->command, buf); proc_stat_table = realloc(proc_stat_table, (count+1) * sizeof (proc_t *)); if (!proc_stat_table) { perror("cannot realloc\n"); exit(1); } proc_stat_table[count] = malloc(sizeof(proc_t)); memcpy(proc_stat_table[count], proc_stat, sizeof(proc_t)); count++; final: