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