350 volume VI os16 Sorgenti della libreria generale 351 « « « 2980038 | // 2980039 | // At this point, ‘name[f]’ and ‘_environment[e][f]’ are 2980040 | // different: if ‘name[f]’ is zero the name string is 2980041 | // terminated; if ‘_environment[e][f]’ is also equal to ‘=’, 2980042 | // the environment item is corresponding to the requested name. 2980043 | // 2980044 | if (name[f] == 0 && _environment[e][f] == ’=’) 2980045 | { 2980046 | // 2980047 | // The pointer to the beginning of the environment value is 2980048 | // calculated, and the external loop exit. 2980049 | // 2980050 | value = &_environment[e][f+1]; 2980051 | break; 2980052 | } 2980053 | } 2980054 | // 2980055 | // The ‘value’ is returned: if it is still NULL, then, no 2980056 | // environment variable with the requested name was found. 2980057 | // 2980058 | return (value); 2980059 |} 105.10.12 lib/stdlib/labs.c Si veda la sezione 98.3. 2990001 |#include 2990002 |//---------------------------------------------------------------------- 2990003 |long int 2990004 |labs (long int j) 2990005 |{ 2990006 | if (j < 0) 2990007 | { 2990008 | return -j; 2990009 | } 2990010 | else 2990011 | { 2990012 | return j; 2990013 | } 2990014 |} 105.10.13 lib/stdlib/ldiv.c Si veda la sezione 98.15. 3000001 |#include 3000002 |//---------------------------------------------------------------------- 3000003 |ldiv_t 3000004 |ldiv (long int numer, long int denom) 3000005 |{ 3000006 | ldiv_t d; 3000007 | d.quot = numer / denom; 3000008 | d.rem = numer % denom; 3000009 | return d; 3000010 |} 105.10.14 lib/stdlib/putenv.c Si veda la sezione 98.82. 3010001 |#include 3010002 |#include 3010003 |#include 3010004 |//---------------------------------------------------------------------- 3010005 |extern char *_environment[]; 3010006 |//---------------------------------------------------------------------- 3010007 |int 3010008 |putenv (const char *string) 3010009 |{ 3010010 | int e; // First index: environment table items. 3010011 | int f; // Second index: environment string scan. 3010012 | // 3010013 | // Check if the input is empty. No error is reported. 3010014 | // 3010015 | if (string == NULL || strlen (string) == 0) 3010016 | { 3010017 | return (0); 3010018 | } 3010019 | // 3010020 | // Check if the input is valid: there must be a ‘=’ sign. 3010021 | // Error here is reported. 3010022 | // 3010023 | if (strchr (string, ’=’) == NULL) 3010024 | { 3010025 | errset(EINVAL); // Invalid argument. 3010026 | return (-1); 3010027 | } 3010028 | // 3010029 | // Scan the environment table items, with index ‘e’. The intent is 3010030 | // to find a previous environment variable with the same name. 3010031 | // 3010032 | for (e = 0; e < ARG_MAX/32; e++) 3010033 | { 3010034 | // 3010035 | // Scan the string of the environment item, with index ‘f’. 3010036 | // The scan continue until ‘string[f]’ and ‘_environment[e][f]’ 3010037 | // are equal. 3010038 | // 3010039 | for (f = 0; 3010040 | f < ARG_MAX/16-1 && string[f] == _environment[e][f]; 3010041 | f++) 3010042 | { 3010043 | ; // Just scan. 3010044 | } 3010045 | // 3010046 | // At this point, ‘string[f-1]’ and ‘_environment[e][f-1]’ 3010047 | // should contain ‘=’. If it is so, the environment is replaced. 3010048 | // 3010049 | if (string[f-1] == ’=’ && _environment[e][f-1] == ’=’) 3010050 | { 3010051 | // 3010052 | // The environment item was found: now replace the pointer. 3010053 | // 3010054 | _environment[e] = string; 3010055 | // 3010056 | // Return. 3010057 | // 3010058 | return (0); 3010059 | } 3010060 | } 3010061 | // 3010062 | // The item was not found. Scan again for a free slot. 3010063 | // 3010064 | for (e = 0; e < ARG_MAX/32; e++) 3010065 | { 3010066 | if (_environment[e] == NULL || _environment[e][0] == 0) 3010067 | { 3010068 | // 3010069 | // An empty item was found and the pointer will be 3010070 | // replaced. 3010071 | // 3010072 | _environment[e] = string; 3010073 | // 3010074 | // Return. 3010075 | // 3010076 | return (0); 3010077 | } 3010078 | } 3010079 | // 3010080 | // Sorry: the empty slot was not found! 3010081 | // 3010082 | errset (ENOMEM); // Not enough space. 3010083 | return (-1); 3010084 |} 105.10.15 lib/stdlib/qsort.c Si veda la sezione 98.84. 3020001 |#include 3020002 |#include 3020003 |#include 3020004 |//---------------------------------------------------------------------- 3020005 |static int part (char *array, size_t size, int a, int z, 3020006 | int (*compare)(const void *, const void *)); 3020007 |static void sort (char *array, size_t size, int a, int z, 3020008 | int (*compare)(const void *, const void *)); 3020009 |//---------------------------------------------------------------------- 3020010 |void 3020011 |qsort (void *base, size_t nmemb, size_t size, 3020012 | int (*compare)(const void *, const void *)) 3020013 |{ 3020014 | if (size a) 3020034 | { 3020035 | loc = part (array, size, a, z, compare); 3020036 | if (loc >= 0) 3020037 | { 3020038 | sort (array, size, a, loc-1, compare); 3020039 | sort (array, size, loc+1, z, compare); 3020040 | } 3020041 | } 3020042 |} 3020043 | 3020044 |//---------------------------------------------------------------------- 3020045 |static int 3020046 |part (char *array, size_t size, int a, int z, 3020047 | int (*compare)(const void *, const void *)) 3020048 |{ 3020049 | int i; 3020050 | int loc; 3020051 | char *swap; 3020052 | // 3020053 | if (z
352 volume VI os16 Sorgenti della libreria generale 353 « « 3020080 | { 3020081 | // 3020082 | // Index ‘i’ goes up... 3020083 | // 3020084 | for (;i < loc; i++) 3020085 | { 3020086 | if (compare (&array[i*size], &array[a*size]) > 0) 3020087 | { 3020088 | break; 3020089 | } 3020090 | } 3020091 | // 3020092 | // Index ‘loc’ gose down... 3020093 | // 3020094 | for (;; loc--) 3020095 | { 3020096 | if (compare (&array[loc*size], &array[a*size]) = ’0’ && C