home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / dos / sbbs_src.exe / SBBS / SMM / DELPHOTO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-13  |  5.0 KB  |  224 lines

  1. /* DELPHOTO.C */
  2.  
  3. /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
  4.  
  5. #include <io.h>
  6. #include <dos.h>
  7. #include <dir.h>
  8. #include <bios.h>
  9. #include <time.h>
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <share.h>
  13. #include <conio.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <malloc.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22.  
  23. #include "gen_defs.h"
  24. #include "crc32.h"
  25. #include "smmdefs.h"
  26.  
  27. extern int daylight=0;
  28. extern long timezone=0L;
  29.  
  30.  
  31.  
  32. char *base41(unsigned int i, char *str)
  33. {
  34.     char c;
  35.     unsigned int j=41*41,k;
  36.  
  37. for(c=0;c<3;c++) {
  38.     k=i/j;
  39.     str[c]='0'+k;
  40.     i-=(k*j);
  41.     j/=41;
  42.     if(str[c]>=':')
  43.         str[c]='A'+(str[c]-':');
  44.     if(str[c]>='[')
  45.         str[c]='#'+(str[c]-'['); }
  46. str[c]=0;
  47. return(str);
  48. }
  49.  
  50. /****************************************************************************/
  51. /* Updates 16-bit "rcrc" with character 'ch'                                */
  52. /****************************************************************************/
  53. void ucrc16(uchar ch, ushort *rcrc) {
  54.     ushort i, cy;
  55.     uchar nch=ch;
  56.  
  57. for (i=0; i<8; i++) {
  58.     cy=*rcrc & 0x8000;
  59.     *rcrc<<=1;
  60.     if (nch & 0x80) *rcrc |= 1;
  61.     nch<<=1;
  62.     if (cy) *rcrc ^= 0x1021; }
  63. }
  64.  
  65. /****************************************************************************/
  66. /* Returns 16-crc of string (not counting terminating NULL)                 */
  67. /****************************************************************************/
  68. ushort crc16(char *str)
  69. {
  70.     int     i=0;
  71.     ushort  crc=0;
  72.  
  73. ucrc16(0,&crc);
  74. while(str[i])
  75.     ucrc16(str[i++],&crc);
  76. ucrc16(0,&crc);
  77. ucrc16(0,&crc);
  78. return(crc);
  79. }
  80.  
  81. /****************************************************************************/
  82. /* Returns 32-crc of string (not counting terminating NULL)                 */
  83. /****************************************************************************/
  84. ulong crc32(char *str)
  85. {
  86.     int i=0;
  87.     ulong crc=0xffffffffUL;
  88.  
  89.     while(str[i])
  90.         crc=ucrc32(str[i++],crc);
  91.     crc=~crc;
  92.     return(crc);
  93. }
  94.  
  95. time_t checktime()
  96. {
  97.     struct tm tm;
  98.  
  99. memset(&tm,0,sizeof(tm));
  100. tm.tm_year=94;
  101. tm.tm_mday=1;
  102. return(mktime(&tm)^0x2D24BD00L);
  103. }
  104.  
  105.  
  106. int main(int argc, char **argv)
  107. {
  108.     char str[128],fname[128],path[128],tmp[128];
  109.     int i,file;
  110.     FILE *index, *stream;
  111.     ulong number,crc;
  112.     user_t user;
  113.     ixb_t ixb;
  114.     struct ffblk ff;
  115.  
  116. printf("\nDELPHOTO v1.00 - Synchronet Match Maker Photograph Deletion\n\n");
  117.  
  118. if(checktime()) {
  119.     printf("Time problem!\n");
  120.     exit(1); }
  121.  
  122. if(argc<3) {
  123.     printf("usage: delphoto user_number system_name\n");
  124.     exit(1); }
  125.  
  126. if((file=open("SMM.DAB",O_RDWR|O_BINARY|SH_DENYNO|O_CREAT
  127.     ,S_IWRITE|S_IREAD))==-1) {
  128.     printf("\n\7Error opening/creating SMM.DAB\n");
  129.     exit(1); }
  130. if((stream=fdopen(file,"w+b"))==NULL) {
  131.     printf("\n\7Error converting SMM.DAB file handle to stream\n");
  132.     exit(1); }
  133. setvbuf(stream,0L,_IOFBF,4096);
  134.  
  135. if((file=open("SMM.IXB",O_RDWR|O_BINARY|SH_DENYNO|O_CREAT
  136.     ,S_IWRITE|S_IREAD))==-1) {
  137.     printf("\n\7Error opening/creating SMM.IXB\n");
  138.     exit(1); }
  139. if((index=fdopen(file,"w+b"))==NULL) {
  140.     printf("\n\7Error converting SMM.IXB file handle to stream\n");
  141.     exit(1); }
  142. setvbuf(stream,0L,_IOFBF,1024);
  143.  
  144. number=atol(argv[1]);
  145. str[0]=0;
  146. for(i=2;i<argc;i++) {
  147.     if(str[0])
  148.         strcat(str," ");
  149.     strcat(str,argv[i]); }
  150.  
  151. strupr(str);
  152. str[25]=0;
  153.  
  154. crc=crc32(str);
  155. rewind(index);
  156. i=0;
  157. while(!feof(index)) {
  158.     if(!fread(&ixb,sizeof(ixb_t),1,index))
  159.         break;
  160.     if(!ixb.number)    /* DELETED */
  161.         continue;
  162.     if(ixb.system!=crc || ixb.number!=number)
  163.         continue;
  164.     fseek(stream
  165.         ,((ftell(index)-sizeof(ixb_t))/sizeof(ixb_t))
  166.         *sizeof(user_t),SEEK_SET);
  167.     if(!fread(&user,sizeof(user_t),1,stream))
  168.         continue;
  169.     i=1;
  170.     break; }
  171. if(!i) {
  172.     printf("\7User #%lu @ %s not found!\n",number,str);
  173.     exit(2); }
  174.  
  175. if(!(user.misc&USER_PHOTO)) {
  176.     printf("\7User #%lu @ %s doesn't have a photo attached to their profile.\n"
  177.         ,number,str);
  178.     exit(3); }
  179.  
  180. for(i=0;user.system[i];i++)
  181.     if(isalnum(user.system[i]))
  182.         break;
  183. if(!user.system[i])
  184.     fname[0]='~';
  185. else
  186.     fname[0]=user.system[i];
  187. for(i=strlen(user.system)-1;i>0;i--)
  188.     if(isalnum(user.system[i]))
  189.         break;
  190. if(i<=0)
  191.     fname[1]='~';
  192. else
  193.     fname[1]=user.system[i];
  194. fname[2]=0;
  195. strupr(user.system);
  196. strcat(fname,base41(crc16(user.system),tmp));
  197. strcat(fname,base41(user.number,tmp));
  198. strcat(fname,".*");
  199. strupr(fname);
  200. sprintf(path,"PHOTO\\%s",fname);
  201. i=findfirst(path,&ff,0);
  202. if(i)
  203.     printf("\7%s doesn't exist!\n",path);
  204. else {
  205.     sprintf(path,"PHOTO\\%s",ff.ff_name);
  206.     if(remove(path))
  207.         printf("\7%s couldn't be removed!\n",path); }
  208.  
  209. user.misc&=~USER_PHOTO;
  210. user.updated=time(NULL);
  211. user.photo=0;
  212. fseek(stream
  213.     ,((ftell(index)-sizeof(ixb_t))/sizeof(ixb_t))
  214.     *sizeof(user_t),SEEK_SET);
  215. fwrite(&user,sizeof(user_t),1,stream);
  216. ixb.updated=user.updated;
  217. fseek(index,ftell(index)-sizeof(ixb_t),SEEK_SET);
  218. fwrite(&ixb,sizeof(ixb_t),1,index);
  219.  
  220. printf("Photo removed successfully from database.\n");
  221. return(0);
  222. }
  223.  
  224.