#include #include #include #include #include #include #include #define COUNT_FILE "../data/counter.c.txt" int main(void) { int fd; FILE *fp; int count = 0; fd = open(COUNT_FILE, O_RDWR | O_CREAT, 0644); if (fd == -1) { perror("open"); return 1; } if (flock(fd, LOCK_EX) == -1) { perror("flock"); close(fd); return 1; } fp = fdopen(fd, "r+"); if (fp == NULL) { perror("fdopen"); close(fd); return 1; } if (fscanf(fp, "%d", &count) != 1) { count = 0; } count++; rewind(fp); fprintf(fp, "%d\n", count); /* fflush(fp); ftruncate(fd, ftell(fp)); */ fclose(fp); printf("Content-Type: text/plain\n"); printf("\n"); printf("%d\n", count); return 0; }