ابزار کاربر

ابزار سایت


system-programming:getenv-setenv

تفاوت‌ها

تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده می‌کنید.

پیوند به صفحه‌ی تفاوت‌ها

system-programming:getenv-setenv [2024/04/19 18:02] – ایجاد شد pejmansystem-programming:getenv-setenv [2024/06/18 01:23] (فعلی) – حذف شد pejman
خط 1: خط 1:
-====== تابع ()getenv و ()setenv - خواندن و نوشتن متغیر های محلی ====== 
- 
----- 
-==== تابع ()getenv ==== 
- 
-<code C> 
-// man 1 env 
- 
-// man 3 getenv 
-/* getenv() returns a pointer to the value in the environment, or NULL if there is no match. */ 
-#include <stdlib.h> 
-char *getenv(const char *name); 
-</code> 
- 
-<code C> 
-/* env-var.c */ 
-#include <stdio.h> 
-#include <stdlib.h> 
-#include <string.h> 
- 
-int main(void) 
-{ 
-   /* Using getenv() to fetch env. variables */ 
-   printf("Your username is %s\n", getenv("USER")); 
-   printf("Your home directory is %s\n", 
-      getenv("HOME")); 
-   printf("Your preferred editor is %s\n", 
-      getenv("EDITOR")); 
-   printf("Your shell is %s\n", getenv("SHELL")); 
- 
-   /* Check if the current terminal support colors*/ 
-   if ( strstr(getenv("TERM"), "256color" ) 
-   { 
-      /* Color the output with \033 + colorcode */ 
-      printf("\033[0;31mYour \033[0;32mterminal " 
-         "\033[0;35msupport " 
-         "\033[0;33mcolors\033[0m\n"); 
-   } 
-   else 
-   { 
-      printf("Your terminal doesn't support" 
-         " colors\n"); 
-   } 
-   return 0; 
-} 
-</code> 
- 
-  $ make env-var 
-  cc     env-var.c   -o env-var 
- 
-  $ ./env-var 
-  Your username is pejman 
-  Your home directory is /home/pejman 
-  Your preferred editor is (null) 
-  Your shell is /bin/bash 
-  Your terminal support colors 
- 
-  $ echo $USER 
-  pejman 
- 
-  $ echo $HOME 
-  /home/pejman 
- 
-  $ echo $EDITOR 
- 
-  $ echo $SHELL 
-  /bin/bash 
- 
-  $ echo $TERM 
-  xterm-256color 
- 
-  $ export TERM=xterm 
-   
-  $ ./env-var 
-  Your username is pejman 
-  Your home directory is /home/pejman 
-  Your preferred editor is (null) 
-  Your shell is /bin/bash 
-  Your terminal doesn't support colors 
-   
-  $ export TERM=xterm-256color 
- 
-  $ echo $TERM 
-  xterm-256color 
-   
-  $ TERM=xterm ./env-var 
-  Your username is pejman 
-  Your home directory is /home/pejman 
-  Your preferred editor is (null) 
-  Your shell is /bin/bash 
-  Your terminal doesn't support colors 
-   
-  $ echo $TERM 
-  xterm-256color 
- 
----- 
-==== تابع ()setenv ==== 
- 
-<code C> 
-// man 3 setenv 
-/* The  setenv()  function adds the variable name to the environment with the value value.  
- * if variable exists and overwrite is zero, then the value of name is not changed. 
-*/ 
-#include <stdlib.h> 
-int setenv(const char *name, const char *value, int overwrite); 
-</code> 
- 
-<code C> 
-/* env-var-set.c */ 
-#define _POSIX_C_SOURCE 200112L 
-#include <stdio.h> 
-#include <stdlib.h> 
- 
-int main(void) 
-{ 
-    setenv("FULLNAME", "Jack-Benny", 1); 
-    printf("Your full name is %s\n", getenv("FULLNAME")); 
-    return 0; 
-} 
-</code> 
- 
-  $ make env-var-set 
-  cc     env-var-set.c   -o env-var-set 
- 
-  $ ./env-var-set 
-  Your full name is Jack-Benny 
-   
-  $ echo $FULLNAME 
  
system-programming/getenv-setenv.1713537179.txt.gz · آخرین ویرایش: 2024/04/19 18:02 توسط pejman

به جز مواردی که ذکر می‌شود، مابقی محتویات ویکی تحت مجوز زیر می‌باشند: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki