home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / SWALIBAS.ZIP / SW_MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-09  |  3.4 KB  |  159 lines

  1. /* sw_misc.c - helper routines for swaplib
  2.    Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  3.  
  4.    This file is part of SWAPLIB (the library), a library for efficient
  5.    execution of child processes under MS-DOS.
  6.  
  7.    The library is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    The library is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with the library; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.    $Header: e:/gnu/swaplib/RCS/sw_misc.c'v 0.9 90/09/09 21:44:02 tho Stable $
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28.  
  29. #include <io.h>
  30.  
  31. #include "swaplib.h"
  32.  
  33. extern void *xmalloc (size_t size);
  34. extern void *xrealloc (void *ptr, size_t size);
  35.  
  36. #define FSLASH        '/'
  37. #define BSLASH        '\\'
  38.  
  39. /* Concatenate and copy ARGV[1], ... into CMDLINE_BUF.  */
  40.  
  41. int
  42. _swap_build_cmdline (char **argv)
  43. {
  44.   int i = 0;
  45.  
  46.   /* paste arguments together */
  47.  
  48.   argv++;
  49.  
  50.   while (*argv)
  51.     {
  52.       int len = strlen (*argv);
  53.       if (i + len > 126)
  54.     {
  55.       /* commandline to long for MeSy-DOS */
  56.  
  57.       strcpy (_swap_cmdline_buf, CMDLINE_TOO_LONG);
  58.       errno = E2BIG;
  59.       return -1;
  60.     }
  61.  
  62.       strcpy (_swap_cmdline_buf + i, *argv++);
  63.       i += len + 1;
  64.       _swap_cmdline_buf[i - 1] = ' ';
  65.     }
  66.  
  67.   /* Terminate */
  68.  
  69.   _swap_cmdline_buf[max(i - 1, 0)] = '\0';
  70.  
  71.   return 0;
  72. }
  73.  
  74.  
  75. /* Return pointer to last path component in PATH.  */
  76.  
  77. char *
  78. swap_basename (char *path)
  79. {
  80.   char *ptr;
  81.   char *base;
  82.  
  83.   ptr = strrchr (path, BSLASH);    /* first find last backslash.  */
  84.   if (ptr == NULL)
  85.     ptr = path;
  86.   else
  87.     ptr++;
  88.  
  89.   base = strrchr (ptr, FSLASH);    /* then find last slash.  */
  90.   if (base == NULL)
  91.     base = ptr;
  92.   else
  93.     base++;
  94.  
  95.   return base;
  96. }
  97.  
  98.  
  99. /* Get a unique filename in the temporary directory from the environment
  100.    entry TMP or TEMP, if this fails, use current directory.  (replacement
  101.    for tempnam(), whish is *too* touchy about trailing path separators!) */
  102.  
  103. char *
  104. swap_mktmpname (char *prefix)
  105. {
  106.   register int len;
  107.   register char *ptr;
  108.   register char *tmpname;
  109.   static char default_prefix[3] = "vm";
  110.  
  111.   if (!prefix[0] || !prefix[1])    /* did the user supply a prefix? */
  112.     prefix = default_prefix;
  113.  
  114.   if (!(ptr = getenv ("TMP")) && !(ptr = getenv ("TEMP")))
  115.     {
  116.  
  117.       ptr = ".";
  118.       len = 1;
  119.     }
  120.   else
  121.     {
  122.       len = strlen (ptr) - 1;
  123.       if ((ptr[len] == FSLASH) || (ptr[len] == BSLASH))
  124.     ptr[len] = '\0';
  125.     }
  126.  
  127.   tmpname = xmalloc (len + 10);
  128.  
  129.   sprintf (tmpname, "%s/%2sXXXXXX", ptr, prefix);
  130.   mktemp (tmpname);
  131.  
  132.   return tmpname;
  133. }
  134.  
  135.  
  136. /* Some loosing Microsoft programs need backslashes in their
  137.    filenames, give it to them.  */
  138.  
  139. char *
  140. swap_back_slashify (char *path)
  141. {
  142.   char *p = path;
  143.   while (*p)
  144.     {
  145.       if (*p == FSLASH)
  146.     *p = BSLASH;
  147.       p++;
  148.     }
  149.   return path;
  150. }
  151.  
  152. /* 
  153.  * Local Variables:
  154.  * mode:C
  155.  * ChangeLog:ChangeLog
  156.  * compile-command:make
  157.  * End:
  158.  */
  159.