24.01.2014 Views

Codice

Codice

Codice

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

388 volume VI os16<br />

Sorgenti della libreria generale 389<br />

«<br />

«<br />

3940027 | //<br />

3940028 | sys (SYS_UAREA, &msg, (sizeof msg));<br />

3940029 | //<br />

3940030 | // Check that the path is still correctly terminated. If it isn’t,<br />

3940031 | // the path is longer than the implementation limits, and it is<br />

3940032 | // really *bad*.<br />

3940033 | //<br />

3940034 | if (msg.path_cwd[PATH_MAX-1] != 0)<br />

3940035 | {<br />

3940036 | errset (E_LIMIT); // Exceeded implementation limits.<br />

3940037 | return (NULL);<br />

3940038 | }<br />

3940039 | //<br />

3940040 | // If the path is larger than the buffer size, return an error.<br />

3940041 | // Please note that the parameter ‘size’ must include the<br />

3940042 | // terminating null character, so, if the string is equal to<br />

3940043 | // the size, it is already beyond the size limit.<br />

3940044 | //<br />

3940045 | if (strlen (msg.path_cwd) >= size)<br />

3940046 | {<br />

3940047 | errset (ERANGE); // Result too large.<br />

3940048 | return (NULL);<br />

3940049 | }<br />

3940050 | //<br />

3940051 | // Everything is fine, so, copy the path to the buffer and return.<br />

3940052 | //<br />

3940053 | strncpy (buffer, msg.path_cwd, size);<br />

3940054 | return (buffer);<br />

3940055 |}<br />

105.17.19 lib/unistd/geteuid.c<br />

Si veda la sezione 97.18.<br />

3950001 |#include <br />

3950002 |#include <br />

3950003 |#include <br />

3950004 |#include <br />

3950005 |//----------------------------------------------------------------------<br />

3950006 |uid_t<br />

3950007 |geteuid (void)<br />

3950008 |{<br />

3950009 | sysmsg_uarea_t msg;<br />

3950010 | sys (SYS_UAREA, &msg, (sizeof msg));<br />

3950011 | return (msg.euid);<br />

3950012 |}<br />

105.17.20 lib/unistd/getopt.c<br />

Si veda la sezione 98.52.<br />

3960001 |#include <br />

3960002 |#include <br />

3960003 |#include <br />

3960004 |#include <br />

3960005 |//----------------------------------------------------------------------<br />

3960006 |char *optarg;<br />

3960007 |int optind = 1;<br />

3960008 |int opterr = 1;<br />

3960009 |int optopt = 0;<br />

3960010 |//----------------------------------------------------------------------<br />

3960011 |static void getopt_no_argument (int opt);<br />

3960012 |//----------------------------------------------------------------------<br />

3960013 |int<br />

3960014 |getopt (int argc, char *const argv[], const char *optstring)<br />

3960015 |{<br />

3960016 | static int o = 0; // Index to scan grouped options.<br />

3960017 | int s; // Index to scan ‘optstring’<br />

3960018 | int opt; // Current option letter.<br />

3960019 | int flag_argument; // If there should be an argument.<br />

3960020 | //<br />

3960021 | // Entering the function, ‘flag_argument’ is zero. Just to make<br />

3960022 | // it clear:<br />

3960023 | //<br />

3960024 | flag_argument = 0;<br />

3960025 | //<br />

3960026 | // Scan ‘argv[]’ elements, starting form the value that ‘optind’<br />

3960027 | // already have.<br />

3960028 | //<br />

3960029 | for (; optind < argc; optind++)<br />

3960030 | {<br />

3960031 | //<br />

3960032 | // If an option is expected, some check must be done at<br />

3960033 | // the beginning.<br />

3960034 | //<br />

3960035 | if (!flag_argument)<br />

3960036 | {<br />

3960037 | //<br />

3960038 | // Check if the scan is finished and ‘optind’ should be kept<br />

3960039 | // untouched:<br />

3960040 | // ‘argv[optind]’ is a null pointer;<br />

3960041 | // ‘argv[optind][0]’ is not the character ‘-’;<br />

3960042 | // ‘argv[optind]’ points to the string "-";<br />

3960043 | // all ‘argv[]’ elements are parsed.<br />

3960044 | //<br />

3960045 | if (argv[optind] == NULL<br />

3960046 | || argv[optind][0] != ’-’<br />

3960047 | || argv[optind][1] == 0<br />

3960048 | || optind >= argc)<br />

3960049 | {<br />

3960050 | return (-1);<br />

3960051 | }<br />

3960052 | //<br />

3960053 | // Check if the scan is finished and ‘optind’ is to be<br />

3960054 | // incremented:<br />

3960055 | // ‘argv[optind]’ points to the string "--".<br />

3960056 | //<br />

3960057 | if (argv[optind][0] == ’-’<br />

3960058 | && argv[optind][1] == ’-’<br />

3960059 | && argv[optind][2] == 0)<br />

3960060 | {<br />

3960061 | optind++;<br />

3960062 | return (-1);<br />

3960063 | }<br />

3960064 | }<br />

3960065 | //<br />

3960066 | // Scan ‘argv[optind]’ using the static index ‘o’.<br />

3960067 | //<br />

3960068 | for (; o < strlen (argv[optind]); o++)<br />

3960069 | {<br />

3960070 | //<br />

3960071 | // If there should be an option, index ‘o’ should<br />

3960072 | // start from 1, because ‘argv[optind][0]’ must<br />

3960073 | // be equal to ’-’.<br />

3960074 | //<br />

3960075 | if (!flag_argument && (o == 0))<br />

3960076 | {<br />

3960077 | //<br />

3960078 | // As there is no options, ‘o’ cannot start<br />

3960079 | // from zero, so a new loop is done.<br />

3960080 | //<br />

3960081 | continue;<br />

3960082 | }<br />

3960083 | //<br />

3960084 | if (flag_argument)<br />

3960085 | {<br />

3960086 | //<br />

3960087 | // There should be an argument, starting from<br />

3960088 | // ‘argv[optind][o]’.<br />

3960089 | //<br />

3960090 | if ((o == 0) && (argv[optind][o] == ’-’))<br />

3960091 | {<br />

3960092 | //<br />

3960093 | // ‘argv[optind][0]’ is equal to ’-’, but there<br />

3960094 | // should be an argument instead: the argument<br />

3960095 | // is missing.<br />

3960096 | //<br />

3960097 | optarg = NULL;<br />

3960098 | //<br />

3960099 | if (optstring[0] == ’:’)<br />

3960100 | {<br />

3960101 | //<br />

3960102 | // As the option string starts with ‘:’ the<br />

3960103 | // function must return ‘:’.<br />

3960104 | //<br />

3960105 | optopt = opt;<br />

3960106 | opt = ’:’;<br />

3960107 | }<br />

3960108 | else<br />

3960109 | {<br />

3960110 | //<br />

3960111 | // As the option string does not start with ‘:’<br />

3960112 | // the function must return ‘?’.<br />

3960113 | //<br />

3960114 | getopt_no_argument (opt);<br />

3960115 | optopt = opt;<br />

3960116 | opt = ’?’;<br />

3960117 | }<br />

3960118 | //<br />

3960119 | // ‘optind’ is left untouched.<br />

3960120 | //<br />

3960121 | }<br />

3960122 | else<br />

3960123 | {<br />

3960124 | //<br />

3960125 | // The argument is found: ‘optind’ is to be<br />

3960126 | // incremented and ‘o’ is reset.<br />

3960127 | //<br />

3960128 | optarg = &argv[optind][o];<br />

3960129 | optind++;<br />

3960130 | o = 0;<br />

3960131 | }<br />

3960132 | //<br />

3960133 | // Return the option, or ’:’, or ’?’.<br />

3960134 | //<br />

3960135 | return (opt);<br />

3960136 | }<br />

3960137 | else<br />

3960138 | {<br />

3960139 | //<br />

3960140 | // It should be an option: ‘optstring[]’ must be<br />

3960141 | // scanned.<br />

3960142 | //<br />

3960143 | opt = argv[optind][o];<br />

3960144 | //<br />

3960145 | for (s = 0, optopt = 0; s < strlen (optstring); s++)<br />

3960146 | {<br />

3960147 | //<br />

3960148 | // If ‘optsting[0]’ is equal to ’:’, index ‘s’ must<br />

3960149 | // start at 1.<br />

3960150 | //<br />

3960151 | if ((s == 0) && (optstring[0] == ’:’))<br />

3960152 | {<br />

3960153 | continue;<br />

3960154 | }<br />

3960155 | //<br />

3960156 | if (opt == optstring[s])<br />

3960157 | {<br />

3960158 | //<br />

3960159 | if (optstring[s+1] == ’:’)<br />

3960160 | {<br />

3960161 | //<br />

3960162 | // There is an argument.<br />

3960163 | //<br />

3960164 | flag_argument = 1;<br />

3960165 | break;<br />

3960166 | }<br />

3960167 | else<br />

3960168 | {<br />

3960169 | //<br />

3960170 | // There is no argument.<br />

3960171 | //<br />

3960172 | o++;<br />

3960173 | return (opt);<br />

3960174 | }<br />

3960175 | }<br />

3960176 | }<br />

3960177 | //<br />

3960178 | if (s >= strlen (optstring))<br />

3960179 | {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!