1: /* 2: * hostname - get (or set) hostname 3: */ 4: 5: #include <stdio.h> 6: #include <sys/types.h> 7: 8: char hostname[32]; 9: extern int errno; 10: 11: main(argc, argv) 12: char *argv[]; 13: { 14: int myerrno; 15: 16: argc--; 17: argv++; 18: if (argc) { 19: if (sethostname(*argv)) 20: perror("sethostname"); 21: myerrno = errno; 22: } 23: else { 24: gethostname(hostname, sizeof(hostname)); 25: myerrno = errno; 26: printf("%s\n", hostname); 27: } 28: exit(myerrno); 29: } 30: 31: sethostname(s) 32: char *s; 33: { 34: FILE *fopen(); 35: register FILE *fp; 36: 37: if ((fp = fopen("/etc/localhostname", "w")) != (FILE *) NULL) { 38: fprintf(fp, "%s\n", s); 39: fclose(fp); 40: (void) chmod("/etc/localhostname", 0644); 41: if (ferror(fp)) 42: return(-1); 43: else 44: return(0); 45: } 46: else 47: return(-1); 48: }