int prelucrare_stoc (stream, fd) FILE *stream; int fd; { /* Intoarce 0 la retur normal, -1 la intalnirea lui EOF de la * client */ enum { INTRARE, IESIRE, STOC, STOP, ILEGAL } Operatie; char buffer_cerere[100]; char cerere[32]; char produs[32]; long cantitate; struct element_stoc *p; /* Citeste cererea de la client */ if (fgets (buffer_cerere, 100, stream) == NULL) return (-1); sscanf (buffer_cerere, "%s %s %d", cerere, produs, &cantitate); Operatie = ILEGAL; if (strcmp (cerere, "INTRARE") == 0) Operatie = INTRARE; if (strcmp (cerere, "IESIRE") == 0) Operatie = IESIRE; if (strcmp (cerere, "STOC") == 0) Operatie = STOC; if (strcmp (cerere, "STOP") == 0) Operatie = STOP; for (p = lista_stocuri; p->cantitate >= 0; p++) if (strcmp (produs, p->produs) == 0) break; if (p->cantitate < 0) p = NULL; switch (Operatie) { case INTRARE: if (p != NULL) { p->cantitate += cantitate; } else { for (p = lista_stocuri; p->cantitate >= 0; p++); if (p - lista_stocuri >= MAX_POZITII - 1) ERR ("Depasire spatiu", p - lista_stocuri); strcpy (p->produs, produs); p->cantitate = cantitate; /* Marcheaza noul sfarsit al listei */ (p + 1)->cantitate = -1; } /* adauga_produs */ fprintf (stream, "A intrat %s %d\n\r", produs, cantitate); return (0); /* INTRARE */ case IESIRE: if (p != NULL) { /* Daca este destul, da cat cere */ if (p->cantitate >= cantitate) { fprintf (stream, "Livrat: %s %d\n\r", produs, cantitate); p->cantitate -= cantitate; } /* ... altfel, da cat poti */ else { fprintf (stream, "Livrat: %s %d\n\r", produs, p->cantitate); p->cantitate = 0; } } else { fprintf (stream, "EROARE: produsul %s este necunoscut!\n\r", produs); } return (0); /* IESIRE */ case STOC: if (p != NULL) { fprintf (stream, "Stocul %s %d\n\r", produs, p->cantitate); } else { fprintf (stream, "EROARE: produsul %s este necunoscut!\n\r", produs); } return (0); /* STOC */ case STOP: fprintf (stream, "STOP. S-a inchis sesiunea.\n\r"); return -1; /* STOP */ default: /* Nu recunoaste comanda de la client */ fprintf (stream, "EROARE: tip de cerere necunoscut!\n\r"); return (0); /* ILEGAL */ } /* switch */ } /* prelucrare_stoc */ /* Programul 3.17 Sursa func›iei prelucrare_stoc.c */