/* Counter.c : Image counter CGI. Programed by Kaburaya Seiden. http://www2.justnet.ne.jp/~kabstudio/ kab-studio@multimedia.design.co.jp This program is "Copyleft". */ /* If you compile in DEBUG mode, set 1.*/ /* else(Release mode), set 0 */ #define MODE_DEBUG 0 /* If you use "flock", set 1 */ /* If you use "lockf", set 0 */ #define MODE_FLOCK 1 #include #include #include #if MODE_FLOCK #include #else #include #endif #if MODE_DEBUG #define TRACE(sz) printf("%s\n",sz) #else #define TRACE(st) #endif #define MAX_URL_LEN 257 #define IMG_DEFAULT "./TPDot.gif" /* programs */ void GetCountImg( p_chRetImg, p_chImgDir, p_iCount, p_iKeta ) char *p_chRetImg; /* Return image file name. */ char *p_chImgDir; /* Image directory. */ int p_iCount; int p_iKeta; { int iCountLen, iStrLen; char chCount[10]; sprintf( chCount, "%d", p_iCount ); /* itoa */ iCountLen = strlen( chCount ); /* Get count text length. */ /* Concatenate strings. */ if( p_iKeta <= iCountLen ) { strcpy( p_chRetImg, "./" ); strcat( p_chRetImg, p_chImgDir ); strcat( p_chRetImg, "/" ); iStrLen = strlen( p_chRetImg ); p_chRetImg[iStrLen] = chCount[iCountLen - p_iKeta]; p_chRetImg[iStrLen + 1] = '\0'; strcat( p_chRetImg, ".gif" ); } else strcpy( p_chRetImg, IMG_DEFAULT ); return; } int GetCount( p_pfLogFile ) FILE *p_pfLogFile; { int iRetCount, iScanRes; char chCount[10]; rewind( p_pfLogFile ); /* Go to top. */ iScanRes = fscanf( p_pfLogFile, "%s", chCount ); /* Read count. */ if( iScanRes != EOF ) iRetCount = atoi( chCount ); else iRetCount = 0; return iRetCount; } int IncCount( p_pchFile, p_iIncCount ) char *p_pchFile; /* Count data file name. */ int p_iIncCount; /* Increment Number. */ { int iRetCount, iLockRes, iFileNo; long lFilePos; FILE *pfLogFile; if( p_iIncCount == 0 ) /* Display only. */ { pfLogFile = fopen( p_pchFile, "r" ); /* Read only. */ if( pfLogFile == NULL ) return 0; iRetCount = GetCount( pfLogFile ); /* Get count. */ } else /* Increment. */ { pfLogFile = fopen( p_pchFile, "r+" ); /* Read and write. */ if( pfLogFile == NULL ) return 0; iFileNo = fileno( pfLogFile ); #if MODE_FLOCK iLockRes = flock( iFileNo, LOCK_EX ); #else lseek( iFileNo, 0L, 0 ); iLockRes = lockf( iFileNo, F_LOCK, 0L ); #endif if( iLockRes == -1 ) { fclose( pfLogFile ); return 0; } /* Lock Start!! */ iRetCount = GetCount( pfLogFile ) + p_iIncCount; /* Get count and increment. */ rewind( pfLogFile ); /* Go to top. */ fprintf( pfLogFile, "%d\n", iRetCount ); /* Write count. */ /* Lock End */ #if MODE_FLOCK flock( iFileNo, LOCK_UN ); #else lseek( iFileNo, 0L, 0 ); lockf( iFileNo, F_ULOCK, 0L ); #endif } fclose( pfLogFile ); return iRetCount; } int WriteImg( p_pchFile ) char *p_pchFile; { int iChar, iScanRes; FILE *pfImg; printf( "Content-type: image/gif\n\n" ); pfImg = fopen( p_pchFile, "rb" ); /* Read Only, Bynary mode */ if( pfImg == NULL ) { pfImg = fopen( IMG_DEFAULT, "rb" ); /* Open default image file. */ if( pfImg == NULL ) return 0; } rewind( pfImg ); /* Go to Top */ while( !feof( pfImg ) ) putchar( fgetc( pfImg ) ); /* Output */ fclose( pfImg ); return 1; } /* Arguments 0: Command ( Counter.cgi ) 1: Count file ( ex: test.txt ) 2: Imgs dir 3: Keta 4: Increment Count */ int main( argc, argv ) int argc; char *argv[]; { int iIncCount, iCount, iKeta; char chCountImg[2]; char chImgDir[MAX_URL_LEN]; if( argc == 5 ) iIncCount = atoi( argv[4] ); else if( argc == 4 ) iIncCount = 0; else exit( 1 ); iKeta = atoi( argv[3] ); strcpy( chImgDir, argv[2] ); /* Count */ iCount = IncCount( argv[1], iIncCount ); GetCountImg( chCountImg, chImgDir, iCount, iKeta ); WriteImg( chCountImg ); exit( 0 ); }