source: tags/1.1/shellenv.cpp @ 5

Last change on this file since 5 was 5, checked in by willem, 11 years ago

willem

File size: 806 bytes
Line 
1#include <string>
2#include "shellenv.h"
3//
4// Example:
5//   shellenv("aap noot mies", "LEESPLANK", SHELL_SH);
6// returns the string:
7//   LEESPLANK='aap noot mies';export LEESPLANK;
8//
9//  "'" characters in s are escaped
10//
11std::string shellenv(std::string s, const std::string name, int shelltype)
12{
13  std::string r = "'";
14  std::string::iterator it;
15  for ( it=s.begin() ; it < s.end(); it++ )
16  {
17    if ( *it == '\'')
18    {
19      r.push_back('\'');
20      r.push_back('\\');
21      r.push_back('\'');
22      r.push_back('\'');
23    }
24    else
25      r.push_back(*it);
26  }
27  r.push_back('\'');
28  switch (shelltype)
29  {
30    case SHELL_CSH:
31      r = "setenv " + name + " " + r + ";\n";
32      break;
33    case SHELL_SH: 
34      r = name + "=" + r + ";" + "export " + name + ";\n";
35      break;
36  }
37  return r;
38}
Note: See TracBrowser for help on using the repository browser.