home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Workbench / Archivers / mpackPPC.lha / mpackPPC / src / amigados.c next >
C/C++ Source or Header  |  1998-04-08  |  8KB  |  252 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. #ifndef __PPC__
  28. #include <libraries/netsupport.h>
  29. #else
  30. #define NODENAME ""
  31. #define DOMAINNAME ""
  32. #endif
  33.  
  34. #ifdef __SASC
  35. #include <proto/exec.h>
  36. #include <proto/dos.h>
  37. #ifndef __PPC__
  38. #include <proto/netsupport.h>
  39. #endif
  40. #else
  41. #include <clib/exec_protos.h>
  42. #include <clib/dos_protos.h>
  43. #include <clib/netsupport_protos.h>
  44. #endif
  45.  
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <time.h>
  49. #include <string.h>
  50. #include <dos.h>
  51. #include <ctype.h>
  52. #include <errno.h>
  53.  
  54. #include "xmalloc.h"
  55. #include "common.h"
  56.  
  57. #define BADCHARS        "#?()|[]%<>:;$&*\\\ \t\`\""
  58. #define FILELEN         29              /* Max file name length */
  59.  
  60. int overwrite_files = 0 ;
  61. static char *output_fname = NULL;
  62. extern int errno, _OSERR ;
  63. void os_perror(char *) ;
  64. char *myGetConfig(char *, char *) ;
  65. int __buffsize = 8192 ;
  66. int didchat;
  67.  
  68. /* Generate a message-id */
  69. char *
  70. os_genid(void) {
  71.         static struct Task *task = NULL ;
  72.         static time_t now ;
  73.         static char hostname[32], domainname[32] ;
  74.         char *result ;
  75.  
  76.         if (task == NULL) {
  77.                 task = FindTask(NULL) ;
  78.                 time(&now) ;
  79.  
  80.                 strcpy(hostname, myGetConfig(NODENAME, "random-amiga"));
  81.                 if (strchr(hostname, '.')) domainname[0] = '\0' ;
  82.                 else strcpy(domainname, myGetConfig(DOMAINNAME, ".random-domain"));
  83.                 if (domainname[0] && domainname[0] != '.') strcat(hostname, ".") ;
  84.                 }
  85.         result = malloc(25 + strlen(hostname) + strlen(domainname)) ;
  86.         sprintf(result, "%d.%d@%s%s", (long) task, now++, hostname, domainname) ;
  87.         return result ;
  88.         }
  89.  
  90. /* Create and return a directory for a message-id */
  91. char *
  92. os_idtodir(char *id) {
  93. static char buf[512] ;
  94.         char *p, save ;
  95.  
  96.         strcpy(buf, myGetConfig("METAMAIL_P_DIR", "T:"));
  97.  
  98.         if (!(p = myGetConfig("USERNAME", NULL)))
  99.                 p = myGetConfig("USER", "anonymous");
  100.         AddPart(buf, p, sizeof(buf)) ;
  101.  
  102.         if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  103.                 os_perror(buf) ;
  104.                 return NULL ;
  105.                 }
  106.  
  107.         p = buf + strlen(buf) ;
  108.         *p++ = '/' ;
  109.         save = id[FILELEN] ;
  110.         id[FILELEN] = '\0' ;
  111.         while (*id) {
  112.                 if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id ;
  113.                 id += 1 ;
  114.                 }
  115.         *p  = '\0' ;
  116.         id[FILELEN] = save ;
  117.  
  118.         if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  119.                 os_perror(buf) ;
  120.                 return NULL ;
  121.                 }
  122.         strcpy(p, "/") ;
  123.         return buf ;
  124.         }
  125.  
  126. /* Delete the directory created by os_idtodir() */
  127. void
  128. os_donewithdir(char *dir) {
  129.  
  130.         /* Chop off trailing slash */
  131.         dir[strlen(dir) - 1] = '\0' ;
  132.         rmdir(dir) ;
  133.         }
  134.  
  135. /* Create a new file of name "fname". Clean up the name first */
  136. FILE *
  137. os_newtypedfile(char *fname, char *contentType, int binary, params contentParams) {
  138.         char *p, *name, *description, buf[512] ;
  139.         FILE *outfile ;
  140.         static int filesuffix = 0 ;
  141.  
  142.         name = FilePart(fname) ;
  143.  
  144.         /* No BADCHARS */
  145.         for (p = name; *p; p += 1)
  146.                 if (!isprint(*p) || strchr(BADCHARS, *p)) *p = 'X' ;
  147.  
  148.         /* Add a count if we don't have a name, or we aren't overwriting */
  149.         if (!*fname || !overwrite_files) {
  150.                 if (*name) strcpy(buf, name) ;
  151.                 else {
  152.                         name = "part" ;
  153.                         sprintf(buf, "part.%d", ++filesuffix) ;
  154.                         }
  155.                 while (outfile = fopen(buf, "r")) {
  156.                         if (outfile) fclose(outfile) ;
  157.                         sprintf(buf, "%s.%d", name, ++filesuffix) ;
  158.                         }
  159.                 name = buf ;
  160.                 fclose(outfile) ;
  161.                 }
  162.  
  163.         if (!(outfile = fopen(name, "w"))) os_perror(name) ;
  164.  
  165.         if (output_fname) free(output_fname) ;
  166.         output_fname = strsave(name) ;
  167.         description = xmalloc(strlen(name) + 6) ;
  168.         strcpy(description, name) ;
  169.         strcat(description, ".desc") ;
  170.         (void) rename(TEMPFILENAME, description) ;
  171.         free(description) ;
  172.  
  173.         fprintf(stdout, "%s (%s)\n", output_fname, contentType) ;
  174.         didchat = 1;
  175.         return outfile ;
  176.         }
  177.  
  178. /* Warn user that MD5 digest didn't match */
  179. void
  180. os_warnMD5mismatch(void) {
  181.         char *warning;
  182.  
  183.         warning = xmalloc(strlen(output_fname) + 100);
  184.         sprintf(warning, "%s was corrupted in transit", output_fname);
  185.         warn(warning);
  186.         free(warning);
  187.         }
  188.  
  189. /* Report an error (in errno) concerning a filename */
  190. void
  191. os_perror(char *file) {
  192.         if (errno != EOSERR) perror(file);
  193.         else poserr(file) ;
  194.         }
  195.  
  196. /*
  197.  * This getenv will use the WB2.0 calls if you have the 2.0
  198.  * rom. If not, it resorts to looking in the ENV: directory.
  199.  */
  200.  
  201. char *
  202. getenv (const char *name) {
  203.         FILE *fp;
  204.         char *ptr;
  205.         static char value[256] ;
  206.         static char buf[256] ;
  207.  
  208.         /* 2.0 style? */
  209.         if (DOSBase->dl_lib.lib_Version >= 36) {
  210.                 if (GetVar ((char *)name,value,256,0L) == -1) return NULL;
  211.         } else {
  212.                 if (strlen (name) > 252) return NULL;
  213.                 strcpy (buf,"ENV:");
  214.                 strcpy (&buf[4],name);
  215.                 if (! (fp = fopen(buf,"r"))) return NULL;
  216.                 for (ptr = value; (*ptr=getc(fp))!=EOF
  217.                         && *ptr != '\n'
  218.                         && ++ptr < &value[256];);
  219.                 fclose(fp);
  220.                 *ptr = 0;
  221.                 }
  222.         return value;
  223.         }
  224.  
  225. /* Get configuarion, either from a file or from a variable */
  226.  
  227. char * myGetConfig(char *keyword, char *def) {
  228.         char *entry;
  229.  
  230. #ifndef __PPC__
  231.         if (NetSupportBase)
  232.                 return (char *) GetConfig(NULL, keyword, NULL, def);
  233. #endif
  234.  
  235.         entry = getenv(keyword);  /* Library is NOT available */
  236.         return ((entry) ? entry : def);
  237.         }
  238.  
  239. /* (Don't) Handle a BinHex'ed file */
  240.  
  241. int
  242. os_binhex(FILE *infile, int dummy1, int dummy2) {
  243.         return 1;
  244.         }
  245. /*
  246.  * Close a file opened by os_newTypedFile()
  247.  */
  248. int
  249. os_closetypedfile(FILE *outfile) {
  250.         return fclose(outfile);
  251.         }
  252.