Back to index

plt-scheme  4.2.1
Net.c
Go to the documentation of this file.
00001 /*                                                      -*- C++ -*-
00002  *
00003  * Purpose: host and user net info
00004  *
00005  * Authors: Markus Holzem and Julian Smart
00006  *
00007  * Copyright: (C) 2004-2009 PLT Scheme Inc.
00008  * Copyright: (C) 1995, AIAI, University of Edinburgh (Julian)
00009  * Copyright: (C) 1995, GNU (Markus)
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00027 #include "gc.h"
00028 #include "Net.h"
00029 
00030 #include <pwd.h>
00031 #include <string.h>
00032 #include <sys/types.h>
00033 #include <unistd.h>
00034 
00035 #if !defined(SVR4) || defined(__sgi)
00036 # define WX_USE_GETHOSTBYNAME
00037 # include <netdb.h>
00038 #endif
00039 
00040 int wxGetHostName(char *buf, int sz)
00041 {
00042 #ifndef WX_USE_GETHOSTBYNAME
00043   return (sysinfo(SI_HOSTNAME, buf, sz - 1) != -1);
00044 #else /* BSD Sockets */
00045   char name[255];
00046   /* Get hostname */
00047   if ((gethostname(name, sizeof(name)/sizeof(char)-1)) == -1)
00048     return 0;
00049   strncpy(buf, name, sz-1);
00050   buf[sz-1] = 0;
00051   return 1;
00052 #endif
00053 }
00054 
00055 int wxGetEmailAddress(char *address, int maxSize)
00056 {
00057   char host[65];
00058   char user[65];
00059   char tmp[130];
00060 
00061   if (wxGetHostName(host, 64) == 0)
00062     return 0;
00063   if (wxGetUserId(user, 64) == 0)
00064     return 0;
00065 
00066   strcpy(tmp, user);
00067   strcat(tmp, "@");
00068   strcat(tmp, host);
00069   strncpy(address, tmp, maxSize - 1);
00070   address[maxSize-1] = '\0';
00071   return 1;
00072 }
00073 
00074 int wxGetUserId(char *buf, int sz)
00075 {
00076   struct passwd *who;
00077   
00078   if ((who = getpwuid(getuid ())) != NULL) {
00079     strncpy (buf, who->pw_name, sz-1);
00080     buf[sz - 1]= 0;
00081     return 1;
00082   }
00083   return 0;
00084 }
00085 
00086 int wxGetUserName(char *buf, int sz)
00087 {
00088   struct passwd *who;
00089   
00090   if ((who = getpwuid (getuid ())) != NULL) {
00091     strncpy (buf, who->pw_gecos, sz - 1);
00092     buf[sz - 1]= 0;
00093     return 1;
00094   }
00095   return 0;
00096 }