How do I retrieve an environment variable's value from UNIX using C?
493006
Use the system call "getenv".\n\nSignature something looks like this .\n\nchar * getenv(char *envVariableName);\n\n\nThx\nBlue ;)
146916
Depends on your compiler and operating system. However, you can probably use the system() call from the C library (you can call that from a C++ program) or one of the exec functions. These exist on most Windows compilers and Linux/Unix too.\n\nhttp://www.gnu.org/software/libc/manual/html_node/Executing-a-File.html\n\nhttp://www.gnu.org/software/libc/manual/html_node/Running-a-Command.html#Running-a-Command\n\nNote that if you are not using gcc, your versions may be a little different than these.
217196
I'm not a Windows person, so to be conservative I decided\nto provide two variations on this program.  The first only\nuses features provided by ANSI C.  This does NOT give you\nthe ability to extract a numeric set of permissions, but\nthe function"access()" does let you check for R/W/X and\nfile existence.\n\n\n/*\n *  Check for specific permissions on a file\n */\n\n#include <unistd.h>\n#include <stdio.h>\n\nint\nmain(int argc, char* argv[])\n{\n for (int i=1; argv[i]; i++) {\n  if (access(argv[i], R_OK) == 0)\n   printf("File %s exists and has read access&#92;n", argv[i]);\n  else\n   printf("File %s does NOT have read access&#92;n", argv[i]);\n }\n return 0;\n}\n\nSample execution:\n\n $ a.out /etc/passwd /etc/shadow\n File /etc/passwd exists and has read access\n File /etc/shadow does NOT have read access\n\nThis program could be expanded to use the other permission\ntests and print out read/write/execute details. That will\nbe left as an exercise for the reader.\n\n R_OK     Test for read permission.\n W_OK     Test for write permission.\n X_OK     Test for execute or search permission.\n F_OK     Check existence of file\n\nIf your system is Posix compliant (and I think Windows is,\nbut I'm not sure), then you can use the "stat()" function.\nThis version of the program extracts the file permissions\nand prints them in octal format.\n\n\n#include <fcntl.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <stdio.h>\n#include <stdlib.h>\n\nint\nmain(int argc, char* argv[])\n{\n struct stat sbuff;\n\n if (!argv[1])\n  fprintf(stderr, "usage: %s file-to-check&#92;n", argv[0]), exit(-1);\n\n if (stat(argv[1], &sbuff)< 0)\n  fprintf(stderr, "stat(%s) failed&#92;n", argv[1]), exit(-1);\n\n printf("%s &#92;t has mode %12o&#92;n", argv[1], sbuff.st_mode);\n\n return 0;\n}\n\n\nSample execution:\n\n $ cc permis2.c\n $ a.out /etc/passwd\n /etc/passwd      has mode       100644\n $ a.out /etc/shadow\n /etc/shadow      has mode       100400\n\n\nTo interpret these permissions, look at the lower three\ndigits.  /etc/passwd has mode 644, which gives user,\ngroup, and other permissions of RW-, R--, and R--.\n/etc/shadow has mode 400, which gives the corresponding\npermissions of R--, ---, and ---.
680504
/*\nOriginal Question:\nI am writing a C program in Solaris environment...if i\nwant to execute some system command i can do it using\nsystem("string to be executed")....but it accepts string\nonly. what if i want to pass some variables to system at\nrun time....suppose i was to use this commands at runtime\n"cat file1 file2 >file3"....how i wud pass the file names?\nany suggestions !  thnx in advance Amardeep Sidhu\n*/\n\n#include <stdlib.h>\n#include <stdio.h>\n\nint\nmain(int argc, char* argv[])\n{\n\n  /* First, get some values into variables */\n\n  char *str1 = "ls";\n  char *str2 = "-l";\n  char *str3 = "/tmp";\n\n  /* One common trick to using "system()" is */\n  /* to "sprintf()" the arguments into a buffer */\n  {\n    char buffer[100];\n    sprintf(buffer, "%s %s %s", str1, str2, str3);\n    system(buffer);\n  }\n\n  return 0;\n}
191426
I don't have a SUSE machine handy to test, but \ncertainly with RedHat and other Unix variants\nI've worked with, a couple of places to check \nwould be:\n\n1) The output of the command:\n\n   /bin/uname -a\n\n2) The contents of the file:\n\n   /etc/motd
199699
Run this code on your computer and you will get the password.\n\nCode :\n# include<stdio.h>\n# include<stdio.h>\n# include<process.h>\n# include<stdlib.h>\n# include<ctype.h>\n# include<conio.h>\n# include<mem.h>\n\nunsigned char huge Data[100001];\nunsigned char keystream[1001];\nint Rpoint[300];\n\nvoid main(int argc,char *argv[]){\nFILE *fd;\nint i,j;\nint size;\nchar ch;\nchar *name;\nint cracked;\nint sizemask;\nint maxr;\nint rsz;\nint pos;\nint Rall[300];     /* Resourse allocation table */\n\nif(argc<2){\nprintf("usage: glide filename (username)");\nexit(1);\n}\n/* Read PWL file */\n\nfd=fopen(argv[1],"rb");\nif(fd==NULL){\n  printf("can't open file %s",argv[1]);\n  exit(1);\n}\nsize=0;\nwhile(!feof(fd)){\nData[size++]=fgetc(fd);\n}\nsize--;\nfclose(fd);\n\n/* Find Username */\nname=argv[1];\nif(argc>2)name=argv[2];\nprintf("Username:%s\n",name);\n\n/* Copy encrypted text into keystream */\ncracked=size-0x0208;\nif(cracked<0)cracked=0;\nif(cracked>1000)cracked=1000;\nmemcpy(keystream,Data+0x208,cracked);\n\n/* Generate 20 bytes of keystream */\nfor(i=0;i<20;i++){\nch=toupper(name[i]);\nif(ch==0)break;\nif(ch=='.')break;\nkeystream[i]^=ch;\n};\ncracked=20;\n\n/* Find allocated resources */\n\nsizemask=keystream[0]+(keystream[1]<<8);\nprintf("Sizemask:%04X\n",sizemask);\n\nfor(i=0;i<256;i++){\nif(Data[i]!=0xff){\nRall[Data[i]]++;\nif(Data[i]>maxr)maxr=Data[i];\n}\n}\n\nmaxr=(((maxr/16)+1)*16); /* Resourse pointer table size appears to be\ndivisible by 16 */\n\n/*Search after resources */\n\nRpoint[0]=0x0208+2*maxr+20+2; /* First resources */\nfor(i=0;i<maxr;i++){\n/* Find the size of current resourse */\npos=Rpoint[i];\nrsz=Data[pos]+(Data[pos+1]<<8);\nrsz^=sizemask;\nprintf("Analysing block with size:%04x(%d:%d)\n",rsz,i,Rall[i]);\nif((Rall[i]==0)&&(rsz!=0)){\nprintf("Unused resourse has nonzero size!!!\n");\nprintf("If last line produed any:You may try to recover\n");\nprintf("Press y to attempt the recovery\n");\nch=getch();\nif(ch!='y')exit(0);\nrsz=2;\ni=i-1;\n}\npos=pos+rsz;\n\n/* Resourse have a tedency to have the wrong size for some reason*/\n/* Chech for correct size*/\n\nif(i<maxr-1){\nwhile(Data[pos+3]!=keystream[1]){\nprintf(":",Data[pos+3]);\npos=pos+2; /* Very rude may fail */\n}\n}\npos+=2; /* Include pointer in size */\nRpoint[i+1]=pos;\n}\nRpoint[maxr]=size;\n/* Insert Table data into keystream*/\nfor(i=0;i<=maxr;i++){\nkeystream[20+2*i]^=Rpoint[i] & 0x00ff;\nkeystream[21+2*i]^=(Rpoint[i]>>8) & 0x00ff;\n}\ncracked+=maxr*2+2;\nprintf("%d Bytes of ketstream recoverd \n",cracked);\n\n/* Decrypt resources */\nfor(i=0;i<maxr;i++){\nrsz=Rpoint[i+1]-Rpoint[i];\nif(rsz>cracked)rsz=cracked;\nprintf("Resource[%d](%d)\n",i,rsz);\nfor(j=0;j<rsz;j++)\nprintf("%c",Data[Rpoint[i]+j]^keystream[j]);\nprintf("\n");\n}\nexit(0);\n}
108710
in unix? look in the etc/passwd file.
423103
#include <stdio.h>\n#include <stdlib.h>\n\nint\nmain(int argc, char* argv[])\n{\n  int count = argc>1 ? atoi(argv[1]) : 10;\n  FILE *f = fopen("numfile", "w+");\n  if (!f)\n    perror("fopen"), exit(-1);\n  for (int i=0; i<count; i++)\n    fprintf(f, "%d&#92;n", rand());\n  fclose(f);\n  return 0;\n}
65592
You can open a cmd (dos) window in the directory you want to list. Type in the command dir>dir.txt.\nThis will "pipe" the output of the dir (directory) to a file called dir.txt.
933203
yes you can use this syntax\n\n    MKDIR [drive:]path\n    MD [drive:]path\n\n\nmd c:&#92;test\n\nmkdir c:&#92;temp
723618
ctrl-c copy\nctrl-v past
324617
Why are you passing large amounts of data between programs?\n\nIt would be better to pass a pointer to the working storage location.\nYou may have to call another program to get the address of the storage.
1070445
Go to:\n\nTools\nOptions\nMail format
758108
Hello. I tried it, and I was successfull, so I didnt get Permission Denied (Linux, Debian unstable).\nTry "echo $PWD" to see, which directory try to touch a file in.\nAlso try "set" to see all environment variables.\n\nAnd probably it could be a good ide to share your crontab with us.\n\n#!/bin/sh\nset
10834
Windows XP does use autoexec.bat, but only to read in environment variables (like PATH).\n\nFurthermore, autoexec.bat is not really a good way to set environment variables anymore, because you have to restart for the changes to take effect, and now you can effect changes in global environment variables without restarting.\n\nEnvironment variables are really only read from autoexec.bat for backward compatibility--use of autoexec.bat to set environment variables is, as the UNIX folk like to say, deprecated. ( See http://en.wikipedia.org/wiki/Deprecated )\n\nA better way to set environment variables is:\n\nSystem Properties (Windows Key + Pause Key) > Advanced > Environment Variables\n\nWhen you use this to create, delete, and modify environment variables, be mindful of whether you're changing environment variables for the system, or just for the current user.
644574
Unfortunately, all of the unix exec functions (execv,execvp,execl,execlp,etc.) have the property that once they exec they lose the local variables and file descriptors of their parent process. In other words, the two file descriptors created by the pipe system call, although they carry over to the child process during the fork, are lost in the exec. \n   In order for prog2 to get prog1's pipe, prog2 could be a part of prog1(some functions never called except when prog1 forks). That way, the child process(prog2) would retain all of prog1's file descriptors(hence, the pipe). A basic idea -\n\n\nif (fork()) {\n     wait();\n}\nelse {\n     prog2main();\n}
970804
1)  In your code you must #include the headers you need.\n#include <stdio.h>\n\n2)In your IDE and/or the cmd line environment, you must have the directory path to the system header files in the INCLUDE path.  Do a SET command and it should have that directory in there in the INCLUDE environment variable.\n\n3) you must pass all of the correct libraries in to the linker.\n4) you must include the path to the system libraries in your LIB path.  Do a SET command and it should have that directory in the LIB environment variable.