/[emacs]/emacs/lib-src/update-game-score.c
ViewVC logotype

Diff of /emacs/lib-src/update-game-score.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by walters, Fri Apr 5 09:18:57 2002 UTC revision 1.4 by walters, Sun Apr 7 05:46:03 2002 UTC
# Line 40  Boston, MA 02111-1307, USA.  */ Line 40  Boston, MA 02111-1307, USA.  */
40  #include <config.h>  #include <config.h>
41    
42  #define MAX_ATTEMPTS 5  #define MAX_ATTEMPTS 5
43  #define SCORE_FILE_PREFIX "/var/games/emacs/"  
44    #ifdef HAVE_SHARED_GAME_DIR
45    #define SCORE_FILE_PREFIX HAVE_SHARED_GAME_DIR
46    #else
47    #define SCORE_FILE_PREFIX "$HOME"
48    #endif
49    
50  int  int
51  usage(int err)  usage(int err)
# Line 78  write_scores(const char *filename, const Line 83  write_scores(const char *filename, const
83               int count);               int count);
84    
85  char *  char *
86  get_user_id()  get_user_id(struct passwd *buf)
87  {  {
88    char *name;    char *name;
   struct passwd *buf = getpwuid(getuid());  
89    if (!buf)    if (!buf)
90      {      {
91        int count = 1;        int count = 1;
# Line 95  get_user_id() Line 99  get_user_id()
99    return buf->pw_name;    return buf->pw_name;
100  }  }
101    
102    char *
103    get_home_dir(struct passwd *buf)
104    {
105      if (!buf)
106        return NULL;
107      return buf->pw_dir;
108    }
109    
110  int  int
111  main(int argc, char **argv)  main(int argc, char **argv)
112  {  {
113    int c;    int c;
114    void *lockstate;    void *lockstate;
115    char *scorefile;    char *scorefile, *prefix;
116    struct stat buf;    struct stat buf;
117    struct score_entry *scores;    struct score_entry *scores;
118    int newscore, scorecount, reverse = 0, max = -1;    int newscore, scorecount, reverse = 0, max = -1;
119    char *newdata;    char *newdata;
120      struct passwd *passwdbuf;
121    
122    srand(time(0));    srand(time(0));
123    
# Line 126  main(int argc, char **argv) Line 139  main(int argc, char **argv)
139    
140    if (optind+3 != argc)    if (optind+3 != argc)
141      usage(1);      usage(1);
142    scorefile = malloc(strlen(SCORE_FILE_PREFIX) + strlen(argv[optind]) + 1);  
143      passwdbuf = getpwuid(getuid());
144    
145      if (!strcmp(SCORE_FILE_PREFIX, "$HOME"))
146        {
147          prefix = get_home_dir(passwdbuf);
148          if (!prefix)
149            {
150              fprintf(stderr, "Unable to determine home directory\n");
151              exit(1);
152            }
153        }
154      else
155        prefix = SCORE_FILE_PREFIX;
156      
157      scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 1);
158    if (!scorefile)    if (!scorefile)
159      {      {
160        fprintf(stderr, "Couldn't create score file name: %s\n",        fprintf(stderr, "Couldn't create score file name: %s\n",
161                strerror(errno));                strerror(errno));
162        goto fail;        goto fail;
163      }      }
164    strcpy(scorefile, SCORE_FILE_PREFIX);    strcpy(scorefile, prefix);
165    strcat(scorefile, argv[optind]);    strcat(scorefile, argv[optind]);
166    newscore = atoi(argv[optind+1]);    newscore = atoi(argv[optind+1]);
167    newdata = argv[optind+2];    newdata = argv[optind+2];
# Line 156  main(int argc, char **argv) Line 184  main(int argc, char **argv)
184                scorefile, strerror(errno));                scorefile, strerror(errno));
185        goto fail_unlock;        goto fail_unlock;
186      }      }
187    push_score(&scores, &scorecount, newscore, get_user_id(), newdata);    push_score(&scores, &scorecount, newscore, get_user_id(passwdbuf), newdata);
188    sort_scores(scores, scorecount, reverse);    sort_scores(scores, scorecount, reverse);
189    if (write_scores(scorefile, scores, scorecount) < 0)    if (write_scores(scorefile, scores, scorecount) < 0)
190      {      {
# Line 229  read_score(FILE *f, struct score_entry * Line 257  read_score(FILE *f, struct score_entry *
257      int len;      int len;
258      if (getline(&score->data, &len, f) < 0)      if (getline(&score->data, &len, f) < 0)
259        return -1;        return -1;
260        score->data[strlen(score->data)-1] = '\0';
261    }    }
262  #else  #else
263    {    {
# Line 249  read_score(FILE *f, struct score_entry * Line 278  read_score(FILE *f, struct score_entry *
278          cur++;          cur++;
279        }        }
280      score->data = buf;      score->data = buf;
281        score->data[cur] = '\0';
282    }    }
283  #endif  #endif
   /* Trim the newline */  
   score->data[strlen(score->data)-1] = '\0';  
284    return 0;    return 0;
285  }  }
286    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26