int main (int argc, char **argv) { long score; if (argc != 2) { fprintf(stderr, "Usage: %s <score>\n", argv[0]); return -1; } score = my_strtol(argv[1]); if (score >= 90) fprintf(stdout, "GRADE: A\n"); else if (score >= 80) fprintf(stdout, "GRADE: B\n"); else if (score >= 70) fprintf(stdout, "GRADE: C\n"); else if (score >= 60) fprintf(stdout, "GRADE: D\n"); else if (score < 60 && score > 0) fprintf(stdout, "GRADE: F\n"); else fprintf(stdout, "GRADE: FAIL!!\n"); return 0; }
int my_apr(char **com, t_zap *data, t_user *usr) { int ig; int ask_nb; t_ask *ask; if (sstrlen(com) > 3 || sstrlen(com) == 1) return (my_sbp(usr)); if (sstrlen(com) == 1) return (aeg_response(data, usr, APR)); if ((ig = my_strtol(com[1])) == -1 || (ask_nb = my_strtol(com[2])) == -1) return (my_sbp(usr)); if ((ask = get_asking(usr, ig, ask_nb)) == NULL) return (my_sbp(usr)); ask->res |= APR; return (0); }
int my_dec2int(const char *s) { long result_long; char *ptr; const int max_int = (int)((unsigned)(-1) / 2); const int min_int = -max_int - 1; result_long = my_strtol(s, &ptr, 10); if (result_long > max_int) return max_int; else if (result_long < min_int) return min_int; else return (int)result_long; }
int recvFileList(int sfd, struct actionParameters *ap, struct serverActionParameters *sap){ int res; struct flEntry file; struct array *fl2; //memmove(&file.ip,&ap->comip, sizeof(struct sockaddr)); file.ip = ap->comip; while (( res = getTokenFromStream( sfd, &ap->combuf, &ap->comline, "\n", "\r\n",NULL ))){ if (res == -1) return -1; strcpy(file.filename, (char *)ap->comline.buf); flushBuf(&ap->comline); if (getTokenFromStream( sfd, &ap->combuf, &ap->comline, "\n", "\r\n",NULL ) == -1) return -1; long size = my_strtol((char *)ap->comline.buf); if ( errno || size < 0 ) { file.size = size; logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, "problem converting file size for %s - %s\n", file.filename, ap->comline.buf); continue; } logmsg(ap->semid, ap->logfd, LOGLEVEL_VERBOSE, "recvFileList: %s - %lu\n", file.filename, file.size); if ( (res = semWait(ap->semid, SEM_FILELIST))) logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, "(recvfile) can't get semaphore, %d", res); fl2 = addArrayItem(sap->filelist, &file); if ( (res = semSignal(ap->semid, SEM_FILELIST))) logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, "(recvfile) can't release semaphore, %d", res); if (fl2) sap->filelist = fl2; else { logmsg(ap->semid, ap->logfd, LOGLEVEL_FATAL, "leaving the functions because realloc failed %d", fl2); return -1; } } return 1; }
int my_pin(char **com, t_zap *data, t_user *usr) { int nb; t_user *player; (void)data; if (sstrlen(com) != 2) return (my_sbp(usr)); nb = my_strtol(com[1]); if (nb == -1) return (my_sbp(usr)); player = usr; while (player != NULL && player->prev != NULL) player = player->prev; while (player != NULL) { if (player->type == AI && player->nb == nb) return (my_send_pin(usr, player)); player = player->next; } return (0); }
int recvResult(int fd, struct actionParameters *ap,struct array * results){ int counter=0, res; struct flEntry file;///, *f; struct array * results2; char port[7]; char ipstr[56]; flushBuf(&ap->combuf); /* I don't flush results on purpose */ /* get a line */ logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, "before loop%lu\n\n", results->itemcount); while (( res = getTokenFromStream(fd, &ap->combuf, &ap->comline, "\n", "\r\n",NULL ))){ /* get first word -> ip */ strncpy(ipstr, (char *)ap->comline.buf, 56); if (res == -1) return -3; logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) ip: %s\n", ipstr); /* get second word -> port */ if (getTokenFromBuffer(&ap->combuf, &ap->comline, " ", "\n",NULL ) == -1) return -3; strncpy(port, (char *)ap->comline.buf, 7); logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) port: %s\n", port); if (parseIP(ipstr, (struct sockaddr *)&file.ip, port, 0) == -1) return -3; /* get third word -> filename */ if (getTokenFromBuffer( &ap->combuf, &ap->comline, " ", "\n",NULL ) == -1) return -3; strcpy(file.filename, (char *)ap->comline.buf); logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) filename: %s\n", file.filename); /* get fourth word -> size */ if (getTokenFromBuffer( &ap->combuf, &ap->comline, " ", "\n",NULL ) == -1) return -3; long size; if ((size = my_strtol((char *)ap->comline.buf)) < 0 || errno) return -3; file.size = size; logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) size: %d\n", file.size); logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "got this: %s\n%d\n%s\n%lu\n", putIP((struct sockaddr *)&file.ip), getPort((struct sockaddr *) &file.ip), file.filename, file.size); results2 = addArrayItem(results, &file); if (results2) results = results2; else { logmsg(ap->semid, ap->logfd, LOGLEVEL_FATAL, "couldn't resize Resultsarray"); return -3; } file = *((struct flEntry *)getArrayItem(results, counter)); logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "Array Memory got this: %s\n%d\n%s\n%lu - entry no %d\n", putIP((struct sockaddr *)&file.ip), getPort((struct sockaddr *) &file.ip), file.filename, file.size, results->itemcount); counter++; } return counter; }
int portAction(struct actionParameters *ap, union additionalActionParameters *aap){ (void) aap; ap->comport = my_strtol( (char *) ap->comline.buf); return (errno) ? -1 : 1; }