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

  1. /* ADDPHOTO.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 <bios.h>
  8. #include <time.h>
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <share.h>
  12. #include <conio.h>
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <malloc.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21.  
  22. #include "gen_defs.h"
  23. #include "crc32.h"
  24. #include "smmdefs.h"
  25.  
  26. extern int daylight=0;
  27. extern long timezone=0L;
  28.  
  29.  
  30. char *base41(unsigned int i, char *str)
  31. {
  32.     char c;
  33.     unsigned int j=41*41,k;
  34.  
  35. for(c=0;c<3;c++) {
  36.     k=i/j;
  37.     str[c]='0'+k;
  38.     i-=(k*j);
  39.     j/=41;
  40.     if(str[c]>=':')
  41.         str[c]='A'+(str[c]-':');
  42.     if(str[c]>='[')
  43.         str[c]='#'+(str[c]-'['); }
  44. str[c]=0;
  45. return(str);
  46. }
  47.  
  48. /****************************************************************************/
  49. /* Updates 16-bit "rcrc" with character 'ch'                                */
  50. /****************************************************************************/
  51. void ucrc16(uchar ch, ushort *rcrc) {
  52.     ushort i, cy;
  53.     uchar nch=ch;
  54.  
  55. for (i=0; i<8; i++) {
  56.     cy=*rcrc & 0x8000;
  57.     *rcrc<<=1;
  58.     if (nch & 0x80) *rcrc |= 1;
  59.     nch<<=1;
  60.     if (cy) *rcrc ^= 0x1021; }
  61. }
  62.  
  63. /****************************************************************************/
  64. /* Returns 16-crc of string (not counting terminating NULL)                 */
  65. /****************************************************************************/
  66. ushort crc16(char *str)
  67. {
  68.     int     i=0;
  69.     ushort  crc=0;
  70.  
  71. ucrc16(0,&crc);
  72. while(str[i])
  73.     ucrc16(str[i++],&crc);
  74. ucrc16(0,&crc);
  75. ucrc16(0,&crc);
  76. return(crc);
  77. }
  78.  
  79. /****************************************************************************/
  80. /* Returns 32-crc of string (not counting terminating NULL)                 */
  81. /****************************************************************************/
  82. ulong crc32(char *str)
  83. {
  84.     int i=0;
  85.     ulong crc=0xffffffffUL;
  86.  
  87.     while(str[i])
  88.         crc=ucrc32(str[i++],crc);
  89.     crc=~crc;
  90.     return(crc);
  91. }
  92.  
  93. /****************************************************************************/
  94. /* Checks the disk drive for the existence of a file. Returns 1 if it       */
  95. /* exists, 0 if it doesn't.                                                 */
  96. /****************************************************************************/
  97. char fexist(char *filespec)
  98. {
  99.     struct ffblk f;
  100.  
  101. if(findfirst(filespec,&f,FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_DIREC)==0)
  102.     return(1);
  103. return(0);
  104. }
  105.  
  106. #define MV_BUFLEN    4096
  107.  
  108. /****************************************************************************/
  109. /* Moves or copies a file from one dir to another                           */
  110. /* both 'src' and 'dest' must contain full path and filename                */
  111. /* returns 0 if successful, -1 if error                                     */
  112. /****************************************************************************/
  113. int mv(char *src, char *dest, char copy)
  114. {
  115.     char str[256],*buf;
  116.     int  ind,outd;
  117.     long length,chunk=MV_BUFLEN,l;
  118.     struct ftime ftime;
  119.     FILE *inp,*outp;
  120.  
  121. if(copy)
  122.     printf("Copying %s to %s\n",src,dest);
  123. else
  124.     printf("Moving %s to %s\n",src,dest);
  125. if(!stricmp(src,dest))     /* source and destination are the same! */
  126.     return(0);
  127. if(!fexist(src))
  128.     return(1);
  129. if(!copy && fexist(dest))
  130.     return(2);
  131. if(!copy && ((src[1]!=':' && dest[1]!=':')
  132.     || (src[1]==':' && dest[1]==':' && toupper(src[0])==toupper(dest[0])))) {
  133.     if(rename(src,dest))                       /* same drive, so move */
  134.         return(3);
  135.     return(0); }
  136. if((ind=open(src,O_RDONLY|O_BINARY))==-1)
  137.     return(4);
  138. if((inp=fdopen(ind,"rb"))==NULL) {
  139.     close(ind);
  140.     return(5); }
  141. setvbuf(inp,NULL,_IOFBF,32*1024);
  142. if((outd=open(dest,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,S_IWRITE|S_IREAD))==-1) {
  143.     fclose(inp);
  144.     return(6); }
  145. if((outp=fdopen(outd,"wb"))==NULL) {
  146.     close(outd);
  147.     fclose(inp);
  148.     return(7); }
  149. setvbuf(outp,NULL,_IOFBF,8*1024);
  150. length=filelength(ind);
  151. if(!length) {
  152.     fclose(inp);
  153.     fclose(outp);
  154.     return(8); }
  155. if((buf=(char *)MALLOC(MV_BUFLEN))==NULL) {
  156.     fclose(inp);
  157.     fclose(outp);
  158.     return(9); }
  159. l=0L;
  160. while(l<length) {
  161.     if(l+chunk>length)
  162.         chunk=length-l;
  163.     if(fread(buf,1,chunk,inp)!=chunk) {
  164.         FREE(buf);
  165.         fclose(inp);
  166.         fclose(outp);
  167.         return(10); }
  168.     if(fwrite(buf,1,chunk,outp)!=chunk) {
  169.         FREE(buf);
  170.         fclose(inp);
  171.         fclose(outp);
  172.         return(11); }
  173.     l+=chunk; }
  174. getftime(ind,&ftime);
  175. setftime(outd,&ftime);
  176. FREE(buf);
  177. fclose(inp);
  178. fclose(outp);
  179. if(!copy && remove(src))
  180.     return(12);
  181. return(0);
  182. }
  183.  
  184. time_t checktime()
  185. {
  186.     struct tm tm;
  187.  
  188. memset(&tm,0,sizeof(tm));
  189. tm.tm_year=94;
  190. tm.tm_mday=1;
  191. return(mktime(&tm)^0x2D24BD00L);
  192. }
  193.  
  194.  
  195. int main(int argc, char **argv)
  196. {
  197.     char str[128],fname[128],path[128],tmp[128],*p;
  198.     int i,file;
  199.     FILE *index, *stream;
  200.     ulong number,crc;
  201.     user_t user;
  202.     ixb_t ixb;
  203.  
  204. printf("\nADDPHOTO v1.01 - Synchronet Match Maker Photograph Addition\n\n");
  205.  
  206. if(checktime()) {
  207.     printf("Time problem!\n");
  208.     exit(1); }
  209.  
  210. if(argc<4) {
  211.     printf("usage: addphoto filename.ext user_number system_name\n");
  212.     exit(1); }
  213.  
  214. strupr(argv[1]);
  215. if(!fexist(argv[1])) {
  216.     printf("%s doesn't exist\n",argv[1]);
  217.     exit(1); }
  218.  
  219. if((file=open("SMM.DAB",O_RDWR|O_BINARY|O_DENYNONE|O_CREAT
  220.     ,S_IWRITE|S_IREAD))==-1) {
  221.     printf("\n\7Error opening/creating SMM.DAB\n");
  222.     exit(1); }
  223. if((stream=fdopen(file,"w+b"))==NULL) {
  224.     printf("\n\7Error converting SMM.DAB file handle to stream\n");
  225.     exit(1); }
  226. setvbuf(stream,0L,_IOFBF,4096);
  227.  
  228. if((file=open("SMM.IXB",O_RDWR|O_BINARY|O_DENYNONE|O_CREAT
  229.     ,S_IWRITE|S_IREAD))==-1) {
  230.     printf("\n\7Error opening/creating SMM.IXB\n");
  231.     exit(1); }
  232. if((index=fdopen(file,"w+b"))==NULL) {
  233.     printf("\n\7Error converting SMM.IXB file handle to stream\n");
  234.     exit(1); }
  235. setvbuf(stream,0L,_IOFBF,1024);
  236.  
  237. number=atol(argv[2]);
  238. str[0]=0;
  239. for(i=3;i<argc;i++) {
  240.     if(str[0])
  241.         strcat(str," ");
  242.     strcat(str,argv[i]); }
  243.  
  244. strupr(str);
  245. str[25]=0;
  246.  
  247. crc=crc32(str);
  248. rewind(index);
  249. i=0;
  250. while(!feof(index)) {
  251.     if(!fread(&ixb,sizeof(ixb_t),1,index))
  252.         break;
  253.     if(!ixb.number)    /* DELETED */
  254.         continue;
  255.     if(ixb.system!=crc || ixb.number!=number)
  256.         continue;
  257.     fseek(stream
  258.         ,((ftell(index)-sizeof(ixb_t))/sizeof(ixb_t))
  259.         *sizeof(user_t),SEEK_SET);
  260.     if(!fread(&user,sizeof(user_t),1,stream))
  261.         continue;
  262.     i=1;
  263.     break; }
  264. if(!i) {
  265.     printf("\7User #%lu @ %s not found!\n",number,str);
  266.     exit(2); }
  267.  
  268. for(i=0;user.system[i];i++)
  269.     if(isalnum(user.system[i]))
  270.         break;
  271. if(!user.system[i])
  272.     fname[0]='~';
  273. else
  274.     fname[0]=user.system[i];
  275. for(i=strlen(user.system)-1;i>0;i--)
  276.     if(isalnum(user.system[i]))
  277.         break;
  278. if(i<=0)
  279.     fname[1]='~';
  280. else
  281.     fname[1]=user.system[i];
  282. fname[2]=0;
  283. strupr(user.system);
  284. strcat(fname,base41(crc16(user.system),tmp));
  285. strcat(fname,base41(user.number,tmp));
  286. p=strrchr(argv[1],'.');
  287. if(p)
  288.     strcat(fname,p);
  289. strupr(fname);
  290. mkdir("PHOTO");
  291. sprintf(path,"PHOTO\\%s",fname);
  292. if((i=mv(argv[1],path,1))!=0) {
  293.     printf("\7ERROR %d copying %s to %s\n",i,argv[1],path);
  294.     exit(3); }
  295.  
  296. user.misc|=USER_PHOTO;
  297. user.updated=time(NULL);
  298. user.photo=0;
  299. fseek(stream
  300.     ,((ftell(index)-sizeof(ixb_t))/sizeof(ixb_t))
  301.     *sizeof(user_t),SEEK_SET);
  302. fwrite(&user,sizeof(user_t),1,stream);
  303. ixb.updated=user.updated;
  304. fseek(index,ftell(index)-sizeof(ixb_t),SEEK_SET);
  305. fwrite(&ixb,sizeof(ixb_t),1,index);
  306.  
  307. printf("%s added successfully!\n",argv[1]);
  308. return(0);
  309. }
  310.  
  311.