home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / mpackppc-wos / src / amigados.c next >
C/C++ Source or Header  |  1998-04-27  |  8KB  |  257 lines

  1. /* (c) Copyright 1993 by Mike W. Meyer
  2.  *
  3.  * Permission to use, copy, modify, distribute, and sell this software
  4.  * and its documentation for any purpose is hereby granted without
  5.  * fee, provided that the above copyright notice appear in all copies
  6.  * and that both that copyright notice and this permission notice
  7.  * appear in supporting documentation, and that the name of Mike W.
  8.  * Meyer not be used in advertising or publicity pertaining to
  9.  * distribution of the software without specific, written prior
  10.  * permission.  Mike W. Meyer makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as
  12.  * is" without express or implied warranty.
  13.  *
  14.  * MIKE W. MEYER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16.  * FITNESS, IN NO EVENT SHALL MIKE W. MEYER BE LIABLE FOR ANY SPECIAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  20.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. #include <exec/types.h>
  24. #include <exec/exec.h>
  25. #include <dos/dos.h>
  26.  
  27. extern struct DosLibrary *DOSBase;
  28.  
  29.  
  30. #define EEXIST 17
  31.  
  32. #ifndef __PPC__
  33. #include <libraries/netsupport.h>
  34. #else
  35. #define NODENAME ""
  36. #define DOMAINNAME ""
  37. #endif
  38.  
  39. #ifdef __SASC
  40. #include <proto/exec.h>
  41. #include <proto/dos.h>
  42. #ifndef __PPC__
  43. #include <proto/netsupport.h>
  44. #endif
  45. #else
  46. #include <clib/exec_protos.h>
  47. #include <clib/dos_protos.h>
  48. //#include <clib/netsupport_protos.h>
  49. #endif
  50.  
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <time.h>
  54. #include <string.h>
  55. #include <dos.h>
  56. #include <ctype.h>
  57. #include <errno.h>
  58.  
  59. #include "xmalloc.h"
  60. #include "common.h"
  61.  
  62. #define BADCHARS        "#?()|[]%<>:;$&*\\\ \t\`\""
  63. #define FILELEN         29              /* Max file name length */
  64.  
  65. int overwrite_files = 0 ;
  66. static char *output_fname = NULL;
  67. extern int errno;
  68. int _OSERR;
  69. void os_perror(char *) ;
  70. char *myGetConfig(char *, char *) ;
  71. int __buffsize = 8192 ;
  72. int didchat;
  73.  
  74. /* Generate a message-id */
  75. char *
  76. os_genid(void) {
  77.         static struct Task *task = NULL ;
  78.         static time_t now ;
  79.         static char hostname[32], domainname[32] ;
  80.         char *result ;
  81.  
  82.         if (task == NULL) {
  83.                 task = FindTask(NULL) ;
  84.                 time(&now) ;
  85.  
  86.                 strcpy(hostname, myGetConfig(NODENAME, "random-amiga"));
  87.                 if (strchr(hostname, '.')) domainname[0] = '\0' ;
  88.                 else strcpy(domainname, myGetConfig(DOMAINNAME, ".random-domain"));
  89.                 if (domainname[0] && domainname[0] != '.') strcat(hostname, ".") ;
  90.                 }
  91.         result = malloc(25 + strlen(hostname) + strlen(domainname)) ;
  92.         sprintf(result, "%d.%d@%s%s", (long) task, now++, hostname, domainname) ;
  93.         return result ;
  94.         }
  95.  
  96. /* Create and return a directory for a message-id */
  97. char *
  98. os_idtodir(char *id) {
  99. static char buf[512] ;
  100.         char *p, save ;
  101.  
  102.         strcpy(buf, myGetConfig("METAMAIL_P_DIR", "T:"));
  103.  
  104.         if (!(p = myGetConfig("USERNAME", NULL)))
  105.                 p = myGetConfig("USER", "anonymous");
  106.         AddPart(buf, p, sizeof(buf)) ;
  107.  
  108.         if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  109.                 os_perror(buf) ;
  110.                 return NULL ;
  111.                 }
  112.  
  113.         p = buf + strlen(buf) ;
  114.         *p++ = '/' ;
  115.         save = id[FILELEN] ;
  116.         id[FILELEN] = '\0' ;
  117.         while (*id) {
  118.                 if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id ;
  119.                 id += 1 ;
  120.                 }
  121.         *p  = '\0' ;
  122.         id[FILELEN] = save ;
  123.  
  124.         if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  125.                 os_perror(buf) ;
  126.                 return NULL ;
  127.                 }
  128.         strcpy(p, "/") ;
  129.         return buf ;
  130.         }
  131.  
  132. /* Delete the directory created by os_idtodir() */
  133. void
  134. os_donewithdir(char *dir) {
  135.  
  136.         /* Chop off trailing slash */
  137.         dir[strlen(dir) - 1] = '\0' ;
  138.         rmdir(dir) ;
  139.         }
  140.  
  141. /* Create a new file of name "fname". Clean up the name first */
  142. FILE *
  143. os_newtypedfile(char *fname, char *contentType, int binary, params contentParams) {
  144.         char *p, *name, *description, buf[512] ;
  145.         FILE *outfile ;
  146.         static int filesuffix = 0 ;
  147.  
  148.         name = FilePart(fname) ;
  149.  
  150.         /* No BADCHARS */
  151.         for (p = name; *p; p += 1)
  152.                 if (!isprint(*p) || strchr(BADCHARS, *p)) *p = 'X' ;
  153.  
  154.         /* Add a count if we don't have a name, or we aren't overwriting */
  155.         if (!*fname || !overwrite_files) {
  156.                 if (*name) strcpy(buf, name) ;
  157.                 else {
  158.                         name = "part" ;
  159.                         sprintf(buf, "part.%d", ++filesuffix) ;
  160.                         }
  161.                 while (outfile = fopen(buf, "r")) {
  162.                         if (outfile) fclose(outfile) ;
  163.                         sprintf(buf, "%s.%d", name, ++filesuffix) ;
  164.                         }
  165.                 name = buf ;
  166.                 if (outfile) fclose(outfile) ;
  167.                 }
  168.  
  169.         if (!(outfile = fopen(name, "w"))) os_perror(name) ;
  170.  
  171.         if (output_fname) free(output_fname) ;
  172.         output_fname = strsave(name) ;
  173.         description = xmalloc(strlen(name) + 6) ;
  174.         strcpy(description, name) ;
  175.         strcat(description, ".desc") ;
  176.         (void) rename(TEMPFILENAME, description) ;
  177.         free(description) ;
  178.  
  179.         fprintf(stdout, "%s (%s)\n", output_fname, contentType) ;
  180.         didchat = 1;
  181.         return outfile ;
  182.         }
  183.  
  184. /* Warn user that MD5 digest didn't match */
  185. void
  186. os_warnMD5mismatch(void) {
  187.         char *warning;
  188.  
  189.         warning = xmalloc(strlen(output_fname) + 100);
  190.         sprintf(warning, "%s was corrupted in transit", output_fname);
  191.         warn(warning);
  192.         free(warning);
  193.         }
  194.  
  195. /* Report an error (in errno) concerning a filename */
  196. void
  197. os_perror(char *file) {
  198.         perror(file);
  199.         }
  200.  
  201. /*
  202.  * This getenv will use the WB2.0 calls if you have the 2.0
  203.  * rom. If not, it resorts to looking in the ENV: directory.
  204.  */
  205.  
  206. char *
  207. getenv (const char *name) {
  208.         FILE *fp;
  209.         char *ptr;
  210.         static char value[256] ;
  211.         static char buf[256] ;
  212.  
  213.         /* 2.0 style? */
  214.         if (DOSBase->dl_lib.lib_Version >= 36) {
  215.                 if (GetVar ((char *)name,value,256,0L) == -1) return NULL;
  216.         } else {
  217.                 if (strlen (name) > 252) return NULL;
  218.                 strcpy (buf,"ENV:");
  219.                 strcpy (&buf[4],name);
  220.                 if (! (fp = fopen(buf,"r"))) return NULL;
  221.                 for (ptr = value; (*ptr=getc(fp))!=EOF
  222.                         && *ptr != '\n'
  223.                         && ++ptr < &value[256];);
  224.                 fclose(fp);
  225.                 *ptr = 0;
  226.                 }
  227.         return value;
  228.         }
  229.  
  230. /* Get configuarion, either from a file or from a variable */
  231.  
  232. char * myGetConfig(char *keyword, char *def) {
  233.         char *entry;
  234.  
  235. #ifndef __PPC__
  236.         if (NetSupportBase)
  237.                 return (char *) GetConfig(NULL, keyword, NULL, def);
  238. #endif
  239.  
  240.         entry = getenv(keyword);  /* Library is NOT available */
  241.         return ((entry) ? entry : def);
  242.         }
  243.  
  244. /* (Don't) Handle a BinHex'ed file */
  245.  
  246. int
  247. os_binhex(FILE *infile, int dummy1, int dummy2) {
  248.         return 1;
  249.         }
  250. /*
  251.  * Close a file opened by os_newTypedFile()
  252.  */
  253. int
  254. os_closetypedfile(FILE *outfile) {
  255.         return fclose(outfile);
  256.         }
  257.