File: sge_string.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (1770 lines) | stat: -rw-r--r-- 45,519 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 *
 *   Copyright (C) 2012 Dave Love, University of Liverpool
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <pthread.h>
#include <fnmatch.h>
#include <errno.h>

#include "uti/sge_rmon.h"
#include "uti/sge_stdlib.h"
#include "uti/sge_string.h"
#include "uti/sge_log.h"
#include "uti/msg_utilib.h"

#define IS_DELIMITOR(c,delimitor) \
   (delimitor?(strchr(delimitor, c)?1:0):isspace(c))

/****** uti/string/sge_basename() *********************************************
*  NAME
*     sge_basename() -- get basename for path
*
*  SYNOPSIS
*     char* sge_basename(const char *name, int delim) 
*
*  FUNCTION
*     Determines the basename for a path like string - the last field 
*     of a string where fields are separated by a fixed one character 
*     delimiter.
*
*  INPUTS
*     const char *name - contains the input string (path)
*     int delim        - delimiter
*
*  RESULT
*     char* - pointer to base of name after the last delimter
*             NULL if "name" is NULL or zero length string or 
*             delimiter is the last character in "name"
*
*  EXAMPLE
*     sge_basename("/usr/local/bin/flex", '/'); returns "flex"
*
*  NOTES
*     MT-NOTE: sge_basename() is MT safe
******************************************************************************/
const char *sge_basename(const char *name, int delim) 
{
   char *cp;

   DENTER(BASIS_LAYER, "sge_basename");

   if (!name) {
      DRETURN(NULL);
   }
   if (name[0] == '\0') {
      DRETURN(NULL);
   }
   
   cp = strrchr(name, delim);
   if (!cp) {
      DRETURN(name); 
   } else {
      cp++;
      if (*cp == '\0') {
         DRETURN(NULL);
      }
      else {
         DRETURN(cp);
      }
   }
}

/****** uti/string/sge_jobname() ***********************************************
*  NAME
*     sge_jobname() -- get jobname of command line string 
*
*  SYNOPSIS
*     const char* sge_jobname(const char *name) 
*
*  FUNCTION
*     Determine the jobname of a command line. The following definition is used
*     for the jobname:
*     - take everything up to the first semicolon
*     - take everything up to the first whitespace
*     - take the basename
*
*  INPUTS
*     const char *name - contains the input string (command line)
*
*  RESULT
*     const char* - pointer to the jobname
*                   NULL if name is NULL or only '\0'
*
*  EXAMPLE
*  Command line                       jobname
*  ----------------------------------------------
*  "cd /home/me/5five; hostname" --> cd
*  "/home/me/4Ujob"              --> 4Ujob (invalid, will be denied)
*  "cat /tmp/5five"              --> cat
*  "bla;blub"                    --> bla
*  "a b"                         --> a
*      
*
*  NOTES
*     MT-NOTE: sge_jobname() is not MT safe 
*
*  SEE ALSO
*     sge_basename()
*******************************************************************************/
const char *sge_jobname(const char *name) {

   const char *cp = NULL;
   
   DENTER(BASIS_LAYER, "sge_jobname");
   if (name && name[0] != '\0' ) {

      cp = sge_strtok(name, ";");
      cp = sge_strtok(cp, " ");
      cp = sge_basename(cp, '/');

   }

   DRETURN(cp);
}

/****** uti/string/sge_dirname() **********************************************
*  NAME
*     sge_dirname() -- Return first part of string up to deliminator 
*
*  SYNOPSIS
*     char* sge_dirname(const char *name, int delim) 
*
*  FUNCTION
*     The function will return a malloced string containing the first 
*     part of 'name' up to, but not including deliminator. NULL will 
*     be returned if 'name' is NULL or a zero length string or if 
*     'delim' is the first character in 'name' 
*
*  INPUTS
*     const char *name - string 
*     int delim        - deliminator 
*
*  RESULT
*     char* - malloced string
*
*  NOTES
*     This routine is called "dirname" in opposite to "basename"
*     but is mostly used to strip off the domainname of a FQDN     
*     MT-NOTE: sge_dirname() is MT safe
******************************************************************************/
char *sge_dirname(const char *name, int delim) 
{
   char *cp, *cp2;

   DENTER(BASIS_LAYER, "sge_dirname");

   if (!name) {
      DRETURN(NULL);
   }

   if (name[0] == '\0' || name[0] == delim) {
      DRETURN(NULL);
   }

   cp = strchr(name, delim);

   if (!cp) {                   /* no occurence of delim */
      cp2 = strdup(name);
      DRETURN(cp2);
   } else {
      if ((cp2 = malloc((cp - name) + 1)) == NULL) {
         DRETURN(NULL);
      } else {
         strncpy(cp2, name, cp - name);
         cp2[cp - name] = '\0';
         DRETURN(cp2);
      }
   }
}

/****** uti/string/sge_strtok() ***********************************************
*  NAME
*     sge_strtok() -- Replacement for strtok() 
*
*  SYNOPSIS
*     char* sge_strtok(const char *str, const char *delimiter)
*
*  FUNCTION
*     Replacement for strtok(). If no delimiter is given
*     isspace() is used.
*
*  INPUTS
*     const char *str       - string which should be tokenized 
*     const char *delimiter - delimiter string
*
*  RESULT
*     char* - first/next token of str.
*
*  NOTES
*     MT-NOTE: sge_strtok() is not MT safe, use sge_strtok_r() instead
*
*  SEE ALSO
*     uti/string/sge_strtok_r()     
******************************************************************************/
char *sge_strtok(const char *str, const char *delimitor) 
{
   char *cp;
   char *saved_cp;
   static char *static_cp = NULL;
   static char *static_str = NULL;
   static unsigned int alloc_len = 0;
   unsigned int n;
   bool done;

   DENTER(BASIS_LAYER, "sge_strtok");

   if (str) {
      n = strlen(str);
      if (static_str) {
         if (n > alloc_len) {
            /* need more memory */
            sge_free(&static_str);
            static_str = malloc(n + 1);
            alloc_len = n;
         }
      } else {
         static_str = malloc(n + 1);
         alloc_len = n;
      }
      strcpy(static_str, str);
      saved_cp = static_str;
   } else {
      saved_cp = static_cp;
   }

   /* seek first character which is not '\0' and not delimiter */
   done = false;
   while (!done) {

      /* found end of string */
      if (saved_cp == NULL || *saved_cp == '\0') {
         DRETURN(NULL);
      }

      /* eat white spaces */
      if (!IS_DELIMITOR((int) saved_cp[0], delimitor)) {
         done = true;
         break;
      }

      saved_cp++;
   }

   /* seek end of string given by '\0' or delimiter */
   cp = saved_cp;
   done = false;
   while (!done) {
      if (!cp[0]) {
         static_cp = cp;

         DRETURN(saved_cp);
      }

      /* test if we found a delimiter */
      if (IS_DELIMITOR((int) cp[0], delimitor)) {
         cp[0] = '\0';
         cp++;
         static_cp = cp;

         DRETURN(saved_cp);
      }
      cp++;
   }

   DRETURN(NULL);
}

/****** uti/string/sge_strlcat() ***********************************************
*  NAME
*     sge_strlcat() -- sge strlcat implementation
*
*  SYNOPSIS
*     size_t sge_strlcat(char *dst, const char *src, size_t dstsize)
*
*  FUNCTION
*     appends characters from from "src" to "dst" and terminates
*     the dst string with '\0'
*     Returns the size required for successfully completing the operation.
*
*  INPUTS
*     char *dst       - destination
*     const char *src - source string (must be '\0' terminated)
*     size_t dstsize  - size of destination string
*
*  RESULT
*     size_t - min{dstsize,strlen(dst)}+strlen(src)
*              this is the size required for successfully completing the strcat.
*
*  NOTES
*     MT-NOTE: sge_strlcat() is MT safe
*******************************************************************************/
size_t sge_strlcat(char *dst, const char *src, size_t dstsize) {
   size_t dst_idx = 0, src_idx = 0;

   /* no destination - do nothing */
   if (dst == NULL) {
      return 0;
   }

   /* no source or source empty - do nothing */
   if (src == NULL || src[0] == '\0') {
      return 0;
   }

   /* find end of dst */
   for (dst_idx = 0; (dst[dst_idx] != '\0') && (dst_idx < dstsize - 1); dst_idx++) {
      ;
   }

   /* copy until source ends or destination is full */
   for (src_idx = 0; (src[src_idx] != '\0') && (dst_idx < dstsize - 1); src_idx++, dst_idx++) {
      dst[dst_idx] = src[src_idx];
   }
   dst[dst_idx] = '\0';

   /* compute the space
    * actually consumed (if we could finish the strcat)
    * or would be needed to fully append all characters in src
    */
   while (src[src_idx++] != '\0') {
     dst_idx++;
   }
   dst_idx++; /* we need space for the trailing 0 byte */

   return dst_idx;
}

/****** uti/string/sge_strlcpy() ***********************************************
*  NAME
*     sge_strlcpy() -- sge strlcpy implementation
*
*  SYNOPSIS
*     size_t sge_strlcpy(char *dst, const char *src, size_t dstsize) 
*
*  FUNCTION
*     copies "dstsize"-1 characters from from "src" to "dst" and terminates
*     the src string with '\0'- Returns the size of the "src" string.
*
*  INPUTS
*     char *dst       - destination
*     const char *src - source string (must be '\0' terminated)
*     size_t dstsize  - size of destination string
*
*  RESULT
*     size_t - strlen of src, not dst !!!
*
*  NOTES
*     MT-NOTE: sge_strlcpy() is MT safe
*******************************************************************************/
size_t sge_strlcpy(char *dst, const char *src, size_t dstsize) {
   size_t index = 0;
   if (dst == NULL) {
      return 0;
   }
   if (src == NULL) {
      dst[0] = '\0';
      return 0;
   }
   for (index = 0; (src[index] != '\0') && (index < dstsize - 1); index++) {
      dst[index] = src[index];
   }
   dst[index] = '\0';
   while ( src[index] != '\0') {
     index++;
   }
   return index;
}

/****** uti/string/sge_strtok_r() *********************************************
*  NAME
*     sge_strtok_r() -- Reentrant version of strtok()
*
*  SYNOPSIS
*     char* sge_strtok_r(const char *str, const char *delimiter,
*                        struct saved_vars_s **context) 
*
*  FUNCTION
*     Reentrant version of sge_strtok. When 'str' is not NULL, 
*     '*context' has to be NULL. If 'str' is NULL, '*context'
*     must not be NULL. The caller is responsible for freeing 
*     '*context' with sge_free_saved_vars(). 
*     If no delimiter is given, isspace() is used.
*
*  INPUTS
*     const char *str               - str which should be tokenized 
*     const char *delimiter         - delimiter string
*     struct saved_vars_s **context - context
*
*  RESULT
*     char* - first/next token
*
*  SEE ALSO
*     uti/string/sge_strtok()     
*     uti/string/sge_free_saved_vars()
*
*  NOTES
*     MT-NOTE: sge_strtok_r() is MT safe
******************************************************************************/
char *sge_strtok_r(const char *str, const char *delimitor, 
                   struct saved_vars_s **context) 
{
   char *cp;
   char *saved_cp;
   struct saved_vars_s *saved;
   bool done;

   DENTER(BASIS_LAYER, "sge_strtok_r");

   if (str != NULL) {
      if (*context != NULL) {
         ERROR((SGE_EVENT, SFNMAX, MSG_POINTER_INVALIDSTRTOKCALL));
      }
      *context = (struct saved_vars_s *)malloc(sizeof(struct saved_vars_s));
      memset(*context, 0, sizeof(struct saved_vars_s));
      saved = *context;

      saved->static_str = malloc(strlen(str) + 1);

      strcpy(saved->static_str, str);
      saved_cp = saved->static_str;
   } else {
      if (*context == NULL) {
         ERROR((SGE_EVENT, SFNMAX, MSG_POINTER_INVALIDSTRTOKCALL1));
         DRETURN(NULL);
      }
      saved = *context;
      saved_cp = saved->static_cp;
   }

   /* seek first character which is no '\0' and no delimiter */
   done = false;
   while (!done) {

      /* found end of string */
      if (saved_cp == NULL || *saved_cp == '\0') {
         DRETURN(NULL);
      }

      /* eat white spaces */
      if (!IS_DELIMITOR((int) saved_cp[0], delimitor)) {
         done = true;
         break;
      }

      saved_cp++;
   }

   /* seek end of string given by '\0' or delimiter */
   cp = saved_cp;
   done = false;
   while (!done) {
      if (!cp[0]) {
         saved->static_cp = cp;

         DRETURN(saved_cp);
      }

      /* test if we found a delimiter */
      if (IS_DELIMITOR((int) cp[0], delimitor)) {
         cp[0] = '\0';
         cp++;
         saved->static_cp = cp;

         DRETURN(saved_cp);
      }
      cp++;
   }

   DRETURN(NULL);
}

/****** uti/string/sge_free_saved_vars() **************************************
*  NAME
*     sge_free_saved_vars() -- Free 'context' of sge_strtok_r() 
*
*  SYNOPSIS
*     void sge_free_saved_vars(struct saved_vars_s *context) 
*
*  FUNCTION
*     Free 'context' of sge_strtok_r() 
*
*  INPUTS
*     struct saved_vars_s *context 
*
*  SEE ALSO
*     uti/string/sge_strtok_r() 
*
*  NOTES
*     MT-NOTE: sge_free_saved_vars() is MT safe
******************************************************************************/
void sge_free_saved_vars(struct saved_vars_s *context) 
{
   if (context) {
      if (context->static_str) {
         sge_free(&(context->static_str));
      }
      sge_free(&context);
   }
}

/****** uti/string/sge_strdup() ***********************************************
*  NAME
*     sge_strdup() -- Replacement for strdup() 
*
*  SYNOPSIS
*     char* sge_strdup(char *old, const char *s) 
*
*  FUNCTION
*     Duplicate string 's'. "Use" 'old' buffer.
*
*  INPUTS
*     char *old     - buffer (will be freed) 
*     const char *s - string 
*
*  RESULT
*     char* - malloced string
*
*  NOTES
*     MT-NOTE: sge_strdup() is MT safe
******************************************************************************/
char *sge_strdup(char *old, const char *s) 
{
   char *ret = NULL;

   /* 
    * target (old) and source (s) might point to the same object!
    * therefore free old only after the dup
    */
   if (s != NULL) {
      int n = strlen(s);
      ret = malloc(n + 1);
      if (ret != NULL) {
         strcpy(ret, s);
      }
   }

   /* free and NULL the old pointer */
   sge_free(&old);

   return ret;
}

/****** uti/string/sge_strip_blanks() *****************************************
*  NAME
*     sge_strip_blanks() -- Strip blanks from string 
*
*  SYNOPSIS
*     void sge_strip_blanks(char *str) 
*
*  FUNCTION
*     Strip all blanks contained in a string. The string is used
*     both as source and drain for the necessary copy operations.
*     The string is '\0' terminated afterwards. 
*
*  INPUTS
*     char *str - pointer to string to be condensed 
*
*  NOTES
*     MT-NOTE: sge_strip_blanks() is MT safe
******************************************************************************/
void sge_strip_blanks(char *str) 
{
   char *cp = str;

   DENTER(BASIS_LAYER, "sge_strip_blanks");

   if (!str) {
      DRETURN_VOID;
   }

   while (*str) {
      if (*str != ' ') {
         if (cp != str)
            *cp = *str;
         cp++;
      }
      str++;
   };
   *cp = '\0';

   DRETURN_VOID;
}

/****** uti/string/sge_strip_white_space_at_eol() ******************************
*  NAME
*     sge_strip_white_space_at_eol() -- truncate white space at EOL 
*
*  SYNOPSIS
*     void sge_strip_white_space_at_eol(char *str) 
*
*  FUNCTION
*     Truncate white space from the end of the string 
*
*  INPUTS
*     char *str - string to be modified 
*
*  RESULT
*     void - NONE
*
*  NOTES
*     MT-NOTE: sge_strip_white_space_at_eol() is MT safe 
*******************************************************************************/
void sge_strip_white_space_at_eol(char *str) 
{
   DENTER(BASIS_LAYER, "sge_strip_white_space_at_eol");

   if (str != NULL) {
      size_t length = strlen(str);

      while (str[length - 1] == ' ' || str[length - 1] == '\t') {
         str[length - 1] = '\0';
         length--;
      }
   }
   DRETURN_VOID;
}

/****** uti/string/sge_strip_slash_at_eol() ******************************
*  NAME
*     sge_strip_slash_at_eol() -- truncate slash at EOL 
*
*  SYNOPSIS
*     void sge_strip_slash_at_eol(char *str) 
*
*  FUNCTION
*     Truncate slash from the end of the string 
*
*  INPUTS
*     char *str - string to be modified 
*
*  RESULT
*     void - NONE
*
*  NOTES
*     MT-NOTE: sge_strip_slash_at_eol() is MT safe 
*******************************************************************************/
void sge_strip_slash_at_eol(char *str) 
{
   DENTER(BASIS_LAYER, "sge_strip_slash_at_eol");

   if (str != NULL) {
      size_t length = strlen(str);

      while (str[length - 1] == '/') {
         str[length - 1] = '\0';
         length--;
      }
   }
   DRETURN_VOID;
}


/****** uti/string/sge_delim_str() *******************************************
*  NAME
*     sge_delim_str() -- Trunc. a str according to a delimiter set 
*
*  SYNOPSIS
*     char* sge_delim_str(char *str, char **delim_pos, 
*                         const char *delim) 
*
*  FUNCTION
*     Truncates a string according to a delimiter set. A copy of 
*     the string truncated according to the delimiter set will be 
*     returned.
*
*     ATTENTION: The user is responsible for freeing the allocated
*     memory outside this routine. If not enough space could be 
*     allocated, NULL is returned.
*
*  INPUTS
*     char *str         - string to be truncated 
*     char **delim_pos  - A placeholder for the delimiting position
*                         in str on exit. 
*                         If set on entry the position of the 
*                         delimiter in the input string 'str' is
*                         returned. If no delimiting character in
*                         string was found, the address of the
*                         closing '\0' in 'str' is returned.
*     const char *delim - string containing delimiter characters 
*
*  RESULT
*     char* - Truncated copy of 'str' (Has to be freed by the caller!)
*
*  NOTES
*     MT-NOTE: sge_delim_str() is MT safe
******************************************************************************/
char *sge_delim_str(char *str, char **delim_pos, const char *delim) 
{
   char *cp = NULL; 
   char *tstr = NULL;

   DENTER(BASIS_LAYER, "sge_delim_str");

   /* we want it non-destructive --> we need a copy of str */
   if ((tstr = strdup(str)) == NULL) {
      DRETURN(NULL);
   }

   /* walk through str to find a character contained in delim or a
    * closing \0
    */

   cp = tstr;
   while (*cp) {
      if (strchr(delim, (int) *cp))     /* found */
         break;
      cp++;
   }

   /* cp now either points to a closing \0 or to a delim character */
   if (*cp) {                    /* if it points to a delim character */
      *cp = '\0';                /* terminate str with a \0 */
   }
   if (delim_pos) {              /* give back delimiting position for name */
      *delim_pos = str + strlen(tstr);
   }
   /* delim_pos either points to the delimiter or the closing \0 in str */

   DRETURN(tstr);
}

/****** uti/string/sge_strnullcmp() *******************************************
*  NAME
*     sge_strnullcmp() -- Like strcmp() but honours NULL ptrs.
*
*  SYNOPSIS
*     int sge_strnullcmp(const char *a, const char *b) 
*
*  FUNCTION
*     Like strcmp() apart from the handling of NULL strings.
*     These are treated as being less than any not-NULL strings.
*     Important for sorting lists where NULL strings can occur.  
*
*  INPUTS
*     const char *a - 1st string 
*     const char *b - 2nd string 
*
*  RESULT
*     int - result
*         0 - strings are the same or both NULL 
*        -1 - a < b or a is NULL
*         1 - a > b or b is NULL
*
*  NOTES
*     MT-NOTE: sge_strnullcmp() is MT safe
******************************************************************************/
int sge_strnullcmp(const char *a, const char *b) 
{
   if (!a && b) {
      return -1;
   }
   if (a && !b) {
      return 1;
   }
   if (!a && !b) {
      return 0;
   }
   return strcmp(a, b);
}

/****** uti/string/sge_patternnullcmp() ****************************************
*  NAME
*     sge_patternnullcmp() -- like fnmatch 
*
*  SYNOPSIS
*     int sge_patternnullcmp(const char *str, const char *pattern) 
*
*  FUNCTION
*     Like fnmatch() apart from the handling of NULL strings.
*     These are treated as being less than any not-NULL strings.
*     Important for sorting lists where NULL strings can occur. 
*
*  INPUTS
*     const char *str     - string 
*     const char *pattern - pattern to match 
*
*  RESULT
*     int - result
*         0 - strings are the same or both NULL 
*        -1 - a < b or a is NULL
*         1 - a > b or b is NULL
*
*  NOTES
*   MT-NOTE: fnmatch uses static variables, not MT safe
*******************************************************************************/
int sge_patternnullcmp(const char *str, const char *pattern) 
{
   if (!str && pattern) {
      return -1;
   }
   if (str && !pattern) {
      return 1;
   }
   if (!str && !pattern) {
      return 0;
   }
   return fnmatch(pattern, str, 0);
}


/****** uti/string/sge_strnullcasecmp() ***************************************
*  NAME
*     sge_strnullcasecmp() -- Like strcasecmp() but honours NULL ptrs.
*
*  SYNOPSIS
*     int sge_strnullcasecmp(const char *a, const char *b) 
*
*  FUNCTION
*     Like strcasecmp() apart from the handling of NULL strings.
*     These are treated as being less than any not-NULL strings.
*     Important for sorting lists where NULL strings can occur.  
*
*  INPUTS
*     const char *a - 1st string 
*     const char *b - 2nd string 
*
*  RESULT
*     int - result
*         0 - strings are the same minus case or both NULL 
*        -1 - a < b or a is NULL
*         1 - a > b or b is NULL
*
*  NOTES
*     MT-NOTE: sge_strnullcasecmp() is MT safe
******************************************************************************/
int sge_strnullcasecmp(const char *a, const char *b) 
{
   if (!a && b)
      return -1;
   if (a && !b)
      return 1;
   if (!a && !b)
      return 0;
   return SGE_STRCASECMP(a, b);
}

/****** uti/string/sge_is_pattern() *******************************************
*  NAME
*     sge_is_pattern() -- Test if string contains  wildcard pattern
*
*  SYNOPSIS
*     int sge_is_pattern(const char *s)
*
*  FUNCTION
*     Check whether string 's' contains a wildcard pattern.
*
*  INPUTS
*     const char *s - string
*
*  RESULT
*     int - result
*         0 - no wildcard pattern
*         1 - it is a wildcard pattern
*
*  NOTES
*     MT-NOTE: sge_is_pattern() is MT safe
******************************************************************************/
bool sge_is_pattern(const char *s) 
{
   char c;
   while ((c = *s++)) {
      switch (c) {
         case '*':
         case '?':
         case '[':
         case ']':
         return true;
      }
   }
   return false;
}

/****** uti/string/sge_is_expression() *******************************************
*  NAME
*     sge_is_expression() -- Test if string contains expressions & wildcard pattern
*
*  SYNOPSIS
*     int sge_is_expression(const char *s)
*
*  FUNCTION
*     Check whether string 's' contains a expressions & a wildcard pattern.
*
*  INPUTS
*     const char *s - string
*
*  RESULT
*     int - result
*         0 - no wildcard pattern
*         1 - it is a wildcard pattern
*
*  NOTES
*     MT-NOTE: sge_is_expression() is MT safe
******************************************************************************/
bool sge_is_expression(const char *s) 
{
   char c;
   
   if (s != NULL) {
      while (*s != '\0') {
         c = *s;
         switch (c) {
            case '*':
            case '?':
            case '[':
            case ']':
            case '&':
            case '|':
            case '!':
            case '(':
            case ')':
           return true;
         }
         s++;
      }
   }
   return false;
}


/****** uti/string/sge_strisint() *********************************************
*  NAME
*     sge_strisint() -- Is string a integer value in characters?
*
*  SYNOPSIS
*     int sge_strisint(const char *str) 
*
*  FUNCTION
*     May we convert 'str' to int? 
*
*  INPUTS
*     const char *str - string 
*
*  RESULT
*     int - result
*         0 - It is no integer
*         1 - It is a integer
*
*  NOTES
*     MT-NOTE: sge_strisint() is MT safe
*
******************************************************************************/
int sge_strisint(const char *str) 
{
   const char *cp = str;
 
   while (*cp) {
      if (!isdigit((int) *cp++)) {
         return 0;
      }
   }
   return 1;
}            

/****** uti/string/sge_strtoupper() *******************************************
*  NAME
*     sge_strtoupper() -- Convert the first n to upper case 
*
*  SYNOPSIS
*     void sge_strtoupper(char *buffer, int max_len) 
*
*  FUNCTION
*     Convert the first 'max_len' characters to upper case. 
*
*  INPUTS
*     char *buffer - string 
*     int max_len  - number of chars 
*
*  NOTES
*     MT-NOTE: sge_strtoupper() is MT safe
******************************************************************************/
void sge_strtoupper(char *buffer, int max_len) 
{
   DENTER(BASIS_LAYER, "sge_strtoupper");

   if (buffer != NULL) {
      int i;
      int length = MIN(strlen(buffer), max_len);
      for (i = 0; i < length; i++) {
         buffer[i] = toupper(buffer[i]); 
      }
   }
   DRETURN_VOID;
} 

/****** uti/hostname/sge_strtolower() ********************************************
*  NAME
*     sge_strtolower() -- convert all upper character in the string to lower case
*
*  SYNOPSIS
*     int sge_strtolower(char *buffer)
*
*  FUNCTION
*     sge_strtolower() for hostnames. Honours some configuration values:
*
*  INPUTS
*     char *buffer - string to be lowered
*
*  RESULT
*     no result, this function modify the argument string
*
*  SEE ALSO
*     uti/string/sge_strtoupper()
*
*  NOTES:
*     MT-NOTE: sge_strtolower() is MT safe
******************************************************************************/
void sge_strtolower(char *buffer, int max_len)
{
   DENTER(BASIS_LAYER, "sge_strtolower");
   if (buffer != NULL) {
      int i;
      for(i=0;buffer[i]!='\0' && i<max_len ;i++){
         buffer[i]=tolower(buffer[i]);
      }
   }
   DRETURN_VOID;
}

/****** uti/string/sge_stradup() **********************************************
*  NAME
*     sge_stradup() -- Duplicate array of strings
*
*  SYNOPSIS
*     char** sge_stradup(char **cpp, int n) 
*
*  FUNCTION
*     Copy list of character pointers including the strings these
*     pointers refer to. If 'n' is 0 strings are '\0'-delimited, if 
*     'n' is not 0 we use n as length of the strings. 
*
*  INPUTS
*     char **cpp - pointer to array of strings 
*     int n      - '\0' terminated? 
*
*  RESULT
*     char** - copy of 'cpp'
*
*  NOTES
*     MT-NOTE: sge_stradup() is MT safe
******************************************************************************/
char **sge_stradup(char **cpp, int n)
{
   int count = 0, len;
   char **cpp1, **cpp2, **cpp3;
 
   /* first count entries */
   cpp2 = cpp;
   while (*cpp2) {
      cpp2++;
      count++;
   }
 
   /* alloc space */
   cpp1 = (char **) malloc((count + 1) * sizeof(char **));
   if (!cpp1)
      return NULL;
 
   /* copy  */
   cpp2 = cpp;
   cpp3 = cpp1;
   while (*cpp2) {
      /* alloc space */
      if (n)
         len = n;
      else
         len = strlen(*cpp2) + 1;
 
      *cpp3 = (char *) malloc(len);
      if (!(*cpp3)) {
         while ((--cpp3) >= cpp1) {
            sge_free(cpp3);
         }
         sge_free(&cpp1);
         return NULL;
      }
 
      /* copy string */
      memcpy(*cpp3, *cpp2, len);
      cpp3++;
      cpp2++;
   }
 
   *cpp3 = NULL;
 
   return cpp1;
}  

/****** uti/string/sge_strafree() *********************************************
*  NAME
*     sge_strafree() -- Free list of character pointers 
*
*  SYNOPSIS
*     void sge_strafree(char **cpp) 
*
*  FUNCTION
*     Free list of character pointers 
*
*  INPUTS
*     char ***cpp - Pointer to array of string pointers 
*
*  NOTES
*     MT-NOTE: sge_strafree() is MT safe
******************************************************************************/
void sge_strafree(char ***cpp)
{
   if (cpp != NULL && *cpp != NULL) {
      char **cpp1 = *cpp;
    
      while (*cpp1 != NULL) {
         sge_free(cpp1);
         cpp1++;
      }
      sge_free(cpp);
   }
}

/****** uti/string/sge_stramemncpy() ******************************************
*  NAME
*     sge_stramemncpy() -- Find string in string array 
*
*  SYNOPSIS
*     char** sge_stramemncpy(const char *cp, char **cpp, int n) 
*
*  FUNCTION
*     Compare string with string field and return the pointer to
*     the matched character pointer. Compare exactly n chars 
*     case insensitive.
*
*  INPUTS
*     const char *cp - string to be found 
*     char **cpp     - pointer to array of strings 
*     int n          - number of chars 
*
*  NOTES:
*     MT-NOTE: sge_stramemncpy() is MT safe
*
*  RESULT
*     char** - NULL or pointer a string
*
*  NOTES
*     MT-NOTE: sge_stramemncpy() is MT safe
******************************************************************************/
char **sge_stramemncpy(const char *cp, char **cpp, int n)
{
   while (*cpp) {
      if (!memcmp(*cpp, cp, n)) {
         return cpp;
      }
      cpp++;
   }
   return NULL;
}     

/****** uti/string/sge_stracasecmp() ******************************************
*  NAME
*     sge_stracasecmp() -- Find string in string array 
*
*  SYNOPSIS
*     char** sge_stracasecmp(const char *cp, char **cpp) 
*
*  FUNCTION
*     Compare string with string field and return the pointer to
*     the matched character pointer. Compare case sensitive. 
*
*  INPUTS
*     const char *cp - string 
*     char **cpp     - pointer to array of strings  
*
*  RESULT
*     char** - NULL or pointer a string
*
*  NOTES
*     MT-NOTE: sge_stracasecmp() is MT safe
******************************************************************************/
char **sge_stracasecmp(const char *cp, char **cpp)
{
   while (*cpp) {
      if (!strcasecmp(*cpp, cp))
         return cpp;
      cpp++;
   }
   return NULL;
}   

#if unused
void
stra_printf(char *stra[])
{
   int i = 0;

   while (stra[i] != NULL) {
      fprintf(stdout, "%s\n", stra[i]);
      i++;
   }
}
#endif

/****** uti/string/stra_from_str() ********************************************
*  NAME
*     str_from_str() -- Extract valid qstat options/paramers from qstat profile.
*
*  SYNOPSIS
*     char **str_from_str(const char *source_str, const char *delim);
*
*  FUNCTION
*     Parse string 'source_str' based on delimeter(s) 'delim' and store
*     resulting tokens in string array 'ret'. Supports comment lines.
*
*  INPUTS
*     const char *source_str - File content of qstat profile as plain string.
*     const char *delim      - Sequence of characters used to identify tokens
*                              and parameters.
*
*  RESULT
*     char** - String array containing tokens found based on 'delim'.
*
*  NOTES
*     It is the caller's responsibilty to free dynamic memory allocated in this
*     routine.
*     MT-NOTE: stra_from_str() is MT safe.
*
*************************************************************************************/
char **
stra_from_str(const char *source_str, const char *delim)
{
   char **ret = NULL;

   if (source_str != NULL && delim != NULL) {
      struct saved_vars_s *context1;
      struct saved_vars_s *context2;
      const char *token_1 = NULL;
      const char *token_2 = NULL;
      int number_of_tokens = 0;
      int index = 0;

      /*
       * Support of comment lines and multiple options per line
       * in qstat profiles requires two level parsing. First
       * level works on a per line basis (delimiter `\n') and
       * sorts out lines starting with comment sign '#'.
       *
       * The result of this process is passed to second
       * level parsing which scans for options and parameters
       * per line. Delimiters are ' ', '\n' and '\t'.
       *
       * We basically need to do this twice: first to determine
       * the number of valid tokens and then to populate the
       * string array.
       *
       */
      /*
       * Count tokens.
       */
      context1 = NULL;
      token_1 = sge_strtok_r(source_str, "\n", &context1);
      while (token_1 != NULL) {
         if (token_1[0] != '#') {
            context2 = NULL;
            token_2 = sge_strtok_r(token_1, " \t", &context2);
            while (token_2 != NULL) {
               token_2 = sge_strtok_r(NULL, " \t", &context2);
               number_of_tokens++;
            }
            sge_free_saved_vars(context2);
         }
         token_1 = sge_strtok_r(NULL, "\n", &context1);
      }
      sge_free_saved_vars(context1);

      /*
       * Note that we need to proceed from here even if we got
       * no valid options/parameters. This is because caller
       * expects a zero entry string array with stopper as minimum.
       */

      /* malloc array memory */
      ret = (char **) malloc(sizeof(char*) * (number_of_tokens + 1));

      if (ret != NULL) {
         /*
          * Allocate and populate string array.
          */
         index = 0;
         context1 = NULL;
         token_1 = sge_strtok_r(source_str, "\n", &context1);
         while (token_1 != NULL) {
            if (token_1[0] != '#') {
               context2 = NULL;
               token_2 = sge_strtok_r(token_1, " \t", &context2);
               while (token_2 != NULL) {
                  ret[index] = strdup(token_2);
                  token_2 = sge_strtok_r(NULL, " \t", &context2);
                  index++;
               }
               sge_free_saved_vars(context2);
            }
            token_1 = sge_strtok_r(NULL, "\n", &context1);
         }
         sge_free_saved_vars(context1);
         ret[index] = NULL; /* Stopper */
      }
   }
   return ret;
}

/****** uti/string/sge_compress_slashes() *************************************
*  NAME
*     sge_compress_slashes() -- compresses sequences of slashes 
*
*  SYNOPSIS
*     void sge_compress_slashes(char *str) 
*
*  FUNCTION
*     Compresses sequences of slashes in str to one slash 
*
*  INPUTS
*     char *str - string (e.g. path) 
*
*  NOTES
*     MT-NOTE: sge_compress_slashes() is MT safe
*******************************************************************************/
void sge_compress_slashes(char *str)
{
   char *p;
   int compressed = 0;
   DENTER(BASIS_LAYER, "sge_compress_slashes");

   for (p = str; *p; p++) {
      while (*p == '/' && *(p+1) == '/') {
         compressed = 1;
         *p = '\0';
         p++;
      }
      if (compressed) {
         strcat(str, p);
         compressed = 0;
      }
   }
   DRETURN_VOID;
}

/****** uti/string/sge_strip_quotes() *****************************************
*  NAME
*     sge_strip_quotes() -- Strip quotes from string
*
*  SYNOPSIS
*     void sge_strip_quotes(char **pstr) 
*
*  FUNCTION
*     Strip quotes from "pstr". 
*
*  INPUTS
*     char **pstr - string to be modified 
*
*  NOTES
*     MT-NOTE: sge_strip_quotes() is MT safe
******************************************************************************/
void sge_strip_quotes(char **pstr) 
{
   char *cp = NULL;
   char *cp2 = NULL;

   DENTER(TOP_LAYER, "sge_strip_quotes");
   
   if (!pstr) {
      DRETURN_VOID;
   }
   
   for (; *pstr; pstr++) {
      for (cp2 = cp = *pstr; *cp; cp++) {
         if ((*cp != '"') && (*cp != '\'')) {
            *cp2++ = *cp;
         }
      }
      
      *cp2 = '\0';
   }
   
   DRETURN_VOID;
}

/*
** problem: modifies input string,
** this is the most frequently used mode
** but allocating extra memory (as it was in
** sge_string2list) should also be possible
** problem: default delimiters should be possible
** note: there is a similar cull function lString2List
*/
/*
** NAME
**   string_list
** PARAMETER
**   str       -    string to be parsed
**   delis     -    string containing delimiters
**   pstr      -    NULL or string array to return
** RETURN
**   NULL      -    error
**   char **   -    pointer to an array of strings containing
**                  the string list
** EXTERNAL
**
** DESCRIPTION
**
** NOTES
**     MT-NOTE: string_list() is MT safe
**
*/
char **string_list(char *str, char *delis, char **pstr) 
{
   unsigned int i = 0, j = 0;
   bool is_space = false;
   int found_first_quote = 0;
   char **head = NULL;
   bool done;

   DENTER(BASIS_LAYER, "string_list");

   if (str == NULL) {
      DRETURN(NULL);
   }

   /* skip heading delimiters */
   while (str[0] != '\0' && strchr(delis, str[0]) != NULL) {
      str++;
   }

   /* at str end: str either was an empty string or only contained delimiters */
   if (str[0] == '\0') {
      DRETURN(NULL);
   }

   /*
    * not more items than length of string is possible
    */
   if (pstr == NULL) {
      head = malloc((sizeof(void *)) * (strlen(str) + 1));
      if (head == NULL) {
         DRETURN(NULL);
      }
   } else {
      head = pstr;
   }

   done = false;
   while (!done) {
      while ((str[i] != '\0') && (strchr(delis, str[i]) != NULL)) {
         i++;
      }

      if (str[i] == '\0') {
         done = true;
         break;
      }

      head[j] = &str[i];
      j++;
      /*
      ** parse one string
      */
      is_space = false;
      
      while ((str[i] != '\0') && !is_space) {
         if ((found_first_quote == 0) && (str[i] == '"')) {
            found_first_quote = 2;
         } else if ((found_first_quote == 0) && (str[i] == '\'')) {
            found_first_quote = 1;
         }

         i++;

         /* If we're inside quotes, we don't count spaces. */
         if (found_first_quote == 0) {
            is_space = (bool)(strchr(delis, str[i]) != NULL);
         }
         
         if (((found_first_quote == 2) && (str[i] == '"')) ||
             ((found_first_quote == 1) && (str[i] == '\''))) {
            found_first_quote = 0;
         }
      }
      
      if (str[i] == '\0') {
         done = true;
         break;
      }

      str[i] = '\0';
      i++;
   }
   
   head[j] = NULL;

   DRETURN(head);
}

/****** uti/string/sge_strerror() **********************************************
*  NAME
*     sge_strerror() -- replacement for strerror
*
*  SYNOPSIS
*     const char* 
*     sge_strerror(int errnum) 
*
*  FUNCTION
*     Returns a string describing an error condition set by system 
*     calls (errno).
*
*     Wrapper arround strerror. Access to strerrror is serialized by the
*     use of a mutex variable to make strerror thread safe.
*
*  INPUTS
*     int errnum        - the errno to explain
*     dstring *buffer   - buffer into which the error message is written
*
*  RESULT
*     const char* - pointer to a string explaining errnum
*
*  NOTES
*     MT-NOTE: sge_strerror() is MT safe
*******************************************************************************/
const char *
sge_strerror(int errnum, dstring *buffer)
{
   static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
   const char *ret;

   pthread_mutex_lock(&mtx);
   ret = strerror(errnum);
   ret = sge_dstring_copy_string(buffer, ret);
   pthread_mutex_unlock(&mtx);

   return ret;
}

/****** uti/string/sge_str_is_number() *****************************************
*  NAME
*     sge_str_is_number() -- check if a string represents a number
*
*  SYNOPSIS
*     bool sge_str_is_number(const char *string)
*
*  FUNCTION
*     This function returns true if the given string represents a
*     floating point number.
*
*  INPUTS
*     const char *string - string to parse
*
*  RESULT
*     bool - result
*        true  - string represents a number
*        false - string is not a number
*
*  NOTES
*     MT-NOTE: sge_str_is_number() is MT safe
*  SEE ALSO
*     man strtod, man strol contains a C example on GNU/Linux
*******************************************************************************/
bool sge_str_is_number(const char *string)
{
   bool ret = true;
   char *end = NULL;
   double val;

   errno = 0;
   val = strtod(string, &end);

   if (errno == ERANGE) {
      if (val == 0) {
         /* underflow - TODO: do we count this as number? */
         ret = false;
      } else {
         /* overflow - TODO: do we count this as number? */
         ret = false;
      }
   }

   if (end == string) {
      /* no digits found */
      ret = false;
   } else if (*end != '\0') {
      /* additional characters after number found */
      ret = false;
   }

   return ret;
}

/****** uti/string/sge_replace_substring() *****************************************
*  NAME
*     sge_replace_substring - replace sub strings in a string
*
*  SYNOPSIS
*     const char *sge_replace_substring(const char *input, char *old, char *new)
*
*  FUNCTION
*     Replaces all occurences of old with new.
*     If old is part of the given string input, a new string is returned
*     where the replacement is done.
*
*  INPUTS
*     const char *input - the input string
*     const char *old   - the string to replace
*     const char *new   - the replacement string
*
*  RESULT
*     NULL, if the input string didn't contain the pattern,
*     else a newly allocated string containing the input string with replacements.
*
*  NOTES
*     MT-NOTE: sge_str_is_number() is MT safe 
*     It is the responsibility of the caller to free the returned string!
*
*******************************************************************************/
const char *sge_replace_substring(const char *input, const char *old, const char *new)
{
   int to_replace = 0;
   int change, new_len;
   char *new_string = NULL;
   char *return_string = NULL;
   char *source = NULL;
   char *source_string = NULL;
   char *tail = NULL;
   char *current_tail = NULL;

   /*
    * Basic sanity checks first.
    */
   if (input == NULL || old == NULL || new == NULL) {
      return NULL;
   }
   /*
    * Determine number for of substrings to replace. We are
    * careful NOT to overrun source string.
    */
   source = source_string = (char *)input;
   tail = source_string + strlen(source_string) - 1;
   while (source <= tail) {
      current_tail = source + strlen(old) - 1;
      if (current_tail > tail) {
         break;
      }
      if (memcmp(old, source, strlen(old)) == 0) {
         to_replace++;
      }
      source++;
   }
   if (to_replace == 0) {
      return NULL;
   }
   /*
    * Calculate size of new string based on number of substrings to replace.
    */
   change = to_replace * (strlen(new) - strlen(old));
   new_len = strlen(source_string) + change + 1;
   /*
    * Allocate new string and re-shuffle original string.
    */
   return_string = new_string = malloc(new_len);
   if (new_string == NULL) {
      return NULL;
   }
   memset(new_string, 0x0, new_len);
   source = source_string;
   while (source <= tail) {
      current_tail = source + strlen(old) - 1;
      if (current_tail <= tail && memcmp(old, source, strlen(old)) == 0) {
         memcpy(new_string, new, strlen(new));
         new_string += strlen(new);
         source += strlen(old);
      } else {
         *new_string = *source;
         source++;
         new_string++;
      }
   }
   return return_string;
}

/****** sge_var/unescape_env_value() *******************************
*  NAME
*     unescape_env_value() -- 
*
*  SYNOPSIS
*     char * unescape_env_value(char *value)
*
*  FUNCTION
*     Undo any backslash escapes (\\ or \n) in value string, returning
*     a new string.
*
*  INPUTS
*     char *value                 - environment variable value
*
*  RESULT
*     new string
*
*  NOTES
*     MT-NOTE: unescape_env_value() is MT safe
*******************************************************************************/

const char *unescape_env_value(const char *value)
{
   char *new_value = strdup(value);
   int i, j, nchars = strlen(value);

   for (i = 0, j = 0; i <= nchars; j++) {
      if ('\\' == value[i])
         switch (value[i+1]) {
         case 'n':
            new_value[j] = '\n';
            i += 2;
            break;
         case '\\':
            new_value[j] = '\\';
            i += 2;
            break;
         default:
            new_value[j] = value[i];
            i++;
         }
      else {                    /* shouldn't happen */
         new_value[j] = value[i];
         i++;
      }
   }
   return new_value;
}