Skip to content
Snippets Groups Projects
NuttxUserGuide.html 201 KiB
Newer Older
patacongo's avatar
patacongo committed
<pre>
patacongo's avatar
patacongo committed
    #include &lt;pthread.h&gt;
    int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
				  const struct timespec *abstime);
patacongo's avatar
patacongo committed
</pre>
<p>
  <b>Description:</b>
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
patacongo's avatar
patacongo committed
  <li><code>To be provided</code>.</li>
patacongo's avatar
patacongo committed
</ul>
<p>
  <b>Returned Values:</b>
</p>
<p>
  If successful, the <code>pthread_cond_timedwait()</code> function will return
  zero (<code>OK</code>).  Otherwise, an error number will be
  returned to indicate the error:
</p>
<ul>
patacongo's avatar
patacongo committed
  <li><code>To be provided</code>. </li>
patacongo's avatar
patacongo committed
5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503
</ul>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadbarrierattrinit">2.9.43 pthread_barrierattr_init</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_barrierattr_init(FAR pthread_barrierattr_t *attr);
</pre>
<p>
  <b>Description:</b>
  The <code>pthread_barrierattr_init()</code> function will initialize a barrier
  attribute object <code>attr</code> with the default value for all of the attributes
  defined by the implementation.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<ul>
  <li>
    <code>attr</code>. Barrier attributes to be initialized.
</li>
</ul>
<p>
  <b>Returned Values:</b>
  0 (<code>OK</code>) on success or <code>EINVAL</code> if <code>attr</code> is invalid.
</p>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadbarrierattrdestroy">2.9.44 pthread_barrierattr_destroy</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_barrierattr_destroy(FAR pthread_barrierattr_t *attr);
</pre>
<p>
  <b>Description:</b>
  The <code>pthread_barrierattr_destroy()</code> function will destroy a barrier attributes object.
  A destroyed attributes object can be reinitialized using <code>pthread_barrierattr_init()</code>;
  the results of otherwise referencing the object after it has been destroyed are undefined.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<ul>
  <li>
    <code>attr</code>. Barrier attributes to be destroyed.
</li>
</ul>
<p>
  <b>Returned Values:</b> 0 (OK) on success or EINVAL if attr is invalid.
</p>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadbarrierattrsetpshared">2.9.45 pthread_barrierattr_setpshared</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_barrierattr_setpshared(FAR pthread_barrierattr_t *attr, int pshared);
</pre>
<p>
  <b>Description:</b>
  The process-shared attribute is set to <code>PTHREAD_PROCESS_SHARED</code> to permit
  a barrier to be operated upon by any thread that has access to the memory where the
  barrier is allocated.
  If the process-shared attribute is <code>PTHREAD_PROCESS_PRIVATE</code>, the barrier can
  only be operated upon by threads created within the same process as the thread that
  initialized the barrier.
  If threads of different processes attempt to operate on such a barrier, the behavior is undefined.
  The default value of the attribute is <code>PTHREAD_PROCESS_PRIVATE</code>.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<ul>
  <li><code>attr</code>. Barrier attributes to be modified.</li>
  <li><code>pshared</code>. The new value of the pshared attribute.</li>
</ul>
<p>
  <b>Returned Values:</b>  0 (<code>OK</code>) on success or <code>EINVAL</code> if either
  <code>attr</code> is invalid or <code>pshared</code> is not one of
  <code>PTHREAD_PROCESS_SHARED</code> or <code>PTHREAD_PROCESS_PRIVATE</code>.
</p>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadbarrierattrgetpshared">2.9.46 pthread_barrierattr_getpshared</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_barrierattr_getpshared(FAR const pthread_barrierattr_t *attr, FAR int *pshared);
</pre>
<p>
  <b>Description:</b>
  The <code>pthread_barrierattr_getpshared()</code> function will obtain the value of the
  process-shared attribute from the attributes object referenced by <code>attr</code>.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li><code>attr</code>. Barrier attributes to be queried.</li>
  <li><code>pshared</code>. The location to stored the current value of the pshared attribute.</li>
</ul>
<p>
  <b>Returned Values:</b> 0 (<code>OK</code>) on success or <code>EINVAL</code> if
  either <code>attr</code> or <code>pshared</code> is invalid.
</p>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadbarrierinit">2.9.47 pthread_barrier_init</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_barrier_init(FAR pthread_barrier_t *barrier,
                             FAR const pthread_barrierattr_t *attr, unsigned int count);
</pre>
<p>
  <b>Description:</b>
  The <code>pthread_barrier_init()</code> function allocates any resources required to
  use the barrier referenced by <code>barrier</code> and initialized the barrier with
  the attributes referenced by <code>attr</code>. 
  If <code>attr</code> is NULL, the default barrier attributes will be used.
  The results are undefined if <code>pthread_barrier_init()</code> is called when any
  thread is blocked on the barrier.
  The results are undefined if a barrier is used without first being initialized.
  The results are undefined if <code>pthread_barrier_init()</code> is called specifying
  an already initialized barrier.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<ul>
  <li>
    <code>barrier</code>.
    The barrier to be initialized.
  </li>
  <li>
    <code>attr</code>.
    Barrier attributes to be used in the initialization.
  </li>
  <li>
    <code>count</code>.
    The count to be associated with the barrier.
    The count argument specifies the number of threads that must call
    <code>pthread_barrier_wait()</code> before any of them successfully return from the call.
    The value specified by count must be greater than zero.
  </li>
</ul>
<p>
  <b>Returned Values:</b>0 (OK) on success or on of the following error numbers:
</p>
<ul>
  <li>
    <code>EAGAIN</code>.
    The system lacks the necessary resources to initialize another barrier.
  </li>
  <li>
    <code>EINVAL</code>.
    The barrier reference is invalid, or the values specified by attr are invalid, or
    the value specified by count is equal to zero.
  </li>
  <li>
    <code>ENOMEM</code>.
    Insufficient memory exists to initialize the barrier.
  </li>
  <li>
    <code>EBUSY</code>.
    The implementation has detected an attempt to reinitialize a barrier while it is in use.
  </li>
</ul>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadbarrierdestroy">2.9.48 pthread_barrier_destroy</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_barrier_destroy(FAR pthread_barrier_t *barrier);
</pre>
<p>
  <b>Description:</b>
  The <code>pthread_barrier_destroy()</code> function destroys the barrier referenced
  by <code>barrie</code> and releases any resources used by the barrier.
  The effect of subsequent use of the barrier is undefined until the barrier is
  reinitialized by another call to <code>pthread_barrier_init()</code>.
  The results are undefined if <code>pthread_barrier_destroy()</code> is called when
  any thread is blocked on the barrier, or if this function is called with an
  uninitialized barrier.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<ul>
  <li><code>barrier</code>. The barrier to be destroyed.</li>
</ul>
<p>
  <b>Returned Values:</b> 0 (<code>OK</code>) on success or on of the following error numbers:
</p>
<ul>
  <li>
    <code>EBUSY</code>.
    The implementation has detected an attempt to destroy a barrier while it is in use.
  </li>
  <li>
    <code>EINVAL</code>.
    The value specified by barrier is invalid.
  </li>
</ul>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadbarrierwait">2.9.49 pthread_barrier_wait</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_barrier_wait(FAR pthread_barrier_t *barrier);
</pre>
<p>
  <b>Description:</b>
  The <code>pthread_barrier_wait()</code> function synchronizse participating
  threads at the barrier referenced by <code>barrier</code>.
  The calling thread is blocked until the required number of threads have called
  <code>pthread_barrier_wait()</code> specifying the same <code>barrier</code>.
  When the required number of threads have called <code>pthread_barrier_wait()</code>
  specifying the <code>barrier</code>, the constant <code>PTHREAD_BARRIER_SERIAL_THREAD</code>
  will be returned to one unspecified thread and zero will be returned to each of
  the remaining threads.
  At this point, the barrier will be reset to the state it had as a result of the most
  recent <code>pthread_barrier_init()</code> function that referenced it.
</p>
<p>
  The constant <code>PTHREAD_BARRIER_SERIAL_THREAD</code> is defined in 
 <code>pthread.h</code> and its value must be distinct from any other value
  returned by <code>pthread_barrier_wait()</code>.
</p>
<p>
  The results are undefined if this function is called with an uninitialized barrier.
</p>
<p>
  If a signal is delivered to a thread blocked on a barrier, upon return from the
  signal handler the thread will resume waiting at the barrier if the barrier wait
  has not completed.
  Otherwise, the thread will continue as normal from the completed barrier wait.
  Until the thread in the signal handler returns from it, it is unspecified whether
  other threads may proceed past the barrier once they have all reached it.
</p>
<p>
  A thread that has blocked on a barrier will not prevent any unblocked thread that
  is eligible to use the same processing resources from eventually making forward
  progress in its execution.
  Eligibility for processing resources will be determined by the scheduling policy.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<ul>
  <li><code>barrier</code>. The barrier on which to wait.</li>
</ul>
<p>
  <b>Returned Values:</b> 0 (<code>OK</code>) on success or <code>EINVAL</code> if the barrier is not valid.
</p>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>


<h3><a name="pthreadonce">2.9.50 pthread_once</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;pthread.h&gt;
    int pthread_once(FAR pthread_once_t *once_control, CODE void (*init_routine)(void));
</pre>
<p>
  <b>Description:</b>
  The  first  call to <code>pthread_once()</code> by any thread with a given
  <code>once_control</code>, will call the <code>init_routine()</code> with no arguments.
  Subsequent calls to <code>pthread_once()</code> with the same <code>once_control</code> will have no effect.
  On return from <code>pthread_once()</code>, <code>init_routine()</code> will have completed.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li>
    <code>once_control</code>.
    Determines if <code>init_routine()</code> should be called.
    <code>once_control</code> should be declared and intialized as follows:
    <ul><pre>pthread_once_t once_control = PTHREAD_ONCE_INIT;
    </pre></ul>
    <code>PTHREAD_ONCE_INIT</code> is defined in <code>pthread.h</code>.
  </li>
  <li>
    <code>init_routine</code>.
    The initialization routine that will be called once.
  </li>
</ul>
<p>
  <b>Returned Values:</b>
  0 (OK) on success or EINVAL if either once_control or init_routine are invalid.
</p>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadkill">2.9.51 pthread_kill</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;signal.h&gt;
    #include &lt;pthread.h&gt;
    int pthread_kill(pthread_t thread, int signo)
</pre>
<p>
  <b>Description:</b>
  The <code>pthread_kill()</code> system call can be used to send any
  signal to a thread.  See <code>kill()</code> for further information
  as this is just a simple wrapper around the <code>kill()</code>
  function.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li>
    <code>thread</code>.
    The id of the thread to receive the signal. Only positive, non-zero values of <code>tthread</code>t are supported.
  </li>
  <li>
    <code>signo</code>.
    The signal number to send.  If <code>signo</code> is zero, no signal is sent, but all error checking is performed.
  </li>
</ul>
<p>
  <b>Returned Values:</b>
</p>
<p>
  On success, the signal was sent and zero is returned.
  On error one of the following error numbers is returned.
</p>
<ul>
  <li>
    <code>EINVAL</code>.
    An invalid signal was specified.
  </li>
  <li>
    <code>EPERM</code>.
    The thread does not have permission to send the signal to the target thread.
  </li>
  <li>
    <code>ESRCH</code>.
    No thread could be found corresponding to that specified by the given thread ID.
  </li>
  <li>
    <code>ENOSYS</code>.
    Do not support sending signals to process groups.
  </li>
</ul>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>

<h3><a name="pthreadsigmask">2.9.52 pthread_sigmask</a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
    #include &lt;signal.h&gt;
    #include &lt;pthread.h&gt;
    int pthread_sigmask(int how, FAR const sigset_t *set, FAR sigset_t *oset);
</pre>
<p>
  <b>Description:</b>
  This function is a simple wrapper around <code>sigprocmask()</code>.
  See the <code>sigprocmask()</code> function description for further information.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li>
    <code>how</code>. How the signal mast will be changed:
    <ul>
      <li>
        <code>SIG_BLOCK</code>:
        The resulting set is the union of the current set and the signal set pointed to by <code>set</code>.
      </li>
      <li>
        <code>SIG_UNBLOCK</code>:
        The resulting set is the intersection of the current set and the complement of the signal set pointed to by <code>set</code>.
      </li>
      <li>
        <code>SIG_SETMASK</code>:
        The resulting set is the signal set pointed to by <code>set</code>.
      </li>
    </ul>
  </li>
  <li>
    <code>set</code>. Location of the new signal mask.
  </li>
  <li>
    <code>oset</code>. Location to store the old signal mask.
  </li>
</ul>
<p>
  <b>Returned Values:</b>
</p>
<p>
  0 (OK) on succes or EINVAL if <code>how</code> is invalid.
</p>
<p>
  <b>Assumptions/Limitations:</b>
</p>
<p>
  <b>POSIX Compatibility:</b> Comparable to the POSIX interface of the same name.
</p>
patacongo's avatar
patacongo committed

<h1><a name="Environ">2.10 Environment Variables</a></h1>
<p><b>Overview</b>.
  NuttX supports environment variables that can be used to control the behavior of programs.
  In the spirit of NuttX the environment variable behavior attempts to emulate the behavior of
  environment variables in the mulit-processing OS:
</p>
<ul>
  <li><b>Task environments</b>.
    When a new task is created using <a href="#taskcreate">task_create</a>, the environment
    of the child task is an inherited, exact copy of the environment of the parent.
    However, after child task has been created, subsequent operations by the child task on
    its environment does not alter the environment of the parent.
    No do operations by the parent effect the child's environment.
    The environments start identical but are independent and may diverge.
  </li>
  <li><b>Thread environments</b>.
    When a pthread is created using <a href="#pthreadcreate">pthread_create</a>, the child
    thread also inherits that envirnment of the parent.
    However, the child does not recieve a copy of the environment but, rather, shares the same
    environment.
    Changes to the environment are visiable to all threads with the same parentage.
  </li>
</ul>
<p><b>Programming Interfaces</b>.
  The following environment variable programming interfaces are provided by Nuttx and are
  described in detail in the following paragraphs.
</p>
<ul>
  <li><a href="#getenv">2.10.1 <code>getenv</code></a></li>
  <li><a href="#putenv">2.10.2 <code>putenv</code></a></li>
  <li><a href="#clearenv">2.10.3 <code>clearenv</code></a></li>
  <li><a href="#setenv">2.10.4 <code>setenv</code></a></li>
  <li><a href="#unsetenv">2.10.5 <code>unsetenv</code></a></li>
</ul>
<p><b>Disabling Environment Variable Support</b>.
  All support for environment variables can be disabled by setting <code>CONFIG_DISABLE_ENVIRONMENT</code>
  in the board configuration file.
</p>

<h2><a name="getenv">2.10.1 <code>getenv</code></a></h2>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <stdlib.h>
  FAR char *getenv(const char *name);
</pre>
<p>
  <b>Description:</b>
    The <code>getenv()</code> function searches the environment list for a string that
    matches the string pointed to by <code>name</code>.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li>
    <code>name</code>.
    The name of the variable to find.
  </li>
</ul>
<p>
  <b>Returned Values:</b>
   The value of the valiable (read-only) or NULL on failure.
</p>

<h2><a name="putenv">2.10.2 <code>putenv</code></a></h2>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <stdlib.h>
  int putenv(char *string);
</pre>
<p>
  <b>Description:</b>
  The <code>putenv()</code> function adds or changes the value of environment variables.
  The argument string is of the form <i>name=value</i>. If name does not already
  exist in  the  environment, then string is added to the environment. If
  name does exist, then the value of name in the environment is changed to
  value.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li>
    <code>string</code>
    name=value string describing the environment setting to add/modify.
  </li>
</ul>
<p>
  <b>Returned Values:</b>
  Zero on sucess.
</p>

<h2><a name="clearenv">2.10.3 <code>clearenv</code></a></h2>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <stdlib.h>
  int clearenv(void);
</pre>
<p>
  <b>Description:</b>
  The <code>clearenv()</code> function clears the environment of all name-value pairs
  and sets the value of the external variable environ to NULL.
</p>
<p>
  <b>Input Parameters:</b>
  None
</p>
<p>
  <b>Returned Values:</b>
  Zero on success.
</p>

<h2><a name="setenv">2.10.4 <code>setenv</code></a></h2>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <stdlib.h>
  int setenv(const char *name, const char *value, int overwrite);
</pre>
<p>
  <b>Description:</b>
  The <code>setenv()</code> function adds the variable <code>name</code> to the environment with the
  specified <code>value</code> if the variable <code>name</code> does not exist. If the <code>name</code>
  does exist in the environment, then its value is changed to <code>value</code> if <code>overwrite</code>
  is non-zero; if <code>overwrite</code> is zero, then the value of <code>name</code> is unaltered.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li>
    <code>name</code>
    The name of the variable to change.
  </li>
  <li>
    <code>value</code>
    The new value of the variable.
  </li>
  <li>
    <code>value</code>
    Replace any existing value if non-zero.
  </li>
</ul>
<p>
  <b>Returned Values:</b>
  Zero on success.
</p>

<h2><a name="unsetenv">2.10.5 <code>unsetenv</code></a></h2>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <stdlib.h>
  int unsetenv(const char *name);
</pre>
<p>
  <b>Description:</b>
  The <code>unsetenv()</code> function deletes the variable <code>name</code> from the environment.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li>
    <code>name</code>
    The name of the variable to delete.
  </li>
</ul>
<p>
  <b>Returned Values:</b>
  Zero on success.
</p>

<h1><a name="FileSystem">2.11 Filesystem Interfaces</a></h1>
<p>
  The NuttX filesystem is very simple; it does not involve any block drivers or
  particular filesystem (like FAT or EXT2 etc.).
  The NuttX filesystem simply supports a set a filesystem APIs
  (<code>open()</code>, <code>close()</code>, <code>read()</code>, <code>write</code>, etc.)
  and a registration mechanism that allows devices drivers to a associated with <i>nodes</i>
  in a file-system-like name space.
</p>

<h2><a name="driveroperations">2.11.1 Driver Operations</a></h2>
<ul><pre>
  #include &lt;fcntl.h&gt;
  int open(const char *path, int oflag, ...);
</pre></ul>

<ul><pre>
  #include &lt;unistd.h&gt;
  int   close(int fd);
  int   dup(int fildes);
  int   dup2(int fildes1, int fildes2);
  off_t lseek(int fd, off_t offset, int whence);
  int   read(int fd, void *buf, unsigned int nbytes);
  int   unlink(const char *path);
  int   write(int fd, const void *buf, unsigned int nbytes);
</pre></ul>

<ul><pre>
  #include &lt;sys/ioctl.h&gt;
  int    ioctl(int fd, int req, unsigned long arg);
</pre></ul>

<h2><a name="directoryoperations">2.11.2 Directory Operations</a></h2>
<ul><pre>
  #include &lt;dirent.h&gt;
  int        closedir(DIR *dirp);
  FAR DIR   *opendir(const char *path);
  FAR struct dirent *readdir(FAR DIR *dirp);
  int        readdir_r(FAR DIR *dirp, FAR struct dirent *entry, FAR struct dirent **result);
  void       rewinddir(FAR DIR *dirp);
  void       seekdir(FAR DIR *dirp, int loc);
  int        telldir(FAR DIR *dirp);
</pre></ul>

<h2><a name="standardio">2.11.3 Standard I/O</a></h2>
<ul><pre>
  #include &lt;stdio.h&gt;
  int    fclose(FILE *stream);
  int    fflush(FILE *stream);
  int    feof(FILE *stream);                               /* Prototyped but not implemented */
  int    ferror(FILE *stream);                             /* Prototyped but not implemented */
  int    fgetc(FILE *stream);
  char  *fgets(char *s, int n, FILE *stream);
  FILE  *fopen(const char *path, const char *type);
  int    fprintf(FILE *stream, const char *format, ...);
  int    fputc(int c, FILE *stream);
  int    fputs(const char *s, FILE *stream);
  size_t fread(void *ptr, size_t size, size_t n_items, FILE *stream);
  int    fseek(FILE *stream, long int offset, int whence); /* Prototyped but not implemented */
  size_t fwrite(const void *ptr, size_t size, size_t n_items, FILE *stream);
  char  *gets(char *s);

  int    printf(const char *format, ...);
  int    puts(const char *s);
  int    rename(const char *source, const char *target);
  int    sprintf(char *dest, const char *format, ...);
  int    ungetc(int c, FILE *stream);
  int    vprintf(const char *s, va_list ap);
  int    vfprintf(FILE *stream, const char *s, va_list ap);
  int    vsprintf(char *buf, const char *s, va_list ap);

  int    chdir(const char *path);                          /* Prototyped but not implemented */
  FILE  *fdopen(int fd, const char *type);
  int    fstat(int fd, FAR struct stat *buf);              /* Prototyped but not implemented */
  char  *getcwd(FAR char *buf, size_t size);               /* Prototyped but not implemented */
  int    mkdir(const char *path, mode_t mode);
  int    rmdir(const char *path);
  int    stat(const char *path, FAR struct stat *buf);
  int    statfs(const char *path, FAR struct statfs *buf); /* Prototyped but not implemented */
</pre></ul>

patacongo's avatar
patacongo committed
<h2>2.12 <a name="Network">Network Interfaces</a></h2>
<p>NuttX includes a simple interface layer based on uIP (see <a href="http://www.sics.se/~adam/uip/index.php/Main_Page">http://www.sics.se</a>).
NuttX supports subset of a standard socket interface to uIP.
These network feature can be enabled by settings in the architecture
<a href="NuttxPortingGuide.html#apndxconfigs">configuration file</a>.
Those socket APIs are discussed in the following paragraphs.</p>
<ul>
<li>
</li>
<li><a href="#socket">2.12.1 socket</a></li>
<li><a href="#bind">2.12.2 bind</a></li>
<li><a href="#connect">2.12.3 connect</a></li>
<li><a href="#send">2.12.4 send</a></li>
<li><a href="#sendto">2.12.5 sendto</a></li>
<li><a href="#recv">2.12.6 recv</a></li>
<li><a href="#recvfrom">2.12.7 recvfrom</a></li>
<li><a href="#setsockopt">2.12.8 setsockopt</a></li>
<li><a href="#getsockopt">2.12.9 getsockopt</a></li>
</ul>

<h3><a name="socket">2.12.1 <code>socket</code></a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <sys/socket.h>
  int socket(int domain, int type, int protocol);
</pre>
<p>
  <b>Description:</b>
   socket() creates an endpoint for communication and returns a descriptor.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li><code>domain</code>: (see sys/socket.h)</li>
  <li><code>type</code>: (see sys/socket.h)</li>
  <li><code>protocol</code>: (see sys/socket.h)</li>
</ul>
<p>
  <b>Returned Values:</b>
  0 on success; -1 on error with<code>errno</code>set appropriately:
</p>
<ul>
  <li><code>EACCES</code>.
    Permission to create a socket of the specified type and/or protocol is denied.</li>
  <li><code>EAFNOSUPPORT</code>.
    The implementation does not support the specified address family.</li>
  <li><code>EINVAL</code>.
    Unknown protocol, or protocol family not available.</li>
  <li><code>EMFILE</code>.
    Process file table overflow.</li>
  <li><code>ENFILE</code>
    The system limit on the total number of open files has been reached.</li>
  <li><code>ENOBUFS</code> or <code>ENOMEM</code>.
    Insufficient memory is available. The socket cannot be created until sufficient resources are freed.</li>
  <li><code>EPROTONOSUPPORT</code>.
    The protocol type or the specified protocol is not supported within this domain.</li>
</ul>

<h3><a name="bind">2.12.2 <code>bind</code></a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <sys/socket.h>
  int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
</pre>
<p>
  <b>Description:</b>
   <code>bind()</code> gives the socket sockfd the local address <code>addr</code>.
   <code>addr</code> is <code>addrlen</code> bytes long. Traditionally, this is called
   &quot;assigning a name to a socket.&quot; When a socket is created with <code>socket()</code>,
   it exists in a name space (address family) but has no name assigned.
<p>
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li><code>sockfd</code>: Socket descriptor from socket.</li>
  <li><code>addr</code>: Socket local address.</li>
  <li><code>addrlen</code>: Length of <code>addr</code>.</li>
</ul>
<p>
  <b>Returned Values:</b>
  0 on success; -1 on error with<code>errno</code>set appropriately:
</p>
<ul>
  <li><code>EACCES</code>
    The address is protected, and the user is not the superuser.</li>
  <li><code>EADDRINUSE</code>
    The given address is already in use.</li>
  <li><code>EBADF</code>
    <code>sockfd</code> is not a valid descriptor.</li>
  <li><code>EINVAL</code>
    The socket is already bound to an address.</li>
  <li><code>ENOTSOCK</code>
    <code>sockfd</code> is a descriptor for a file, not a socket.</li>
</ul>

<h3><a name="connect">2.12.3 <code>connect</code></a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <sys/socket.h>
  int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
</pre>
<p>
  <b>Description:</b>
  <code>connect()</code> connects the socket referred to by the file descriptor
  <code>sockfd</code> to the address specified by <code>addr</code>.
  The <code>addrlen</code> argument specifies the size of <code>addr</code>.
  The format of the address in <code>addr</code> is determined by the address space
  of the socket sockfd.

  If the socket sockfd is of type SOCK_DGRAM then <code>addr</code> is the address
  to which datagrams are sent by default, and the only address from which
  datagrams are received. If the socket is of type SOCK_STREAM or
  SOCK_SEQPACKET, this call attempts to make a connection to the socket
  that is bound to the address specified by <code>addr</code>.

  Generally, connection-based protocol sockets may successfully <code>connect()</code>
  only once; connectionless protocol sockets may use <code>connect()</code> multiple
  times to change their association.  Connectionless sockets may dissolve
  the association by connecting to an address with the sa_family member of
  sockaddr set to AF_UNSPEC.
<p>
</p>
<p>
  <b>Input Parameters:</b>
</p>
<p>
<ul>
  <li><code>sockfd</code>: Socket descriptor returned by <code>socket()</code></li>
  <li><code>addr</code>: Server address (form depends on type of socket)</li>
  <li><code>addrlen</code>: Length of actual <code>addr</code></li>
</ul>
<p>
  <b>Returned Values:</b>
  0 on success; -1 on error with<code>errno</code>set appropriately:
</p>
  <li><code>EACCES</code> or </code>EPERM</code>:
    The user tried to connect to a broadcast address without having the
    socket broadcast flag enabled or the connection request failed
    because of a local firewall rule.</li>
  <li><code>EADDRINUSE</code>
    Local address is already in use.</li>
  <li><code>EAFNOSUPPORT</code>
    The passed address didn't have the correct address family in its
    sa_family field.</li>
  <li><code>EAGAIN</code>
    No more free local ports or insufficient entries in the routing
    cache. For PF_INET.</li>
  <li><code>EALREADY</code>
    The socket is non-blocking and a previous connection attempt has
    not yet been completed.</li>
  <li><code>EBADF</code>
    The file descriptor is not a valid index in the descriptor table.</li>
  <li><code>ECONNREFUSED</code>
    No one listening on the remote address.</li>
  <li><code>EFAULT</code>
    The socket structure address is outside the user's address space.</li>
  <li><code>EINPROGRESS</code>
    The socket is non-blocking and the connection cannot be completed
    immediately.</li>
  <li><code>EINTR</code>
    The system call was interrupted by a signal that was caught.</li>
  <li><code>EISCONN</code>
    The socket is already connected.</li>
  <li><code>ENETUNREACH</code>
    Network is unreachable.</li>
  <li><code>ENOTSOCK</code>
    The file descriptor is not associated with a socket.</li>
  <li><code>ETIMEDOUT</code>
    Timeout while attempting connection. The server may be too busy
    to accept new connections.</li>
</ul>

<h3><a name="send">2.12.4 <code>send</code></a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <sys/socket.h>
  ssize_t send(int sockfd, const void *buf, size_t len, int flags);
</pre>
<p>
  <b>Description:</b>
  The <code>send()</code> call may be used only when the socket is in a connected state
  (so that the intended recipient is known).
  The only difference between <code>send()</code> and <code>write()</code> is the
  presence of <code>flags</code>.
  With <code>zero</code> flags parameter, <code>send()</code> is equivalent to
  <code>write()</code>. Also, <code>send(s,buf,len,flags)</code> is
  equivalent to <code>sendto(s,buf,len,flags,NULL,0)</code>.
</p>
<p>
  <b>Input Parameters:</b>
</p>
<ul>
  <li><code>sockfd</code>: Socket descriptor of socket
  <li><code>buf</code>: Data to send
  <li><code>len</code>: Length of data to send
  <li><code>flags</code>: Send flags
</ul>
<p>
  <b>Returned Values:</b>
  See <a href="#sendto"><code>sendto()</code></a>.
</p>

<h3><a name="sendto">2.12.5 <code>sendto</code></a></h3>
<p>
  <b>Function Prototype:</b>
</p>
<pre>
  #include <sys/socket.h>
  ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
                 const struct sockaddr *to, socklen_t tolen);
</pre>
<p>
  <b>Description:</b>
  If <code>sendto()</code> is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
  socket, the parameters to and tolen are ignored (and the error EISCONN
  may be returned when they are not NULL and 0), and the error ENOTCONN is
  returned when the socket was not actually connected.