int main(void) { FILE *fp_mail; char *status = (char *)alloca(200); int b, d, m; b = BATTERY_INTERVAL; d = DATE_INTERVAL; m = MAIL_INTERVAL; struct timespec interval = { .tv_sec = UPDATE_INTERVAL, .tv_nsec = 0 }; int sockfd = socket(AF_INET, SOCK_DGRAM, 0); for ( ; ; nanosleep(&interval, NULL)) { if (++b > BATTERY_INTERVAL) { print_battery(); b = 0; } if (++d > DATE_INTERVAL) { print_time(); d = 0; } if (++m > MAIL_INTERVAL) { if (print_mail(fp_mail) == 0) { m = 0; } if (errno == EBADF) { fp_mail = mail_init(); } } if (sockfd != -1) { print_iw(sockfd); } strncpy(_root, print_free("/"), _FREE_LEN); strncpy(_home, print_free("/home"), _FREE_LEN); print_time(); print_cpu(); print_memory(); snprintf(status, 200, "%s %s | %s | %s | %s | %s | %s | %s" , _root, _home, _iw, _battery, _mail, _cpu , _mem, _date); printf("%s\n", status); }//break; } pclose(fp_mail); cleanup(sockfd); } double get_battery_stats(const char *path) { FILE *stats; double value = 0; if ((stats = fopen(path, "r"))== NULL) { return -1; } fscanf(stats, "%lf", &value); fclose(stats); return value; }
int main(int argc, char **argv) { if (argc != 4) print_help("Wrong number of arguments"); struct sockaddr_in serv_addr; int sockfd = 0; char buf[1024]; char cmd[1024]; char ip[32]; int n, k; if (get_ip_by_hostname(ip, argv[3]) != 0) { perror("Error by getting ip"); print_help("Bad server"); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("Can't create socket"); return -1; } memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); if(inet_pton(AF_INET, ip, &serv_addr.sin_addr) <= 0) { perror("Inet_Pton error"); return -1; } if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("Connect error"); return -1; } //wrte(sockfd, "\n", 1); int state = 0; char *token; while ((n = read(sockfd, buf, sizeof(buf))) > 0) { if (strstr(buf, "+OK") != NULL) { state++; } else{ if (state == 1) { print_help("Bad username or password"); } if (state == 2) { print_help("Bad username or password"); } } if (state == 1) { write(sockfd, "USER ", 5); write(sockfd, argv[1], strlen(argv[1])); write(sockfd, "\n", 1); //Write password continue; } if (state == 2){ write(sockfd, "PASS ",5); write(sockfd, argv[2], strlen(argv[2])); write(sockfd, "\n", 1); continue; } if (state == 3) break; } printf("Success login\n\t<list> - to print list\n\t<msg> <n> - get <n>th message\n\t<exit> - exit\n"); while ((k = read(0, cmd, sizeof(cmd))) > 0) { if (strstr(cmd, "list") != NULL) { print_list(sockfd); handle_socket(sockfd); } else if (strstr(cmd, "msg") != NULL) { token = strtok(cmd, " \t"); char* msg = strtok(NULL," \t"); print_mail(sockfd, msg); handle_socket(sockfd); } else if (strstr(cmd, "exit") != NULL) { disconnect(sockfd); break; } } close(sockfd); }