/* envreg - Register TIGCC environment variables into bashrc Copyright (C) 2004 Kevin Kofler This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include int main(void) { char *bashrc; char *tigcc; char *oldtigcc=NULL; FILE *f=NULL; char **lines=NULL; int numlines=0; int i; char *p; // Find out what bashrc file to use if (geteuid()) { const char *homedir; puts("envreg: Running as normal user"); homedir=getenv("HOME"); if (!homedir) { puts("envreg: error: $HOME not set"); return 1; } bashrc=malloc(strlen(homedir)+9); if (!bashrc) goto outofmem; strcpy(bashrc,homedir); if (bashrc[strlen(bashrc)-1]!='/') strcat(bashrc,"/"); strcat(bashrc,".bashrc"); } else { puts("envreg: Running as root"); bashrc=malloc(12); if (!bashrc) goto outofmem; strcpy(bashrc,"/etc/bashrc"); } printf("envreg: Using bashrc file: `%s'\n",bashrc); // Read the current file contents f=fopen(bashrc,"rb"); // If we can't open the file, assume it doesn't exist and treat it as empty if (f) { // Get the file size fseek(f,0,SEEK_END); i=ftell(f); fseek(f,0,SEEK_SET); // Allocate a buffer for the entire file and read it into memory lines=malloc(sizeof(unsigned char *)); if (!lines) goto outofmem; *lines=malloc(i+1); if (!*lines) goto outofmem; fread(*lines,1,i,f); (*lines)[i]=0; // Close the file fclose(f); f=NULL; // Now split the file into lines while((p=strchr(lines[numlines],'\n'))) { lines=realloc(lines,((++numlines)+1)*sizeof(unsigned char *)); if (!lines) goto outofmem; lines[numlines]=malloc(strlen(p+1)+1); if (!lines[numlines]) goto outofmem; strcpy(lines[numlines],p+1); *p=0; if (p[-1]=='\r') p[-1]=0; lines[numlines-1]=realloc(lines[numlines-1],strlen(lines[numlines-1])+1); if (!lines[numlines-1]) goto outofmem; // Some stupid systems copy even when cutting. } // If the last line is empty, drop it, else count it if (*(lines[numlines])) numlines++; else { free(lines[numlines]); lines=realloc(lines,numlines*sizeof(unsigned char *)); if (!lines) goto outofmem; // Some stupid systems copy even when cutting. } } else { printf("envreg: `%s' doesn't exist, creating a new one\n",bashrc); } // Look for existing $TIGCC setting for (i=0;i